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