shadowsocks.go 3.7 KB

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