vmess.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package conf
  2. import (
  3. "encoding/json"
  4. "v2ray.com/core/common/errors"
  5. "v2ray.com/core/common/loader"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. "v2ray.com/core/proxy/vmess"
  10. "v2ray.com/core/proxy/vmess/inbound"
  11. "v2ray.com/core/proxy/vmess/outbound"
  12. )
  13. type VMessAccount struct {
  14. ID string `json:"id"`
  15. AlterIds uint16 `json:"alterId"`
  16. }
  17. func (v *VMessAccount) Build() *vmess.Account {
  18. return &vmess.Account{
  19. Id: v.ID,
  20. AlterId: uint32(v.AlterIds),
  21. }
  22. }
  23. type VMessDetourConfig struct {
  24. ToTag string `json:"to"`
  25. }
  26. func (v *VMessDetourConfig) Build() *inbound.DetourConfig {
  27. return &inbound.DetourConfig{
  28. To: v.ToTag,
  29. }
  30. }
  31. type FeaturesConfig struct {
  32. Detour *VMessDetourConfig `json:"detour"`
  33. }
  34. type VMessDefaultConfig struct {
  35. AlterIDs uint16 `json:"alterId"`
  36. Level byte `json:"level"`
  37. }
  38. func (v *VMessDefaultConfig) Build() *inbound.DefaultConfig {
  39. config := new(inbound.DefaultConfig)
  40. config.AlterId = uint32(v.AlterIDs)
  41. if config.AlterId == 0 {
  42. config.AlterId = 32
  43. }
  44. config.Level = uint32(v.Level)
  45. return config
  46. }
  47. type VMessInboundConfig struct {
  48. Users []json.RawMessage `json:"clients"`
  49. Features *FeaturesConfig `json:"features"`
  50. Defaults *VMessDefaultConfig `json:"default"`
  51. DetourConfig *VMessDetourConfig `json:"detour"`
  52. }
  53. func (v *VMessInboundConfig) Build() (*loader.TypedSettings, error) {
  54. config := new(inbound.Config)
  55. if v.Defaults != nil {
  56. config.Default = v.Defaults.Build()
  57. }
  58. if v.DetourConfig != nil {
  59. config.Detour = v.DetourConfig.Build()
  60. } else if v.Features != nil && v.Features.Detour != nil {
  61. config.Detour = v.Features.Detour.Build()
  62. }
  63. config.User = make([]*protocol.User, len(v.Users))
  64. for idx, rawData := range v.Users {
  65. user := new(protocol.User)
  66. if err := json.Unmarshal(rawData, user); err != nil {
  67. return nil, errors.New("VMess|Inbound: Invalid user: " + err.Error())
  68. }
  69. account := new(VMessAccount)
  70. if err := json.Unmarshal(rawData, account); err != nil {
  71. return nil, errors.New("VMess|Inbound: Invalid user: " + err.Error())
  72. }
  73. user.Account = loader.NewTypedSettings(account.Build())
  74. config.User[idx] = user
  75. }
  76. return loader.NewTypedSettings(config), nil
  77. }
  78. type VMessOutboundTarget struct {
  79. Address *Address `json:"address"`
  80. Port uint16 `json:"port"`
  81. Users []json.RawMessage `json:"users"`
  82. }
  83. type VMessOutboundConfig struct {
  84. Receivers []*VMessOutboundTarget `json:"vnext"`
  85. }
  86. func (v *VMessOutboundConfig) Build() (*loader.TypedSettings, error) {
  87. config := new(outbound.Config)
  88. if len(v.Receivers) == 0 {
  89. return nil, errors.New("0 VMess receiver configured.")
  90. }
  91. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Receivers))
  92. for idx, rec := range v.Receivers {
  93. if len(rec.Users) == 0 {
  94. return nil, errors.New("0 user configured for VMess outbound.")
  95. }
  96. if rec.Address == nil {
  97. return nil, errors.New("Address is not set in VMess outbound config.")
  98. }
  99. if rec.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
  100. rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
  101. }
  102. spec := &protocol.ServerEndpoint{
  103. Address: rec.Address.Build(),
  104. Port: uint32(rec.Port),
  105. }
  106. for _, rawUser := range rec.Users {
  107. user := new(protocol.User)
  108. if err := json.Unmarshal(rawUser, user); err != nil {
  109. return nil, errors.New("VMess|Outbound: Invalid user: " + err.Error())
  110. }
  111. account := new(VMessAccount)
  112. if err := json.Unmarshal(rawUser, account); err != nil {
  113. return nil, errors.New("VMess|Outbound: Invalid user: " + err.Error())
  114. }
  115. user.Account = loader.NewTypedSettings(account.Build())
  116. spec.User = append(spec.User, user)
  117. }
  118. serverSpecs[idx] = spec
  119. }
  120. config.Receiver = serverSpecs
  121. return loader.NewTypedSettings(config), nil
  122. }