config_json.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // +build json
  2. package shadowsocks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/log"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/proxy/registry"
  11. "github.com/golang/protobuf/ptypes"
  12. )
  13. func (this *Config) UnmarshalJSON(data []byte) error {
  14. type JsonConfig struct {
  15. Cipher string `json:"method"`
  16. Password string `json:"password"`
  17. UDP bool `json:"udp"`
  18. Level byte `json:"level"`
  19. Email string `json:"email"`
  20. }
  21. jsonConfig := new(JsonConfig)
  22. if err := json.Unmarshal(data, jsonConfig); err != nil {
  23. return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
  24. }
  25. this.UdpEnabled = jsonConfig.UDP
  26. jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
  27. switch jsonConfig.Cipher {
  28. case "aes-256-cfb":
  29. this.Cipher = Config_AES_256_CFB
  30. case "aes-128-cfb":
  31. this.Cipher = Config_AES_128_CFB
  32. case "chacha20":
  33. this.Cipher = Config_CHACHA20
  34. case "chacha20-ietf":
  35. this.Cipher = Config_CHACHA20_IEFT
  36. default:
  37. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  38. return common.ErrBadConfiguration
  39. }
  40. if len(jsonConfig.Password) == 0 {
  41. log.Error("Shadowsocks: Password is not specified.")
  42. return common.ErrBadConfiguration
  43. }
  44. account, err := ptypes.MarshalAny(&Account{
  45. Password: jsonConfig.Password,
  46. })
  47. if err != nil {
  48. log.Error("Shadowsocks: Failed to create account: ", err)
  49. return common.ErrBadConfiguration
  50. }
  51. this.User = &protocol.User{
  52. Email: jsonConfig.Email,
  53. Level: uint32(jsonConfig.Level),
  54. Account: account,
  55. }
  56. return nil
  57. }
  58. func init() {
  59. registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
  60. }