vmess.go 4.1 KB

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