From 3beb71b6e11c24ba767f4c93af92e52d28c9cf9c Mon Sep 17 00:00:00 2001 From: admin <2833154405@qq.com> Date: Mon, 21 Mar 2022 19:38:36 +0800 Subject: [PATCH 1/2] auto change interface for tproxy --- hub/executor/executor.go | 15 +++++++++++---- listener/tun/ipstack/commons/router.go | 5 ++++- listener/tun/ipstack/commons/router_darwin.go | 2 +- listener/tun/ipstack/commons/router_linux.go | 2 +- listener/tun/ipstack/commons/router_windows.go | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index e261ef9ef..dd62c7f24 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -2,7 +2,7 @@ package executor import ( "fmt" - + "github.com/Dreamacro/clash/listener/tun/ipstack/commons" "net" "os" "runtime" @@ -75,14 +75,14 @@ func ApplyConfig(cfg *config.Config, force bool) { defer mux.Unlock() updateUsers(cfg.Users) - updateHosts(cfg.Hosts) updateProxies(cfg.Proxies, cfg.Providers) updateRules(cfg.Rules, cfg.RuleProviders) updateGeneral(cfg.General, force) updateDNS(cfg.DNS, cfg.Tun) updateTun(cfg.Tun) - updateIPTables(cfg.DNS, cfg.General, cfg.Tun) + autoUpdateIPTables(cfg.DNS, cfg.General, cfg.Tun) updateExperimental(cfg) + updateHosts(cfg.Hosts) loadProvider(cfg.RuleProviders, cfg.Providers) updateProfile(cfg) @@ -301,6 +301,13 @@ func patchSelectGroup(proxies map[string]C.Proxy) { } } +func autoUpdateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { + updateIPTables(dns, general, tun) + go commons.DefaultInterfaceChangeMonitor(func(_ string) { + updateIPTables(dns, general, tun) + }) +} + func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { tproxy.CleanUpTProxyLinuxIPTables() @@ -340,7 +347,7 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { dialer.DefaultRoutingMark.Store(2158) } if general.AutoIptables { - err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort) + err = tproxy.SetTProxyLinuxIPTables(dialer.DefaultInterface.Load(), general.TProxyPort, dnsPort) } } diff --git a/listener/tun/ipstack/commons/router.go b/listener/tun/ipstack/commons/router.go index 1cebfa98d..86458948a 100644 --- a/listener/tun/ipstack/commons/router.go +++ b/listener/tun/ipstack/commons/router.go @@ -24,7 +24,7 @@ func ipv4MaskString(bits int) string { return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) } -func defaultInterfaceChangeMonitor() { +func DefaultInterfaceChangeMonitor(cb func(ifName string)) { t := time.NewTicker(defaultInterfaceMonitorDuration) defer t.Stop() @@ -43,6 +43,9 @@ func defaultInterfaceChangeMonitor() { } dialer.DefaultInterface.Store(interfaceName) + if cb != nil { + cb(interfaceName) + } log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, interfaceName) } diff --git a/listener/tun/ipstack/commons/router_darwin.go b/listener/tun/ipstack/commons/router_darwin.go index 4185a48a5..77f0abaf9 100644 --- a/listener/tun/ipstack/commons/router_darwin.go +++ b/listener/tun/ipstack/commons/router_darwin.go @@ -54,7 +54,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error { } } - go defaultInterfaceChangeMonitor() + go DefaultInterfaceChangeMonitor(nil) return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) } diff --git a/listener/tun/ipstack/commons/router_linux.go b/listener/tun/ipstack/commons/router_linux.go index 63479baeb..be2399895 100644 --- a/listener/tun/ipstack/commons/router_linux.go +++ b/listener/tun/ipstack/commons/router_linux.go @@ -42,7 +42,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error { } } - go defaultInterfaceChangeMonitor() + go DefaultInterfaceChangeMonitor(nil) return nil } diff --git a/listener/tun/ipstack/commons/router_windows.go b/listener/tun/ipstack/commons/router_windows.go index 5ddcf7099..8c761c66c 100644 --- a/listener/tun/ipstack/commons/router_windows.go +++ b/listener/tun/ipstack/commons/router_windows.go @@ -203,7 +203,7 @@ startOver: wintunInterfaceName = dev.Name() - go defaultInterfaceChangeMonitor() + go DefaultInterfaceChangeMonitor(nil) return nil } From e0040b7e5d13a873ef90ab385a5bdc816e9b08d1 Mon Sep 17 00:00:00 2001 From: admin <2833154405@qq.com> Date: Mon, 21 Mar 2022 20:29:07 +0800 Subject: [PATCH 2/2] [Fix] do not monitor when auto-iptables is false --- hub/executor/executor.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index dd62c7f24..bf4e2a1c4 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -302,15 +302,6 @@ func patchSelectGroup(proxies map[string]C.Proxy) { } func autoUpdateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { - updateIPTables(dns, general, tun) - go commons.DefaultInterfaceChangeMonitor(func(_ string) { - updateIPTables(dns, general, tun) - }) -} - -func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { - tproxy.CleanUpTProxyLinuxIPTables() - if runtime.GOOS != "linux" || general.TProxyPort == 0 { return } @@ -333,14 +324,23 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { return } + err = updateIPTables(dns, general) + go commons.DefaultInterfaceChangeMonitor(func(_ string) { + updateIPTables(dns, general) + }) +} + +func updateIPTables(dns *config.DNS, general *config.General) error { + tproxy.CleanUpTProxyLinuxIPTables() + _, dnsPortStr, err := net.SplitHostPort(dns.Listen) if dnsPortStr == "0" || dnsPortStr == "" || err != nil { - return + return err } dnsPort, err := strconv.Atoi(dnsPortStr) if err != nil { - return + return err } if dialer.DefaultRoutingMark.Load() == 0 { @@ -349,6 +349,7 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { if general.AutoIptables { err = tproxy.SetTProxyLinuxIPTables(dialer.DefaultInterface.Load(), general.TProxyPort, dnsPort) } + return err } func Cleanup() {