inbound.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package option
  2. import (
  3. C "github.com/sagernet/sing-box/constant"
  4. "github.com/sagernet/sing/common"
  5. "github.com/sagernet/sing/common/auth"
  6. E "github.com/sagernet/sing/common/exceptions"
  7. "github.com/goccy/go-json"
  8. )
  9. type _Inbound struct {
  10. Type string `json:"type"`
  11. Tag string `json:"tag,omitempty"`
  12. DirectOptions DirectInboundOptions `json:"-"`
  13. SocksOptions SocksInboundOptions `json:"-"`
  14. HTTPOptions HTTPMixedInboundOptions `json:"-"`
  15. MixedOptions HTTPMixedInboundOptions `json:"-"`
  16. ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
  17. TunOptions TunInboundOptions `json:"-"`
  18. RedirectOptions RedirectInboundOptions `json:"-"`
  19. TProxyOptions TProxyInboundOptions `json:"-"`
  20. DNSOptions DNSInboundOptions `json:"-"`
  21. }
  22. type Inbound _Inbound
  23. func (h Inbound) Equals(other Inbound) bool {
  24. return h.Type == other.Type &&
  25. h.Tag == other.Tag &&
  26. h.DirectOptions == other.DirectOptions &&
  27. h.SocksOptions.Equals(other.SocksOptions) &&
  28. h.HTTPOptions.Equals(other.HTTPOptions) &&
  29. h.MixedOptions.Equals(other.MixedOptions) &&
  30. h.ShadowsocksOptions.Equals(other.ShadowsocksOptions) &&
  31. h.TunOptions == other.TunOptions &&
  32. h.RedirectOptions == other.RedirectOptions &&
  33. h.TProxyOptions == other.TProxyOptions &&
  34. h.DNSOptions == other.DNSOptions
  35. }
  36. func (h Inbound) MarshalJSON() ([]byte, error) {
  37. var v any
  38. switch h.Type {
  39. case C.TypeDirect:
  40. v = h.DirectOptions
  41. case C.TypeSocks:
  42. v = h.SocksOptions
  43. case C.TypeHTTP:
  44. v = h.HTTPOptions
  45. case C.TypeMixed:
  46. v = h.MixedOptions
  47. case C.TypeShadowsocks:
  48. v = h.ShadowsocksOptions
  49. case C.TypeTun:
  50. v = h.TunOptions
  51. case C.TypeRedirect:
  52. v = h.RedirectOptions
  53. case C.TypeTProxy:
  54. v = h.TProxyOptions
  55. case C.TypeDNS:
  56. v = h.DNSOptions
  57. default:
  58. return nil, E.New("unknown inbound type: ", h.Type)
  59. }
  60. return MarshallObjects((_Inbound)(h), v)
  61. }
  62. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  63. err := json.Unmarshal(bytes, (*_Inbound)(h))
  64. if err != nil {
  65. return err
  66. }
  67. var v any
  68. switch h.Type {
  69. case C.TypeDirect:
  70. v = &h.DirectOptions
  71. case C.TypeSocks:
  72. v = &h.SocksOptions
  73. case C.TypeHTTP:
  74. v = &h.HTTPOptions
  75. case C.TypeMixed:
  76. v = &h.MixedOptions
  77. case C.TypeShadowsocks:
  78. v = &h.ShadowsocksOptions
  79. case C.TypeTun:
  80. v = &h.TunOptions
  81. case C.TypeRedirect:
  82. v = &h.RedirectOptions
  83. case C.TypeTProxy:
  84. v = &h.TProxyOptions
  85. case C.TypeDNS:
  86. v = &h.DNSOptions
  87. default:
  88. return nil
  89. }
  90. err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
  91. if err != nil {
  92. return E.Cause(err, "inbound options")
  93. }
  94. return nil
  95. }
  96. type InboundOptions struct {
  97. SniffEnabled bool `json:"sniff,omitempty"`
  98. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  99. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  100. }
  101. type ListenOptions struct {
  102. Listen ListenAddress `json:"listen"`
  103. ListenPort uint16 `json:"listen_port"`
  104. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  105. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  106. InboundOptions
  107. }
  108. type SocksInboundOptions struct {
  109. ListenOptions
  110. Users []auth.User `json:"users,omitempty"`
  111. }
  112. func (o SocksInboundOptions) Equals(other SocksInboundOptions) bool {
  113. return o.ListenOptions == other.ListenOptions &&
  114. common.ComparableSliceEquals(o.Users, other.Users)
  115. }
  116. type HTTPMixedInboundOptions struct {
  117. ListenOptions
  118. Users []auth.User `json:"users,omitempty"`
  119. SetSystemProxy bool `json:"set_system_proxy,omitempty"`
  120. }
  121. func (o HTTPMixedInboundOptions) Equals(other HTTPMixedInboundOptions) bool {
  122. return o.ListenOptions == other.ListenOptions &&
  123. common.ComparableSliceEquals(o.Users, other.Users) &&
  124. o.SetSystemProxy == other.SetSystemProxy
  125. }
  126. type DirectInboundOptions struct {
  127. ListenOptions
  128. Network NetworkList `json:"network,omitempty"`
  129. OverrideAddress string `json:"override_address,omitempty"`
  130. OverridePort uint16 `json:"override_port,omitempty"`
  131. }
  132. type ShadowsocksInboundOptions struct {
  133. ListenOptions
  134. Network NetworkList `json:"network,omitempty"`
  135. Method string `json:"method"`
  136. Password string `json:"password"`
  137. Users []ShadowsocksUser `json:"users,omitempty"`
  138. Destinations []ShadowsocksDestination `json:"destinations,omitempty"`
  139. }
  140. func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
  141. return o.ListenOptions == other.ListenOptions &&
  142. o.Network == other.Network &&
  143. o.Method == other.Method &&
  144. o.Password == other.Password &&
  145. common.ComparableSliceEquals(o.Users, other.Users) &&
  146. common.ComparableSliceEquals(o.Destinations, other.Destinations)
  147. }
  148. type ShadowsocksUser struct {
  149. Name string `json:"name"`
  150. Password string `json:"password"`
  151. }
  152. type ShadowsocksDestination struct {
  153. Name string `json:"name"`
  154. Password string `json:"password"`
  155. ServerOptions
  156. }
  157. type TunInboundOptions struct {
  158. InterfaceName string `json:"interface_name,omitempty"`
  159. MTU uint32 `json:"mtu,omitempty"`
  160. Inet4Address *ListenPrefix `json:"inet4_address,omitempty"`
  161. Inet6Address *ListenPrefix `json:"inet6_address,omitempty"`
  162. AutoRoute bool `json:"auto_route,omitempty"`
  163. HijackDNS bool `json:"hijack_dns,omitempty"`
  164. InboundOptions
  165. }
  166. type RedirectInboundOptions struct {
  167. ListenOptions
  168. }
  169. type TProxyInboundOptions struct {
  170. ListenOptions
  171. Network NetworkList `json:"network,omitempty"`
  172. }
  173. type DNSInboundOptions struct {
  174. ListenOptions
  175. Network NetworkList `json:"network,omitempty"`
  176. }