config_json.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. UDP bool `json:"udp"`
  14. }
  15. jsonConfig := new(JsonConfig)
  16. if err := json.Unmarshal(data, jsonConfig); err != nil {
  17. return err
  18. }
  19. if len(jsonConfig.Password) == 0 {
  20. log.Error("Shadowsocks: Password is not specified.")
  21. return internal.ErrorBadConfiguration
  22. }
  23. this.UDP = jsonConfig.UDP
  24. this.Password = jsonConfig.Password.String()
  25. if this.Cipher == nil {
  26. log.Error("Shadowsocks: Cipher method is not specified.")
  27. return internal.ErrorBadConfiguration
  28. }
  29. jsonConfig.Cipher = jsonConfig.Cipher.ToLower()
  30. switch jsonConfig.Cipher.String() {
  31. case "aes-256-cfb":
  32. this.Cipher = &AesCfb{
  33. KeyBytes: 32,
  34. }
  35. case "aes-128-cfb":
  36. this.Cipher = &AesCfb{
  37. KeyBytes: 32,
  38. }
  39. default:
  40. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  41. return internal.ErrorBadConfiguration
  42. }
  43. return nil
  44. }