shadowsocks.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package conf
  2. import (
  3. "strings"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/xtls/xray-core/common/protocol"
  6. "github.com/xtls/xray-core/common/serial"
  7. "github.com/xtls/xray-core/proxy/shadowsocks"
  8. )
  9. func cipherFromString(c string) shadowsocks.CipherType {
  10. switch strings.ToLower(c) {
  11. case "aes-256-cfb":
  12. return shadowsocks.CipherType_AES_256_CFB
  13. case "aes-128-cfb":
  14. return shadowsocks.CipherType_AES_128_CFB
  15. case "chacha20":
  16. return shadowsocks.CipherType_CHACHA20
  17. case "chacha20-ietf":
  18. return shadowsocks.CipherType_CHACHA20_IETF
  19. case "aes-128-gcm", "aead_aes_128_gcm":
  20. return shadowsocks.CipherType_AES_128_GCM
  21. case "aes-256-gcm", "aead_aes_256_gcm":
  22. return shadowsocks.CipherType_AES_256_GCM
  23. case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
  24. return shadowsocks.CipherType_CHACHA20_POLY1305
  25. case "none", "plain":
  26. return shadowsocks.CipherType_NONE
  27. default:
  28. return shadowsocks.CipherType_UNKNOWN
  29. }
  30. }
  31. type ShadowsocksUserConfig struct {
  32. Cipher string `json:"method"`
  33. Password string `json:"password"`
  34. Level byte `json:"level"`
  35. Email string `json:"email"`
  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. config := new(shadowsocks.ServerConfig)
  47. config.Network = v.NetworkList.Build()
  48. if v.Users != nil {
  49. for _, user := range v.Users {
  50. account := &shadowsocks.Account{
  51. Password: user.Password,
  52. CipherType: cipherFromString(user.Cipher),
  53. }
  54. if account.Password == "" {
  55. return nil, newError("Shadowsocks password is not specified.")
  56. }
  57. if account.CipherType < 5 || account.CipherType > 7 {
  58. return nil, newError("unsupported cipher method: ", user.Cipher)
  59. }
  60. config.Users = append(config.Users, &protocol.User{
  61. Email: user.Email,
  62. Level: uint32(user.Level),
  63. Account: serial.ToTypedMessage(account),
  64. })
  65. }
  66. } else {
  67. account := &shadowsocks.Account{
  68. Password: v.Password,
  69. CipherType: cipherFromString(v.Cipher),
  70. }
  71. if account.Password == "" {
  72. return nil, newError("Shadowsocks password is not specified.")
  73. }
  74. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  75. return nil, newError("unknown cipher method: ", v.Cipher)
  76. }
  77. config.Users = append(config.Users, &protocol.User{
  78. Email: v.Email,
  79. Level: uint32(v.Level),
  80. Account: serial.ToTypedMessage(account),
  81. })
  82. }
  83. return config, nil
  84. }
  85. type ShadowsocksServerTarget struct {
  86. Address *Address `json:"address"`
  87. Port uint16 `json:"port"`
  88. Cipher string `json:"method"`
  89. Password string `json:"password"`
  90. Email string `json:"email"`
  91. Level byte `json:"level"`
  92. }
  93. type ShadowsocksClientConfig struct {
  94. Servers []*ShadowsocksServerTarget `json:"servers"`
  95. }
  96. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  97. config := new(shadowsocks.ClientConfig)
  98. if len(v.Servers) == 0 {
  99. return nil, newError("0 Shadowsocks server configured.")
  100. }
  101. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  102. for idx, server := range v.Servers {
  103. if server.Address == nil {
  104. return nil, newError("Shadowsocks server address is not set.")
  105. }
  106. if server.Port == 0 {
  107. return nil, newError("Invalid Shadowsocks port.")
  108. }
  109. if server.Password == "" {
  110. return nil, newError("Shadowsocks password is not specified.")
  111. }
  112. account := &shadowsocks.Account{
  113. Password: server.Password,
  114. }
  115. account.CipherType = cipherFromString(server.Cipher)
  116. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  117. return nil, newError("unknown cipher method: ", server.Cipher)
  118. }
  119. ss := &protocol.ServerEndpoint{
  120. Address: server.Address.Build(),
  121. Port: uint32(server.Port),
  122. User: []*protocol.User{
  123. {
  124. Level: uint32(server.Level),
  125. Email: server.Email,
  126. Account: serial.ToTypedMessage(account),
  127. },
  128. },
  129. }
  130. serverSpecs[idx] = ss
  131. }
  132. config.Server = serverSpecs
  133. return config, nil
  134. }