inbound.go 3.2 KB

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