vless.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package conf
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/serial"
  8. "v2ray.com/core/proxy/vless"
  9. "v2ray.com/core/proxy/vless/inbound"
  10. "v2ray.com/core/proxy/vless/outbound"
  11. )
  12. type VLessInboundFallback struct {
  13. Addr *Address `json:"addr"`
  14. Port uint16 `json:"port"`
  15. Unix string `json:"unix"`
  16. }
  17. type VLessInboundConfig struct {
  18. Users []json.RawMessage `json:"clients"`
  19. Decryption string `json:"decryption"`
  20. Fallback *VLessInboundFallback `json:"fallback"`
  21. }
  22. // Build implements Buildable
  23. func (c *VLessInboundConfig) Build() (proto.Message, error) {
  24. config := new(inbound.Config)
  25. if c.Decryption != "none" {
  26. return nil, newError(`please add/set "decryption":"none" directly to every VLESS "settings"`)
  27. }
  28. config.Decryption = c.Decryption
  29. if c.Fallback != nil {
  30. if c.Fallback.Unix != "" {
  31. if c.Fallback.Unix[0] == '@' {
  32. c.Fallback.Unix = "\x00" + c.Fallback.Unix[1:]
  33. }
  34. } else {
  35. if c.Fallback.Port == 0 {
  36. return nil, newError(`please fill in a valid value for "port" in VLESS "fallback"`)
  37. }
  38. }
  39. if c.Fallback.Addr == nil {
  40. c.Fallback.Addr = &Address{
  41. Address: net.ParseAddress("127.0.0.1"),
  42. }
  43. }
  44. config.Fallback = &inbound.Fallback{
  45. Addr: c.Fallback.Addr.Build(),
  46. Port: uint32(c.Fallback.Port),
  47. Unix: c.Fallback.Unix,
  48. }
  49. }
  50. config.User = make([]*protocol.User, len(c.Users))
  51. for idx, rawData := range c.Users {
  52. user := new(protocol.User)
  53. if err := json.Unmarshal(rawData, user); err != nil {
  54. return nil, newError("invalid VLESS user").Base(err)
  55. }
  56. account := new(vless.Account)
  57. if err := json.Unmarshal(rawData, account); err != nil {
  58. return nil, newError("invalid VLESS user").Base(err)
  59. }
  60. if account.Schedulers != "" {
  61. return nil, newError(`VLESS attr "schedulers" is not available in this version`)
  62. }
  63. if account.Encryption != "" {
  64. return nil, newError(`VLESS attr "encryption" should not in inbound settings`)
  65. }
  66. user.Account = serial.ToTypedMessage(account)
  67. config.User[idx] = user
  68. }
  69. return config, nil
  70. }
  71. type VLessOutboundTarget struct {
  72. Address *Address `json:"address"`
  73. Port uint16 `json:"port"`
  74. Users []json.RawMessage `json:"users"`
  75. }
  76. type VLessOutboundConfig struct {
  77. Receivers []*VLessOutboundTarget `json:"vnext"`
  78. }
  79. // Build implements Buildable
  80. func (c *VLessOutboundConfig) Build() (proto.Message, error) {
  81. config := new(outbound.Config)
  82. if len(c.Receivers) == 0 {
  83. return nil, newError("0 VLESS receiver configured")
  84. }
  85. serverSpecs := make([]*protocol.ServerEndpoint, len(c.Receivers))
  86. for idx, rec := range c.Receivers {
  87. if len(rec.Users) == 0 {
  88. return nil, newError("0 user configured for VLESS outbound")
  89. }
  90. if rec.Address == nil {
  91. return nil, newError("address is not set in VLESS outbound config")
  92. }
  93. spec := &protocol.ServerEndpoint{
  94. Address: rec.Address.Build(),
  95. Port: uint32(rec.Port),
  96. }
  97. for _, rawUser := range rec.Users {
  98. user := new(protocol.User)
  99. if err := json.Unmarshal(rawUser, user); err != nil {
  100. return nil, newError("invalid VLESS user").Base(err)
  101. }
  102. account := new(vless.Account)
  103. if err := json.Unmarshal(rawUser, account); err != nil {
  104. return nil, newError("invalid VLESS user").Base(err)
  105. }
  106. if account.Schedulers != "" {
  107. return nil, newError(`VLESS attr "schedulers" is not available in this version`)
  108. }
  109. if account.Encryption != "none" {
  110. return nil, newError(`please add/set "encryption":"none" for every VLESS user in "users"`)
  111. }
  112. user.Account = serial.ToTypedMessage(account)
  113. spec.User = append(spec.User, user)
  114. }
  115. serverSpecs[idx] = spec
  116. }
  117. config.Receiver = serverSpecs
  118. return config, nil
  119. }