outbound.go 4.0 KB

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