config_json.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // +build json
  2. package shadowsocks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/loader"
  9. "v2ray.com/core/common/log"
  10. "v2ray.com/core/common/protocol"
  11. )
  12. func (this *ServerConfig) UnmarshalJSON(data []byte) error {
  13. type JsonConfig struct {
  14. Cipher string `json:"method"`
  15. Password string `json:"password"`
  16. UDP bool `json:"udp"`
  17. Level byte `json:"level"`
  18. Email string `json:"email"`
  19. }
  20. jsonConfig := new(JsonConfig)
  21. if err := json.Unmarshal(data, jsonConfig); err != nil {
  22. return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
  23. }
  24. this.UdpEnabled = jsonConfig.UDP
  25. if len(jsonConfig.Password) == 0 {
  26. log.Error("Shadowsocks: Password is not specified.")
  27. return common.ErrBadConfiguration
  28. }
  29. account := &Account{
  30. Password: jsonConfig.Password,
  31. }
  32. jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
  33. switch jsonConfig.Cipher {
  34. case "aes-256-cfb":
  35. account.CipherType = CipherType_AES_256_CFB
  36. case "aes-128-cfb":
  37. account.CipherType = CipherType_AES_128_CFB
  38. case "chacha20":
  39. account.CipherType = CipherType_CHACHA20
  40. case "chacha20-ietf":
  41. account.CipherType = CipherType_CHACHA20_IEFT
  42. default:
  43. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  44. return common.ErrBadConfiguration
  45. }
  46. this.User = &protocol.User{
  47. Email: jsonConfig.Email,
  48. Level: uint32(jsonConfig.Level),
  49. Account: loader.NewTypedSettings(account),
  50. }
  51. return nil
  52. }