outbound.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package option
  2. import (
  3. "context"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/sagernet/sing/common/json"
  7. "github.com/sagernet/sing/common/json/badjson"
  8. "github.com/sagernet/sing/common/json/badoption"
  9. M "github.com/sagernet/sing/common/metadata"
  10. "github.com/sagernet/sing/service"
  11. )
  12. type OutboundOptionsRegistry interface {
  13. CreateOptions(outboundType string) (any, bool)
  14. }
  15. type _Outbound struct {
  16. Type string `json:"type"`
  17. Tag string `json:"tag,omitempty"`
  18. Options any `json:"-"`
  19. }
  20. type Outbound _Outbound
  21. func (h *Outbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
  22. return badjson.MarshallObjectsContext(ctx, (*_Outbound)(h), h.Options)
  23. }
  24. func (h *Outbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
  25. err := json.UnmarshalContext(ctx, content, (*_Outbound)(h))
  26. if err != nil {
  27. return err
  28. }
  29. registry := service.FromContext[OutboundOptionsRegistry](ctx)
  30. if registry == nil {
  31. return E.New("missing outbound options registry in context")
  32. }
  33. switch h.Type {
  34. case C.TypeDNS:
  35. return E.New("dns outbound is deprecated in sing-box 1.11.0 and removed in sing-box 1.13.0, use rule actions instead")
  36. }
  37. options, loaded := registry.CreateOptions(h.Type)
  38. if !loaded {
  39. return E.New("unknown outbound type: ", h.Type)
  40. }
  41. err = badjson.UnmarshallExcludedContext(ctx, content, (*_Outbound)(h), options)
  42. if err != nil {
  43. return err
  44. }
  45. if listenWrapper, isListen := options.(ListenOptionsWrapper); isListen {
  46. //nolint:staticcheck
  47. if listenWrapper.TakeListenOptions().InboundOptions != (InboundOptions{}) {
  48. return E.New("legacy inbound fields are deprecated in sing-box 1.11.0 and removed in sing-box 1.13.0, use rule actions instead")
  49. }
  50. }
  51. h.Options = options
  52. return nil
  53. }
  54. type DialerOptionsWrapper interface {
  55. TakeDialerOptions() DialerOptions
  56. ReplaceDialerOptions(options DialerOptions)
  57. }
  58. type DialerOptions struct {
  59. Detour string `json:"detour,omitempty"`
  60. BindInterface string `json:"bind_interface,omitempty"`
  61. Inet4BindAddress *badoption.Addr `json:"inet4_bind_address,omitempty"`
  62. Inet6BindAddress *badoption.Addr `json:"inet6_bind_address,omitempty"`
  63. BindAddressNoPort bool `json:"bind_address_no_port,omitempty"`
  64. ProtectPath string `json:"protect_path,omitempty"`
  65. RoutingMark FwMark `json:"routing_mark,omitempty"`
  66. ReuseAddr bool `json:"reuse_addr,omitempty"`
  67. NetNs string `json:"netns,omitempty"`
  68. ConnectTimeout badoption.Duration `json:"connect_timeout,omitempty"`
  69. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  70. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  71. DisableTCPKeepAlive bool `json:"disable_tcp_keep_alive,omitempty"`
  72. TCPKeepAlive badoption.Duration `json:"tcp_keep_alive,omitempty"`
  73. TCPKeepAliveInterval badoption.Duration `json:"tcp_keep_alive_interval,omitempty"`
  74. UDPFragment *bool `json:"udp_fragment,omitempty"`
  75. UDPFragmentDefault bool `json:"-"`
  76. DomainResolver *DomainResolveOptions `json:"domain_resolver,omitempty"`
  77. NetworkStrategy *NetworkStrategy `json:"network_strategy,omitempty"`
  78. NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
  79. FallbackNetworkType badoption.Listable[InterfaceType] `json:"fallback_network_type,omitempty"`
  80. FallbackDelay badoption.Duration `json:"fallback_delay,omitempty"`
  81. // Deprecated: migrated to domain resolver
  82. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  83. }
  84. type _DomainResolveOptions struct {
  85. Server string `json:"server"`
  86. Strategy DomainStrategy `json:"strategy,omitempty"`
  87. DisableCache bool `json:"disable_cache,omitempty"`
  88. RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
  89. ClientSubnet *badoption.Prefixable `json:"client_subnet,omitempty"`
  90. }
  91. type DomainResolveOptions _DomainResolveOptions
  92. func (o DomainResolveOptions) MarshalJSON() ([]byte, error) {
  93. if o.Server == "" {
  94. return []byte("{}"), nil
  95. } else if o.Strategy == DomainStrategy(C.DomainStrategyAsIS) &&
  96. !o.DisableCache &&
  97. o.RewriteTTL == nil &&
  98. o.ClientSubnet == nil {
  99. return json.Marshal(o.Server)
  100. } else {
  101. return json.Marshal((_DomainResolveOptions)(o))
  102. }
  103. }
  104. func (o *DomainResolveOptions) UnmarshalJSON(bytes []byte) error {
  105. var stringValue string
  106. err := json.Unmarshal(bytes, &stringValue)
  107. if err == nil {
  108. o.Server = stringValue
  109. return nil
  110. }
  111. err = json.Unmarshal(bytes, (*_DomainResolveOptions)(o))
  112. if err != nil {
  113. return err
  114. }
  115. if o.Server == "" {
  116. return E.New("empty domain_resolver.server")
  117. }
  118. return nil
  119. }
  120. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  121. return *o
  122. }
  123. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  124. *o = options
  125. }
  126. type ServerOptionsWrapper interface {
  127. TakeServerOptions() ServerOptions
  128. ReplaceServerOptions(options ServerOptions)
  129. }
  130. type ServerOptions struct {
  131. Server string `json:"server"`
  132. ServerPort uint16 `json:"server_port"`
  133. }
  134. func (o ServerOptions) Build() M.Socksaddr {
  135. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  136. }
  137. func (o ServerOptions) ServerIsDomain() bool {
  138. return o.Build().IsDomain()
  139. }
  140. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  141. return *o
  142. }
  143. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  144. *o = options
  145. }