inbound.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if listenWrapper, isListen := options.(ListenOptionsWrapper); isListen {
  41. //nolint:staticcheck
  42. if listenWrapper.TakeListenOptions().InboundOptions != (InboundOptions{}) {
  43. return E.New("legacy inbound fields are deprecated in sing-box 1.11.0 and removed in sing-box 1.13.0, checkout migration: https://sing-box.sagernet.org/migration/#migrate-legacy-inbound-fields-to-rule-actions")
  44. }
  45. }
  46. h.Options = options
  47. return nil
  48. }
  49. // Deprecated: Use rule action instead
  50. type InboundOptions struct {
  51. SniffEnabled bool `json:"sniff,omitempty"`
  52. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  53. SniffTimeout badoption.Duration `json:"sniff_timeout,omitempty"`
  54. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  55. UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
  56. }
  57. type ListenOptions struct {
  58. Listen *badoption.Addr `json:"listen,omitempty"`
  59. ListenPort uint16 `json:"listen_port,omitempty"`
  60. BindInterface string `json:"bind_interface,omitempty"`
  61. RoutingMark FwMark `json:"routing_mark,omitempty"`
  62. ReuseAddr bool `json:"reuse_addr,omitempty"`
  63. NetNs string `json:"netns,omitempty"`
  64. DisableTCPKeepAlive bool `json:"disable_tcp_keep_alive,omitempty"`
  65. TCPKeepAlive badoption.Duration `json:"tcp_keep_alive,omitempty"`
  66. TCPKeepAliveInterval badoption.Duration `json:"tcp_keep_alive_interval,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. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  72. Detour string `json:"detour,omitempty"`
  73. // Deprecated: removed
  74. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  75. // Deprecated: removed
  76. ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
  77. InboundOptions
  78. }
  79. type UDPTimeoutCompat badoption.Duration
  80. func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
  81. return json.Marshal((time.Duration)(c).String())
  82. }
  83. func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
  84. var valueNumber int64
  85. err := json.Unmarshal(data, &valueNumber)
  86. if err == nil {
  87. *c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
  88. return nil
  89. }
  90. return json.Unmarshal(data, (*badoption.Duration)(c))
  91. }
  92. type ListenOptionsWrapper interface {
  93. TakeListenOptions() ListenOptions
  94. ReplaceListenOptions(options ListenOptions)
  95. }
  96. func (o *ListenOptions) TakeListenOptions() ListenOptions {
  97. return *o
  98. }
  99. func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
  100. *o = options
  101. }