From 675687952f8bb892b7465d37bf5d6a273dbbb6cc Mon Sep 17 00:00:00 2001 From: Dariush Ahmadi <68775308+gdariushahmadi@users.noreply.github.com> Date: Wed, 29 May 2024 15:57:29 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20apply=20pro?= =?UTF-8?q?xy=20settings=20for=20each=20active=20hardware=20port=20on=20Ma?= =?UTF-8?q?cOS.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dariush Ahmadi <68775308+gdariushahmadi@users.noreply.github.com> --- common/settings/proxy_darwin.go | 81 --------------------------------- 1 file changed, 81 deletions(-) diff --git a/common/settings/proxy_darwin.go b/common/settings/proxy_darwin.go index f03658a8..4d140b65 100644 --- a/common/settings/proxy_darwin.go +++ b/common/settings/proxy_darwin.go @@ -40,84 +40,3 @@ func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bo func (p *DarwinSystemProxy) IsEnabled() bool { return p.isEnabled } - -func (p *DarwinSystemProxy) Enable() error { - return p.update0() -} - -func (p *DarwinSystemProxy) Disable() error { - interfaceDisplayName, err := getInterfaceDisplayName(p.interfaceName) - if err != nil { - return err - } - if p.supportSOCKS { - 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() - } - if err == nil { - p.isEnabled = false - } - return err -} - -func (p *DarwinSystemProxy) update(event int) { - if event&tun.EventInterfaceUpdate == 0 { - return - } - if !p.isEnabled { - return - } - _ = p.update0() -} - -func (p *DarwinSystemProxy) update0() error { - newInterfaceName := p.monitor.DefaultInterfaceName(netip.IPv4Unspecified()) - if p.interfaceName == newInterfaceName { - return nil - } - if p.interfaceName != "" { - _ = p.Disable() - } - p.interfaceName = newInterfaceName - interfaceDisplayName, err := getInterfaceDisplayName(p.interfaceName) - if err != nil { - return err - } - if p.supportSOCKS { - err = shell.Exec("networksetup", "-setsocksfirewallproxy", interfaceDisplayName, p.serverAddr.AddrString(), strconv.Itoa(int(p.serverAddr.Port))).Attach().Run() - } - if err != nil { - return err - } - err = shell.Exec("networksetup", "-setwebproxy", interfaceDisplayName, p.serverAddr.AddrString(), strconv.Itoa(int(p.serverAddr.Port))).Attach().Run() - if err != nil { - return err - } - err = shell.Exec("networksetup", "-setsecurewebproxy", interfaceDisplayName, p.serverAddr.AddrString(), strconv.Itoa(int(p.serverAddr.Port))).Attach().Run() - if err != nil { - return err - } - p.isEnabled = true - return nil -} - -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 "", E.New(name, " not found in networksetup -listallhardwareports") -}