outbound.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.TypeBlock, 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. ConnectTimeout badoption.Duration `json:"connect_timeout,omitempty"`
  67. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  68. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  69. UDPFragment *bool `json:"udp_fragment,omitempty"`
  70. UDPFragmentDefault bool `json:"-"`
  71. DomainResolver *DomainResolveOptions `json:"domain_resolver,omitempty"`
  72. NetworkStrategy *NetworkStrategy `json:"network_strategy,omitempty"`
  73. NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
  74. FallbackNetworkType badoption.Listable[InterfaceType] `json:"fallback_network_type,omitempty"`
  75. FallbackDelay badoption.Duration `json:"fallback_delay,omitempty"`
  76. IsWireGuardListener bool `json:"-"`
  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.Strategy == DomainStrategy(C.DomainStrategyAsIS) &&
  90. !o.DisableCache &&
  91. o.RewriteTTL == nil &&
  92. o.ClientSubnet == nil {
  93. return json.Marshal(o.Server)
  94. } else {
  95. return json.Marshal((_DomainResolveOptions)(o))
  96. }
  97. }
  98. func (o *DomainResolveOptions) UnmarshalJSON(bytes []byte) error {
  99. var stringValue string
  100. err := json.Unmarshal(bytes, &stringValue)
  101. if err == nil {
  102. o.Server = stringValue
  103. return nil
  104. }
  105. return json.Unmarshal(bytes, (*_DomainResolveOptions)(o))
  106. }
  107. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  108. return *o
  109. }
  110. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  111. *o = options
  112. }
  113. type ServerOptionsWrapper interface {
  114. TakeServerOptions() ServerOptions
  115. ReplaceServerOptions(options ServerOptions)
  116. }
  117. type ServerOptions struct {
  118. Server string `json:"server"`
  119. ServerPort uint16 `json:"server_port"`
  120. }
  121. func (o ServerOptions) Build() M.Socksaddr {
  122. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  123. }
  124. func (o ServerOptions) ServerIsDomain() bool {
  125. return M.IsDomainName(o.Server)
  126. }
  127. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  128. return *o
  129. }
  130. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  131. *o = options
  132. }