Repository navigation
Expand file tree
/
Copy pathmonitor_linux.go
More file actions
194 lines (181 loc) · 4.39 KB
/
Copy pathmonitor_linux.go
File metadata and controls
194 lines (181 loc) · 4.39 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tun
import (
"errors"
"os"
"runtime"
"slices"
"sync"
"syscall"
"time"
"github.com/sagernet/netlink/nl"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list"
"golang.org/x/sys/unix"
)
const (
netlinkGroups = unix.RTMGRP_LINK |
unix.RTMGRP_IPV4_IFADDR |
unix.RTMGRP_IPV6_IFADDR |
unix.RTMGRP_IPV4_ROUTE |
unix.RTMGRP_IPV6_ROUTE
netlinkReceiveBufferSize = 1 << 20
)
type networkUpdateMonitor struct {
socket *os.File
update chan struct{}
close chan struct{}
access sync.Mutex
callbacks list.List[NetworkUpdateCallback]
logger logger.Logger
}
var ErrNetlinkBanned = E.New(
"netlink socket in Android is banned by Google, " +
"use the root or system (ADB) user to run sing-box, " +
"or switch to the sing-box Android graphical interface client",
)
func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
monitor := &networkUpdateMonitor{
update: make(chan struct{}, 1),
close: make(chan struct{}),
logger: logger,
}
// check is netlink banned by google
if runtime.GOOS == "android" {
netlinkSocket, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_DGRAM, unix.NETLINK_ROUTE)
if err != nil {
return nil, ErrNetlinkBanned
}
err = unix.Bind(netlinkSocket, &unix.SockaddrNetlink{
Family: unix.AF_NETLINK,
})
unix.Close(netlinkSocket)
if err != nil {
return nil, ErrNetlinkBanned
}
}
return monitor, nil
}
func (m *networkUpdateMonitor) Start() error {
netlinkSocket, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_RAW|unix.SOCK_CLOEXEC|unix.SOCK_NONBLOCK, unix.NETLINK_ROUTE)
if err != nil {
return E.Cause(err, "create netlink socket")
}
groups := uint32(netlinkGroups)
if runtime.GOOS == "android" {
groups |= unix.RTMGRP_IPV4_RULE | 1<<(unix.RTNLGRP_IPV6_RULE-1)
}
err = unix.Bind(netlinkSocket, &unix.SockaddrNetlink{
Family: unix.AF_NETLINK,
Groups: groups,
})
if err != nil {
unix.Close(netlinkSocket)
// gVisor rejects these netlink groups with EOPNOTSUPP, or EACCES in releases before 2025-11
if E.IsMulti(err, unix.EOPNOTSUPP, unix.EACCES) {
return nil
}
return E.Cause(err, "subscribe netlink groups")
}
err = unix.SetsockoptInt(netlinkSocket, unix.SOL_SOCKET, unix.SO_RCVBUFFORCE, netlinkReceiveBufferSize)
if err != nil {
unix.SetsockoptInt(netlinkSocket, unix.SOL_SOCKET, unix.SO_RCVBUF, netlinkReceiveBufferSize)
}
m.socket = os.NewFile(uintptr(netlinkSocket), "netlink")
go m.loopRead()
go m.loopUpdate(time.Second)
return nil
}
func (m *networkUpdateMonitor) loopRead() {
buffer := make([]byte, unix.Getpagesize())
for {
n, err := m.socket.Read(buffer)
if err != nil && !errors.Is(err, unix.ENOBUFS) {
select {
case <-m.close:
default:
m.logger.Error("read netlink socket: ", err)
}
return
}
if err == nil && runtime.GOOS != "android" && !isNetworkUpdate(buffer[:n]) {
continue
}
select {
case m.update <- struct{}{}:
default:
}
}
}
func isNetworkUpdate(buffer []byte) bool {
messages, err := syscall.ParseNetlinkMessage(buffer)
if err != nil {
return true
}
return slices.ContainsFunc(messages, func(message syscall.NetlinkMessage) bool {
if message.Header.Type != unix.RTM_NEWROUTE && message.Header.Type != unix.RTM_DELROUTE {
return true
}
if nl.DeserializeRtMsg(message.Data).Dst_len == 0 {
return true
}
attributes, err := syscall.ParseNetlinkRouteAttr(&message)
if err != nil {
return true
}
var hasInterface, hasNextHop bool
for _, attribute := range attributes {
switch attribute.Attr.Type {
case unix.RTA_OIF:
hasInterface = true
case unix.RTA_GATEWAY, unix.RTA_VIA:
hasNextHop = true
}
}
return hasInterface && !hasNextHop
})
}
func (m *networkUpdateMonitor) loopUpdate(minDuration time.Duration) {
timer := time.NewTimer(minDuration)
timer.Stop()
defer timer.Stop()
var (
timerC <-chan time.Time
pending bool
)
for {
select {
case <-m.close:
return
case <-m.update:
case <-timerC:
if pending {
m.emit()
pending = false
timer.Reset(minDuration)
continue
}
timerC = nil
continue
}
if timerC != nil {
pending = true
continue
}
m.emit()
timer.Reset(minDuration)
timerC = timer.C
}
}
func (m *networkUpdateMonitor) Close() error {
select {
case <-m.close:
return os.ErrClosed
default:
}
close(m.close)
if m.socket != nil {
return m.socket.Close()
}
return nil
}