sing-box/experimental/libbox/config.go

150 lines
3.7 KiB
Go
Raw Normal View History

2022-10-25 12:55:00 +08:00
package libbox
import (
2023-04-05 04:38:56 +08:00
"bytes"
2023-04-02 10:35:03 +08:00
"context"
2023-04-05 04:38:56 +08:00
"encoding/json"
2023-12-12 20:18:49 +08:00
"net/netip"
"os"
2023-04-02 10:35:03 +08:00
"github.com/sagernet/sing-box"
2023-12-12 20:18:49 +08:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/process"
"github.com/sagernet/sing-box/experimental/libbox/platform"
2022-10-25 12:55:00 +08:00
"github.com/sagernet/sing-box/option"
2023-12-12 20:18:49 +08:00
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control"
2022-10-25 12:55:00 +08:00
E "github.com/sagernet/sing/common/exceptions"
2023-12-12 20:18:49 +08:00
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list"
2022-10-25 12:55:00 +08:00
)
func parseConfig(configContent string) (option.Options, error) {
var options option.Options
err := options.UnmarshalJSON([]byte(configContent))
if err != nil {
return option.Options{}, E.Cause(err, "decode config")
}
return options, nil
}
2023-04-02 10:35:03 +08:00
func CheckConfig(configContent string) error {
options, err := parseConfig(configContent)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2023-04-03 18:24:20 +08:00
instance, err := box.New(box.Options{
2023-12-12 20:18:49 +08:00
Context: ctx,
Options: options,
PlatformInterface: (*platformInterfaceStub)(nil),
2023-04-03 18:24:20 +08:00
})
2023-04-02 10:35:03 +08:00
if err == nil {
instance.Close()
}
return err
}
2023-04-05 04:38:56 +08:00
2023-12-12 20:18:49 +08:00
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]) {
}
2023-04-05 04:38:56 +08:00
func FormatConfig(configContent string) (string, error) {
options, err := parseConfig(configContent)
if err != nil {
return "", err
}
var buffer bytes.Buffer
json.NewEncoder(&buffer)
encoder := json.NewEncoder(&buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(options)
if err != nil {
return "", err
}
return buffer.String(), nil
}