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

View file

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