vless.go 4.9 KB

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