shadowsocks.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package conf
  2. import (
  3. "strings"
  4. "github.com/golang/protobuf/proto"
  5. "v2ray.com/core/common/protocol"
  6. "v2ray.com/core/common/serial"
  7. "v2ray.com/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 ShadowsocksServerConfig struct {
  32. Cipher string `json:"method"`
  33. Password string `json:"password"`
  34. UDP bool `json:"udp"`
  35. Level byte `json:"level"`
  36. Email string `json:"email"`
  37. NetworkList *NetworkList `json:"network"`
  38. }
  39. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  40. config := new(shadowsocks.ServerConfig)
  41. config.UdpEnabled = v.UDP
  42. config.Network = v.NetworkList.Build()
  43. if v.Password == "" {
  44. return nil, newError("Shadowsocks password is not specified.")
  45. }
  46. account := &shadowsocks.Account{
  47. Password: v.Password,
  48. }
  49. account.CipherType = cipherFromString(v.Cipher)
  50. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  51. return nil, newError("unknown cipher method: ", v.Cipher)
  52. }
  53. config.User = &protocol.User{
  54. Email: v.Email,
  55. Level: uint32(v.Level),
  56. Account: serial.ToTypedMessage(account),
  57. }
  58. return config, nil
  59. }
  60. type ShadowsocksServerTarget struct {
  61. Address *Address `json:"address"`
  62. Port uint16 `json:"port"`
  63. Cipher string `json:"method"`
  64. Password string `json:"password"`
  65. Email string `json:"email"`
  66. Ota bool `json:"ota"`
  67. Level byte `json:"level"`
  68. }
  69. type ShadowsocksClientConfig struct {
  70. Servers []*ShadowsocksServerTarget `json:"servers"`
  71. }
  72. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  73. config := new(shadowsocks.ClientConfig)
  74. if len(v.Servers) == 0 {
  75. return nil, newError("0 Shadowsocks server configured.")
  76. }
  77. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  78. for idx, server := range v.Servers {
  79. if server.Address == nil {
  80. return nil, newError("Shadowsocks server address is not set.")
  81. }
  82. if server.Port == 0 {
  83. return nil, newError("Invalid Shadowsocks port.")
  84. }
  85. if server.Password == "" {
  86. return nil, newError("Shadowsocks password is not specified.")
  87. }
  88. account := &shadowsocks.Account{
  89. Password: server.Password,
  90. }
  91. account.CipherType = cipherFromString(server.Cipher)
  92. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  93. return nil, newError("unknown cipher method: ", server.Cipher)
  94. }
  95. ss := &protocol.ServerEndpoint{
  96. Address: server.Address.Build(),
  97. Port: uint32(server.Port),
  98. User: []*protocol.User{
  99. {
  100. Level: uint32(server.Level),
  101. Email: server.Email,
  102. Account: serial.ToTypedMessage(account),
  103. },
  104. },
  105. }
  106. serverSpecs[idx] = ss
  107. }
  108. config.Server = serverSpecs
  109. return config, nil
  110. }