account.go 1.0 KB

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