inbound.go 4.7 KB

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