validator.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package shadowsocks
  2. import (
  3. "crypto/cipher"
  4. "strings"
  5. "sync"
  6. "github.com/xtls/xray-core/common/protocol"
  7. )
  8. // Validator stores valid Shadowsocks users.
  9. type Validator struct {
  10. // Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
  11. email sync.Map
  12. users sync.Map
  13. }
  14. // Add a Shadowsocks user, Email must be empty or unique.
  15. func (v *Validator) Add(u *protocol.MemoryUser) error {
  16. account := u.Account.(*MemoryAccount)
  17. if !account.Cipher.IsAEAD() && v.Count() > 0 {
  18. return newError("The cipher do not support Single-port Multi-user")
  19. }
  20. if u.Email != "" {
  21. _, loaded := v.email.LoadOrStore(strings.ToLower(u.Email), u)
  22. if loaded {
  23. return newError("User ", u.Email, " already exists.")
  24. }
  25. }
  26. v.users.Store(string(account.Key)+"&"+account.GetCipherName(), u)
  27. return nil
  28. }
  29. // Del a Shadowsocks user with a non-empty Email.
  30. func (v *Validator) Del(e string) error {
  31. if e == "" {
  32. return newError("Email must not be empty.")
  33. }
  34. le := strings.ToLower(e)
  35. u, _ := v.email.Load(le)
  36. if u == nil {
  37. return newError("User ", e, " not found.")
  38. }
  39. account := u.(*protocol.MemoryUser).Account.(*MemoryAccount)
  40. v.email.Delete(le)
  41. v.users.Delete(string(account.Key) + "&" + account.GetCipherName())
  42. return nil
  43. }
  44. // Count the number of Shadowsocks users
  45. func (v *Validator) Count() int {
  46. length := 0
  47. v.users.Range(func(_, _ interface{}) bool {
  48. length++
  49. return true
  50. })
  51. return length
  52. }
  53. // Get a Shadowsocks user and the user's cipher.
  54. func (v *Validator) Get(bs []byte, command protocol.RequestCommand) (u *protocol.MemoryUser, aead cipher.AEAD, ret []byte, ivLen int32, err error) {
  55. var dataSize int
  56. switch command {
  57. case protocol.RequestCommandTCP:
  58. dataSize = 16
  59. case protocol.RequestCommandUDP:
  60. dataSize = 8192
  61. }
  62. var aeadCipher *AEADCipher
  63. subkey := make([]byte, 32)
  64. data := make([]byte, dataSize)
  65. v.users.Range(func(key, user interface{}) bool {
  66. account := user.(*protocol.MemoryUser).Account.(*MemoryAccount)
  67. aeadCipher = account.Cipher.(*AEADCipher)
  68. ivLen = aeadCipher.IVSize()
  69. subkey = subkey[:aeadCipher.KeyBytes]
  70. hkdfSHA1(account.Key, bs[:ivLen], subkey)
  71. aead = aeadCipher.AEADAuthCreator(subkey)
  72. switch command {
  73. case protocol.RequestCommandTCP:
  74. ret, err = aead.Open(data[:0], data[4:16], bs[ivLen:ivLen+18], nil)
  75. case protocol.RequestCommandUDP:
  76. ret, err = aead.Open(data[:0], data[8180:8192], bs[ivLen:], nil)
  77. }
  78. if err == nil {
  79. u = user.(*protocol.MemoryUser)
  80. return false
  81. }
  82. return true
  83. })
  84. return
  85. }
  86. // Get the only user without authentication
  87. func (v *Validator) GetOnlyUser() (u *protocol.MemoryUser, ivLen int32) {
  88. v.users.Range(func(_, user interface{}) bool {
  89. u = user.(*protocol.MemoryUser)
  90. return false
  91. })
  92. ivLen = u.Account.(*MemoryAccount).Cipher.IVSize()
  93. return
  94. }