shadowsocks.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. }
  45. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  46. errors.PrintNonRemovalDeprecatedFeatureWarning("Shadowsocks (with no Forward Secrecy, etc.)", "VLESS Encryption")
  47. if C.Contains(shadowaead_2022.List, v.Cipher) {
  48. return buildShadowsocks2022(v)
  49. }
  50. config := new(shadowsocks.ServerConfig)
  51. config.Network = v.NetworkList.Build()
  52. if v.Users != nil {
  53. for _, user := range v.Users {
  54. account := &shadowsocks.Account{
  55. Password: user.Password,
  56. CipherType: cipherFromString(user.Cipher),
  57. }
  58. if account.Password == "" {
  59. return nil, errors.New("Shadowsocks password is not specified.")
  60. }
  61. if account.CipherType < shadowsocks.CipherType_AES_128_GCM ||
  62. account.CipherType > shadowsocks.CipherType_XCHACHA20_POLY1305 {
  63. return nil, errors.New("unsupported cipher method: ", user.Cipher)
  64. }
  65. config.Users = append(config.Users, &protocol.User{
  66. Email: user.Email,
  67. Level: uint32(user.Level),
  68. Account: serial.ToTypedMessage(account),
  69. })
  70. }
  71. } else {
  72. account := &shadowsocks.Account{
  73. Password: v.Password,
  74. CipherType: cipherFromString(v.Cipher),
  75. }
  76. if account.Password == "" {
  77. return nil, errors.New("Shadowsocks password is not specified.")
  78. }
  79. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  80. return nil, errors.New("unknown cipher method: ", v.Cipher)
  81. }
  82. config.Users = append(config.Users, &protocol.User{
  83. Email: v.Email,
  84. Level: uint32(v.Level),
  85. Account: serial.ToTypedMessage(account),
  86. })
  87. }
  88. return config, nil
  89. }
  90. func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
  91. if len(v.Users) == 0 {
  92. config := new(shadowsocks_2022.ServerConfig)
  93. config.Method = v.Cipher
  94. config.Key = v.Password
  95. config.Network = v.NetworkList.Build()
  96. config.Email = v.Email
  97. return config, nil
  98. }
  99. if v.Cipher == "" {
  100. return nil, errors.New("shadowsocks 2022 (multi-user): missing server method")
  101. }
  102. if !strings.Contains(v.Cipher, "aes") {
  103. return nil, errors.New("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
  104. }
  105. if v.Users[0].Address == nil {
  106. config := new(shadowsocks_2022.MultiUserServerConfig)
  107. config.Method = v.Cipher
  108. config.Key = v.Password
  109. config.Network = v.NetworkList.Build()
  110. for _, user := range v.Users {
  111. if user.Cipher != "" {
  112. return nil, errors.New("shadowsocks 2022 (multi-user): users must have empty method")
  113. }
  114. account := &shadowsocks_2022.Account{
  115. Key: user.Password,
  116. }
  117. config.Users = append(config.Users, &protocol.User{
  118. Email: user.Email,
  119. Level: uint32(user.Level),
  120. Account: serial.ToTypedMessage(account),
  121. })
  122. }
  123. return config, nil
  124. }
  125. config := new(shadowsocks_2022.RelayServerConfig)
  126. config.Method = v.Cipher
  127. config.Key = v.Password
  128. config.Network = v.NetworkList.Build()
  129. for _, user := range v.Users {
  130. if user.Cipher != "" {
  131. return nil, errors.New("shadowsocks 2022 (relay): users must have empty method")
  132. }
  133. if user.Address == nil {
  134. return nil, errors.New("shadowsocks 2022 (relay): all users must have relay address")
  135. }
  136. config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
  137. Key: user.Password,
  138. Email: user.Email,
  139. Address: user.Address.Build(),
  140. Port: uint32(user.Port),
  141. })
  142. }
  143. return config, nil
  144. }
  145. type ShadowsocksServerTarget struct {
  146. Address *Address `json:"address"`
  147. Port uint16 `json:"port"`
  148. Level byte `json:"level"`
  149. Email string `json:"email"`
  150. Cipher string `json:"method"`
  151. Password string `json:"password"`
  152. UoT bool `json:"uot"`
  153. UoTVersion int `json:"uotVersion"`
  154. }
  155. type ShadowsocksClientConfig struct {
  156. Address *Address `json:"address"`
  157. Port uint16 `json:"port"`
  158. Level byte `json:"level"`
  159. Email string `json:"email"`
  160. Cipher string `json:"method"`
  161. Password string `json:"password"`
  162. UoT bool `json:"uot"`
  163. UoTVersion int `json:"uotVersion"`
  164. Servers []*ShadowsocksServerTarget `json:"servers"`
  165. }
  166. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  167. errors.PrintNonRemovalDeprecatedFeatureWarning("Shadowsocks (with no Forward Secrecy, etc.)", "VLESS Encryption")
  168. if v.Address != nil {
  169. v.Servers = []*ShadowsocksServerTarget{
  170. {
  171. Address: v.Address,
  172. Port: v.Port,
  173. Level: v.Level,
  174. Email: v.Email,
  175. Cipher: v.Cipher,
  176. Password: v.Password,
  177. UoT: v.UoT,
  178. UoTVersion: v.UoTVersion,
  179. },
  180. }
  181. }
  182. if len(v.Servers) != 1 {
  183. 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`)
  184. }
  185. if len(v.Servers) == 1 {
  186. server := v.Servers[0]
  187. if C.Contains(shadowaead_2022.List, server.Cipher) {
  188. if server.Address == nil {
  189. return nil, errors.New("Shadowsocks server address is not set.")
  190. }
  191. if server.Port == 0 {
  192. return nil, errors.New("Invalid Shadowsocks port.")
  193. }
  194. if server.Password == "" {
  195. return nil, errors.New("Shadowsocks password is not specified.")
  196. }
  197. config := new(shadowsocks_2022.ClientConfig)
  198. config.Address = server.Address.Build()
  199. config.Port = uint32(server.Port)
  200. config.Method = server.Cipher
  201. config.Key = server.Password
  202. config.UdpOverTcp = server.UoT
  203. config.UdpOverTcpVersion = uint32(server.UoTVersion)
  204. return config, nil
  205. }
  206. }
  207. config := new(shadowsocks.ClientConfig)
  208. for _, server := range v.Servers {
  209. if C.Contains(shadowaead_2022.List, server.Cipher) {
  210. return nil, errors.New("Shadowsocks 2022 accept no multi servers")
  211. }
  212. if server.Address == nil {
  213. return nil, errors.New("Shadowsocks server address is not set.")
  214. }
  215. if server.Port == 0 {
  216. return nil, errors.New("Invalid Shadowsocks port.")
  217. }
  218. if server.Password == "" {
  219. return nil, errors.New("Shadowsocks password is not specified.")
  220. }
  221. account := &shadowsocks.Account{
  222. Password: server.Password,
  223. }
  224. account.CipherType = cipherFromString(server.Cipher)
  225. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  226. return nil, errors.New("unknown cipher method: ", server.Cipher)
  227. }
  228. ss := &protocol.ServerEndpoint{
  229. Address: server.Address.Build(),
  230. Port: uint32(server.Port),
  231. User: &protocol.User{
  232. Level: uint32(server.Level),
  233. Email: server.Email,
  234. Account: serial.ToTypedMessage(account),
  235. },
  236. }
  237. config.Server = ss
  238. break
  239. }
  240. return config, nil
  241. }