inbound.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package option
  2. import (
  3. "github.com/sagernet/sing/common"
  4. "github.com/sagernet/sing/common/auth"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. C "github.com/sagernet/sing-box/constant"
  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. }
  18. type Inbound _Inbound
  19. func (h Inbound) Equals(other Inbound) bool {
  20. return h.Type == other.Type &&
  21. h.Tag == other.Tag &&
  22. h.DirectOptions == other.DirectOptions &&
  23. h.SocksOptions.Equals(other.SocksOptions) &&
  24. h.HTTPOptions.Equals(other.HTTPOptions) &&
  25. h.MixedOptions.Equals(other.MixedOptions) &&
  26. h.ShadowsocksOptions.Equals(other.ShadowsocksOptions)
  27. }
  28. func (h Inbound) MarshalJSON() ([]byte, error) {
  29. var v any
  30. switch h.Type {
  31. case C.TypeDirect:
  32. v = h.DirectOptions
  33. case C.TypeSocks:
  34. v = h.SocksOptions
  35. case C.TypeHTTP:
  36. v = h.HTTPOptions
  37. case C.TypeMixed:
  38. v = h.MixedOptions
  39. case C.TypeShadowsocks:
  40. v = h.ShadowsocksOptions
  41. default:
  42. return nil, E.New("unknown inbound type: ", h.Type)
  43. }
  44. return MarshallObjects((_Inbound)(h), v)
  45. }
  46. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  47. err := json.Unmarshal(bytes, (*_Inbound)(h))
  48. if err != nil {
  49. return err
  50. }
  51. var v any
  52. switch h.Type {
  53. case C.TypeDirect:
  54. v = &h.DirectOptions
  55. case C.TypeSocks:
  56. v = &h.SocksOptions
  57. case C.TypeHTTP:
  58. v = &h.HTTPOptions
  59. case C.TypeMixed:
  60. v = &h.MixedOptions
  61. case C.TypeShadowsocks:
  62. v = &h.ShadowsocksOptions
  63. default:
  64. return nil
  65. }
  66. err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
  67. if err != nil {
  68. return E.Cause(err, "inbound options")
  69. }
  70. return nil
  71. }
  72. type ListenOptions struct {
  73. Listen ListenAddress `json:"listen"`
  74. Port uint16 `json:"listen_port"`
  75. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  76. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  77. SniffEnabled bool `json:"sniff,omitempty"`
  78. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  79. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  80. }
  81. type SimpleInboundOptions struct {
  82. ListenOptions
  83. Users []auth.User `json:"users,omitempty"`
  84. }
  85. func (o SimpleInboundOptions) Equals(other SimpleInboundOptions) bool {
  86. return o.ListenOptions == other.ListenOptions &&
  87. common.ComparableSliceEquals(o.Users, other.Users)
  88. }
  89. type DirectInboundOptions struct {
  90. ListenOptions
  91. Network NetworkList `json:"network,omitempty"`
  92. OverrideAddress string `json:"override_address,omitempty"`
  93. OverridePort uint16 `json:"override_port,omitempty"`
  94. }
  95. type ShadowsocksInboundOptions struct {
  96. ListenOptions
  97. Network NetworkList `json:"network,omitempty"`
  98. Method string `json:"method"`
  99. Password string `json:"password"`
  100. Users []ShadowsocksUser `json:"users,omitempty"`
  101. Destinations []ShadowsocksDestination `json:"destinations,omitempty"`
  102. }
  103. func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
  104. return o.ListenOptions == other.ListenOptions &&
  105. o.Network == other.Network &&
  106. o.Method == other.Method &&
  107. o.Password == other.Password &&
  108. common.ComparableSliceEquals(o.Users, other.Users) &&
  109. common.ComparableSliceEquals(o.Destinations, other.Destinations)
  110. }
  111. type ShadowsocksUser struct {
  112. Name string `json:"name"`
  113. Password string `json:"password"`
  114. }
  115. type ShadowsocksDestination struct {
  116. Name string `json:"name"`
  117. Password string `json:"password"`
  118. ServerOptions
  119. }