vless.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package conf
  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/proxy/vless"
  12. "github.com/v2fly/v2ray-core/v4/proxy/vless/inbound"
  13. "github.com/v2fly/v2ray-core/v4/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. if account.Encryption != "" {
  42. return nil, newError(`VLESS clients: "encryption" should not in inbound settings`)
  43. }
  44. user.Account = serial.ToTypedMessage(account)
  45. config.Clients[idx] = user
  46. }
  47. if c.Decryption != "none" {
  48. return nil, newError(`VLESS settings: please add/set "decryption":"none" to every settings`)
  49. }
  50. config.Decryption = c.Decryption
  51. if c.Fallback != nil {
  52. return nil, newError(`VLESS settings: please use "fallbacks":[{}] instead of "fallback":{}`)
  53. }
  54. for _, fb := range c.Fallbacks {
  55. var i uint16
  56. var s string
  57. if err := json.Unmarshal(fb.Dest, &i); err == nil {
  58. s = strconv.Itoa(int(i))
  59. } else {
  60. _ = json.Unmarshal(fb.Dest, &s)
  61. }
  62. config.Fallbacks = append(config.Fallbacks, &inbound.Fallback{
  63. Alpn: fb.Alpn,
  64. Path: fb.Path,
  65. Type: fb.Type,
  66. Dest: s,
  67. Xver: fb.Xver,
  68. })
  69. }
  70. for _, fb := range config.Fallbacks {
  71. /*
  72. if fb.Alpn == "h2" && fb.Path != "" {
  73. return nil, newError(`VLESS fallbacks: "alpn":"h2" doesn't support "path"`)
  74. }
  75. */
  76. if fb.Path != "" && fb.Path[0] != '/' {
  77. return nil, newError(`VLESS fallbacks: "path" must be empty or start with "/"`)
  78. }
  79. if fb.Type == "" && fb.Dest != "" {
  80. if fb.Dest == "serve-ws-none" {
  81. fb.Type = "serve"
  82. } else {
  83. switch fb.Dest[0] {
  84. case '@', '/':
  85. fb.Type = "unix"
  86. if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
  87. fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
  88. copy(fullAddr, fb.Dest[1:])
  89. fb.Dest = string(fullAddr)
  90. }
  91. default:
  92. if _, err := strconv.Atoi(fb.Dest); err == nil {
  93. fb.Dest = "127.0.0.1:" + fb.Dest
  94. }
  95. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  96. fb.Type = "tcp"
  97. }
  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 *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. }