shadowsocks.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package conf
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  5. C "github.com/sagernet/sing/common"
  6. "github.com/xtls/xray-core/common/errors"
  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. "google.golang.org/protobuf/proto"
  12. )
  13. func cipherFromString(c string) shadowsocks.CipherType {
  14. switch strings.ToLower(c) {
  15. case "aes-128-gcm", "aead_aes_128_gcm":
  16. return shadowsocks.CipherType_AES_128_GCM
  17. case "aes-256-gcm", "aead_aes_256_gcm":
  18. return shadowsocks.CipherType_AES_256_GCM
  19. case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
  20. return shadowsocks.CipherType_CHACHA20_POLY1305
  21. case "xchacha20-poly1305", "aead_xchacha20_poly1305", "xchacha20-ietf-poly1305":
  22. return shadowsocks.CipherType_XCHACHA20_POLY1305
  23. case "none", "plain":
  24. return shadowsocks.CipherType_NONE
  25. default:
  26. return shadowsocks.CipherType_UNKNOWN
  27. }
  28. }
  29. type ShadowsocksUserConfig struct {
  30. Cipher string `json:"method"`
  31. Password string `json:"password"`
  32. Level byte `json:"level"`
  33. Email string `json:"email"`
  34. Address *Address `json:"address"`
  35. Port uint16 `json:"port"`
  36. }
  37. type ShadowsocksServerConfig struct {
  38. Cipher string `json:"method"`
  39. Password string `json:"password"`
  40. Level byte `json:"level"`
  41. Email string `json:"email"`
  42. Users []*ShadowsocksUserConfig `json:"clients"`
  43. NetworkList *NetworkList `json:"network"`
  44. IVCheck bool `json:"ivCheck"`
  45. }
  46. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  47. errors.PrintDeprecatedFeatureWarning("Shadowsocks", "VLESS Encryption")
  48. if C.Contains(shadowaead_2022.List, v.Cipher) {
  49. return buildShadowsocks2022(v)
  50. }
  51. config := new(shadowsocks.ServerConfig)
  52. config.Network = v.NetworkList.Build()
  53. if v.Users != nil {
  54. for _, user := range v.Users {
  55. account := &shadowsocks.Account{
  56. Password: user.Password,
  57. CipherType: cipherFromString(user.Cipher),
  58. IvCheck: v.IVCheck,
  59. }
  60. if account.Password == "" {
  61. return nil, errors.New("Shadowsocks password is not specified.")
  62. }
  63. if account.CipherType < shadowsocks.CipherType_AES_128_GCM ||
  64. account.CipherType > shadowsocks.CipherType_XCHACHA20_POLY1305 {
  65. return nil, errors.New("unsupported cipher method: ", user.Cipher)
  66. }
  67. config.Users = append(config.Users, &protocol.User{
  68. Email: user.Email,
  69. Level: uint32(user.Level),
  70. Account: serial.ToTypedMessage(account),
  71. })
  72. }
  73. } else {
  74. account := &shadowsocks.Account{
  75. Password: v.Password,
  76. CipherType: cipherFromString(v.Cipher),
  77. IvCheck: v.IVCheck,
  78. }
  79. if account.Password == "" {
  80. return nil, errors.New("Shadowsocks password is not specified.")
  81. }
  82. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  83. return nil, errors.New("unknown cipher method: ", v.Cipher)
  84. }
  85. config.Users = append(config.Users, &protocol.User{
  86. Email: v.Email,
  87. Level: uint32(v.Level),
  88. Account: serial.ToTypedMessage(account),
  89. })
  90. }
  91. return config, nil
  92. }
  93. func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
  94. if len(v.Users) == 0 {
  95. config := new(shadowsocks_2022.ServerConfig)
  96. config.Method = v.Cipher
  97. config.Key = v.Password
  98. config.Network = v.NetworkList.Build()
  99. config.Email = v.Email
  100. return config, nil
  101. }
  102. if v.Cipher == "" {
  103. return nil, errors.New("shadowsocks 2022 (multi-user): missing server method")
  104. }
  105. if !strings.Contains(v.Cipher, "aes") {
  106. return nil, errors.New("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
  107. }
  108. if v.Users[0].Address == nil {
  109. config := new(shadowsocks_2022.MultiUserServerConfig)
  110. config.Method = v.Cipher
  111. config.Key = v.Password
  112. config.Network = v.NetworkList.Build()
  113. for _, user := range v.Users {
  114. if user.Cipher != "" {
  115. return nil, errors.New("shadowsocks 2022 (multi-user): users must have empty method")
  116. }
  117. account := &shadowsocks_2022.Account{
  118. Key: user.Password,
  119. }
  120. config.Users = append(config.Users, &protocol.User{
  121. Email: user.Email,
  122. Level: uint32(user.Level),
  123. Account: serial.ToTypedMessage(account),
  124. })
  125. }
  126. return config, nil
  127. }
  128. config := new(shadowsocks_2022.RelayServerConfig)
  129. config.Method = v.Cipher
  130. config.Key = v.Password
  131. config.Network = v.NetworkList.Build()
  132. for _, user := range v.Users {
  133. if user.Cipher != "" {
  134. return nil, errors.New("shadowsocks 2022 (relay): users must have empty method")
  135. }
  136. if user.Address == nil {
  137. return nil, errors.New("shadowsocks 2022 (relay): all users must have relay address")
  138. }
  139. config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
  140. Key: user.Password,
  141. Email: user.Email,
  142. Address: user.Address.Build(),
  143. Port: uint32(user.Port),
  144. })
  145. }
  146. return config, nil
  147. }
  148. type ShadowsocksServerTarget struct {
  149. Address *Address `json:"address"`
  150. Port uint16 `json:"port"`
  151. Level byte `json:"level"`
  152. Email string `json:"email"`
  153. Cipher string `json:"method"`
  154. Password string `json:"password"`
  155. IVCheck bool `json:"ivCheck"`
  156. UoT bool `json:"uot"`
  157. UoTVersion int `json:"uotVersion"`
  158. }
  159. type ShadowsocksClientConfig struct {
  160. Address *Address `json:"address"`
  161. Port uint16 `json:"port"`
  162. Level byte `json:"level"`
  163. Email string `json:"email"`
  164. Cipher string `json:"method"`
  165. Password string `json:"password"`
  166. IVCheck bool `json:"ivCheck"`
  167. UoT bool `json:"uot"`
  168. UoTVersion int `json:"uotVersion"`
  169. Servers []*ShadowsocksServerTarget `json:"servers"`
  170. }
  171. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  172. errors.PrintDeprecatedFeatureWarning("Shadowsocks", "VLESS Encryption")
  173. if v.Address != nil {
  174. v.Servers = []*ShadowsocksServerTarget{
  175. {
  176. Address: v.Address,
  177. Port: v.Port,
  178. Level: v.Level,
  179. Email: v.Email,
  180. Cipher: v.Cipher,
  181. Password: v.Password,
  182. IVCheck: v.IVCheck,
  183. UoT: v.UoT,
  184. UoTVersion: v.UoTVersion,
  185. },
  186. }
  187. }
  188. if len(v.Servers) != 1 {
  189. return nil, errors.New(`Shadowsocks settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple Shadowsocks outbounds and routing balancer instead`)
  190. }
  191. if len(v.Servers) == 1 {
  192. server := v.Servers[0]
  193. if C.Contains(shadowaead_2022.List, server.Cipher) {
  194. if server.Address == nil {
  195. return nil, errors.New("Shadowsocks server address is not set.")
  196. }
  197. if server.Port == 0 {
  198. return nil, errors.New("Invalid Shadowsocks port.")
  199. }
  200. if server.Password == "" {
  201. return nil, errors.New("Shadowsocks password is not specified.")
  202. }
  203. config := new(shadowsocks_2022.ClientConfig)
  204. config.Address = server.Address.Build()
  205. config.Port = uint32(server.Port)
  206. config.Method = server.Cipher
  207. config.Key = server.Password
  208. config.UdpOverTcp = server.UoT
  209. config.UdpOverTcpVersion = uint32(server.UoTVersion)
  210. return config, nil
  211. }
  212. }
  213. config := new(shadowsocks.ClientConfig)
  214. for _, server := range v.Servers {
  215. if C.Contains(shadowaead_2022.List, server.Cipher) {
  216. return nil, errors.New("Shadowsocks 2022 accept no multi servers")
  217. }
  218. if server.Address == nil {
  219. return nil, errors.New("Shadowsocks server address is not set.")
  220. }
  221. if server.Port == 0 {
  222. return nil, errors.New("Invalid Shadowsocks port.")
  223. }
  224. if server.Password == "" {
  225. return nil, errors.New("Shadowsocks password is not specified.")
  226. }
  227. account := &shadowsocks.Account{
  228. Password: server.Password,
  229. }
  230. account.CipherType = cipherFromString(server.Cipher)
  231. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  232. return nil, errors.New("unknown cipher method: ", server.Cipher)
  233. }
  234. account.IvCheck = server.IVCheck
  235. ss := &protocol.ServerEndpoint{
  236. Address: server.Address.Build(),
  237. Port: uint32(server.Port),
  238. User: &protocol.User{
  239. Level: uint32(server.Level),
  240. Email: server.Email,
  241. Account: serial.ToTypedMessage(account),
  242. },
  243. }
  244. config.Server = ss
  245. break
  246. }
  247. return config, nil
  248. }