config_json.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 *ServerConfig) 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. if len(jsonConfig.Password) == 0 {
  27. log.Error("Shadowsocks: Password is not specified.")
  28. return common.ErrBadConfiguration
  29. }
  30. account := &Account{
  31. Password: jsonConfig.Password,
  32. }
  33. jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
  34. switch jsonConfig.Cipher {
  35. case "aes-256-cfb":
  36. account.CipherType = CipherType_AES_256_CFB
  37. case "aes-128-cfb":
  38. account.CipherType = CipherType_AES_128_CFB
  39. case "chacha20":
  40. account.CipherType = CipherType_CHACHA20
  41. case "chacha20-ietf":
  42. account.CipherType = CipherType_CHACHA20_IEFT
  43. default:
  44. log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
  45. return common.ErrBadConfiguration
  46. }
  47. anyAccount, err := ptypes.MarshalAny(account)
  48. if err != nil {
  49. log.Error("Shadowsocks: Failed to create account: ", err)
  50. return common.ErrBadConfiguration
  51. }
  52. this.User = &protocol.User{
  53. Email: jsonConfig.Email,
  54. Level: uint32(jsonConfig.Level),
  55. Account: anyAccount,
  56. }
  57. return nil
  58. }
  59. func init() {
  60. registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(ServerConfig) })
  61. }