Skip to content

Commit

Permalink
control: Refactor interface finder
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 11, 2024
1 parent 0998999 commit 04574bd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 56 deletions.
21 changes: 0 additions & 21 deletions common/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions common/control/bind_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions common/control/bind_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 19 additions & 25 deletions common/control/bind_finder_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package control
import (
"net"
"net/netip"
_ "unsafe"

"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions common/control/bind_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions common/control/bind_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 04574bd

Please sign in to comment.