vless.go 5.0 KB

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