vless.go 5.7 KB

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