config.go 4.2 KB

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