inbound.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package option
  2. import (
  3. "time"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/sagernet/sing/common/json"
  7. "github.com/sagernet/sing/common/json/badjson"
  8. )
  9. type _Inbound struct {
  10. Type string `json:"type"`
  11. Tag string `json:"tag,omitempty"`
  12. TunOptions TunInboundOptions `json:"-"`
  13. RedirectOptions RedirectInboundOptions `json:"-"`
  14. TProxyOptions TProxyInboundOptions `json:"-"`
  15. DirectOptions DirectInboundOptions `json:"-"`
  16. SocksOptions SocksInboundOptions `json:"-"`
  17. HTTPOptions HTTPMixedInboundOptions `json:"-"`
  18. MixedOptions HTTPMixedInboundOptions `json:"-"`
  19. ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
  20. VMessOptions VMessInboundOptions `json:"-"`
  21. TrojanOptions TrojanInboundOptions `json:"-"`
  22. NaiveOptions NaiveInboundOptions `json:"-"`
  23. HysteriaOptions HysteriaInboundOptions `json:"-"`
  24. ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
  25. VLESSOptions VLESSInboundOptions `json:"-"`
  26. TUICOptions TUICInboundOptions `json:"-"`
  27. Hysteria2Options Hysteria2InboundOptions `json:"-"`
  28. }
  29. type Inbound _Inbound
  30. func (h *Inbound) RawOptions() (any, error) {
  31. var rawOptionsPtr any
  32. switch h.Type {
  33. case C.TypeTun:
  34. rawOptionsPtr = &h.TunOptions
  35. case C.TypeRedirect:
  36. rawOptionsPtr = &h.RedirectOptions
  37. case C.TypeTProxy:
  38. rawOptionsPtr = &h.TProxyOptions
  39. case C.TypeDirect:
  40. rawOptionsPtr = &h.DirectOptions
  41. case C.TypeSOCKS:
  42. rawOptionsPtr = &h.SocksOptions
  43. case C.TypeHTTP:
  44. rawOptionsPtr = &h.HTTPOptions
  45. case C.TypeMixed:
  46. rawOptionsPtr = &h.MixedOptions
  47. case C.TypeShadowsocks:
  48. rawOptionsPtr = &h.ShadowsocksOptions
  49. case C.TypeVMess:
  50. rawOptionsPtr = &h.VMessOptions
  51. case C.TypeTrojan:
  52. rawOptionsPtr = &h.TrojanOptions
  53. case C.TypeNaive:
  54. rawOptionsPtr = &h.NaiveOptions
  55. case C.TypeHysteria:
  56. rawOptionsPtr = &h.HysteriaOptions
  57. case C.TypeShadowTLS:
  58. rawOptionsPtr = &h.ShadowTLSOptions
  59. case C.TypeVLESS:
  60. rawOptionsPtr = &h.VLESSOptions
  61. case C.TypeTUIC:
  62. rawOptionsPtr = &h.TUICOptions
  63. case C.TypeHysteria2:
  64. rawOptionsPtr = &h.Hysteria2Options
  65. case "":
  66. return nil, E.New("missing inbound type")
  67. default:
  68. return nil, E.New("unknown inbound type: ", h.Type)
  69. }
  70. return rawOptionsPtr, nil
  71. }
  72. func (h Inbound) MarshalJSON() ([]byte, error) {
  73. rawOptions, err := h.RawOptions()
  74. if err != nil {
  75. return nil, err
  76. }
  77. return badjson.MarshallObjects((_Inbound)(h), rawOptions)
  78. }
  79. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  80. err := json.Unmarshal(bytes, (*_Inbound)(h))
  81. if err != nil {
  82. return err
  83. }
  84. rawOptions, err := h.RawOptions()
  85. if err != nil {
  86. return err
  87. }
  88. err = badjson.UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions)
  89. if err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. // Deprecated: Use rule action instead
  95. type InboundOptions struct {
  96. SniffEnabled bool `json:"sniff,omitempty"`
  97. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  98. SniffTimeout Duration `json:"sniff_timeout,omitempty"`
  99. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  100. UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
  101. }
  102. type ListenOptions struct {
  103. Listen *ListenAddress `json:"listen,omitempty"`
  104. ListenPort uint16 `json:"listen_port,omitempty"`
  105. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  106. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  107. UDPFragment *bool `json:"udp_fragment,omitempty"`
  108. UDPFragmentDefault bool `json:"-"`
  109. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  110. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  111. ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
  112. Detour string `json:"detour,omitempty"`
  113. InboundOptions
  114. }
  115. type UDPTimeoutCompat Duration
  116. func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
  117. return json.Marshal((time.Duration)(c).String())
  118. }
  119. func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
  120. var valueNumber int64
  121. err := json.Unmarshal(data, &valueNumber)
  122. if err == nil {
  123. *c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
  124. return nil
  125. }
  126. return json.Unmarshal(data, (*Duration)(c))
  127. }
  128. type ListenOptionsWrapper interface {
  129. TakeListenOptions() ListenOptions
  130. ReplaceListenOptions(options ListenOptions)
  131. }
  132. func (o *ListenOptions) TakeListenOptions() ListenOptions {
  133. return *o
  134. }
  135. func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
  136. *o = options
  137. }