2020-11-25 13:01:53 +02:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2021-04-29 01:29:42 +03:00
|
|
|
"strings"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/dice"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MemoryAccount is an in-memory form of VMess account.
|
|
|
|
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
|
|
|
|
// Security type of the account. Used for client connections.
|
|
|
|
Security protocol.SecurityType
|
2021-04-29 01:29:42 +03:00
|
|
|
|
|
|
|
AuthenticatedLengthExperiment bool
|
2021-04-29 16:28:52 +03:00
|
|
|
NoTerminationSignal bool
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
|
|
|
|
func (a *MemoryAccount) AnyValidID() *protocol.ID {
|
|
|
|
if len(a.AlterIDs) == 0 {
|
|
|
|
return a.ID
|
|
|
|
}
|
|
|
|
return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals implements protocol.Account.
|
|
|
|
func (a *MemoryAccount) Equals(account protocol.Account) bool {
|
|
|
|
vmessAccount, ok := account.(*MemoryAccount)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// TODO: handle AlterIds difference
|
|
|
|
return a.ID.Equals(vmessAccount.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsAccount implements protocol.Account.
|
|
|
|
func (a *Account) AsAccount() (protocol.Account, error) {
|
|
|
|
id, err := uuid.ParseString(a.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse ID").Base(err).AtError()
|
|
|
|
}
|
|
|
|
protoID := protocol.NewID(id)
|
2021-04-29 16:28:52 +03:00
|
|
|
var AuthenticatedLength, NoTerminationSignal bool
|
2021-04-29 01:29:42 +03:00
|
|
|
if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
|
|
|
|
AuthenticatedLength = true
|
|
|
|
}
|
2021-04-29 16:28:52 +03:00
|
|
|
if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
|
|
|
|
NoTerminationSignal = true
|
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
return &MemoryAccount{
|
2021-04-29 01:29:42 +03:00
|
|
|
ID: protoID,
|
|
|
|
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
|
|
|
|
Security: a.SecuritySettings.GetSecurityType(),
|
|
|
|
AuthenticatedLengthExperiment: AuthenticatedLength,
|
2021-04-29 16:28:52 +03:00
|
|
|
NoTerminationSignal: NoTerminationSignal,
|
2020-11-25 13:01:53 +02:00
|
|
|
}, nil
|
|
|
|
}
|