inbound.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package option
  2. import (
  3. "context"
  4. "time"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/sagernet/sing/common/json"
  7. "github.com/sagernet/sing/common/json/badjson"
  8. "github.com/sagernet/sing/common/json/badoption"
  9. "github.com/sagernet/sing/service"
  10. )
  11. type InboundOptionsRegistry interface {
  12. CreateOptions(outboundType string) (any, bool)
  13. }
  14. type _Inbound struct {
  15. Type string `json:"type"`
  16. Tag string `json:"tag,omitempty"`
  17. Options any `json:"-"`
  18. }
  19. type Inbound _Inbound
  20. func (h *Inbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
  21. return badjson.MarshallObjectsContext(ctx, (*_Inbound)(h), h.Options)
  22. }
  23. func (h *Inbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
  24. err := json.UnmarshalContext(ctx, content, (*_Inbound)(h))
  25. if err != nil {
  26. return err
  27. }
  28. registry := service.FromContext[InboundOptionsRegistry](ctx)
  29. if registry == nil {
  30. return E.New("missing Inbound fields registry in context")
  31. }
  32. options, loaded := registry.CreateOptions(h.Type)
  33. if !loaded {
  34. return E.New("unknown inbound type: ", h.Type)
  35. }
  36. err = badjson.UnmarshallExcludedContext(ctx, content, (*_Inbound)(h), options)
  37. if err != nil {
  38. return err
  39. }
  40. h.Options = options
  41. return nil
  42. }
  43. // Deprecated: Use rule action instead
  44. type InboundOptions struct {
  45. SniffEnabled bool `json:"sniff,omitempty"`
  46. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  47. SniffTimeout badoption.Duration `json:"sniff_timeout,omitempty"`
  48. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  49. UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
  50. Detour string `json:"detour,omitempty"`
  51. }
  52. type ListenOptions struct {
  53. Listen *badoption.Addr `json:"listen,omitempty"`
  54. ListenPort uint16 `json:"listen_port,omitempty"`
  55. TCPKeepAlive badoption.Duration `json:"tcp_keep_alive,omitempty"`
  56. TCPKeepAliveInterval badoption.Duration `json:"tcp_keep_alive_interval,omitempty"`
  57. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  58. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  59. UDPFragment *bool `json:"udp_fragment,omitempty"`
  60. UDPFragmentDefault bool `json:"-"`
  61. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  62. NetNs string `json:"netns,omitempty"`
  63. // Deprecated: removed
  64. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  65. // Deprecated: removed
  66. ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
  67. InboundOptions
  68. }
  69. type UDPTimeoutCompat badoption.Duration
  70. func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
  71. return json.Marshal((time.Duration)(c).String())
  72. }
  73. func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
  74. var valueNumber int64
  75. err := json.Unmarshal(data, &valueNumber)
  76. if err == nil {
  77. *c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
  78. return nil
  79. }
  80. return json.Unmarshal(data, (*badoption.Duration)(c))
  81. }
  82. type ListenOptionsWrapper interface {
  83. TakeListenOptions() ListenOptions
  84. ReplaceListenOptions(options ListenOptions)
  85. }
  86. func (o *ListenOptions) TakeListenOptions() ListenOptions {
  87. return *o
  88. }
  89. func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
  90. *o = options
  91. }