2022-01-10 14:56:41 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-05-13 09:49:05 +00:00
|
|
|
"fmt"
|
2022-01-10 14:56:41 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-05-13 09:49:05 +00:00
|
|
|
func SetOsProxy(port int) error {
|
2022-01-10 14:56:41 +00:00
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 13:37:48 +00:00
|
|
|
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()
|
|
|
|
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
_, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnsetOsProxy() error {
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 13:37:48 +00:00
|
|
|
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()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
_, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|