config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(fd int) error {
  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() ([]control.Interface, error) {
  66. return nil, os.ErrInvalid
  67. }
  68. func (s *platformInterfaceStub) UnderNetworkExtension() bool {
  69. return false
  70. }
  71. func (s *platformInterfaceStub) IncludeAllNetworks() bool {
  72. return false
  73. }
  74. func (s *platformInterfaceStub) ClearDNSCache() {
  75. }
  76. func (s *platformInterfaceStub) ReadWIFIState() adapter.WIFIState {
  77. return adapter.WIFIState{}
  78. }
  79. func (s *platformInterfaceStub) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) {
  80. return nil, os.ErrInvalid
  81. }
  82. type interfaceMonitorStub struct{}
  83. func (s *interfaceMonitorStub) Start() error {
  84. return os.ErrInvalid
  85. }
  86. func (s *interfaceMonitorStub) Close() error {
  87. return os.ErrInvalid
  88. }
  89. func (s *interfaceMonitorStub) DefaultInterfaceName(destination netip.Addr) string {
  90. return ""
  91. }
  92. func (s *interfaceMonitorStub) DefaultInterfaceIndex(destination netip.Addr) int {
  93. return -1
  94. }
  95. func (s *interfaceMonitorStub) DefaultInterface(destination netip.Addr) (string, int) {
  96. return "", -1
  97. }
  98. func (s *interfaceMonitorStub) OverrideAndroidVPN() bool {
  99. return false
  100. }
  101. func (s *interfaceMonitorStub) AndroidVPNEnabled() bool {
  102. return false
  103. }
  104. func (s *interfaceMonitorStub) RegisterCallback(callback tun.DefaultInterfaceUpdateCallback) *list.Element[tun.DefaultInterfaceUpdateCallback] {
  105. return nil
  106. }
  107. func (s *interfaceMonitorStub) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) {
  108. }
  109. func (s *platformInterfaceStub) SendNotification(notification *platform.Notification) error {
  110. return nil
  111. }
  112. func FormatConfig(configContent string) (*StringBox, error) {
  113. options, err := parseConfig(configContent)
  114. if err != nil {
  115. return nil, err
  116. }
  117. var buffer bytes.Buffer
  118. encoder := json.NewEncoder(&buffer)
  119. encoder.SetIndent("", " ")
  120. err = encoder.Encode(options)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return wrapString(buffer.String()), nil
  125. }