config_json.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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.String(), this.Cipher.KeySize())
  50. this.Level = protocol.UserLevel(jsonConfig.Level)
  51. return nil
  52. }
  53. func init() {
  54. config.RegisterInboundConfig("shadowsocks", func(data []byte) (interface{}, error) {
  55. rawConfig := new(Config)
  56. err := json.Unmarshal(data, rawConfig)
  57. return rawConfig, err
  58. })
  59. }