vless.go 5.0 KB

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