config_json.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "github.com/v2ray/v2ray-core/proxy/internal/config"
  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 err
  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.ErrorBadConfiguration
  45. }
  46. if len(jsonConfig.Password) == 0 {
  47. log.Error("Shadowsocks: Password is not specified.")
  48. return internal.ErrorBadConfiguration
  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. config.RegisterInboundConfig("shadowsocks", func(data []byte) (interface{}, error) {
  57. rawConfig := new(Config)
  58. err := json.Unmarshal(data, rawConfig)
  59. return rawConfig, err
  60. })
  61. }