config_json.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build json
  2. package socks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "v2ray.com/core/common/loader"
  7. v2net "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/protocol"
  9. )
  10. func (this *Account) UnmarshalJSON(data []byte) error {
  11. type JsonConfig struct {
  12. Username string `json:"user"`
  13. Password string `json:"pass"`
  14. }
  15. jsonConfig := new(JsonConfig)
  16. if err := json.Unmarshal(data, jsonConfig); err != nil {
  17. return errors.New("Socks: Failed to parse account: " + err.Error())
  18. }
  19. this.Username = jsonConfig.Username
  20. this.Password = jsonConfig.Password
  21. return nil
  22. }
  23. func (this *ClientConfig) UnmarshalJSON(data []byte) error {
  24. type ServerConfig struct {
  25. Address *v2net.IPOrDomain `json:"address"`
  26. Port v2net.Port `json:"port"`
  27. Users []json.RawMessage `json:"users"`
  28. }
  29. type JsonConfig struct {
  30. Servers []*ServerConfig `json:"servers"`
  31. }
  32. jsonConfig := new(JsonConfig)
  33. if err := json.Unmarshal(data, jsonConfig); err != nil {
  34. return errors.New("Socks|Client: Failed to parse config: " + err.Error())
  35. }
  36. this.Server = make([]*protocol.ServerEndpoint, len(jsonConfig.Servers))
  37. for idx, serverConfig := range jsonConfig.Servers {
  38. server := &protocol.ServerEndpoint{
  39. Address: serverConfig.Address,
  40. Port: uint32(serverConfig.Port),
  41. }
  42. for _, rawUser := range serverConfig.Users {
  43. user := new(protocol.User)
  44. if err := json.Unmarshal(rawUser, user); err != nil {
  45. return errors.New("Socks|Client: Failed to parse user: " + err.Error())
  46. }
  47. account := new(Account)
  48. if err := json.Unmarshal(rawUser, account); err != nil {
  49. return errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
  50. }
  51. user.Account = loader.NewTypedSettings(account)
  52. server.User = append(server.User, user)
  53. }
  54. this.Server[idx] = server
  55. }
  56. return nil
  57. }