client_config_json.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // +build json
  2. package socks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/common/protocol"
  8. "github.com/v2ray/v2ray-core/proxy/registry"
  9. )
  10. func (this *ClientConfig) UnmarshalJSON(data []byte) error {
  11. type ServerConfig struct {
  12. Address *v2net.AddressJson `json:"address"`
  13. Port v2net.Port `json:"port"`
  14. Users []json.RawMessage `json:"users"`
  15. }
  16. type JsonConfig struct {
  17. Servers []*ServerConfig `json:"servers"`
  18. }
  19. jsonConfig := new(JsonConfig)
  20. if err := json.Unmarshal(data, jsonConfig); err != nil {
  21. return errors.New("Socks|Client: Failed to parse config: " + err.Error())
  22. }
  23. this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers))
  24. for idx, serverConfig := range jsonConfig.Servers {
  25. server := protocol.NewServerSpec(v2net.TCPDestination(serverConfig.Address.Address, serverConfig.Port), protocol.AlwaysValid())
  26. for _, rawUser := range serverConfig.Users {
  27. user := new(protocol.User)
  28. if err := json.Unmarshal(rawUser, user); err != nil {
  29. return errors.New("Socks|Client: Failed to parse user: " + err.Error())
  30. }
  31. account := new(Account)
  32. if err := json.Unmarshal(rawUser, account); err != nil {
  33. return errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
  34. }
  35. user.Account = account
  36. server.AddUser(user)
  37. }
  38. this.Servers[idx] = server
  39. }
  40. return nil
  41. }
  42. func init() {
  43. registry.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) })
  44. }