diff --git a/common/cond.go b/common/cond.go index 6fe11bc2..9a447543 100644 --- a/common/cond.go +++ b/common/cond.go @@ -362,24 +362,3 @@ func Close(closers ...any) error { } return retErr } - -// Deprecated: wtf is this? -type Starter interface { - Start() error -} - -// Deprecated: wtf is this? -func Start(starters ...any) error { - for _, rawStarter := range starters { - if rawStarter == nil { - continue - } - if starter, isStarter := rawStarter.(Starter); isStarter { - err := starter.Start() - if err != nil { - return err - } - } - } - return nil -} diff --git a/common/control/bind_darwin.go b/common/control/bind_darwin.go index bff6c293..2fb3db96 100644 --- a/common/control/bind_darwin.go +++ b/common/control/bind_darwin.go @@ -9,15 +9,15 @@ import ( func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error { return Raw(conn, func(fd uintptr) error { - var err error if interfaceIndex == -1 { if finder == nil { return os.ErrInvalid } - interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + iif, err := finder.ByName(interfaceName) if err != nil { return err } + interfaceIndex = iif.Index } switch network { case "tcp6", "udp6": diff --git a/common/control/bind_finder.go b/common/control/bind_finder.go index 9b013d34..130886a0 100644 --- a/common/control/bind_finder.go +++ b/common/control/bind_finder.go @@ -8,9 +8,9 @@ import ( type InterfaceFinder interface { Update() error Interfaces() []Interface - InterfaceIndexByName(name string) (int, error) - InterfaceNameByIndex(index int) (string, error) - InterfaceByAddr(addr netip.Addr) (*Interface, error) + ByName(name string) (*Interface, error) + ByIndex(index int) (*Interface, error) + ByAddr(addr netip.Addr) (*Interface, error) } type Interface struct { diff --git a/common/control/bind_finder_default.go b/common/control/bind_finder_default.go index 804497b6..4f996ce7 100644 --- a/common/control/bind_finder_default.go +++ b/common/control/bind_finder_default.go @@ -3,7 +3,6 @@ package control import ( "net" "net/netip" - _ "unsafe" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" @@ -52,46 +51,41 @@ func (f *DefaultInterfaceFinder) Interfaces() []Interface { return f.interfaces } -func (f *DefaultInterfaceFinder) InterfaceIndexByName(name string) (int, error) { +func (f *DefaultInterfaceFinder) ByName(name string) (*Interface, error) { for _, netInterface := range f.interfaces { if netInterface.Name == name { - return netInterface.Index, nil + return &netInterface, nil } } - netInterface, err := net.InterfaceByName(name) + _, err := net.InterfaceByName(name) if err != nil { - return 0, err + err = f.Update() + if err != nil { + return nil, err + } + return f.ByName(name) } - f.Update() - return netInterface.Index, nil + return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: &net.IPAddr{IP: nil}, Err: E.New("no such network interface")} } -func (f *DefaultInterfaceFinder) InterfaceNameByIndex(index int) (string, error) { +func (f *DefaultInterfaceFinder) ByIndex(index int) (*Interface, error) { for _, netInterface := range f.interfaces { if netInterface.Index == index { - return netInterface.Name, nil + return &netInterface, nil } } - netInterface, err := net.InterfaceByIndex(index) + _, err := net.InterfaceByIndex(index) if err != nil { - return "", err + err = f.Update() + if err != nil { + return nil, err + } + return f.ByIndex(index) } - f.Update() - return netInterface.Name, nil + return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: &net.IPAddr{IP: nil}, Err: E.New("no such network interface")} } -func (f *DefaultInterfaceFinder) InterfaceByAddr(addr netip.Addr) (*Interface, error) { - for _, netInterface := range f.interfaces { - for _, prefix := range netInterface.Addresses { - if prefix.Contains(addr) { - return &netInterface, nil - } - } - } - err := f.Update() - if err != nil { - return nil, err - } +func (f *DefaultInterfaceFinder) ByAddr(addr netip.Addr) (*Interface, error) { for _, netInterface := range f.interfaces { for _, prefix := range netInterface.Addresses { if prefix.Contains(addr) { diff --git a/common/control/bind_linux.go b/common/control/bind_linux.go index c92bf6b0..c5e668d7 100644 --- a/common/control/bind_linux.go +++ b/common/control/bind_linux.go @@ -19,11 +19,11 @@ func bindToInterface(conn syscall.RawConn, network string, address string, finde if interfaceName == "" { return os.ErrInvalid } - var err error - interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + iif, err := finder.ByName(interfaceName) if err != nil { return err } + interfaceIndex = iif.Index } err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex) if err == nil { diff --git a/common/control/bind_windows.go b/common/control/bind_windows.go index a499556f..cf833868 100644 --- a/common/control/bind_windows.go +++ b/common/control/bind_windows.go @@ -11,19 +11,19 @@ import ( func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error { return Raw(conn, func(fd uintptr) error { - var err error if interfaceIndex == -1 { if finder == nil { return os.ErrInvalid } - interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + iif, err := finder.ByName(interfaceName) if err != nil { return err } + interfaceIndex = iif.Index } handle := syscall.Handle(fd) if M.ParseSocksaddr(address).AddrString() == "" { - err = bind4(handle, interfaceIndex) + err := bind4(handle, interfaceIndex) if err != nil { return err }