outbound.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. UDPFragment *bool `json:"udp_fragment,omitempty"`
  71. UDPFragmentDefault bool `json:"-"`
  72. DomainResolver *DomainResolveOptions `json:"domain_resolver,omitempty"`
  73. NetworkStrategy *NetworkStrategy `json:"network_strategy,omitempty"`
  74. NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
  75. FallbackNetworkType badoption.Listable[InterfaceType] `json:"fallback_network_type,omitempty"`
  76. FallbackDelay badoption.Duration `json:"fallback_delay,omitempty"`
  77. // Deprecated: migrated to domain resolver
  78. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  79. }
  80. type _DomainResolveOptions struct {
  81. Server string `json:"server"`
  82. Strategy DomainStrategy `json:"strategy,omitempty"`
  83. DisableCache bool `json:"disable_cache,omitempty"`
  84. RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
  85. ClientSubnet *badoption.Prefixable `json:"client_subnet,omitempty"`
  86. }
  87. type DomainResolveOptions _DomainResolveOptions
  88. func (o DomainResolveOptions) MarshalJSON() ([]byte, error) {
  89. if o.Server == "" {
  90. return []byte("{}"), nil
  91. } else if o.Strategy == DomainStrategy(C.DomainStrategyAsIS) &&
  92. !o.DisableCache &&
  93. o.RewriteTTL == nil &&
  94. o.ClientSubnet == nil {
  95. return json.Marshal(o.Server)
  96. } else {
  97. return json.Marshal((_DomainResolveOptions)(o))
  98. }
  99. }
  100. func (o *DomainResolveOptions) UnmarshalJSON(bytes []byte) error {
  101. var stringValue string
  102. err := json.Unmarshal(bytes, &stringValue)
  103. if err == nil {
  104. o.Server = stringValue
  105. return nil
  106. }
  107. err = json.Unmarshal(bytes, (*_DomainResolveOptions)(o))
  108. if err != nil {
  109. return err
  110. }
  111. if o.Server == "" {
  112. return E.New("empty domain_resolver.server")
  113. }
  114. return nil
  115. }
  116. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  117. return *o
  118. }
  119. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  120. *o = options
  121. }
  122. type ServerOptionsWrapper interface {
  123. TakeServerOptions() ServerOptions
  124. ReplaceServerOptions(options ServerOptions)
  125. }
  126. type ServerOptions struct {
  127. Server string `json:"server"`
  128. ServerPort uint16 `json:"server_port"`
  129. }
  130. func (o ServerOptions) Build() M.Socksaddr {
  131. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  132. }
  133. func (o ServerOptions) ServerIsDomain() bool {
  134. return M.IsDomainName(o.Server)
  135. }
  136. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  137. return *o
  138. }
  139. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  140. *o = options
  141. }