-
Notifications
You must be signed in to change notification settings - Fork 129
/
monitor_android.go
69 lines (61 loc) · 1.55 KB
/
monitor_android.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
}
oldVPNEnabled := m.androidVPNEnabled
var defaultTableIndex int
var vpnEnabled bool
for _, rule := range ruleList {
if rule.Mask == 0x20000 {
if rule.UIDRange == nil {
continue
}
vpnEnabled = true
if m.overrideAndroidVPN {
defaultTableIndex = rule.Table
break
}
}
if rule.Mask == 0xFFFF {
defaultTableIndex = rule.Table
break
}
}
m.androidVPNEnabled = vpnEnabled
if defaultTableIndex == 0 {
return ErrNoRoute
}
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.defaultInterface.Load()
newInterface, err := m.interfaceFinder.ByIndex(link.Attrs().Index)
if err != nil {
return E.Cause(err, "find updated interface: ", link.Attrs().Name)
}
m.defaultInterface.Store(newInterface)
if oldInterface != nil && oldInterface.Equals(*newInterface) && oldVPNEnabled == m.androidVPNEnabled {
return nil
}
var flags int
if oldVPNEnabled != m.androidVPNEnabled {
flags = FlagAndroidVPNUpdate
}
m.emit(newInterface, flags)
return nil
}