vmess.go 4.1 KB

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