config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package libbox
  2. import (
  3. "bytes"
  4. "context"
  5. "net/netip"
  6. "os"
  7. "github.com/sagernet/sing-box"
  8. "github.com/sagernet/sing-box/adapter"
  9. "github.com/sagernet/sing-box/common/process"
  10. "github.com/sagernet/sing-box/experimental/libbox/platform"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing-tun"
  13. "github.com/sagernet/sing/common/control"
  14. E "github.com/sagernet/sing/common/exceptions"
  15. "github.com/sagernet/sing/common/json"
  16. "github.com/sagernet/sing/common/logger"
  17. "github.com/sagernet/sing/common/x/list"
  18. )
  19. func parseConfig(configContent string) (option.Options, error) {
  20. options, err := json.UnmarshalExtended[option.Options]([]byte(configContent))
  21. if err != nil {
  22. return option.Options{}, E.Cause(err, "decode config")
  23. }
  24. return options, nil
  25. }
  26. func CheckConfig(configContent string) error {
  27. options, err := parseConfig(configContent)
  28. if err != nil {
  29. return err
  30. }
  31. ctx, cancel := context.WithCancel(context.Background())
  32. defer cancel()
  33. instance, err := box.New(box.Options{
  34. Context: ctx,
  35. Options: options,
  36. PlatformInterface: (*platformInterfaceStub)(nil),
  37. })
  38. if err == nil {
  39. instance.Close()
  40. }
  41. return err
  42. }
  43. type platformInterfaceStub struct{}
  44. func (s *platformInterfaceStub) Initialize(ctx context.Context, router adapter.Router) error {
  45. return nil
  46. }
  47. func (s *platformInterfaceStub) UsePlatformAutoDetectInterfaceControl() bool {
  48. return true
  49. }
  50. func (s *platformInterfaceStub) AutoDetectInterfaceControl() control.Func {
  51. return nil
  52. }
  53. func (s *platformInterfaceStub) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) {
  54. return nil, os.ErrInvalid
  55. }
  56. func (s *platformInterfaceStub) UsePlatformDefaultInterfaceMonitor() bool {
  57. return true
  58. }
  59. func (s *platformInterfaceStub) CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor {
  60. return (*interfaceMonitorStub)(nil)
  61. }
  62. func (s *platformInterfaceStub) UsePlatformInterfaceGetter() bool {
  63. return true
  64. }
  65. func (s *platformInterfaceStub) Interfaces() ([]platform.NetworkInterface, error) {
  66. return nil, os.ErrInvalid
  67. }
  68. func (s *platformInterfaceStub) UnderNetworkExtension() bool {
  69. return false
  70. }
  71. func (s *platformInterfaceStub) ClearDNSCache() {
  72. }
  73. func (s *platformInterfaceStub) ReadWIFIState() adapter.WIFIState {
  74. return adapter.WIFIState{}
  75. }
  76. func (s *platformInterfaceStub) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) {
  77. return nil, os.ErrInvalid
  78. }
  79. type interfaceMonitorStub struct{}
  80. func (s *interfaceMonitorStub) Start() error {
  81. return os.ErrInvalid
  82. }
  83. func (s *interfaceMonitorStub) Close() error {
  84. return os.ErrInvalid
  85. }
  86. func (s *interfaceMonitorStub) DefaultInterfaceName(destination netip.Addr) string {
  87. return ""
  88. }
  89. func (s *interfaceMonitorStub) DefaultInterfaceIndex(destination netip.Addr) int {
  90. return -1
  91. }
  92. func (s *interfaceMonitorStub) DefaultInterface(destination netip.Addr) (string, int) {
  93. return "", -1
  94. }
  95. func (s *interfaceMonitorStub) OverrideAndroidVPN() bool {
  96. return false
  97. }
  98. func (s *interfaceMonitorStub) AndroidVPNEnabled() bool {
  99. return false
  100. }
  101. func (s *interfaceMonitorStub) RegisterCallback(callback tun.DefaultInterfaceUpdateCallback) *list.Element[tun.DefaultInterfaceUpdateCallback] {
  102. return nil
  103. }
  104. func (s *interfaceMonitorStub) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) {
  105. }
  106. func FormatConfig(configContent string) (string, error) {
  107. options, err := parseConfig(configContent)
  108. if err != nil {
  109. return "", err
  110. }
  111. var buffer bytes.Buffer
  112. json.NewEncoder(&buffer)
  113. encoder := json.NewEncoder(&buffer)
  114. encoder.SetIndent("", " ")
  115. err = encoder.Encode(options)
  116. if err != nil {
  117. return "", err
  118. }
  119. return buffer.String(), nil
  120. }