You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/proxy/vmess/account.go

55 lines
1.4 KiB

6 years ago
// +build !confonly
package vmess
import (
"v2ray.com/core/common/dice"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
)
4 years ago
// MemoryAccount is an in-memory form of VMess account.
6 years ago
type MemoryAccount struct {
// ID is the main ID of the account.
ID *protocol.ID
// AlterIDs are the alternative IDs of the account.
AlterIDs []*protocol.ID
6 years ago
// Security type of the account. Used for client connections.
Security protocol.SecurityType
TestsEnabled string
}
6 years ago
// AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
func (a *MemoryAccount) AnyValidID() *protocol.ID {
7 years ago
if len(a.AlterIDs) == 0 {
return a.ID
}
7 years ago
return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
}
6 years ago
// Equals implements protocol.Account.
func (a *MemoryAccount) Equals(account protocol.Account) bool {
vmessAccount, ok := account.(*MemoryAccount)
if !ok {
return false
}
// TODO: handle AlterIds difference
7 years ago
return a.ID.Equals(vmessAccount.ID)
}
6 years ago
// AsAccount implements protocol.Account.
7 years ago
func (a *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(a.Id)
if err != nil {
7 years ago
return nil, newError("failed to parse ID").Base(err).AtError()
}
protoID := protocol.NewID(id)
6 years ago
return &MemoryAccount{
ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
Security: a.SecuritySettings.GetSecurityType(),
TestsEnabled: a.TestsEnabled,
}, nil
}