account.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package vless
  2. import (
  3. "google.golang.org/protobuf/proto"
  4. "github.com/xtls/xray-core/common/errors"
  5. "github.com/xtls/xray-core/common/protocol"
  6. "github.com/xtls/xray-core/common/uuid"
  7. )
  8. // AsAccount implements protocol.Account.AsAccount().
  9. func (a *Account) AsAccount() (protocol.Account, error) {
  10. id, err := uuid.ParseString(a.Id)
  11. if err != nil {
  12. return nil, errors.New("failed to parse ID").Base(err).AtError()
  13. }
  14. return &MemoryAccount{
  15. ID: protocol.NewID(id),
  16. Flow: a.Flow, // needs parser here?
  17. Encryption: a.Encryption, // needs parser here?
  18. XorMode: a.XorMode,
  19. Seconds: a.Seconds,
  20. Padding: a.Padding,
  21. }, nil
  22. }
  23. // MemoryAccount is an in-memory form of VLess account.
  24. type MemoryAccount struct {
  25. // ID of the account.
  26. ID *protocol.ID
  27. // Flow of the account. May be "xtls-rprx-vision".
  28. Flow string
  29. Encryption string
  30. XorMode uint32
  31. Seconds uint32
  32. Padding string
  33. }
  34. // Equals implements protocol.Account.Equals().
  35. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  36. vlessAccount, ok := account.(*MemoryAccount)
  37. if !ok {
  38. return false
  39. }
  40. return a.ID.Equals(vlessAccount.ID)
  41. }
  42. func (a *MemoryAccount) ToProto() proto.Message {
  43. return &Account{
  44. Id: a.ID.String(),
  45. Flow: a.Flow,
  46. Encryption: a.Encryption,
  47. XorMode: a.XorMode,
  48. Seconds: a.Seconds,
  49. Padding: a.Padding,
  50. }
  51. }