config_json.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // +build json
  2. package socks
  3. import (
  4. "encoding/json"
  5. "errors"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/proxy/registry"
  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.AddressPB `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.ServerSpecPB, len(jsonConfig.Servers))
  37. for idx, serverConfig := range jsonConfig.Servers {
  38. server := &protocol.ServerSpecPB{
  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. anyAccount, err := account.AsAny()
  52. if err != nil {
  53. return err
  54. }
  55. user.Account = anyAccount
  56. server.Users = append(server.Users, user)
  57. }
  58. this.Server[idx] = server
  59. }
  60. return nil
  61. }
  62. func init() {
  63. registry.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) })
  64. }