config_json.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build json
  2. package outbound
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/common/serial"
  11. "v2ray.com/core/proxy/registry"
  12. "v2ray.com/core/proxy/vmess"
  13. )
  14. func (this *Config) UnmarshalJSON(data []byte) error {
  15. type RawConfigTarget struct {
  16. Address *v2net.AddressPB `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.ServerSpec, 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.AddressPB_Ip{
  44. Ip: serial.Uint32ToBytes(757086633, nil),
  45. }
  46. }
  47. spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.AsAddress(), rec.Port), protocol.AlwaysValid())
  48. for _, rawUser := range rec.Users {
  49. user := new(protocol.User)
  50. if err := json.Unmarshal(rawUser, user); err != nil {
  51. log.Error("VMess|Outbound: Invalid user: ", err)
  52. return err
  53. }
  54. account := new(vmess.Account)
  55. if err := json.Unmarshal(rawUser, account); err != nil {
  56. log.Error("VMess|Outbound: Invalid user: ", err)
  57. return err
  58. }
  59. user.Account = account
  60. spec.AddUser(user)
  61. }
  62. serverSpecs[idx] = spec
  63. }
  64. this.Receivers = serverSpecs
  65. return nil
  66. }
  67. func init() {
  68. registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
  69. }