vless.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package conf
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "github.com/golang/protobuf/proto"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. "v2ray.com/core/proxy/vless"
  10. "v2ray.com/core/proxy/vless/inbound"
  11. "v2ray.com/core/proxy/vless/outbound"
  12. )
  13. type VLessInboundFallback struct {
  14. Alpn string `json:"alpn"`
  15. Path string `json:"path"`
  16. Type string `json:"type"`
  17. Dest json.RawMessage `json:"dest"`
  18. Xver uint64 `json:"xver"`
  19. }
  20. type VLessInboundConfig struct {
  21. Clients []json.RawMessage `json:"clients"`
  22. Decryption string `json:"decryption"`
  23. Fallback json.RawMessage `json:"fallback"`
  24. Fallbacks []*VLessInboundFallback `json:"fallbacks"`
  25. }
  26. // Build implements Buildable
  27. func (c *VLessInboundConfig) Build() (proto.Message, error) {
  28. config := new(inbound.Config)
  29. if len(c.Clients) == 0 {
  30. return nil, newError(`VLESS settings: "clients" is empty`)
  31. }
  32. config.Clients = make([]*protocol.User, len(c.Clients))
  33. for idx, rawUser := range c.Clients {
  34. user := new(protocol.User)
  35. if err := json.Unmarshal(rawUser, user); err != nil {
  36. return nil, newError(`VLESS clients: invalid user`).Base(err)
  37. }
  38. account := new(vless.Account)
  39. if err := json.Unmarshal(rawUser, account); err != nil {
  40. return nil, newError(`VLESS clients: invalid user`).Base(err)
  41. }
  42. switch account.Flow {
  43. case "", "xtls-rprx-origin":
  44. default:
  45. return nil, newError(`VLESS clients: "flow" only accepts "", "xtls-rprx-origin" in this version`)
  46. }
  47. if account.Encryption != "" {
  48. return nil, newError(`VLESS clients: "encryption" should not in inbound settings`)
  49. }
  50. user.Account = serial.ToTypedMessage(account)
  51. config.Clients[idx] = user
  52. }
  53. if c.Decryption != "none" {
  54. return nil, newError(`VLESS settings: please add/set "decryption":"none" to every settings`)
  55. }
  56. config.Decryption = c.Decryption
  57. if c.Fallback != nil {
  58. return nil, newError(`VLESS settings: please use "fallbacks":[{}] instead of "fallback":{}`)
  59. }
  60. for _, fb := range c.Fallbacks {
  61. var i uint16
  62. var s string
  63. if err := json.Unmarshal(fb.Dest, &i); err == nil {
  64. s = strconv.Itoa(int(i))
  65. } else {
  66. _ = json.Unmarshal(fb.Dest, &s)
  67. }
  68. config.Fallbacks = append(config.Fallbacks, &inbound.Fallback{
  69. Alpn: fb.Alpn,
  70. Path: fb.Path,
  71. Type: fb.Type,
  72. Dest: s,
  73. Xver: fb.Xver,
  74. })
  75. }
  76. for _, fb := range config.Fallbacks {
  77. /*
  78. if fb.Alpn == "h2" && fb.Path != "" {
  79. return nil, newError(`VLESS fallbacks: "alpn":"h2" doesn't support "path"`)
  80. }
  81. */
  82. if fb.Path != "" && fb.Path[0] != '/' {
  83. return nil, newError(`VLESS fallbacks: "path" must be empty or start with "/"`)
  84. }
  85. if fb.Type == "" && fb.Dest != "" {
  86. if fb.Dest == "serve-ws-none" {
  87. fb.Type = "serve"
  88. } else {
  89. switch fb.Dest[0] {
  90. case '@', '/':
  91. fb.Type = "unix"
  92. default:
  93. if _, err := strconv.Atoi(fb.Dest); err == nil {
  94. fb.Dest = "127.0.0.1:" + fb.Dest
  95. }
  96. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  97. fb.Type = "tcp"
  98. }
  99. }
  100. }
  101. }
  102. if fb.Type == "" {
  103. return nil, newError(`VLESS fallbacks: please fill in a valid value for every "dest"`)
  104. }
  105. if fb.Xver > 2 {
  106. return nil, newError(`VLESS fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  107. }
  108. }
  109. return config, nil
  110. }
  111. type VLessOutboundVnext struct {
  112. Address *Address `json:"address"`
  113. Port uint16 `json:"port"`
  114. Users []json.RawMessage `json:"users"`
  115. }
  116. type VLessOutboundConfig struct {
  117. Vnext []*VLessOutboundVnext `json:"vnext"`
  118. }
  119. // Build implements Buildable
  120. func (c *VLessOutboundConfig) Build() (proto.Message, error) {
  121. config := new(outbound.Config)
  122. if len(c.Vnext) == 0 {
  123. return nil, newError(`VLESS settings: "vnext" is empty`)
  124. }
  125. config.Vnext = make([]*protocol.ServerEndpoint, len(c.Vnext))
  126. for idx, rec := range c.Vnext {
  127. if rec.Address == nil {
  128. return nil, newError(`VLESS vnext: "address" is not set`)
  129. }
  130. if len(rec.Users) == 0 {
  131. return nil, newError(`VLESS vnext: "users" is empty`)
  132. }
  133. spec := &protocol.ServerEndpoint{
  134. Address: rec.Address.Build(),
  135. Port: uint32(rec.Port),
  136. User: make([]*protocol.User, len(rec.Users)),
  137. }
  138. for idx, rawUser := range rec.Users {
  139. user := new(protocol.User)
  140. if err := json.Unmarshal(rawUser, user); err != nil {
  141. return nil, newError(`VLESS users: invalid user`).Base(err)
  142. }
  143. account := new(vless.Account)
  144. if err := json.Unmarshal(rawUser, account); err != nil {
  145. return nil, newError(`VLESS users: invalid user`).Base(err)
  146. }
  147. switch account.Flow {
  148. case "", "xtls-rprx-origin", "xtls-rprx-origin-udp443":
  149. default:
  150. return nil, newError(`VLESS users: "flow" only accepts "", "xtls-rprx-origin", "xtls-rprx-origin-udp443" in this version`)
  151. }
  152. if account.Encryption != "none" {
  153. return nil, newError(`VLESS users: please add/set "encryption":"none" for every user`)
  154. }
  155. user.Account = serial.ToTypedMessage(account)
  156. spec.User[idx] = user
  157. }
  158. config.Vnext[idx] = spec
  159. }
  160. return config, nil
  161. }