diff --git a/inbound/tun.go b/inbound/tun.go index 286c0b4f..da3b66b4 100644 --- a/inbound/tun.go +++ b/inbound/tun.go @@ -1,3 +1,5 @@ +//go:build linux || windows + package inbound import ( diff --git a/inbound/tun_stub.go b/inbound/tun_stub.go new file mode 100644 index 00000000..e04e2494 --- /dev/null +++ b/inbound/tun_stub.go @@ -0,0 +1,16 @@ +//go:build !linux && !windows + +package inbound + +import ( + "context" + "os" + + "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing-box/option" +) + +func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TunInboundOptions) (adapter.Inbound, error) { + return nil, os.ErrInvalid +} diff --git a/route/iface.go b/route/iface.go new file mode 100644 index 00000000..c3a5f619 --- /dev/null +++ b/route/iface.go @@ -0,0 +1,22 @@ +package route + +import "github.com/sagernet/sing/common/x/list" + +type ( + NetworkUpdateCallback = func() error + DefaultInterfaceUpdateCallback = func() +) + +type NetworkUpdateMonitor interface { + Start() error + Close() error + RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback] + UnregisterCallback(element *list.Element[NetworkUpdateCallback]) +} + +type DefaultInterfaceMonitor interface { + Start() error + Close() error + DefaultInterfaceName() string + DefaultInterfaceIndex() int +} diff --git a/route/iface_stub.go b/route/iface_stub.go new file mode 100644 index 00000000..7f14f1b4 --- /dev/null +++ b/route/iface_stub.go @@ -0,0 +1,17 @@ +//go:build !linux && !windows + +package route + +import ( + "os" + + E "github.com/sagernet/sing/common/exceptions" +) + +func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { + return nil, os.ErrInvalid +} + +func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, callback DefaultInterfaceUpdateCallback) (DefaultInterfaceMonitor, error) { + return nil, os.ErrInvalid +} diff --git a/route/iface_tun.go b/route/iface_tun.go new file mode 100644 index 00000000..62b5d141 --- /dev/null +++ b/route/iface_tun.go @@ -0,0 +1,16 @@ +//go:build linux || windows + +package route + +import ( + "github.com/sagernet/sing-tun" + E "github.com/sagernet/sing/common/exceptions" +) + +func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { + return tun.NewNetworkUpdateMonitor(errorHandler) +} + +func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, callback DefaultInterfaceUpdateCallback) (DefaultInterfaceMonitor, error) { + return tun.NewDefaultInterfaceMonitor(networkMonitor, callback) +} diff --git a/route/router.go b/route/router.go index 31d2c971..bd55aee3 100644 --- a/route/router.go +++ b/route/router.go @@ -22,7 +22,6 @@ import ( "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-dns" - "github.com/sagernet/sing-tun" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" @@ -66,10 +65,10 @@ type Router struct { transportMap map[string]dns.Transport interfaceBindManager control.BindManager - networkMonitor tun.NetworkUpdateMonitor + networkMonitor NetworkUpdateMonitor autoDetectInterface bool defaultInterface string - interfaceMonitor tun.DefaultInterfaceMonitor + interfaceMonitor DefaultInterfaceMonitor } func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.ContextLogger, options option.RouteOptions, dnsOptions option.DNSOptions) (*Router, error) { @@ -199,7 +198,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont router.transportMap = transportMap if router.interfaceBindManager != nil || options.AutoDetectInterface { - networkMonitor, err := tun.NewNetworkUpdateMonitor(router) + networkMonitor, err := NewNetworkUpdateMonitor(router) if err == nil { router.networkMonitor = networkMonitor if router.interfaceBindManager != nil { @@ -209,7 +208,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont } if router.networkMonitor != nil && options.AutoDetectInterface { - interfaceMonitor, err := tun.NewDefaultInterfaceMonitor(router.networkMonitor, func() { + interfaceMonitor, err := NewDefaultInterfaceMonitor(router.networkMonitor, func() { router.logger.Info("updated default interface ", router.interfaceMonitor.DefaultInterfaceName(), ", index ", router.interfaceMonitor.DefaultInterfaceIndex()) }) if err != nil {