-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy_darwin.go
76 lines (71 loc) · 2.34 KB
/
proxy_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sysproxy
import (
"bufio"
"bytes"
"errors"
"fmt"
"net/textproto"
"regexp"
"strconv"
"strings"
"sysproxy/shell"
)
func getInterfaceDisplayName(name string) (string, error) {
content, err := shell.Exec("networksetup", "-listallhardwareports").ReadOutput()
if err != nil {
return "", err
}
for _, deviceSpan := range strings.Split(string(content), "Ethernet Address") {
if strings.Contains(deviceSpan, "Device: "+name) {
substr := "Hardware Port: "
deviceSpan = deviceSpan[strings.Index(deviceSpan, substr)+len(substr):]
deviceSpan = deviceSpan[:strings.Index(deviceSpan, "\n")]
return deviceSpan, nil
}
}
return "", errors.New(name + " not found in networksetup -listallhardwareports")
}
func SetSystemProxy(port uint16, isMixed bool, bypasses ...string) (clearFunc func() error, err error) {
interfaceDisplayName, err := getNetworkInterface()
if err != nil {
return nil, err
}
if isMixed {
err = shell.Exec("networksetup", "-setsocksfirewallproxy", interfaceDisplayName, "127.0.0.1", strconv.Itoa(int(port))).Attach().Run()
}
if err == nil {
err = shell.Exec("networksetup", "-setwebproxy", interfaceDisplayName, "127.0.0.1", strconv.Itoa(int(port))).Attach().Run()
}
if err == nil {
err = shell.Exec("networksetup", "-setsecurewebproxy", interfaceDisplayName, "127.0.0.1", strconv.Itoa(int(port))).Attach().Run()
}
return func() error {
if isMixed {
err = shell.Exec("networksetup", "-setsocksfirewallproxystate", interfaceDisplayName, "off").Attach().Run()
}
if err == nil {
err = shell.Exec("networksetup", "-setwebproxystate", interfaceDisplayName, "off").Attach().Run()
}
if err == nil {
err = shell.Exec("networksetup", "-setsecurewebproxystate", interfaceDisplayName, "off").Attach().Run()
}
return err
}, err
}
func getNetworkInterface() (string, error) {
buf, err := shell.Exec("sh", "-c", "networksetup -listnetworkserviceorder | grep -B 1 $(route -n get default | grep interface | awk '{print $2}')").ReadOutput()
if err != nil {
return "", err
}
reader := textproto.NewReader(bufio.NewReader(bytes.NewBufferString(buf)))
reg := regexp.MustCompile(`^\(\d+\)\s(.*)$`)
device, err := reader.ReadLine()
if err != nil {
return "", err
}
match := reg.FindStringSubmatch(device)
if len(match) <= 1 {
return "", fmt.Errorf("unable to get network interface")
}
return match[1], nil
}