shadowsocks.go 3.6 KB

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