2022-10-25 04:55:00 +00:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
2023-04-04 20:38:56 +00:00
|
|
|
"bytes"
|
2023-04-02 02:35:03 +00:00
|
|
|
"context"
|
2023-12-12 12:18:49 +00:00
|
|
|
"net/netip"
|
|
|
|
"os"
|
2023-04-02 02:35:03 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box"
|
2023-12-12 12:18:49 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/process"
|
2024-11-06 04:51:53 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing-box/include"
|
2022-10-25 04:55:00 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-12-12 12:18:49 +00:00
|
|
|
"github.com/sagernet/sing-tun"
|
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-10-25 04:55:00 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2023-12-01 12:15:11 +00:00
|
|
|
"github.com/sagernet/sing/common/json"
|
2023-12-12 12:18:49 +00:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
|
|
|
"github.com/sagernet/sing/common/x/list"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-10-25 04:55:00 +00:00
|
|
|
)
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
func parseConfig(ctx context.Context, configContent string) (option.Options, error) {
|
|
|
|
options, err := json.UnmarshalExtendedContext[option.Options](ctx, []byte(configContent))
|
2022-10-25 04:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return option.Options{}, E.Cause(err, "decode config")
|
|
|
|
}
|
|
|
|
return options, nil
|
|
|
|
}
|
2023-04-02 02:35:03 +00:00
|
|
|
|
|
|
|
func CheckConfig(configContent string) error {
|
2024-11-21 10:10:41 +00:00
|
|
|
ctx := box.Context(context.Background(), include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry())
|
2024-11-01 16:39:02 +00:00
|
|
|
options, err := parseConfig(ctx, configContent)
|
2023-04-02 02:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-01 16:39:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2023-04-02 02:35:03 +00:00
|
|
|
defer cancel()
|
2024-11-01 16:39:02 +00:00
|
|
|
ctx = service.ContextWith[platform.Interface](ctx, (*platformInterfaceStub)(nil))
|
2023-04-03 10:24:20 +00:00
|
|
|
instance, err := box.New(box.Options{
|
2024-11-01 16:39:02 +00:00
|
|
|
Context: ctx,
|
|
|
|
Options: options,
|
2023-04-03 10:24:20 +00:00
|
|
|
})
|
2023-04-02 02:35:03 +00:00
|
|
|
if err == nil {
|
|
|
|
instance.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-04-04 20:38:56 +00:00
|
|
|
|
2023-12-12 12:18:49 +00:00
|
|
|
type platformInterfaceStub struct{}
|
|
|
|
|
2024-11-10 04:11:21 +00:00
|
|
|
func (s *platformInterfaceStub) Initialize(networkManager adapter.NetworkManager) error {
|
2023-12-12 12:18:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *platformInterfaceStub) UsePlatformAutoDetectInterfaceControl() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-11-06 04:51:53 +00:00
|
|
|
func (s *platformInterfaceStub) AutoDetectInterfaceControl(fd int) error {
|
2023-12-12 12:18:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-11-11 08:23:45 +00:00
|
|
|
func (s *platformInterfaceStub) Interfaces() ([]adapter.NetworkInterface, error) {
|
2023-12-12 12:18:49 +00:00
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *platformInterfaceStub) UnderNetworkExtension() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-07 12:34:24 +00:00
|
|
|
func (s *platformInterfaceStub) IncludeAllNetworks() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-12 12:18:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-11 08:23:45 +00:00
|
|
|
func (s *interfaceMonitorStub) DefaultInterface() *control.Interface {
|
|
|
|
return nil
|
2023-12-12 12:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]) {
|
|
|
|
}
|
|
|
|
|
2024-11-06 04:51:53 +00:00
|
|
|
func (s *platformInterfaceStub) SendNotification(notification *platform.Notification) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 20:38:56 +00:00
|
|
|
func FormatConfig(configContent string) (string, error) {
|
2024-11-21 10:10:41 +00:00
|
|
|
options, err := parseConfig(box.Context(context.Background(), include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry()), configContent)
|
2023-04-04 20:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buffer)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
err = encoder.Encode(options)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buffer.String(), nil
|
|
|
|
}
|