config_json.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/protocol"
  7. "github.com/v2ray/v2ray-core/common/serial"
  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 serial.StringLiteral `json:"method"`
  14. Password serial.StringLiteral `json:"password"`
  15. UDP bool `json:"udp"`
  16. Level byte `json:"level"`
  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 = jsonConfig.Cipher.ToLower()
  24. switch jsonConfig.Cipher.String() {
  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. default:
  34. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  35. return internal.ErrorBadConfiguration
  36. }
  37. if len(jsonConfig.Password) == 0 {
  38. log.Error("Shadowsocks: Password is not specified.")
  39. return internal.ErrorBadConfiguration
  40. }
  41. this.Key = PasswordToCipherKey(jsonConfig.Password.String(), this.Cipher.KeySize())
  42. this.Level = protocol.UserLevel(jsonConfig.Level)
  43. return nil
  44. }
  45. func init() {
  46. config.RegisterInboundConfig("shadowsocks", func(data []byte) (interface{}, error) {
  47. rawConfig := new(Config)
  48. err := json.Unmarshal(data, rawConfig)
  49. return rawConfig, err
  50. })
  51. }