config_json.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // +build json
  2. package outbound
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/loader"
  8. "v2ray.com/core/common/log"
  9. v2net "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/protocol"
  11. "v2ray.com/core/common/serial"
  12. "v2ray.com/core/proxy/vmess"
  13. )
  14. func (this *Config) UnmarshalJSON(data []byte) error {
  15. type RawConfigTarget struct {
  16. Address *v2net.IPOrDomain `json:"address"`
  17. Port v2net.Port `json:"port"`
  18. Users []json.RawMessage `json:"users"`
  19. }
  20. type RawOutbound struct {
  21. Receivers []*RawConfigTarget `json:"vnext"`
  22. }
  23. rawOutbound := &RawOutbound{}
  24. err := json.Unmarshal(data, rawOutbound)
  25. if err != nil {
  26. return errors.New("VMessOut: Failed to parse config: " + err.Error())
  27. }
  28. if len(rawOutbound.Receivers) == 0 {
  29. log.Error("VMessOut: 0 VMess receiver configured.")
  30. return common.ErrBadConfiguration
  31. }
  32. serverSpecs := make([]*protocol.ServerEndpoint, len(rawOutbound.Receivers))
  33. for idx, rec := range rawOutbound.Receivers {
  34. if len(rec.Users) == 0 {
  35. log.Error("VMess: 0 user configured for VMess outbound.")
  36. return common.ErrBadConfiguration
  37. }
  38. if rec.Address == nil {
  39. log.Error("VMess: Address is not set in VMess outbound config.")
  40. return common.ErrBadConfiguration
  41. }
  42. if rec.Address.AsAddress().String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
  43. rec.Address.Address = &v2net.IPOrDomain_Ip{
  44. Ip: serial.Uint32ToBytes(757086633, nil),
  45. }
  46. }
  47. spec := &protocol.ServerEndpoint{
  48. Address: rec.Address,
  49. Port: uint32(rec.Port),
  50. }
  51. for _, rawUser := range rec.Users {
  52. user := new(protocol.User)
  53. if err := json.Unmarshal(rawUser, user); err != nil {
  54. log.Error("VMess|Outbound: Invalid user: ", err)
  55. return err
  56. }
  57. account := new(vmess.Account)
  58. if err := json.Unmarshal(rawUser, account); err != nil {
  59. log.Error("VMess|Outbound: Invalid user: ", err)
  60. return err
  61. }
  62. user.Account = loader.NewTypedSettings(account)
  63. spec.User = append(spec.User, user)
  64. }
  65. serverSpecs[idx] = spec
  66. }
  67. this.Receiver = serverSpecs
  68. return nil
  69. }