config_json.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.ServerSpec, 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.NewServerSpec(vmess.NewAccount, v2net.TCPDestination(rec.Address.AsAddress(), rec.Port), protocol.AlwaysValid())
  49. for _, rawUser := range rec.Users {
  50. user := new(protocol.User)
  51. if err := json.Unmarshal(rawUser, user); err != nil {
  52. log.Error("VMess|Outbound: Invalid user: ", err)
  53. return err
  54. }
  55. account := new(vmess.AccountPB)
  56. if err := json.Unmarshal(rawUser, account); err != nil {
  57. log.Error("VMess|Outbound: Invalid user: ", err)
  58. return err
  59. }
  60. anyAccount, err := ptypes.MarshalAny(account)
  61. if err != nil {
  62. log.Error("VMess|Outbound: Failed to create account: ", err)
  63. return common.ErrBadConfiguration
  64. }
  65. user.Account = anyAccount
  66. spec.AddUser(user)
  67. }
  68. serverSpecs[idx] = spec
  69. }
  70. this.Receivers = serverSpecs
  71. return nil
  72. }
  73. func init() {
  74. registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
  75. }