outbound.go 3.0 KB

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