outbound.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. DisableOptimisticCache bool `json:"disable_optimistic_cache,omitempty"`
  89. RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
  90. ClientSubnet *badoption.Prefixable `json:"client_subnet,omitempty"`
  91. }
  92. type DomainResolveOptions _DomainResolveOptions
  93. func (o DomainResolveOptions) MarshalJSON() ([]byte, error) {
  94. if o.Server == "" {
  95. return []byte("{}"), nil
  96. } else if o.Strategy == DomainStrategy(C.DomainStrategyAsIS) &&
  97. !o.DisableCache &&
  98. !o.DisableOptimisticCache &&
  99. o.RewriteTTL == nil &&
  100. o.ClientSubnet == nil {
  101. return json.Marshal(o.Server)
  102. } else {
  103. return json.Marshal((_DomainResolveOptions)(o))
  104. }
  105. }
  106. func (o *DomainResolveOptions) UnmarshalJSON(bytes []byte) error {
  107. var stringValue string
  108. err := json.Unmarshal(bytes, &stringValue)
  109. if err == nil {
  110. o.Server = stringValue
  111. return nil
  112. }
  113. err = json.Unmarshal(bytes, (*_DomainResolveOptions)(o))
  114. if err != nil {
  115. return err
  116. }
  117. if o.Server == "" {
  118. return E.New("empty domain_resolver.server")
  119. }
  120. return nil
  121. }
  122. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  123. return *o
  124. }
  125. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  126. *o = options
  127. }
  128. type ServerOptionsWrapper interface {
  129. TakeServerOptions() ServerOptions
  130. ReplaceServerOptions(options ServerOptions)
  131. }
  132. type ServerOptions struct {
  133. Server string `json:"server"`
  134. ServerPort uint16 `json:"server_port"`
  135. }
  136. func (o ServerOptions) Build() M.Socksaddr {
  137. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  138. }
  139. func (o ServerOptions) ServerIsDomain() bool {
  140. return o.Build().IsDomain()
  141. }
  142. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  143. return *o
  144. }
  145. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  146. *o = options
  147. }