1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/proxy/vmess/account.go

66 lines
1.9 KiB
Go
Raw Normal View History

2019-02-02 16:19:40 -05:00
// +build !confonly
2016-09-17 18:41:21 -04:00
package vmess
import (
2021-04-28 18:29:42 -04:00
"strings"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/dice"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/uuid"
2016-09-17 18:41:21 -04:00
)
2020-06-26 09:27:23 -04:00
// MemoryAccount is an in-memory form of VMess account.
2018-10-18 03:25:58 -04:00
type MemoryAccount struct {
// ID is the main ID of the account.
ID *protocol.ID
// AlterIDs are the alternative IDs of the account.
2016-09-17 18:41:21 -04:00
AlterIDs []*protocol.ID
2018-10-18 03:25:58 -04:00
// Security type of the account. Used for client connections.
Security protocol.SecurityType
2021-04-28 18:29:42 -04:00
AuthenticatedLengthExperiment bool
NoTerminationSignal bool
2016-09-17 18:41:21 -04:00
}
2018-10-18 03:25:58 -04: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 {
2017-09-19 17:27:49 -04:00
if len(a.AlterIDs) == 0 {
return a.ID
2016-09-17 18:41:21 -04:00
}
2017-09-19 17:27:49 -04:00
return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
2016-09-17 18:41:21 -04:00
}
2018-10-18 03:25:58 -04:00
// Equals implements protocol.Account.
func (a *MemoryAccount) Equals(account protocol.Account) bool {
vmessAccount, ok := account.(*MemoryAccount)
2016-09-17 18:41:21 -04:00
if !ok {
return false
}
// TODO: handle AlterIds difference
2017-09-19 17:27:49 -04:00
return a.ID.Equals(vmessAccount.ID)
2016-09-17 18:41:21 -04:00
}
2018-10-18 03:25:58 -04:00
// AsAccount implements protocol.Account.
2017-09-19 17:27:49 -04:00
func (a *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(a.Id)
2016-09-17 18:41:21 -04:00
if err != nil {
2018-01-18 05:35:04 -05:00
return nil, newError("failed to parse ID").Base(err).AtError()
2016-09-17 18:41:21 -04:00
}
2016-12-27 15:34:14 -05:00
protoID := protocol.NewID(id)
var AuthenticatedLength, NoTerminationSignal bool
2021-04-28 18:29:42 -04:00
if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
AuthenticatedLength = true
}
if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
NoTerminationSignal = true
}
2018-10-18 03:25:58 -04:00
return &MemoryAccount{
2021-04-28 18:29:42 -04:00
ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
Security: a.SecuritySettings.GetSecurityType(),
AuthenticatedLengthExperiment: AuthenticatedLength,
NoTerminationSignal: NoTerminationSignal,
2016-09-17 18:41:21 -04:00
}, nil
}