Skip to content

Commit 689e608

Browse files
committed
Fix darwin monitor
1 parent 6ef2a6c commit 689e608

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

monitor_darwin.go

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (m *networkUpdateMonitor) loopUpdate() {
4242
select {
4343
case <-m.done:
4444
return
45-
case <-time.After(time.Second):
45+
default:
4646
}
4747
err := m.loopUpdate0()
4848
if err != nil {
@@ -67,7 +67,16 @@ func (m *networkUpdateMonitor) loopUpdate1(routeSocketFile *os.File) {
6767
defer routeSocketFile.Close()
6868
buffer := buf.NewPacket()
6969
defer buffer.Release()
70+
done := make(chan struct{})
71+
go func() {
72+
select {
73+
case <-m.done:
74+
routeSocketFile.Close()
75+
case <-done:
76+
}
77+
}()
7078
n, err := routeSocketFile.Read(buffer.FreeBytes())
79+
close(done)
7180
if err != nil {
7281
return
7382
}
@@ -92,57 +101,59 @@ func (m *networkUpdateMonitor) Close() error {
92101
}
93102

94103
func (m *defaultInterfaceMonitor) checkUpdate() error {
95-
ribMessage, err := route.FetchRIB(unix.AF_UNSPEC, route.RIBTypeRoute, 0)
96-
if err != nil {
97-
return err
98-
}
99-
routeMessages, err := route.ParseRIB(route.RIBTypeRoute, ribMessage)
100-
if err != nil {
101-
return err
102-
}
103-
var defaultInterface *net.Interface
104-
for _, rawRouteMessage := range routeMessages {
105-
routeMessage := rawRouteMessage.(*route.RouteMessage)
106-
if len(routeMessage.Addrs) <= unix.RTAX_NETMASK {
107-
continue
108-
}
109-
destination, isIPv4Destination := routeMessage.Addrs[unix.RTAX_DST].(*route.Inet4Addr)
110-
if !isIPv4Destination {
111-
continue
112-
}
113-
if destination.IP != netip.IPv4Unspecified().As4() {
114-
continue
115-
}
116-
mask, isIPv4Mask := routeMessage.Addrs[unix.RTAX_NETMASK].(*route.Inet4Addr)
117-
if !isIPv4Mask {
118-
continue
119-
}
120-
ones, _ := net.IPMask(mask.IP[:]).Size()
121-
if ones != 0 {
122-
continue
123-
}
124-
routeInterface, err := net.InterfaceByIndex(routeMessage.Index)
104+
var (
105+
defaultInterface *net.Interface
106+
err error
107+
)
108+
if m.options.UnderNetworkExtension {
109+
defaultInterface, err = getDefaultInterfaceBySocket()
125110
if err != nil {
126111
return err
127112
}
128-
if routeMessage.Flags&unix.RTF_UP == 0 {
129-
continue
130-
}
131-
if routeMessage.Flags&unix.RTF_GATEWAY == 0 {
132-
continue
113+
} else {
114+
ribMessage, err := route.FetchRIB(unix.AF_UNSPEC, route.RIBTypeRoute, 0)
115+
if err != nil {
116+
return err
133117
}
134-
if routeMessage.Flags&unix.RTF_IFSCOPE != 0 {
135-
// continue
118+
routeMessages, err := route.ParseRIB(route.RIBTypeRoute, ribMessage)
119+
if err != nil {
120+
return err
136121
}
137-
defaultInterface = routeInterface
138-
break
139-
}
140-
if defaultInterface == nil {
141-
if m.options.UnderNetworkExtension {
142-
defaultInterface, err = getDefaultInterfaceBySocket()
122+
for _, rawRouteMessage := range routeMessages {
123+
routeMessage := rawRouteMessage.(*route.RouteMessage)
124+
if len(routeMessage.Addrs) <= unix.RTAX_NETMASK {
125+
continue
126+
}
127+
destination, isIPv4Destination := routeMessage.Addrs[unix.RTAX_DST].(*route.Inet4Addr)
128+
if !isIPv4Destination {
129+
continue
130+
}
131+
if destination.IP != netip.IPv4Unspecified().As4() {
132+
continue
133+
}
134+
mask, isIPv4Mask := routeMessage.Addrs[unix.RTAX_NETMASK].(*route.Inet4Addr)
135+
if !isIPv4Mask {
136+
continue
137+
}
138+
ones, _ := net.IPMask(mask.IP[:]).Size()
139+
if ones != 0 {
140+
continue
141+
}
142+
routeInterface, err := net.InterfaceByIndex(routeMessage.Index)
143143
if err != nil {
144144
return err
145145
}
146+
if routeMessage.Flags&unix.RTF_UP == 0 {
147+
continue
148+
}
149+
if routeMessage.Flags&unix.RTF_GATEWAY == 0 {
150+
continue
151+
}
152+
if routeMessage.Flags&unix.RTF_IFSCOPE != 0 {
153+
// continue
154+
}
155+
defaultInterface = routeInterface
156+
break
146157
}
147158
}
148159
if defaultInterface == nil {

monitor_shared.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"net"
88
"net/netip"
9-
"runtime"
109
"sync"
1110
"time"
1211

@@ -44,6 +43,7 @@ type defaultInterfaceMonitor struct {
4443
defaultInterfaceIndex int
4544
androidVPNEnabled bool
4645
networkMonitor NetworkUpdateMonitor
46+
checkUpdateTimer *time.Timer
4747
element *list.Element[NetworkUpdateCallback]
4848
access sync.Mutex
4949
callbacks list.List[DefaultInterfaceUpdateCallback]
@@ -72,9 +72,13 @@ func (m *defaultInterfaceMonitor) Start() error {
7272
}
7373

7474
func (m *defaultInterfaceMonitor) delayCheckUpdate() {
75-
if runtime.GOOS == "android" {
76-
time.Sleep(time.Second)
75+
if m.checkUpdateTimer != nil {
76+
m.checkUpdateTimer.Stop()
7777
}
78+
m.checkUpdateTimer = time.AfterFunc(time.Second, m.postCheckUpdate)
79+
}
80+
81+
func (m *defaultInterfaceMonitor) postCheckUpdate() {
7882
err := m.updateInterfaces()
7983
if err != nil {
8084
m.logger.Error("update interfaces: ", err)

0 commit comments

Comments
 (0)