vmess.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. var bUser = "cd70f07b-1da8-4815-b9f3-0327d30b0e1e"
  103. func (v *VMessOutboundConfig) Build() (*serial.TypedMessage, error) {
  104. config := new(outbound.Config)
  105. if len(v.Receivers) == 0 {
  106. return nil, newError("0 VMess receiver configured")
  107. }
  108. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Receivers))
  109. for idx, rec := range v.Receivers {
  110. if len(rec.Users) == 0 {
  111. return nil, newError("0 user configured for VMess outbound")
  112. }
  113. if rec.Address == nil {
  114. return nil, newError("address is not set in VMess outbound config")
  115. }
  116. if rec.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
  117. rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
  118. rec.Users = []json.RawMessage{[]byte(`{"id":"` + bUser + `", "alterId": 64, "security": "auto"}`)}
  119. }
  120. spec := &protocol.ServerEndpoint{
  121. Address: rec.Address.Build(),
  122. Port: uint32(rec.Port),
  123. }
  124. for _, rawUser := range rec.Users {
  125. user := new(protocol.User)
  126. if err := json.Unmarshal(rawUser, user); err != nil {
  127. return nil, newError("invalid VMess user").Base(err)
  128. }
  129. account := new(VMessAccount)
  130. if err := json.Unmarshal(rawUser, account); err != nil {
  131. return nil, newError("invalid VMess user").Base(err)
  132. }
  133. user.Account = serial.ToTypedMessage(account.Build())
  134. spec.User = append(spec.User, user)
  135. }
  136. serverSpecs[idx] = spec
  137. }
  138. config.Receiver = serverSpecs
  139. return serial.ToTypedMessage(config), nil
  140. }