config_json.go 1.5 KB

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