Skip to content

Commit

Permalink
chore: don't start UpdateMonitor when AutoRoute not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 10, 2024
1 parent 89b9438 commit c6f69d5
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions listener/sing_tun/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
if options.GSOMaxSize == 0 {
options.GSOMaxSize = 65536
}
if !supportRedirect {
if !supportRedirect || !options.AutoRoute {
options.AutoRedirect = false
}
tunName := options.Device
Expand Down Expand Up @@ -264,31 +264,34 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis

interfaceFinder := DefaultInterfaceFinder

networkUpdateMonitor, err := tun.NewNetworkUpdateMonitor(log.SingLogger)
if err != nil {
err = E.Cause(err, "create NetworkUpdateMonitor")
return
}
l.networkUpdateMonitor = networkUpdateMonitor
err = networkUpdateMonitor.Start()
if err != nil {
err = E.Cause(err, "start NetworkUpdateMonitor")
return
}
if options.AutoRoute {
var networkUpdateMonitor tun.NetworkUpdateMonitor
networkUpdateMonitor, err = tun.NewNetworkUpdateMonitor(log.SingLogger)
if err != nil {
err = E.Cause(err, "create NetworkUpdateMonitor")
return
}
l.networkUpdateMonitor = networkUpdateMonitor
err = networkUpdateMonitor.Start()
if err != nil {
err = E.Cause(err, "start NetworkUpdateMonitor")
return
}

defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, log.SingLogger, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true})
if err != nil {
err = E.Cause(err, "create DefaultInterfaceMonitor")
return
}
l.defaultInterfaceMonitor = defaultInterfaceMonitor
defaultInterfaceMonitor.RegisterCallback(func(event int) {
l.FlushDefaultInterface()
})
err = defaultInterfaceMonitor.Start()
if err != nil {
err = E.Cause(err, "start DefaultInterfaceMonitor")
return
defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, log.SingLogger, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true})

Check failure on line 281 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64, v3, amd64-go122, 1.22)

inner declaration of var err error

Check failure on line 281 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64, v3, amd64-go122, 1.22)

inner declaration of var err error

Check failure on line 281 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64, arm64-go120, 1.20)

inner declaration of var err error

Check failure on line 281 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64, arm64-go120, 1.20)

inner declaration of var err error
if err != nil {
err = E.Cause(err, "create DefaultInterfaceMonitor")
return

Check failure on line 284 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64, v3, amd64-go122, 1.22)

result parameter err not in scope at return

Check failure on line 284 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64, arm64-go120, 1.20)

result parameter err not in scope at return
}
l.defaultInterfaceMonitor = defaultInterfaceMonitor
defaultInterfaceMonitor.RegisterCallback(func(event int) {
l.FlushDefaultInterface()
})
err = defaultInterfaceMonitor.Start()
if err != nil {
err = E.Cause(err, "start DefaultInterfaceMonitor")
return

Check failure on line 293 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64, v3, amd64-go122, 1.22)

result parameter err not in scope at return

Check failure on line 293 in listener/sing_tun/server.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64, arm64-go120, 1.20)

result parameter err not in scope at return
}
}

tunOptions := tun.Options{
Expand All @@ -315,7 +318,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
IncludePackage: options.IncludePackage,
ExcludePackage: options.ExcludePackage,
FileDescriptor: options.FileDescriptor,
InterfaceMonitor: defaultInterfaceMonitor,
InterfaceMonitor: l.defaultInterfaceMonitor,
}

if options.AutoRedirect {
Expand All @@ -331,7 +334,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
Context: ctx,
Handler: handler.TypeMutation(C.REDIR),
Logger: log.SingLogger,
NetworkMonitor: networkUpdateMonitor,
NetworkMonitor: l.networkUpdateMonitor,
InterfaceFinder: interfaceFinder,
TableName: "mihomo",
DisableNFTables: dErr == nil && disableNFTables,
Expand Down Expand Up @@ -489,7 +492,7 @@ func (l *Listener) updateRule(ruleProvider provider.RuleProvider, exclude bool,
}

func (l *Listener) FlushDefaultInterface() {
if l.options.AutoDetectInterface {
if l.options.AutoDetectInterface && l.defaultInterfaceMonitor != nil {
for _, destination := range []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("1.1.1.1")} {
autoDetectInterfaceName := l.defaultInterfaceMonitor.DefaultInterfaceName(destination)
if autoDetectInterfaceName == l.tunName {
Expand Down

0 comments on commit c6f69d5

Please sign in to comment.