mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 20:37:40 +03:00
Improve linux bind interface
This commit is contained in:
parent
96a05f9afe
commit
7b03639e20
5 changed files with 65 additions and 38 deletions
|
@ -1,10 +1,9 @@
|
||||||
package control
|
package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
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 {
|
func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
||||||
if interfaceName == "" && interfaceIndex == -1 {
|
if interfaceName == "" && interfaceIndex == -1 {
|
||||||
return nil
|
return E.New("interface not found: ", interfaceName)
|
||||||
}
|
}
|
||||||
if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
|
if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName {
|
return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
package control
|
package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 {
|
||||||
if interfaceIndex == -1 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return Raw(conn, func(fd uintptr) 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 {
|
switch network {
|
||||||
case "tcp6", "udp6":
|
case "tcp6", "udp6":
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex)
|
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex)
|
||||||
|
|
|
@ -1,13 +1,48 @@
|
||||||
package control
|
package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common/atomic"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"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 {
|
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)
|
return unix.BindToDevice(int(fd), interfaceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@ package control
|
||||||
|
|
||||||
import "syscall"
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,28 @@ package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
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 {
|
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)
|
handle := syscall.Handle(fd)
|
||||||
if M.ParseSocksaddr(address).AddrString() == "" {
|
if M.ParseSocksaddr(address).AddrString() == "" {
|
||||||
err := bind4(handle, interfaceIndex)
|
err = bind4(handle, interfaceIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue