From b5d934e0a7e50e75b8444ab5d04f74ee88cfa10f Mon Sep 17 00:00:00 2001 From: xvzc Date: Mon, 10 Jan 2022 23:56:41 +0900 Subject: [PATCH] add util/os.go --- util/os.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 util/os.go diff --git a/util/os.go b/util/os.go new file mode 100644 index 0000000..1fc11cf --- /dev/null +++ b/util/os.go @@ -0,0 +1,53 @@ +package util + +import ( + "os/exec" + "runtime" + "strings" +) + +func SetOsProxy(port string) 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 ' ' -f2").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+strings.TrimSpace(string(network))+" 127.0.0.1 "+port).Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+strings.TrimSpace(string(network))+" 127.0.0.1 "+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 ' ' -f2").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 +}