1
0

shadowsocks.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package conf
  2. import (
  3. "strings"
  4. "github.com/golang/protobuf/proto"
  5. C "github.com/sagernet/sing/common"
  6. "github.com/sagernet/sing/protocol/shadowsocks/shadowaead_2022"
  7. "github.com/xtls/xray-core/common/protocol"
  8. "github.com/xtls/xray-core/common/serial"
  9. "github.com/xtls/xray-core/proxy/shadowsocks"
  10. "github.com/xtls/xray-core/proxy/shadowsocks_2022"
  11. )
  12. func cipherFromString(c string) shadowsocks.CipherType {
  13. switch strings.ToLower(c) {
  14. case "aes-128-gcm", "aead_aes_128_gcm":
  15. return shadowsocks.CipherType_AES_128_GCM
  16. case "aes-256-gcm", "aead_aes_256_gcm":
  17. return shadowsocks.CipherType_AES_256_GCM
  18. case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
  19. return shadowsocks.CipherType_CHACHA20_POLY1305
  20. case "xchacha20-poly1305", "aead_xchacha20_poly1305", "xchacha20-ietf-poly1305":
  21. return shadowsocks.CipherType_XCHACHA20_POLY1305
  22. case "none", "plain":
  23. return shadowsocks.CipherType_NONE
  24. default:
  25. return shadowsocks.CipherType_UNKNOWN
  26. }
  27. }
  28. type ShadowsocksUserConfig struct {
  29. Cipher string `json:"method"`
  30. Password string `json:"password"`
  31. Level byte `json:"level"`
  32. Email string `json:"email"`
  33. }
  34. type ShadowsocksServerConfig struct {
  35. Cipher string `json:"method"`
  36. Password string `json:"password"`
  37. Level byte `json:"level"`
  38. Email string `json:"email"`
  39. Users []*ShadowsocksUserConfig `json:"clients"`
  40. NetworkList *NetworkList `json:"network"`
  41. IVCheck bool `json:"ivCheck"`
  42. }
  43. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  44. if C.Contains(shadowaead_2022.List, v.Cipher) {
  45. if len(v.Users) > 0 {
  46. if v.Cipher == "" {
  47. return nil, newError("shadowsocks 2022 (multi-user): missing server method")
  48. }
  49. if !strings.Contains(v.Cipher, "aes") {
  50. return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
  51. }
  52. config := new(shadowsocks_2022.MultiUserServerConfig)
  53. config.Method = v.Cipher
  54. config.Key = v.Password
  55. config.Network = v.NetworkList.Build()
  56. for _, user := range v.Users {
  57. if user.Cipher != "" {
  58. return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
  59. }
  60. config.Users = append(config.Users, &shadowsocks_2022.User{
  61. Key: user.Password,
  62. Email: user.Email,
  63. })
  64. }
  65. return config, nil
  66. }
  67. config := new(shadowsocks_2022.ServerConfig)
  68. config.Method = v.Cipher
  69. config.Key = v.Password
  70. config.Network = v.NetworkList.Build()
  71. return config, nil
  72. }
  73. config := new(shadowsocks.ServerConfig)
  74. config.Network = v.NetworkList.Build()
  75. if v.Users != nil {
  76. for _, user := range v.Users {
  77. account := &shadowsocks.Account{
  78. Password: user.Password,
  79. CipherType: cipherFromString(user.Cipher),
  80. IvCheck: v.IVCheck,
  81. }
  82. if account.Password == "" {
  83. return nil, newError("Shadowsocks password is not specified.")
  84. }
  85. if account.CipherType < shadowsocks.CipherType_AES_128_GCM ||
  86. account.CipherType > shadowsocks.CipherType_XCHACHA20_POLY1305 {
  87. return nil, newError("unsupported cipher method: ", user.Cipher)
  88. }
  89. config.Users = append(config.Users, &protocol.User{
  90. Email: user.Email,
  91. Level: uint32(user.Level),
  92. Account: serial.ToTypedMessage(account),
  93. })
  94. }
  95. } else {
  96. account := &shadowsocks.Account{
  97. Password: v.Password,
  98. CipherType: cipherFromString(v.Cipher),
  99. IvCheck: v.IVCheck,
  100. }
  101. if account.Password == "" {
  102. return nil, newError("Shadowsocks password is not specified.")
  103. }
  104. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  105. return nil, newError("unknown cipher method: ", v.Cipher)
  106. }
  107. config.Users = append(config.Users, &protocol.User{
  108. Email: v.Email,
  109. Level: uint32(v.Level),
  110. Account: serial.ToTypedMessage(account),
  111. })
  112. }
  113. return config, nil
  114. }
  115. type ShadowsocksServerTarget struct {
  116. Address *Address `json:"address"`
  117. Port uint16 `json:"port"`
  118. Cipher string `json:"method"`
  119. Password string `json:"password"`
  120. Email string `json:"email"`
  121. Level byte `json:"level"`
  122. IVCheck bool `json:"ivCheck"`
  123. }
  124. type ShadowsocksClientConfig struct {
  125. Servers []*ShadowsocksServerTarget `json:"servers"`
  126. }
  127. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  128. if len(v.Servers) == 0 {
  129. return nil, newError("0 Shadowsocks server configured.")
  130. }
  131. if len(v.Servers) == 1 {
  132. server := v.Servers[0]
  133. if C.Contains(shadowaead_2022.List, server.Cipher) {
  134. if server.Address == nil {
  135. return nil, newError("Shadowsocks server address is not set.")
  136. }
  137. if server.Port == 0 {
  138. return nil, newError("Invalid Shadowsocks port.")
  139. }
  140. if server.Password == "" {
  141. return nil, newError("Shadowsocks password is not specified.")
  142. }
  143. config := new(shadowsocks_2022.ClientConfig)
  144. config.Address = server.Address.Build()
  145. config.Port = uint32(server.Port)
  146. config.Method = server.Cipher
  147. config.Key = server.Password
  148. return config, nil
  149. }
  150. }
  151. config := new(shadowsocks.ClientConfig)
  152. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  153. for idx, server := range v.Servers {
  154. if C.Contains(shadowaead_2022.List, server.Cipher) {
  155. return nil, newError("Shadowsocks 2022 accept no multi servers")
  156. }
  157. if server.Address == nil {
  158. return nil, newError("Shadowsocks server address is not set.")
  159. }
  160. if server.Port == 0 {
  161. return nil, newError("Invalid Shadowsocks port.")
  162. }
  163. if server.Password == "" {
  164. return nil, newError("Shadowsocks password is not specified.")
  165. }
  166. account := &shadowsocks.Account{
  167. Password: server.Password,
  168. }
  169. account.CipherType = cipherFromString(server.Cipher)
  170. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  171. return nil, newError("unknown cipher method: ", server.Cipher)
  172. }
  173. account.IvCheck = server.IVCheck
  174. ss := &protocol.ServerEndpoint{
  175. Address: server.Address.Build(),
  176. Port: uint32(server.Port),
  177. User: []*protocol.User{
  178. {
  179. Level: uint32(server.Level),
  180. Email: server.Email,
  181. Account: serial.ToTypedMessage(account),
  182. },
  183. },
  184. }
  185. serverSpecs[idx] = ss
  186. }
  187. config.Server = serverSpecs
  188. return config, nil
  189. }