diff --git a/core/src/main/golang/native/config/process.go b/core/src/main/golang/native/config/process.go index 0b60b1c520..85842704d6 100644 --- a/core/src/main/golang/native/config/process.go +++ b/core/src/main/golang/native/config/process.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "strings" + "sync" "github.com/dlclark/regexp2" @@ -30,6 +31,19 @@ var processors = []processor{ type processor func(cfg *config.RawConfig, profileDir string) error +var ( + tunICMPMu sync.RWMutex + tunDisableICMPForwarding bool + tunICMPTimeout int64 + tunOptionsReady bool +) + +func GetTunICMPOptions() (bool, int64, bool) { + tunICMPMu.RLock() + defer tunICMPMu.RUnlock() + return tunDisableICMPForwarding, tunICMPTimeout, tunOptionsReady +} + func patchOverride(cfg *config.RawConfig, _ string) error { if err := json.NewDecoder(strings.NewReader(ReadOverride(OverrideSlotPersist))).Decode(cfg); err != nil { log.Warnln("Apply persist override: %s", err.Error()) @@ -85,6 +99,11 @@ func patchDns(cfg *config.RawConfig, _ string) error { } func patchTun(cfg *config.RawConfig, _ string) error { + tunICMPMu.Lock() + tunDisableICMPForwarding = cfg.Tun.DisableICMPForwarding + tunICMPTimeout = cfg.Tun.ICMPTimeout + tunOptionsReady = true + tunICMPMu.Unlock() cfg.Tun.Enable = false cfg.Tun.AutoRoute = false cfg.Tun.AutoDetectInterface = false diff --git a/core/src/main/golang/native/tun/tun.go b/core/src/main/golang/native/tun/tun.go index ee2c2a9124..fa31f76a4c 100644 --- a/core/src/main/golang/native/tun/tun.go +++ b/core/src/main/golang/native/tun/tun.go @@ -6,6 +6,9 @@ import ( "net" "net/netip" "strings" + "time" + + "cfa/native/config" C "github.com/metacubex/mihomo/constant" LC "github.com/metacubex/mihomo/listener/config" @@ -64,6 +67,17 @@ func Start(fd int, stack, gateway, portal, dns string) (io.Closer, error) { FileDescriptor: fd, } + disableICMPForwarding, icmpTimeout, ready := config.GetTunICMPOptions() + if !ready { + deadline := time.Now().Add(3 * time.Second) + for !ready && time.Now().Before(deadline) { + time.Sleep(50 * time.Millisecond) + disableICMPForwarding, icmpTimeout, ready = config.GetTunICMPOptions() + } + } + options.DisableICMPForwarding = disableICMPForwarding + options.ICMPTimeout = icmpTimeout + tunOptions, _ := json.Marshal(options) log.Debugln(string(tunOptions))