Skip to content

Commit

Permalink
Add DefaultInterface func
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Oct 6, 2023
1 parent 847f394 commit 19cc8b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type DefaultInterfaceMonitor interface {
Close() error
DefaultInterfaceName(destination netip.Addr) string
DefaultInterfaceIndex(destination netip.Addr) int
DefaultInterface(destination netip.Addr) (string, int)
OverrideAndroidVPN() bool
AndroidVPNEnabled() bool
RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback]
Expand Down
14 changes: 14 additions & 0 deletions monitor_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ func (m *defaultInterfaceMonitor) DefaultInterfaceIndex(destination netip.Addr)
return m.defaultInterfaceIndex
}

func (m *defaultInterfaceMonitor) DefaultInterface(destination netip.Addr) (string, int) {
for _, address := range m.networkAddresses {
for _, prefix := range address.addresses {
if prefix.Contains(destination) {
return address.interfaceName, address.interfaceIndex
}
}
}
if m.defaultInterfaceIndex == -1 {
m.checkUpdate()
}
return m.defaultInterfaceName, m.defaultInterfaceIndex
}

func (m *defaultInterfaceMonitor) OverrideAndroidVPN() bool {
return m.options.OverrideAndroidVPN
}
Expand Down

0 comments on commit 19cc8b9

Please sign in to comment.