mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Add 'preferInterfaceName' parameter to BindToInterface0
This commit is contained in:
parent
c9319a35ee
commit
c501a58ae7
5 changed files with 19 additions and 27 deletions
|
@ -12,18 +12,19 @@ import (
|
|||
|
||||
var ifIndexDisabled atomic.Bool
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
|
||||
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 !ifIndexDisabled.Load() {
|
||||
if interfaceIndex == -1 {
|
||||
if finder == nil {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if interfaceIndex != -1 {
|
||||
return unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
||||
}
|
||||
if interfaceName == "" {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
if !preferInterfaceName && finder != nil && !ifIndexDisabled.Load() {
|
||||
var err error
|
||||
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
||||
if err == nil {
|
||||
|
@ -34,15 +35,6 @@ func bindToInterface(conn syscall.RawConn, network string, address string, finde
|
|||
return err
|
||||
}
|
||||
}
|
||||
if interfaceName == "" {
|
||||
if finder == nil {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
interfaceName, err = finder.InterfaceNameByIndex(interfaceIndex)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return unix.BindToDevice(int(fd), interfaceName)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue