outbound.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. M "github.com/sagernet/sing/common/metadata"
  10. "github.com/sagernet/sing/service"
  11. )
  12. type OutboundOptionsRegistry interface {
  13. CreateOptions(outboundType string) (any, bool)
  14. }
  15. type _Outbound struct {
  16. Type string `json:"type"`
  17. Tag string `json:"tag,omitempty"`
  18. Options any `json:"-"`
  19. }
  20. type Outbound _Outbound
  21. func (h *Outbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
  22. return badjson.MarshallObjectsContext(ctx, (*_Outbound)(h), h.Options)
  23. }
  24. func (h *Outbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
  25. err := json.Unmarshal(content, (*_Outbound)(h))
  26. if err != nil {
  27. return err
  28. }
  29. registry := service.FromContext[OutboundOptionsRegistry](ctx)
  30. if registry == nil {
  31. return E.New("missing outbound options registry in context")
  32. }
  33. switch h.Type {
  34. case C.TypeBlock, C.TypeDNS:
  35. deprecated.Report(ctx, deprecated.OptionSpecialOutbounds)
  36. }
  37. options, loaded := registry.CreateOptions(h.Type)
  38. if !loaded {
  39. return E.New("unknown outbound type: ", h.Type)
  40. }
  41. err = badjson.UnmarshallExcludedContext(ctx, content, (*_Outbound)(h), options)
  42. if err != nil {
  43. return err
  44. }
  45. if listenWrapper, isListen := options.(ListenOptionsWrapper); isListen {
  46. if listenWrapper.TakeListenOptions().InboundOptions != (InboundOptions{}) {
  47. deprecated.Report(ctx, deprecated.OptionInboundOptions)
  48. }
  49. }
  50. h.Options = options
  51. return nil
  52. }
  53. type DialerOptionsWrapper interface {
  54. TakeDialerOptions() DialerOptions
  55. ReplaceDialerOptions(options DialerOptions)
  56. }
  57. type DialerOptions struct {
  58. Detour string `json:"detour,omitempty"`
  59. BindInterface string `json:"bind_interface,omitempty"`
  60. Inet4BindAddress *ListenAddress `json:"inet4_bind_address,omitempty"`
  61. Inet6BindAddress *ListenAddress `json:"inet6_bind_address,omitempty"`
  62. ProtectPath string `json:"protect_path,omitempty"`
  63. RoutingMark uint32 `json:"routing_mark,omitempty"`
  64. ReuseAddr bool `json:"reuse_addr,omitempty"`
  65. ConnectTimeout Duration `json:"connect_timeout,omitempty"`
  66. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  67. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  68. UDPFragment *bool `json:"udp_fragment,omitempty"`
  69. UDPFragmentDefault bool `json:"-"`
  70. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  71. FallbackDelay Duration `json:"fallback_delay,omitempty"`
  72. IsWireGuardListener bool `json:"-"`
  73. }
  74. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  75. return *o
  76. }
  77. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  78. *o = options
  79. }
  80. type ServerOptionsWrapper interface {
  81. TakeServerOptions() ServerOptions
  82. ReplaceServerOptions(options ServerOptions)
  83. }
  84. type ServerOptions struct {
  85. Server string `json:"server"`
  86. ServerPort uint16 `json:"server_port"`
  87. }
  88. func (o ServerOptions) Build() M.Socksaddr {
  89. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  90. }
  91. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  92. return *o
  93. }
  94. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  95. *o = options
  96. }