shadowsocks.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-128-gcm", "aead_aes_128_gcm":
  12. return shadowsocks.CipherType_AES_128_GCM
  13. case "aes-256-gcm", "aead_aes_256_gcm":
  14. return shadowsocks.CipherType_AES_256_GCM
  15. case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
  16. return shadowsocks.CipherType_CHACHA20_POLY1305
  17. case "none", "plain":
  18. return shadowsocks.CipherType_NONE
  19. default:
  20. return shadowsocks.CipherType_UNKNOWN
  21. }
  22. }
  23. type ShadowsocksServerConfig struct {
  24. Cipher string `json:"method"`
  25. Password string `json:"password"`
  26. UDP bool `json:"udp"`
  27. Level byte `json:"level"`
  28. Email string `json:"email"`
  29. NetworkList *NetworkList `json:"network"`
  30. }
  31. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  32. config := new(shadowsocks.ServerConfig)
  33. config.UdpEnabled = v.UDP
  34. config.Network = v.NetworkList.Build()
  35. if v.Password == "" {
  36. return nil, newError("Shadowsocks password is not specified.")
  37. }
  38. account := &shadowsocks.Account{
  39. Password: v.Password,
  40. }
  41. account.CipherType = cipherFromString(v.Cipher)
  42. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  43. return nil, newError("unknown cipher method: ", v.Cipher)
  44. }
  45. config.User = &protocol.User{
  46. Email: v.Email,
  47. Level: uint32(v.Level),
  48. Account: serial.ToTypedMessage(account),
  49. }
  50. return config, nil
  51. }
  52. type ShadowsocksServerTarget struct {
  53. Address *Address `json:"address"`
  54. Port uint16 `json:"port"`
  55. Cipher string `json:"method"`
  56. Password string `json:"password"`
  57. Email string `json:"email"`
  58. Ota bool `json:"ota"`
  59. Level byte `json:"level"`
  60. }
  61. type ShadowsocksClientConfig struct {
  62. Servers []*ShadowsocksServerTarget `json:"servers"`
  63. }
  64. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  65. config := new(shadowsocks.ClientConfig)
  66. if len(v.Servers) == 0 {
  67. return nil, newError("0 Shadowsocks server configured.")
  68. }
  69. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  70. for idx, server := range v.Servers {
  71. if server.Address == nil {
  72. return nil, newError("Shadowsocks server address is not set.")
  73. }
  74. if server.Port == 0 {
  75. return nil, newError("Invalid Shadowsocks port.")
  76. }
  77. if server.Password == "" {
  78. return nil, newError("Shadowsocks password is not specified.")
  79. }
  80. account := &shadowsocks.Account{
  81. Password: server.Password,
  82. }
  83. account.CipherType = cipherFromString(server.Cipher)
  84. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  85. return nil, newError("unknown cipher method: ", server.Cipher)
  86. }
  87. ss := &protocol.ServerEndpoint{
  88. Address: server.Address.Build(),
  89. Port: uint32(server.Port),
  90. User: []*protocol.User{
  91. {
  92. Level: uint32(server.Level),
  93. Email: server.Email,
  94. Account: serial.ToTypedMessage(account),
  95. },
  96. },
  97. }
  98. serverSpecs[idx] = ss
  99. }
  100. config.Server = serverSpecs
  101. return config, nil
  102. }