inbound.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 SimpleInboundOptions `json:"-"`
  14. HTTPOptions SimpleInboundOptions `json:"-"`
  15. MixedOptions SimpleInboundOptions `json:"-"`
  16. ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
  17. TunOptions TunInboundOptions `json:"-"`
  18. }
  19. type Inbound _Inbound
  20. func (h Inbound) Equals(other Inbound) bool {
  21. return h.Type == other.Type &&
  22. h.Tag == other.Tag &&
  23. h.DirectOptions == other.DirectOptions &&
  24. h.SocksOptions.Equals(other.SocksOptions) &&
  25. h.HTTPOptions.Equals(other.HTTPOptions) &&
  26. h.MixedOptions.Equals(other.MixedOptions) &&
  27. h.ShadowsocksOptions.Equals(other.ShadowsocksOptions) &&
  28. h.TunOptions == other.TunOptions
  29. }
  30. func (h Inbound) MarshalJSON() ([]byte, error) {
  31. var v any
  32. switch h.Type {
  33. case C.TypeDirect:
  34. v = h.DirectOptions
  35. case C.TypeSocks:
  36. v = h.SocksOptions
  37. case C.TypeHTTP:
  38. v = h.HTTPOptions
  39. case C.TypeMixed:
  40. v = h.MixedOptions
  41. case C.TypeShadowsocks:
  42. v = h.ShadowsocksOptions
  43. case C.TypeTun:
  44. v = h.TunOptions
  45. default:
  46. return nil, E.New("unknown inbound type: ", h.Type)
  47. }
  48. return MarshallObjects((_Inbound)(h), v)
  49. }
  50. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  51. err := json.Unmarshal(bytes, (*_Inbound)(h))
  52. if err != nil {
  53. return err
  54. }
  55. var v any
  56. switch h.Type {
  57. case C.TypeDirect:
  58. v = &h.DirectOptions
  59. case C.TypeSocks:
  60. v = &h.SocksOptions
  61. case C.TypeHTTP:
  62. v = &h.HTTPOptions
  63. case C.TypeMixed:
  64. v = &h.MixedOptions
  65. case C.TypeShadowsocks:
  66. v = &h.ShadowsocksOptions
  67. case C.TypeTun:
  68. v = &h.TunOptions
  69. default:
  70. return nil
  71. }
  72. err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
  73. if err != nil {
  74. return E.Cause(err, "inbound options")
  75. }
  76. return nil
  77. }
  78. type InboundOptions struct {
  79. SniffEnabled bool `json:"sniff,omitempty"`
  80. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  81. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  82. }
  83. type ListenOptions struct {
  84. Listen ListenAddress `json:"listen"`
  85. ListenPort uint16 `json:"listen_port"`
  86. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  87. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  88. InboundOptions
  89. }
  90. type SimpleInboundOptions struct {
  91. ListenOptions
  92. Users []auth.User `json:"users,omitempty"`
  93. }
  94. func (o SimpleInboundOptions) Equals(other SimpleInboundOptions) bool {
  95. return o.ListenOptions == other.ListenOptions &&
  96. common.ComparableSliceEquals(o.Users, other.Users)
  97. }
  98. type DirectInboundOptions struct {
  99. ListenOptions
  100. Network NetworkList `json:"network,omitempty"`
  101. OverrideAddress string `json:"override_address,omitempty"`
  102. OverridePort uint16 `json:"override_port,omitempty"`
  103. }
  104. type ShadowsocksInboundOptions struct {
  105. ListenOptions
  106. Network NetworkList `json:"network,omitempty"`
  107. Method string `json:"method"`
  108. Password string `json:"password"`
  109. Users []ShadowsocksUser `json:"users,omitempty"`
  110. Destinations []ShadowsocksDestination `json:"destinations,omitempty"`
  111. }
  112. func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
  113. return o.ListenOptions == other.ListenOptions &&
  114. o.Network == other.Network &&
  115. o.Method == other.Method &&
  116. o.Password == other.Password &&
  117. common.ComparableSliceEquals(o.Users, other.Users) &&
  118. common.ComparableSliceEquals(o.Destinations, other.Destinations)
  119. }
  120. type ShadowsocksUser struct {
  121. Name string `json:"name"`
  122. Password string `json:"password"`
  123. }
  124. type ShadowsocksDestination struct {
  125. Name string `json:"name"`
  126. Password string `json:"password"`
  127. ServerOptions
  128. }
  129. type TunInboundOptions struct {
  130. InterfaceName string `json:"interface_name,omitempty"`
  131. MTU uint32 `json:"mtu,omitempty,omitempty"`
  132. Inet4Address ListenPrefix `json:"inet4_address,omitempty"`
  133. Inet6Address ListenPrefix `json:"inet6_address,omitempty"`
  134. AutoRoute bool `json:"auto_route,omitempty"`
  135. HijackDNS bool `json:"hijack_dns,omitempty"`
  136. InboundOptions
  137. }