account.go 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 InternalAccount struct {
  9. ID *protocol.ID
  10. AlterIDs []*protocol.ID
  11. }
  12. func (v *InternalAccount) AnyValidID() *protocol.ID {
  13. if len(v.AlterIDs) == 0 {
  14. return v.ID
  15. }
  16. return v.AlterIDs[dice.Roll(len(v.AlterIDs))]
  17. }
  18. func (v *InternalAccount) Equals(account protocol.Account) bool {
  19. vmessAccount, ok := account.(*InternalAccount)
  20. if !ok {
  21. return false
  22. }
  23. // TODO: handle AlterIds difference
  24. return v.ID.Equals(vmessAccount.ID)
  25. }
  26. func (v *Account) AsAccount() (protocol.Account, error) {
  27. id, err := uuid.ParseString(v.Id)
  28. if err != nil {
  29. log.Error("VMess: Failed to parse ID: ", err)
  30. return nil, err
  31. }
  32. protoId := protocol.NewID(id)
  33. return &InternalAccount{
  34. ID: protoId,
  35. AlterIDs: protocol.NewAlterIDs(protoId, uint16(v.AlterId)),
  36. }, nil
  37. }