config_json.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "github.com/v2ray/v2ray-core/proxy/internal/config"
  9. )
  10. func (this *Config) UnmarshalJSON(data []byte) error {
  11. type JsonConfig struct {
  12. Cipher serial.StringLiteral `json:"method"`
  13. Password serial.StringLiteral `json:"password"`
  14. UDP bool `json:"udp"`
  15. }
  16. jsonConfig := new(JsonConfig)
  17. if err := json.Unmarshal(data, jsonConfig); err != nil {
  18. return err
  19. }
  20. this.UDP = jsonConfig.UDP
  21. jsonConfig.Cipher = jsonConfig.Cipher.ToLower()
  22. switch jsonConfig.Cipher.String() {
  23. case "aes-256-cfb":
  24. this.Cipher = &AesCfb{
  25. KeyBytes: 32,
  26. }
  27. case "aes-128-cfb":
  28. this.Cipher = &AesCfb{
  29. KeyBytes: 16,
  30. }
  31. default:
  32. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  33. return internal.ErrorBadConfiguration
  34. }
  35. if len(jsonConfig.Password) == 0 {
  36. log.Error("Shadowsocks: Password is not specified.")
  37. return internal.ErrorBadConfiguration
  38. }
  39. this.Key = PasswordToCipherKey(jsonConfig.Password.String(), this.Cipher.KeySize())
  40. return nil
  41. }
  42. func init() {
  43. config.RegisterInboundConfig("shadowsocks", func(data []byte) (interface{}, error) {
  44. rawConfig := new(Config)
  45. err := json.Unmarshal(data, rawConfig)
  46. return rawConfig, err
  47. })
  48. }