config_json.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // +build json
  2. package shadowsocks
  3. import (
  4. "encoding/json"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. "github.com/v2ray/v2ray-core/proxy/internal"
  8. )
  9. func (this *Config) UnmarshalJSON(data []byte) error {
  10. type JsonConfig struct {
  11. Cipher serial.StringLiteral `json:"method"`
  12. Password serial.StringLiteral `json:"password"`
  13. }
  14. jsonConfig := new(JsonConfig)
  15. if err := json.Unmarshal(data, jsonConfig); err != nil {
  16. return err
  17. }
  18. if len(jsonConfig.Password) == 0 {
  19. log.Error("Shadowsocks: Password is not specified.")
  20. return internal.ErrorBadConfiguration
  21. }
  22. this.Password = jsonConfig.Password.String()
  23. if this.Cipher == nil {
  24. log.Error("Shadowsocks: Cipher method is not specified.")
  25. return internal.ErrorBadConfiguration
  26. }
  27. jsonConfig.Cipher = jsonConfig.Cipher.ToLower()
  28. switch jsonConfig.Cipher.String() {
  29. case "aes-256-cfb":
  30. this.Cipher = &AesCfb{
  31. KeyBytes: 32,
  32. }
  33. case "aes-128-cfb":
  34. this.Cipher = &AesCfb{
  35. KeyBytes: 32,
  36. }
  37. default:
  38. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  39. return internal.ErrorBadConfiguration
  40. }
  41. return nil
  42. }