config_json.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/internal"
  11. )
  12. func (this *Config) UnmarshalJSON(data []byte) error {
  13. type RawConfigTarget struct {
  14. Address *v2net.AddressJson `json:"address"`
  15. Port v2net.Port `json:"port"`
  16. Users []*protocol.User `json:"users"`
  17. }
  18. type RawOutbound struct {
  19. Receivers []*RawConfigTarget `json:"vnext"`
  20. }
  21. rawOutbound := &RawOutbound{}
  22. err := json.Unmarshal(data, rawOutbound)
  23. if err != nil {
  24. return errors.New("VMessOut: Failed to parse config: " + err.Error())
  25. }
  26. if len(rawOutbound.Receivers) == 0 {
  27. log.Error("VMessOut: 0 VMess receiver configured.")
  28. return internal.ErrBadConfiguration
  29. }
  30. serverSpecs := make([]*protocol.ServerSpec, len(rawOutbound.Receivers))
  31. for idx, rec := range rawOutbound.Receivers {
  32. if len(rec.Users) == 0 {
  33. log.Error("VMess: 0 user configured for VMess outbound.")
  34. return internal.ErrBadConfiguration
  35. }
  36. if rec.Address == nil {
  37. log.Error("VMess: Address is not set in VMess outbound config.")
  38. return internal.ErrBadConfiguration
  39. }
  40. if rec.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
  41. rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854, nil))
  42. }
  43. spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.Address, rec.Port), protocol.AlwaysValid())
  44. for _, user := range rec.Users {
  45. spec.AddUser(user)
  46. }
  47. serverSpecs[idx] = spec
  48. }
  49. this.Receivers = serverSpecs
  50. return nil
  51. }
  52. func init() {
  53. internal.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
  54. }