account.go 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package vmess
  2. import (
  3. "v2ray.com/core/common/dice"
  4. "v2ray.com/core/common/log"
  5. "v2ray.com/core/common/protocol"
  6. "v2ray.com/core/common/uuid"
  7. )
  8. type Account struct {
  9. ID *protocol.ID
  10. AlterIDs []*protocol.ID
  11. }
  12. func NewAccount() protocol.AsAccount {
  13. return &AccountPB{}
  14. }
  15. func (this *Account) AnyValidID() *protocol.ID {
  16. if len(this.AlterIDs) == 0 {
  17. return this.ID
  18. }
  19. return this.AlterIDs[dice.Roll(len(this.AlterIDs))]
  20. }
  21. func (this *Account) Equals(account protocol.Account) bool {
  22. vmessAccount, ok := account.(*Account)
  23. if !ok {
  24. return false
  25. }
  26. // TODO: handle AlterIds difference
  27. return this.ID.Equals(vmessAccount.ID)
  28. }
  29. func (this *AccountPB) AsAccount() (protocol.Account, error) {
  30. id, err := uuid.ParseString(this.Id)
  31. if err != nil {
  32. log.Error("VMess: Failed to parse ID: ", err)
  33. return nil, err
  34. }
  35. protoId := protocol.NewID(id)
  36. return &Account{
  37. ID: protoId,
  38. AlterIDs: protocol.NewAlterIDs(protoId, uint16(this.AlterId)),
  39. }, nil
  40. }