inbound.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package option
  2. import (
  3. "github.com/goccy/go-json"
  4. C "github.com/sagernet/sing-box/constant"
  5. "github.com/sagernet/sing/common"
  6. "github.com/sagernet/sing/common/auth"
  7. E "github.com/sagernet/sing/common/exceptions"
  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. }
  80. type SimpleInboundOptions struct {
  81. ListenOptions
  82. Users []auth.User `json:"users,omitempty"`
  83. }
  84. func (o SimpleInboundOptions) Equals(other SimpleInboundOptions) bool {
  85. return o.ListenOptions == other.ListenOptions &&
  86. common.ComparableSliceEquals(o.Users, other.Users)
  87. }
  88. type DirectInboundOptions struct {
  89. ListenOptions
  90. Network NetworkList `json:"network,omitempty"`
  91. OverrideAddress string `json:"override_address,omitempty"`
  92. OverridePort uint16 `json:"override_port,omitempty"`
  93. }
  94. type ShadowsocksInboundOptions struct {
  95. ListenOptions
  96. Network NetworkList `json:"network,omitempty"`
  97. Method string `json:"method"`
  98. Password string `json:"password"`
  99. Users []ShadowsocksUser `json:"users,omitempty"`
  100. Destinations []ShadowsocksDestination `json:"destinations,omitempty"`
  101. }
  102. func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
  103. return o.ListenOptions == other.ListenOptions &&
  104. o.Network == other.Network &&
  105. o.Method == other.Method &&
  106. o.Password == other.Password &&
  107. common.ComparableSliceEquals(o.Users, other.Users) &&
  108. common.ComparableSliceEquals(o.Destinations, other.Destinations)
  109. }
  110. type ShadowsocksUser struct {
  111. Name string `json:"name"`
  112. Password string `json:"password"`
  113. }
  114. type ShadowsocksDestination struct {
  115. Name string `json:"name"`
  116. Password string `json:"password"`
  117. ServerOptions
  118. }