outbound.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 string `json:"domain_resolver,omitempty"`
  72. DomainStrategy DomainStrategy `json:"domain_strategy,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. IsWireGuardListener bool `json:"-"`
  78. }
  79. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  80. return *o
  81. }
  82. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  83. *o = options
  84. }
  85. type ServerOptionsWrapper interface {
  86. TakeServerOptions() ServerOptions
  87. ReplaceServerOptions(options ServerOptions)
  88. }
  89. type ServerOptions struct {
  90. Server string `json:"server"`
  91. ServerPort uint16 `json:"server_port"`
  92. }
  93. func (o ServerOptions) Build() M.Socksaddr {
  94. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  95. }
  96. func (o ServerOptions) ServerIsDomain() bool {
  97. return M.IsDomainName(o.Server)
  98. }
  99. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  100. return *o
  101. }
  102. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  103. *o = options
  104. }