vless.go 6.0 KB

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