Skip to content

Commit

Permalink
feature: Add support to windows setup command
Browse files Browse the repository at this point in the history
 * Add windows setup suport (cristianoliveira#30)
  • Loading branch information
adiclepcea authored and cristianoliveira committed Oct 2, 2017
1 parent 19a2f77 commit 04e1d08
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ Ergo comes with a setup command that can configure that for you. The current sys

- osx
- linux-gnome
- windows

(Contributions are welcomed)

```bash
ergo setup <operation-system>
```

In case of errors or if it doesn't work please take a look on detailed config session below.

### Showtime
Expand Down
17 changes: 16 additions & 1 deletion commands/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ package commands

import (
"fmt"
"github.com/cristianoliveira/ergo/proxy"
"log"
"os/exec"
"runtime"
"strings"

"github.com/cristianoliveira/ergo/proxy"
)

// Setup command tries set ergo as the proxy on networking settings.
// For now, this feature is only supported for:
// - OSX
// - Linux-gnome
// - Windows
//
// Usage:
// `ergo setup osx`
func Setup(system string, remove bool, config *proxy.Config) {

fmt.Println("Current detected system: " + runtime.GOOS)
proxyURL := "http://127.0.0.1:" + config.Port + "/proxy.pac"
script := ""
Expand Down Expand Up @@ -60,12 +63,24 @@ func Setup(system string, remove bool, config *proxy.Config) {
log.Fatal(err)
}

case "windows":
if remove {
cmd = exec.Command("reg", "delete", `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, "/v", "AutoConfigURL", "/f")
} else {
cmd = exec.Command("reg", "add", `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, "/v", "AutoConfigURL", "/t", "REG_SZ", "/d", proxyURL, "/f")
}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
//this is because windows needs to be told about the change
InetRefresh()
default:
fmt.Println(`
List of supported system
-linux-gnome
-osx
-windows
For more support please open an issue on: https://github.com/cristianoliveira/ergo
`)
Expand Down
10 changes: 10 additions & 0 deletions commands/setup_hlp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// +build linux darwin

package commands

//ShowInternetOptions is just a dummy for now
//this is only used for Windows
func ShowInternetOptions() {}
//InetRefresh is just a dummy. It only has functionality
//in Windows
func InetRefresh() {}
57 changes: 57 additions & 0 deletions commands/setup_hlp_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// +build windows

package commands

import (
"fmt"
"log"
"os/exec"
"syscall"
)

//ShowInternetOptions will display the Internet Options
//control panel. If the user clicks OK here, then the modifications
//will be visible without a restart
//this is just a fallback for the InetRefresh function
func ShowInternetOptions() {
cmd := exec.Command("control", "inetcpl.cpl,Connections,4")
fmt.Println("Starting the Internet Options control panel")
err := cmd.Run()
if err != nil {
log.Printf("Internet Options control panel could not be started.\r\n%s\r\n", err.Error())
}
}

//InetRefresh will inform windows that a proxy change was performed.
//if windows is not informed about the change, then the modification will only be noticed
//after a restart
func InetRefresh() {
wininet := syscall.MustLoadDLL("wininet.dll")
inetsetoption := wininet.MustFindProc("InternetSetOptionW")
//Option 39 is INTERNET_OPTION_SETTINGS_CHANGED
ret, _, callErr := inetsetoption.Call(0, 39, 0, 0)

if ret == 0 {
log.Printf(`
Could not auto refresh the proxy settings.
Step 1
The received error is: %s
Please press OK on the next dialog.\r\n`,
callErr.Error())

ShowInternetOptions()
}
//Option 37 is INTERNET_OPTION_REFRESH
ret, _, callErr = inetsetoption.Call(0, 37, 0, 0)
if ret == 0 {
log.Printf(`
Could not auto refresh the proxy settings.
Step 2
The received error is: %s
Please press OK on the next dialog.\r\n`,
callErr.Error())
ShowInternetOptions()
}

return
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/cristianoliveira/ergo/commands"
"github.com/cristianoliveira/ergo/proxy"
"os"
)

//VERSION of ergo.
Expand All @@ -21,7 +22,7 @@ Usage:
ergo list
ergo list-names
ergo url <name>
ergo setup [linux-gnome|osx]
ergo setup [linux-gnome|osx|windows]
Options:
-h Shows this message.
Expand Down

0 comments on commit 04e1d08

Please sign in to comment.