2022-01-10 14:56:41 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-08-21 08:19:59 +00:00
|
|
|
"errors"
|
2022-05-13 09:49:05 +00:00
|
|
|
"fmt"
|
2022-01-10 14:56:41 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2024-08-25 06:57:38 +00:00
|
|
|
"strconv"
|
2022-01-10 14:56:41 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-08-25 06:57:38 +00:00
|
|
|
const (
|
|
|
|
getDefaultNetworkCMD = "networksetup -listnetworkserviceorder | grep" +
|
|
|
|
" `(route -n get default | grep 'interface' || route -n get -inet6 default | grep 'interface') | cut -d ':' -f2`" +
|
|
|
|
" -B 1 | head -n 1 | cut -d ' ' -f 2-"
|
|
|
|
darwinOS = "darwin"
|
|
|
|
)
|
2024-08-21 08:19:59 +00:00
|
|
|
|
2024-08-25 06:57:38 +00:00
|
|
|
func SetOsProxy(port uint16) error {
|
|
|
|
if runtime.GOOS != darwinOS {
|
2022-01-10 14:56:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 08:19:59 +00:00
|
|
|
network, err := getDefaultNetwork()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-25 06:57:38 +00:00
|
|
|
return setProxy(getProxyTypes(), network, "127.0.0.1", port)
|
2022-01-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnsetOsProxy() error {
|
2024-08-25 06:57:38 +00:00
|
|
|
if runtime.GOOS != darwinOS {
|
2022-01-10 14:56:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 08:19:59 +00:00
|
|
|
network, err := getDefaultNetwork()
|
2022-01-10 14:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-25 06:57:38 +00:00
|
|
|
return unsetProxy(getProxyTypes(), network)
|
2022-01-10 14:56:41 +00:00
|
|
|
}
|
2024-08-21 08:19:59 +00:00
|
|
|
|
|
|
|
func getDefaultNetwork() (string, error) {
|
|
|
|
network, err := exec.Command("sh", "-c", getDefaultNetworkCMD).Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if len(network) == 0 {
|
|
|
|
return "", errors.New("no available networks")
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(network)), nil
|
|
|
|
}
|
2024-08-25 06:57:38 +00:00
|
|
|
|
|
|
|
func getProxyTypes() []string {
|
|
|
|
return []string{"webproxy", "securewebproxy"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setProxy(proxyTypes []string, network, domain string, port uint16) error {
|
|
|
|
args := []string{"", network, domain, strconv.FormatUint(uint64(port), 10)}
|
|
|
|
|
|
|
|
for _, proxyType := range proxyTypes {
|
|
|
|
args[0] = "-set" + proxyType
|
|
|
|
if err := networkSetup(args); err != nil {
|
|
|
|
return fmt.Errorf("setting %s: %w", proxyType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsetProxy(proxyTypes []string, network string) error {
|
|
|
|
args := []string{"", network, "off"}
|
|
|
|
|
|
|
|
for _, proxyType := range proxyTypes {
|
|
|
|
args[0] = "-set" + proxyType + "state"
|
|
|
|
if err := networkSetup(args); err != nil {
|
|
|
|
return fmt.Errorf("unsetting %s: %w", proxyType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkSetup(args []string) error {
|
|
|
|
cmd := exec.Command("networksetup", args...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", cmd.String(), out)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|