config.go 3.7 KB

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