From c6947eb0b8e48c452ec5affd122f951533f0139e Mon Sep 17 00:00:00 2001 From: incubus013 Date: Thu, 15 Aug 2024 23:28:12 +0700 Subject: [PATCH] Setting up a system-wide proxy (windows) --- util/os.go | 49 +++------------------------------------------- util/os_darwin.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ util/os_windows.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 util/os_darwin.go create mode 100644 util/os_windows.go diff --git a/util/os.go b/util/os.go index 9c402dc..842b400 100644 --- a/util/os.go +++ b/util/os.go @@ -1,55 +1,12 @@ +//go:build !windows && !darwin +// +build !windows,!darwin + package util -import ( - "fmt" - "os/exec" - "runtime" - "strings" -) - func SetOsProxy(port int) error { - if runtime.GOOS != "darwin" { - return nil - } - - network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() - - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() - if err != nil { - return err - } - return nil } func UnsetOsProxy() error { - if runtime.GOOS != "darwin" { - return nil - } - - network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() - if err != nil { - return err - } - return nil } diff --git a/util/os_darwin.go b/util/os_darwin.go new file mode 100644 index 0000000..268334e --- /dev/null +++ b/util/os_darwin.go @@ -0,0 +1,49 @@ +//go:build darwin +// +build darwin + +package util + +import ( + "fmt" + "os/exec" + "strings" +) + +func SetOsProxy(port int) error { + network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() + + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() + if err != nil { + return err + } + + return nil +} + +func UnsetOsProxy() error { + network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() + if err != nil { + return err + } + + return nil +} diff --git a/util/os_windows.go b/util/os_windows.go new file mode 100644 index 0000000..7737a08 --- /dev/null +++ b/util/os_windows.go @@ -0,0 +1,45 @@ +//go:build windows +// +build windows + +package util + +import ( + "fmt" + + "golang.org/x/sys/windows/registry" +) + +func SetOsProxy(port int) error { + key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE) + if err != nil { + return err + } + defer key.Close() + + err = key.SetDWordValue("ProxyEnable", 1) + if err != nil { + return err + } + + err = key.SetStringValue("ProxyServer", "127.0.0.1:"+fmt.Sprint(port)) + if err != nil { + return err + } + + return nil +} + +func UnsetOsProxy() error { + key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE) + if err != nil { + return err + } + defer key.Close() + + err = key.SetDWordValue("ProxyEnable", 0) + if err != nil { + return err + } + + return nil +}