outbound.go 5.6 KB

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