config.go 3.7 KB

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