vless.go 5.3 KB

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