Merge pull request #23 from Adlyq/Alpha-pr-iptabls

[skip ci] auto change interface for tproxy
This commit is contained in:
gVisor bot 2022-03-22 00:32:47 +08:00
commit c9fb87dbf1
5 changed files with 24 additions and 13 deletions

View file

@ -2,7 +2,7 @@ package executor
import ( import (
"fmt" "fmt"
"github.com/Dreamacro/clash/listener/tun/ipstack/commons"
"net" "net"
"os" "os"
"runtime" "runtime"
@ -75,14 +75,14 @@ func ApplyConfig(cfg *config.Config, force bool) {
defer mux.Unlock() defer mux.Unlock()
updateUsers(cfg.Users) updateUsers(cfg.Users)
updateHosts(cfg.Hosts)
updateProxies(cfg.Proxies, cfg.Providers) updateProxies(cfg.Proxies, cfg.Providers)
updateRules(cfg.Rules, cfg.RuleProviders) updateRules(cfg.Rules, cfg.RuleProviders)
updateGeneral(cfg.General, force) updateGeneral(cfg.General, force)
updateDNS(cfg.DNS, cfg.Tun) updateDNS(cfg.DNS, cfg.Tun)
updateTun(cfg.Tun) updateTun(cfg.Tun)
updateIPTables(cfg.DNS, cfg.General, cfg.Tun) autoUpdateIPTables(cfg.DNS, cfg.General, cfg.Tun)
updateExperimental(cfg) updateExperimental(cfg)
updateHosts(cfg.Hosts)
loadProvider(cfg.RuleProviders, cfg.Providers) loadProvider(cfg.RuleProviders, cfg.Providers)
updateProfile(cfg) updateProfile(cfg)
@ -301,9 +301,7 @@ func patchSelectGroup(proxies map[string]C.Proxy) {
} }
} }
func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { func autoUpdateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) {
tproxy.CleanUpTProxyLinuxIPTables()
if runtime.GOOS != "linux" || general.TProxyPort == 0 { if runtime.GOOS != "linux" || general.TProxyPort == 0 {
return return
} }
@ -326,22 +324,32 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) {
return 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) _, dnsPortStr, err := net.SplitHostPort(dns.Listen)
if dnsPortStr == "0" || dnsPortStr == "" || err != nil { if dnsPortStr == "0" || dnsPortStr == "" || err != nil {
return return err
} }
dnsPort, err := strconv.Atoi(dnsPortStr) dnsPort, err := strconv.Atoi(dnsPortStr)
if err != nil { if err != nil {
return return err
} }
if dialer.DefaultRoutingMark.Load() == 0 { if dialer.DefaultRoutingMark.Load() == 0 {
dialer.DefaultRoutingMark.Store(2158) dialer.DefaultRoutingMark.Store(2158)
} }
if general.AutoIptables { if general.AutoIptables {
err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort) err = tproxy.SetTProxyLinuxIPTables(dialer.DefaultInterface.Load(), general.TProxyPort, dnsPort)
} }
return err
} }
func Cleanup() { func Cleanup() {

View file

@ -24,7 +24,7 @@ func ipv4MaskString(bits int) string {
return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) 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) t := time.NewTicker(defaultInterfaceMonitorDuration)
defer t.Stop() defer t.Stop()
@ -43,6 +43,9 @@ func defaultInterfaceChangeMonitor() {
} }
dialer.DefaultInterface.Store(interfaceName) dialer.DefaultInterface.Store(interfaceName)
if cb != nil {
cb(interfaceName)
}
log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, interfaceName) log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, interfaceName)
} }

View file

@ -54,7 +54,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
} }
} }
go defaultInterfaceChangeMonitor() go DefaultInterfaceChangeMonitor(nil)
return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)
} }

View file

@ -42,7 +42,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
} }
} }
go defaultInterfaceChangeMonitor() go DefaultInterfaceChangeMonitor(nil)
return nil return nil
} }

View file

@ -203,7 +203,7 @@ startOver:
wintunInterfaceName = dev.Name() wintunInterfaceName = dev.Name()
go defaultInterfaceChangeMonitor() go DefaultInterfaceChangeMonitor(nil)
return nil return nil
} }