config_json.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "github.com/golang/protobuf/ptypes"
  14. )
  15. func (this *Config) UnmarshalJSON(data []byte) error {
  16. type RawConfigTarget struct {
  17. Address *v2net.AddressPB `json:"address"`
  18. Port v2net.Port `json:"port"`
  19. Users []json.RawMessage `json:"users"`
  20. }
  21. type RawOutbound struct {
  22. Receivers []*RawConfigTarget `json:"vnext"`
  23. }
  24. rawOutbound := &RawOutbound{}
  25. err := json.Unmarshal(data, rawOutbound)
  26. if err != nil {
  27. return errors.New("VMessOut: Failed to parse config: " + err.Error())
  28. }
  29. if len(rawOutbound.Receivers) == 0 {
  30. log.Error("VMessOut: 0 VMess receiver configured.")
  31. return common.ErrBadConfiguration
  32. }
  33. serverSpecs := make([]*protocol.ServerSpecPB, len(rawOutbound.Receivers))
  34. for idx, rec := range rawOutbound.Receivers {
  35. if len(rec.Users) == 0 {
  36. log.Error("VMess: 0 user configured for VMess outbound.")
  37. return common.ErrBadConfiguration
  38. }
  39. if rec.Address == nil {
  40. log.Error("VMess: Address is not set in VMess outbound config.")
  41. return common.ErrBadConfiguration
  42. }
  43. if rec.Address.AsAddress().String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
  44. rec.Address.Address = &v2net.AddressPB_Ip{
  45. Ip: serial.Uint32ToBytes(757086633, nil),
  46. }
  47. }
  48. spec := &protocol.ServerSpecPB{
  49. Address: rec.Address,
  50. Port: uint32(rec.Port),
  51. }
  52. for _, rawUser := range rec.Users {
  53. user := new(protocol.User)
  54. if err := json.Unmarshal(rawUser, user); err != nil {
  55. log.Error("VMess|Outbound: Invalid user: ", err)
  56. return err
  57. }
  58. account := new(vmess.AccountPB)
  59. if err := json.Unmarshal(rawUser, account); err != nil {
  60. log.Error("VMess|Outbound: Invalid user: ", err)
  61. return err
  62. }
  63. anyAccount, err := ptypes.MarshalAny(account)
  64. if err != nil {
  65. log.Error("VMess|Outbound: Failed to create account: ", err)
  66. return common.ErrBadConfiguration
  67. }
  68. user.Account = anyAccount
  69. spec.User = append(spec.User, user)
  70. }
  71. serverSpecs[idx] = spec
  72. }
  73. this.Receiver = serverSpecs
  74. return nil
  75. }
  76. func init() {
  77. registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
  78. }