client_config_json.go 1.5 KB

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