Setting up a system-wide proxy (windows)

This commit is contained in:
incubus013 2024-08-15 23:28:12 +07:00
parent ab4d6819c7
commit c6947eb0b8
3 changed files with 97 additions and 46 deletions

View File

@ -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
}

49
util/os_darwin.go Normal file
View File

@ -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
}

45
util/os_windows.go Normal file
View File

@ -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
}