shadowsocks.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/protocol"
  7. "github.com/xtls/xray-core/common/serial"
  8. "github.com/xtls/xray-core/proxy/shadowsocks"
  9. "github.com/xtls/xray-core/proxy/shadowsocks_2022"
  10. "google.golang.org/protobuf/proto"
  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. Address *Address `json:"address"`
  34. Port uint16 `json:"port"`
  35. }
  36. type ShadowsocksServerConfig struct {
  37. Cipher string `json:"method"`
  38. Password string `json:"password"`
  39. Level byte `json:"level"`
  40. Email string `json:"email"`
  41. Users []*ShadowsocksUserConfig `json:"clients"`
  42. NetworkList *NetworkList `json:"network"`
  43. IVCheck bool `json:"ivCheck"`
  44. }
  45. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  46. if C.Contains(shadowaead_2022.List, v.Cipher) {
  47. return buildShadowsocks2022(v)
  48. }
  49. config := new(shadowsocks.ServerConfig)
  50. config.Network = v.NetworkList.Build()
  51. if v.Users != nil {
  52. for _, user := range v.Users {
  53. account := &shadowsocks.Account{
  54. Password: user.Password,
  55. CipherType: cipherFromString(user.Cipher),
  56. IvCheck: v.IVCheck,
  57. }
  58. if account.Password == "" {
  59. return nil, newError("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, newError("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. IvCheck: v.IVCheck,
  76. }
  77. if account.Password == "" {
  78. return nil, newError("Shadowsocks password is not specified.")
  79. }
  80. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  81. return nil, newError("unknown cipher method: ", v.Cipher)
  82. }
  83. config.Users = append(config.Users, &protocol.User{
  84. Email: v.Email,
  85. Level: uint32(v.Level),
  86. Account: serial.ToTypedMessage(account),
  87. })
  88. }
  89. return config, nil
  90. }
  91. func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
  92. if len(v.Users) == 0 {
  93. config := new(shadowsocks_2022.ServerConfig)
  94. config.Method = v.Cipher
  95. config.Key = v.Password
  96. config.Network = v.NetworkList.Build()
  97. config.Email = v.Email
  98. return config, nil
  99. }
  100. if v.Cipher == "" {
  101. return nil, newError("shadowsocks 2022 (multi-user): missing server method")
  102. }
  103. if !strings.Contains(v.Cipher, "aes") {
  104. return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
  105. }
  106. if v.Users[0].Address == nil {
  107. config := new(shadowsocks_2022.MultiUserServerConfig)
  108. config.Method = v.Cipher
  109. config.Key = v.Password
  110. config.Network = v.NetworkList.Build()
  111. for _, user := range v.Users {
  112. if user.Cipher != "" {
  113. return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
  114. }
  115. config.Users = append(config.Users, &shadowsocks_2022.User{
  116. Key: user.Password,
  117. Email: user.Email,
  118. })
  119. }
  120. return config, nil
  121. }
  122. config := new(shadowsocks_2022.RelayServerConfig)
  123. config.Method = v.Cipher
  124. config.Key = v.Password
  125. config.Network = v.NetworkList.Build()
  126. for _, user := range v.Users {
  127. if user.Cipher != "" {
  128. return nil, newError("shadowsocks 2022 (relay): users must have empty method")
  129. }
  130. if user.Address == nil {
  131. return nil, newError("shadowsocks 2022 (relay): all users must have relay address")
  132. }
  133. config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
  134. Key: user.Password,
  135. Email: user.Email,
  136. Address: user.Address.Build(),
  137. Port: uint32(user.Port),
  138. })
  139. }
  140. return config, nil
  141. }
  142. type ShadowsocksServerTarget struct {
  143. Address *Address `json:"address"`
  144. Port uint16 `json:"port"`
  145. Cipher string `json:"method"`
  146. Password string `json:"password"`
  147. Email string `json:"email"`
  148. Level byte `json:"level"`
  149. IVCheck bool `json:"ivCheck"`
  150. UoT bool `json:"uot"`
  151. UoTVersion int `json:"uotVersion"`
  152. }
  153. type ShadowsocksClientConfig struct {
  154. Servers []*ShadowsocksServerTarget `json:"servers"`
  155. }
  156. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  157. if len(v.Servers) == 0 {
  158. return nil, newError("0 Shadowsocks server configured.")
  159. }
  160. if len(v.Servers) == 1 {
  161. server := v.Servers[0]
  162. if C.Contains(shadowaead_2022.List, server.Cipher) {
  163. if server.Address == nil {
  164. return nil, newError("Shadowsocks server address is not set.")
  165. }
  166. if server.Port == 0 {
  167. return nil, newError("Invalid Shadowsocks port.")
  168. }
  169. if server.Password == "" {
  170. return nil, newError("Shadowsocks password is not specified.")
  171. }
  172. config := new(shadowsocks_2022.ClientConfig)
  173. config.Address = server.Address.Build()
  174. config.Port = uint32(server.Port)
  175. config.Method = server.Cipher
  176. config.Key = server.Password
  177. config.UdpOverTcp = server.UoT
  178. config.UdpOverTcpVersion = uint32(server.UoTVersion)
  179. return config, nil
  180. }
  181. }
  182. config := new(shadowsocks.ClientConfig)
  183. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  184. for idx, server := range v.Servers {
  185. if C.Contains(shadowaead_2022.List, server.Cipher) {
  186. return nil, newError("Shadowsocks 2022 accept no multi servers")
  187. }
  188. if server.Address == nil {
  189. return nil, newError("Shadowsocks server address is not set.")
  190. }
  191. if server.Port == 0 {
  192. return nil, newError("Invalid Shadowsocks port.")
  193. }
  194. if server.Password == "" {
  195. return nil, newError("Shadowsocks password is not specified.")
  196. }
  197. account := &shadowsocks.Account{
  198. Password: server.Password,
  199. }
  200. account.CipherType = cipherFromString(server.Cipher)
  201. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  202. return nil, newError("unknown cipher method: ", server.Cipher)
  203. }
  204. account.IvCheck = server.IVCheck
  205. ss := &protocol.ServerEndpoint{
  206. Address: server.Address.Build(),
  207. Port: uint32(server.Port),
  208. User: []*protocol.User{
  209. {
  210. Level: uint32(server.Level),
  211. Email: server.Email,
  212. Account: serial.ToTypedMessage(account),
  213. },
  214. },
  215. }
  216. serverSpecs[idx] = ss
  217. }
  218. config.Server = serverSpecs
  219. return config, nil
  220. }