Fix darwin monitor

This commit is contained in:
世界 2024-02-26 13:20:38 +08:00
parent e8633c66d2
commit 8d285f70fb
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 16 additions and 4 deletions

View file

@ -170,6 +170,8 @@ func getDefaultInterfaceBySocket() (*net.Interface, error) {
Port: 80, Port: 80,
}) })
result := make(chan netip.Addr, 1) result := make(chan netip.Addr, 1)
done := make(chan struct{})
defer close(done)
go func() { go func() {
for { for {
sockname, sockErr := unix.Getsockname(socketFd) sockname, sockErr := unix.Getsockname(socketFd)
@ -182,8 +184,13 @@ func getDefaultInterfaceBySocket() (*net.Interface, error) {
} }
addr := netip.AddrFrom4(sockaddr.Addr) addr := netip.AddrFrom4(sockaddr.Addr)
if addr.IsUnspecified() { if addr.IsUnspecified() {
time.Sleep(time.Millisecond) select {
continue case <-done:
break
default:
time.Sleep(10 * time.Millisecond)
continue
}
} }
result <- addr result <- addr
break break
@ -193,7 +200,7 @@ func getDefaultInterfaceBySocket() (*net.Interface, error) {
select { select {
case selectedAddr = <-result: case selectedAddr = <-result:
case <-time.After(time.Second): case <-time.After(time.Second):
return nil, os.ErrDeadlineExceeded return nil, nil
} }
interfaces, err := net.Interfaces() interfaces, err := net.Interfaces()
if err != nil { if err != nil {

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"net" "net"
"net/netip" "net/netip"
"runtime"
"sync" "sync"
"time" "time"
@ -71,7 +72,9 @@ func (m *defaultInterfaceMonitor) Start() error {
} }
func (m *defaultInterfaceMonitor) delayCheckUpdate() { func (m *defaultInterfaceMonitor) delayCheckUpdate() {
time.Sleep(time.Second) if runtime.GOOS == "android" {
time.Sleep(time.Second)
}
err := m.updateInterfaces() err := m.updateInterfaces()
if err != nil { if err != nil {
m.logger.Error("update interfaces: ", err) m.logger.Error("update interfaces: ", err)
@ -81,6 +84,8 @@ func (m *defaultInterfaceMonitor) delayCheckUpdate() {
m.defaultInterfaceName = "" m.defaultInterfaceName = ""
m.defaultInterfaceIndex = -1 m.defaultInterfaceIndex = -1
m.emit(EventNoRoute) m.emit(EventNoRoute)
} else if err != nil {
m.logger.Error("check interface: ", err)
} }
} }