vless.go 5.7 KB

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