shadowsocks.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. config.Users = append(config.Users, &shadowsocks_2022.User{
  117. Key: user.Password,
  118. Email: user.Email,
  119. })
  120. }
  121. return config, nil
  122. }
  123. config := new(shadowsocks_2022.RelayServerConfig)
  124. config.Method = v.Cipher
  125. config.Key = v.Password
  126. config.Network = v.NetworkList.Build()
  127. for _, user := range v.Users {
  128. if user.Cipher != "" {
  129. return nil, errors.New("shadowsocks 2022 (relay): users must have empty method")
  130. }
  131. if user.Address == nil {
  132. return nil, errors.New("shadowsocks 2022 (relay): all users must have relay address")
  133. }
  134. config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
  135. Key: user.Password,
  136. Email: user.Email,
  137. Address: user.Address.Build(),
  138. Port: uint32(user.Port),
  139. })
  140. }
  141. return config, nil
  142. }
  143. type ShadowsocksServerTarget struct {
  144. Address *Address `json:"address"`
  145. Port uint16 `json:"port"`
  146. Cipher string `json:"method"`
  147. Password string `json:"password"`
  148. Email string `json:"email"`
  149. Level byte `json:"level"`
  150. IVCheck bool `json:"ivCheck"`
  151. UoT bool `json:"uot"`
  152. UoTVersion int `json:"uotVersion"`
  153. }
  154. type ShadowsocksClientConfig struct {
  155. Servers []*ShadowsocksServerTarget `json:"servers"`
  156. }
  157. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  158. if len(v.Servers) == 0 {
  159. return nil, errors.New("0 Shadowsocks server configured.")
  160. }
  161. if len(v.Servers) == 1 {
  162. server := v.Servers[0]
  163. if C.Contains(shadowaead_2022.List, server.Cipher) {
  164. if server.Address == nil {
  165. return nil, errors.New("Shadowsocks server address is not set.")
  166. }
  167. if server.Port == 0 {
  168. return nil, errors.New("Invalid Shadowsocks port.")
  169. }
  170. if server.Password == "" {
  171. return nil, errors.New("Shadowsocks password is not specified.")
  172. }
  173. config := new(shadowsocks_2022.ClientConfig)
  174. config.Address = server.Address.Build()
  175. config.Port = uint32(server.Port)
  176. config.Method = server.Cipher
  177. config.Key = server.Password
  178. config.UdpOverTcp = server.UoT
  179. config.UdpOverTcpVersion = uint32(server.UoTVersion)
  180. return config, nil
  181. }
  182. }
  183. config := new(shadowsocks.ClientConfig)
  184. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  185. for idx, server := range v.Servers {
  186. if C.Contains(shadowaead_2022.List, server.Cipher) {
  187. return nil, errors.New("Shadowsocks 2022 accept no multi servers")
  188. }
  189. if server.Address == nil {
  190. return nil, errors.New("Shadowsocks server address is not set.")
  191. }
  192. if server.Port == 0 {
  193. return nil, errors.New("Invalid Shadowsocks port.")
  194. }
  195. if server.Password == "" {
  196. return nil, errors.New("Shadowsocks password is not specified.")
  197. }
  198. account := &shadowsocks.Account{
  199. Password: server.Password,
  200. }
  201. account.CipherType = cipherFromString(server.Cipher)
  202. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  203. return nil, errors.New("unknown cipher method: ", server.Cipher)
  204. }
  205. account.IvCheck = server.IVCheck
  206. ss := &protocol.ServerEndpoint{
  207. Address: server.Address.Build(),
  208. Port: uint32(server.Port),
  209. User: []*protocol.User{
  210. {
  211. Level: uint32(server.Level),
  212. Email: server.Email,
  213. Account: serial.ToTypedMessage(account),
  214. },
  215. },
  216. }
  217. serverSpecs[idx] = ss
  218. }
  219. config.Server = serverSpecs
  220. return config, nil
  221. }