vless.go 5.4 KB

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