vless.go 5.2 KB

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