Skip to content

Commit

Permalink
fix: tun lookback when don't have an activated network
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Mar 29, 2024
1 parent 914bc8a commit 4542fc0
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions listener/sing_tun/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
closed: false,
options: options,
handler: handler,
tunName: tunName,
}
defer func() {
if err != nil {
Expand Down Expand Up @@ -279,25 +280,23 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis

func (l *Listener) FlushDefaultInterface() {
if l.options.AutoDetectInterface {
targetInterface := dialer.DefaultInterface.Load()
for _, destination := range []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("1.1.1.1")} {
autoDetectInterfaceName := l.defaultInterfaceMonitor.DefaultInterfaceName(destination)
if autoDetectInterfaceName == l.tunName {
log.Warnln("[TUN] Auto detect interface by %s get same name with tun", destination.String())
} else if autoDetectInterfaceName == "" || autoDetectInterfaceName == "<nil>" {
log.Warnln("[TUN] Auto detect interface by %s get empty name.", destination.String())
} else {
targetInterface = autoDetectInterfaceName
if old := dialer.DefaultInterface.Load(); old != targetInterface {
log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, targetInterface)

dialer.DefaultInterface.Store(targetInterface)

if old := dialer.DefaultInterface.Swap(autoDetectInterfaceName); old != autoDetectInterfaceName {
log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, autoDetectInterfaceName)
iface.FlushCache()
}
return
}
}
if dialer.DefaultInterface.CompareAndSwap("", "<invalid>") {
log.Warnln("[TUN] Auto detect interface failed, set '<invalid>' to DefaultInterface to avoid lookback")
}
}
}

Expand Down

0 comments on commit 4542fc0

Please sign in to comment.