2022-08-04 14:01:20 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
2022-08-26 12:59:22 +00:00
|
|
|
"net/netip"
|
2022-08-04 14:01:20 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-tun"
|
2022-08-16 10:19:48 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-08-04 14:01:20 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
"github.com/sagernet/sing/common/x/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
type systemProxy struct {
|
|
|
|
monitor tun.DefaultInterfaceMonitor
|
|
|
|
interfaceName string
|
|
|
|
element *list.Element[tun.DefaultInterfaceUpdateCallback]
|
|
|
|
port uint16
|
|
|
|
isMixed bool
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:12:29 +00:00
|
|
|
func (p *systemProxy) update(event int) error {
|
2022-08-26 12:59:22 +00:00
|
|
|
newInterfaceName := p.monitor.DefaultInterfaceName(netip.IPv4Unspecified())
|
2022-08-04 14:01:20 +00:00
|
|
|
if p.interfaceName == newInterfaceName {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if p.interfaceName != "" {
|
|
|
|
_ = p.unset()
|
|
|
|
}
|
|
|
|
p.interfaceName = newInterfaceName
|
|
|
|
interfaceDisplayName, err := getInterfaceDisplayName(p.interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.isMixed {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setsocksfirewallproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setwebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setsecurewebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *systemProxy) unset() error {
|
|
|
|
interfaceDisplayName, err := getInterfaceDisplayName(p.interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.isMixed {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setsocksfirewallproxystate", interfaceDisplayName, "off").Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setwebproxystate", interfaceDisplayName, "off").Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2022-08-16 10:19:48 +00:00
|
|
|
err = common.Exec("networksetup", "-setsecurewebproxystate", interfaceDisplayName, "off").Attach().Run()
|
2022-08-04 14:01:20 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInterfaceDisplayName(name string) (string, error) {
|
2022-08-16 10:19:48 +00:00
|
|
|
content, err := common.Exec("networksetup", "-listallhardwareports").Read()
|
2022-08-04 14:01:20 +00:00
|
|
|
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 "", E.New(name, " not found in networksetup -listallhardwareports")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSystemProxy(router adapter.Router, port uint16, isMixed bool) (func() error, error) {
|
|
|
|
interfaceMonitor := router.InterfaceMonitor()
|
|
|
|
if interfaceMonitor == nil {
|
|
|
|
return nil, E.New("missing interface monitor")
|
|
|
|
}
|
|
|
|
proxy := &systemProxy{
|
|
|
|
monitor: interfaceMonitor,
|
|
|
|
port: port,
|
|
|
|
isMixed: isMixed,
|
|
|
|
}
|
2022-09-05 05:12:29 +00:00
|
|
|
err := proxy.update(tun.EventInterfaceUpdate)
|
2022-08-04 14:01:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proxy.element = interfaceMonitor.RegisterCallback(proxy.update)
|
|
|
|
return func() error {
|
|
|
|
interfaceMonitor.UnregisterCallback(proxy.element)
|
|
|
|
return proxy.unset()
|
|
|
|
}, nil
|
|
|
|
}
|