config_json.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // +build json
  2. package shadowsocks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. "github.com/v2ray/v2ray-core/common/protocol"
  9. "github.com/v2ray/v2ray-core/proxy/internal"
  10. )
  11. func (this *Config) UnmarshalJSON(data []byte) error {
  12. type JsonConfig struct {
  13. Cipher string `json:"method"`
  14. Password string `json:"password"`
  15. UDP bool `json:"udp"`
  16. Level byte `json:"level"`
  17. Email string `json:"email"`
  18. }
  19. jsonConfig := new(JsonConfig)
  20. if err := json.Unmarshal(data, jsonConfig); err != nil {
  21. return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
  22. }
  23. this.UDP = jsonConfig.UDP
  24. jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
  25. switch jsonConfig.Cipher {
  26. case "aes-256-cfb":
  27. this.Cipher = &AesCfb{
  28. KeyBytes: 32,
  29. }
  30. case "aes-128-cfb":
  31. this.Cipher = &AesCfb{
  32. KeyBytes: 16,
  33. }
  34. case "chacha20":
  35. this.Cipher = &ChaCha20{
  36. IVBytes: 8,
  37. }
  38. case "chacha20-ietf":
  39. this.Cipher = &ChaCha20{
  40. IVBytes: 12,
  41. }
  42. default:
  43. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  44. return internal.ErrBadConfiguration
  45. }
  46. if len(jsonConfig.Password) == 0 {
  47. log.Error("Shadowsocks: Password is not specified.")
  48. return internal.ErrBadConfiguration
  49. }
  50. this.Key = PasswordToCipherKey(jsonConfig.Password, this.Cipher.KeySize())
  51. this.Level = protocol.UserLevel(jsonConfig.Level)
  52. this.Email = jsonConfig.Email
  53. return nil
  54. }
  55. func init() {
  56. internal.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
  57. }