vless.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package conf
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/serial"
  8. "v2ray.com/core/proxy/vless"
  9. "v2ray.com/core/proxy/vless/inbound"
  10. "v2ray.com/core/proxy/vless/outbound"
  11. )
  12. type VLessInboundFallback struct {
  13. Addr *Address `json:"addr"`
  14. Port uint16 `json:"port"`
  15. Unix string `json:"unix"`
  16. Xver uint16 `json:"xver"`
  17. }
  18. type VLessInboundConfig struct {
  19. Users []json.RawMessage `json:"clients"`
  20. Decryption string `json:"decryption"`
  21. Fallback *VLessInboundFallback `json:"fallback"`
  22. Fallback_h2 *VLessInboundFallback `json:"fallback_h2"`
  23. }
  24. // Build implements Buildable
  25. func (c *VLessInboundConfig) Build() (proto.Message, error) {
  26. config := new(inbound.Config)
  27. if c.Decryption != "none" {
  28. return nil, newError(`please add/set "decryption":"none" directly to every VLESS "settings"`)
  29. }
  30. config.Decryption = c.Decryption
  31. if c.Fallback != nil {
  32. if c.Fallback.Xver > 2 {
  33. return nil, newError(`VLESS "fallback": invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  34. }
  35. if c.Fallback.Unix != "" {
  36. if c.Fallback.Unix[0] == '@' {
  37. c.Fallback.Unix = "\x00" + c.Fallback.Unix[1:]
  38. }
  39. } else {
  40. if c.Fallback.Port == 0 {
  41. return nil, newError(`please fill in a valid value for "port" in VLESS "fallback"`)
  42. }
  43. }
  44. if c.Fallback.Addr == nil {
  45. c.Fallback.Addr = &Address{
  46. Address: net.ParseAddress("127.0.0.1"),
  47. }
  48. }
  49. config.Fallback = &inbound.Fallback{
  50. Addr: c.Fallback.Addr.Build(),
  51. Port: uint32(c.Fallback.Port),
  52. Unix: c.Fallback.Unix,
  53. Xver: uint32(c.Fallback.Xver),
  54. }
  55. }
  56. if c.Fallback_h2 != nil {
  57. if config.Fallback == nil {
  58. return nil, newError(`VLESS "fallback_h2" can't exist alone without "fallback"`)
  59. }
  60. if c.Fallback_h2.Xver > 2 {
  61. return nil, newError(`VLESS "fallback_h2": invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  62. }
  63. if c.Fallback_h2.Unix != "" {
  64. if c.Fallback_h2.Unix[0] == '@' {
  65. c.Fallback_h2.Unix = "\x00" + c.Fallback_h2.Unix[1:]
  66. }
  67. } else {
  68. if c.Fallback_h2.Port == 0 {
  69. return nil, newError(`please fill in a valid value for "port" in VLESS "fallback_h2"`)
  70. }
  71. }
  72. if c.Fallback_h2.Addr == nil {
  73. c.Fallback_h2.Addr = &Address{
  74. Address: net.ParseAddress("127.0.0.1"),
  75. }
  76. }
  77. config.FallbackH2 = &inbound.FallbackH2{
  78. Addr: c.Fallback_h2.Addr.Build(),
  79. Port: uint32(c.Fallback_h2.Port),
  80. Unix: c.Fallback_h2.Unix,
  81. Xver: uint32(c.Fallback_h2.Xver),
  82. }
  83. }
  84. config.User = make([]*protocol.User, len(c.Users))
  85. for idx, rawData := range c.Users {
  86. user := new(protocol.User)
  87. if err := json.Unmarshal(rawData, user); err != nil {
  88. return nil, newError("invalid VLESS user").Base(err)
  89. }
  90. account := new(vless.Account)
  91. if err := json.Unmarshal(rawData, account); err != nil {
  92. return nil, newError("invalid VLESS user").Base(err)
  93. }
  94. if account.Schedulers != "" {
  95. return nil, newError(`VLESS attr "schedulers" is not available in this version`)
  96. }
  97. if account.Encryption != "" {
  98. return nil, newError(`VLESS attr "encryption" should not in inbound settings`)
  99. }
  100. user.Account = serial.ToTypedMessage(account)
  101. config.User[idx] = user
  102. }
  103. return config, nil
  104. }
  105. type VLessOutboundTarget struct {
  106. Address *Address `json:"address"`
  107. Port uint16 `json:"port"`
  108. Users []json.RawMessage `json:"users"`
  109. }
  110. type VLessOutboundConfig struct {
  111. Receivers []*VLessOutboundTarget `json:"vnext"`
  112. }
  113. // Build implements Buildable
  114. func (c *VLessOutboundConfig) Build() (proto.Message, error) {
  115. config := new(outbound.Config)
  116. if len(c.Receivers) == 0 {
  117. return nil, newError("0 VLESS receiver configured")
  118. }
  119. serverSpecs := make([]*protocol.ServerEndpoint, len(c.Receivers))
  120. for idx, rec := range c.Receivers {
  121. if len(rec.Users) == 0 {
  122. return nil, newError("0 user configured for VLESS outbound")
  123. }
  124. if rec.Address == nil {
  125. return nil, newError("address is not set in VLESS outbound config")
  126. }
  127. spec := &protocol.ServerEndpoint{
  128. Address: rec.Address.Build(),
  129. Port: uint32(rec.Port),
  130. }
  131. for _, rawUser := range rec.Users {
  132. user := new(protocol.User)
  133. if err := json.Unmarshal(rawUser, user); err != nil {
  134. return nil, newError("invalid VLESS user").Base(err)
  135. }
  136. account := new(vless.Account)
  137. if err := json.Unmarshal(rawUser, account); err != nil {
  138. return nil, newError("invalid VLESS user").Base(err)
  139. }
  140. if account.Schedulers != "" {
  141. return nil, newError(`VLESS attr "schedulers" is not available in this version`)
  142. }
  143. if account.Encryption != "none" {
  144. return nil, newError(`please add/set "encryption":"none" for every VLESS user in "users"`)
  145. }
  146. user.Account = serial.ToTypedMessage(account)
  147. spec.User = append(spec.User, user)
  148. }
  149. serverSpecs[idx] = spec
  150. }
  151. config.Receiver = serverSpecs
  152. return config, nil
  153. }