From 2badcec76547674b6ce4226a3acc6b571554d2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 12 Dec 2023 20:18:49 +0800 Subject: [PATCH] platform: Fix check config --- experimental/libbox/config.go | 100 +++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/experimental/libbox/config.go b/experimental/libbox/config.go index 66651b60..73ee7e63 100644 --- a/experimental/libbox/config.go +++ b/experimental/libbox/config.go @@ -4,10 +4,19 @@ import ( "bytes" "context" "encoding/json" + "net/netip" + "os" "github.com/sagernet/sing-box" + "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/common/process" + "github.com/sagernet/sing-box/experimental/libbox/platform" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-tun" + "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" + "github.com/sagernet/sing/common/logger" + "github.com/sagernet/sing/common/x/list" ) func parseConfig(configContent string) (option.Options, error) { @@ -27,8 +36,9 @@ func CheckConfig(configContent string) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() instance, err := box.New(box.Options{ - Context: ctx, - Options: options, + Context: ctx, + Options: options, + PlatformInterface: (*platformInterfaceStub)(nil), }) if err == nil { instance.Close() @@ -36,6 +46,92 @@ func CheckConfig(configContent string) error { return err } +type platformInterfaceStub struct{} + +func (s *platformInterfaceStub) Initialize(ctx context.Context, router adapter.Router) error { + return nil +} + +func (s *platformInterfaceStub) UsePlatformAutoDetectInterfaceControl() bool { + return true +} + +func (s *platformInterfaceStub) AutoDetectInterfaceControl() control.Func { + return nil +} + +func (s *platformInterfaceStub) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) { + return nil, os.ErrInvalid +} + +func (s *platformInterfaceStub) UsePlatformDefaultInterfaceMonitor() bool { + return true +} + +func (s *platformInterfaceStub) CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor { + return (*interfaceMonitorStub)(nil) +} + +func (s *platformInterfaceStub) UsePlatformInterfaceGetter() bool { + return true +} + +func (s *platformInterfaceStub) Interfaces() ([]platform.NetworkInterface, error) { + return nil, os.ErrInvalid +} + +func (s *platformInterfaceStub) UnderNetworkExtension() bool { + return false +} + +func (s *platformInterfaceStub) ClearDNSCache() { +} + +func (s *platformInterfaceStub) ReadWIFIState() adapter.WIFIState { + return adapter.WIFIState{} +} + +func (s *platformInterfaceStub) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) { + return nil, os.ErrInvalid +} + +type interfaceMonitorStub struct{} + +func (s *interfaceMonitorStub) Start() error { + return os.ErrInvalid +} + +func (s *interfaceMonitorStub) Close() error { + return os.ErrInvalid +} + +func (s *interfaceMonitorStub) DefaultInterfaceName(destination netip.Addr) string { + return "" +} + +func (s *interfaceMonitorStub) DefaultInterfaceIndex(destination netip.Addr) int { + return -1 +} + +func (s *interfaceMonitorStub) DefaultInterface(destination netip.Addr) (string, int) { + return "", -1 +} + +func (s *interfaceMonitorStub) OverrideAndroidVPN() bool { + return false +} + +func (s *interfaceMonitorStub) AndroidVPNEnabled() bool { + return false +} + +func (s *interfaceMonitorStub) RegisterCallback(callback tun.DefaultInterfaceUpdateCallback) *list.Element[tun.DefaultInterfaceUpdateCallback] { + return nil +} + +func (s *interfaceMonitorStub) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) { +} + func FormatConfig(configContent string) (string, error) { options, err := parseConfig(configContent) if err != nil {