mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-04-03 03:47:39 +03:00
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package tun
|
|
|
|
import (
|
|
"github.com/sagernet/netlink"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
)
|
|
|
|
func (m *defaultInterfaceMonitor) checkUpdate() error {
|
|
ruleList, err := netlink.RuleList(netlink.FAMILY_ALL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var defaultTableIndex int
|
|
for _, rule := range ruleList {
|
|
if rule.Mask == 0xFFFF {
|
|
defaultTableIndex = rule.Table
|
|
}
|
|
}
|
|
|
|
if defaultTableIndex == 0 {
|
|
return E.Extend(ErrNoRoute, "no rule 0xFFFF")
|
|
}
|
|
|
|
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: defaultTableIndex}, netlink.RT_FILTER_TABLE)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(routes) == 0 {
|
|
return E.Extend(ErrNoRoute, "no route in default table ", defaultTableIndex)
|
|
}
|
|
|
|
var link netlink.Link
|
|
link, err = netlink.LinkByIndex(routes[0].LinkIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
oldInterface := m.defaultInterfaceName
|
|
oldIndex := m.defaultInterfaceIndex
|
|
|
|
m.defaultInterfaceName = link.Attrs().Name
|
|
m.defaultInterfaceIndex = link.Attrs().Index
|
|
|
|
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
|
|
return nil
|
|
}
|
|
|
|
m.emit()
|
|
return nil
|
|
}
|