Add auto_route and auto_detect_interface for linux
This commit is contained in:
23
common/dialer/auto_linux.go
Normal file
23
common/dialer/auto_linux.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
func BindToInterface(router adapter.Router) control.Func {
|
||||
return func(network, address string, conn syscall.RawConn) error {
|
||||
interfaceName := router.DefaultInterfaceName()
|
||||
if interfaceName == "" {
|
||||
return nil
|
||||
}
|
||||
var innerErr error
|
||||
err := conn.Control(func(fd uintptr) {
|
||||
innerErr = syscall.BindToDevice(int(fd), interfaceName)
|
||||
})
|
||||
return E.Errors(innerErr, err)
|
||||
}
|
||||
}
|
||||
12
common/dialer/auto_other.go
Normal file
12
common/dialer/auto_other.go
Normal file
@@ -0,0 +1,12 @@
|
||||
//go:build !linux
|
||||
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
)
|
||||
|
||||
func BindToInterface(router adapter.Router) control.Func {
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
@@ -18,12 +19,15 @@ type DefaultDialer struct {
|
||||
net.ListenConfig
|
||||
}
|
||||
|
||||
func NewDefault(options option.DialerOptions) *DefaultDialer {
|
||||
func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDialer {
|
||||
var dialer net.Dialer
|
||||
var listener net.ListenConfig
|
||||
if options.BindInterface != "" {
|
||||
dialer.Control = control.Append(dialer.Control, control.BindToInterface(options.BindInterface))
|
||||
listener.Control = control.Append(listener.Control, control.BindToInterface(options.BindInterface))
|
||||
} else if router.AutoDetectInterface() {
|
||||
dialer.Control = BindToInterface(router)
|
||||
listener.Control = BindToInterface(router)
|
||||
}
|
||||
if options.RoutingMark != 0 {
|
||||
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
func New(router adapter.Router, options option.DialerOptions) N.Dialer {
|
||||
if options.Detour == "" {
|
||||
return NewDefault(options)
|
||||
return NewDefault(router, options)
|
||||
} else {
|
||||
return NewDetour(router, options.Detour)
|
||||
}
|
||||
|
||||
9
common/iffmonitor/monitor.go
Normal file
9
common/iffmonitor/monitor.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package iffmonitor
|
||||
|
||||
import "github.com/sagernet/sing-box/adapter"
|
||||
|
||||
type InterfaceMonitor interface {
|
||||
adapter.Service
|
||||
DefaultInterfaceName() string
|
||||
DefaultInterfaceIndex() int
|
||||
}
|
||||
100
common/iffmonitor/monitor_linux.go
Normal file
100
common/iffmonitor/monitor_linux.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package iffmonitor
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/log"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
var _ InterfaceMonitor = (*monitor)(nil)
|
||||
|
||||
type monitor struct {
|
||||
logger log.Logger
|
||||
defaultInterfaceName string
|
||||
defaultInterfaceIndex int
|
||||
update chan netlink.RouteUpdate
|
||||
close chan struct{}
|
||||
}
|
||||
|
||||
func New(logger log.Logger) (InterfaceMonitor, error) {
|
||||
return &monitor{
|
||||
logger: logger,
|
||||
update: make(chan netlink.RouteUpdate, 2),
|
||||
close: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *monitor) Start() error {
|
||||
err := netlink.RouteSubscribe(m.update, m.close)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = m.checkUpdate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *monitor) loopUpdate() {
|
||||
for {
|
||||
select {
|
||||
case <-m.close:
|
||||
return
|
||||
case <-m.update:
|
||||
err := m.checkUpdate()
|
||||
if err != nil {
|
||||
m.logger.Error(E.Cause(err, "check default interface"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *monitor) checkUpdate() error {
|
||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, route := range routes {
|
||||
if route.Dst != nil {
|
||||
continue
|
||||
}
|
||||
var link netlink.Link
|
||||
link, err = netlink.LinkByIndex(route.LinkIndex)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if link.Type() == "tuntap" {
|
||||
continue
|
||||
}
|
||||
|
||||
m.defaultInterfaceName = link.Attrs().Name
|
||||
m.defaultInterfaceIndex = link.Attrs().Index
|
||||
|
||||
m.logger.Info("updated default interface ", m.defaultInterfaceName, ", index ", m.defaultInterfaceIndex)
|
||||
return nil
|
||||
}
|
||||
return E.New("no route to internet")
|
||||
}
|
||||
|
||||
func (m *monitor) Close() error {
|
||||
select {
|
||||
case <-m.close:
|
||||
return os.ErrClosed
|
||||
default:
|
||||
}
|
||||
close(m.close)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *monitor) DefaultInterfaceName() string {
|
||||
return m.defaultInterfaceName
|
||||
}
|
||||
|
||||
func (m *monitor) DefaultInterfaceIndex() int {
|
||||
return m.defaultInterfaceIndex
|
||||
}
|
||||
13
common/iffmonitor/monitor_other.go
Normal file
13
common/iffmonitor/monitor_other.go
Normal file
@@ -0,0 +1,13 @@
|
||||
//go:build !linux
|
||||
|
||||
package iffmonitor
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/log"
|
||||
)
|
||||
|
||||
func New(logger log.Logger) (InterfaceMonitor, error) {
|
||||
return nil, os.ErrInvalid
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
@@ -15,7 +16,7 @@ func Open(name string) (uintptr, error) {
|
||||
return uintptr(tunFd), nil
|
||||
}
|
||||
|
||||
func Configure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix, mtu uint32) error {
|
||||
func Configure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix, mtu uint32, autoRoute bool) error {
|
||||
tunLink, err := netlink.LinkByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -47,5 +48,66 @@ func Configure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix
|
||||
return err
|
||||
}
|
||||
|
||||
if autoRoute {
|
||||
if inet4Address.IsValid() {
|
||||
err = netlink.RouteAdd(&netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.CIDRMask(0, 32),
|
||||
},
|
||||
LinkIndex: tunLink.Attrs().Index,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if inet6Address.IsValid() {
|
||||
err = netlink.RouteAdd(&netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.CIDRMask(0, 128),
|
||||
},
|
||||
LinkIndex: tunLink.Attrs().Index,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnConfigure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix, autoRoute bool) error {
|
||||
if autoRoute {
|
||||
tunLink, err := netlink.LinkByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if inet4Address.IsValid() {
|
||||
err = netlink.RouteDel(&netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.CIDRMask(0, 32),
|
||||
},
|
||||
LinkIndex: tunLink.Attrs().Index,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if inet6Address.IsValid() {
|
||||
err = netlink.RouteDel(&netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.CIDRMask(0, 128),
|
||||
},
|
||||
LinkIndex: tunLink.Attrs().Index,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,9 +3,18 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Open(name string) (uintptr, error) {
|
||||
return 0, os.ErrInvalid
|
||||
}
|
||||
|
||||
func Configure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix, mtu uint32, autoRoute bool) error {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
func UnConfigure(name string, inet4Address netip.Prefix, inet6Address netip.Prefix, autoRoute bool) error {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user