config_json.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/log"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/proxy/registry"
  11. )
  12. func (this *Config) 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.UDP = jsonConfig.UDP
  25. jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
  26. switch jsonConfig.Cipher {
  27. case "aes-256-cfb":
  28. this.Cipher = &AesCfb{
  29. KeyBytes: 32,
  30. }
  31. case "aes-128-cfb":
  32. this.Cipher = &AesCfb{
  33. KeyBytes: 16,
  34. }
  35. case "chacha20":
  36. this.Cipher = &ChaCha20{
  37. IVBytes: 8,
  38. }
  39. case "chacha20-ietf":
  40. this.Cipher = &ChaCha20{
  41. IVBytes: 12,
  42. }
  43. default:
  44. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  45. return common.ErrBadConfiguration
  46. }
  47. if len(jsonConfig.Password) == 0 {
  48. log.Error("Shadowsocks: Password is not specified.")
  49. return common.ErrBadConfiguration
  50. }
  51. this.Key = PasswordToCipherKey(jsonConfig.Password, this.Cipher.KeySize())
  52. this.Level = protocol.UserLevel(jsonConfig.Level)
  53. this.Email = jsonConfig.Email
  54. return nil
  55. }
  56. func init() {
  57. registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
  58. }