config.go 4.1 KB

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