mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 11:57:39 +03:00
Improve linux bind interface
This commit is contained in:
parent
570295cd12
commit
27518fdf12
5 changed files with 65 additions and 38 deletions
|
@ -1,10 +1,9 @@
|
|||
package control
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
@ -25,38 +24,12 @@ func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, addr
|
|||
}
|
||||
}
|
||||
|
||||
const useInterfaceName = runtime.GOOS == "linux" || runtime.GOOS == "android"
|
||||
|
||||
func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||
if interfaceName == "" && interfaceIndex == -1 {
|
||||
return nil
|
||||
return E.New("interface not found: ", interfaceName)
|
||||
}
|
||||
if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
|
||||
return nil
|
||||
}
|
||||
if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName {
|
||||
return bindToInterface(conn, network, address, interfaceName, interfaceIndex)
|
||||
}
|
||||
if finder == nil {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
var err error
|
||||
if useInterfaceName {
|
||||
interfaceName, err = finder.InterfaceNameByIndex(interfaceIndex)
|
||||
} else {
|
||||
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if useInterfaceName {
|
||||
if interfaceName == "" {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
if interfaceIndex == -1 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return bindToInterface(conn, network, address, interfaceName, interfaceIndex)
|
||||
return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex)
|
||||
}
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
package control
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||
if interfaceIndex == -1 {
|
||||
return nil
|
||||
}
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) 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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
switch network {
|
||||
case "tcp6", "udp6":
|
||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex)
|
||||
|
|
|
@ -1,13 +1,48 @@
|
|||
package control
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/sagernet/sing/common/atomic"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||
var ifIndexDisabled atomic.Bool
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) 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
|
||||
}
|
||||
}
|
||||
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
||||
if err == nil {
|
||||
return nil
|
||||
} else if E.IsMulti(err, unix.ENOPROTOOPT, unix.EINVAL) {
|
||||
ifIndexDisabled.Store(true)
|
||||
} else {
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -4,6 +4,6 @@ package control
|
|||
|
||||
import "syscall"
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,17 +2,28 @@ package control
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
)
|
||||
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) 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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
handle := syscall.Handle(fd)
|
||||
if M.ParseSocksaddr(address).AddrString() == "" {
|
||||
err := bind4(handle, interfaceIndex)
|
||||
err = bind4(handle, interfaceIndex)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue