config_json.go 2.2 KB

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