vmess.go 4.2 KB

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