2020-11-25 11:01:53 +00:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2024-11-03 04:25:23 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-04-28 22:29:42 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MemoryAccount is an in-memory form of VMess account.
|
|
|
|
type MemoryAccount struct {
|
|
|
|
// ID is the main ID of the account.
|
|
|
|
ID *protocol.ID
|
|
|
|
// Security type of the account. Used for client connections.
|
|
|
|
Security protocol.SecurityType
|
2021-04-28 22:29:42 +00:00
|
|
|
|
|
|
|
AuthenticatedLengthExperiment bool
|
2021-04-29 13:28:52 +00:00
|
|
|
NoTerminationSignal bool
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equals implements protocol.Account.
|
|
|
|
func (a *MemoryAccount) Equals(account protocol.Account) bool {
|
|
|
|
vmessAccount, ok := account.(*MemoryAccount)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.ID.Equals(vmessAccount.ID)
|
|
|
|
}
|
|
|
|
|
2024-11-03 04:25:23 +00:00
|
|
|
func (a *MemoryAccount) ToProto() proto.Message {
|
|
|
|
var test = ""
|
|
|
|
if a.AuthenticatedLengthExperiment {
|
|
|
|
test = "AuthenticatedLength|"
|
|
|
|
}
|
|
|
|
if a.NoTerminationSignal {
|
|
|
|
test = test + "NoTerminationSignal"
|
|
|
|
}
|
|
|
|
return &Account{
|
2024-11-09 11:16:11 +00:00
|
|
|
Id: a.ID.String(),
|
|
|
|
TestsEnabled: test,
|
2024-11-03 04:25:23 +00:00
|
|
|
SecuritySettings: &protocol.SecurityConfig{Type: a.Security},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
// AsAccount implements protocol.Account.
|
|
|
|
func (a *Account) AsAccount() (protocol.Account, error) {
|
|
|
|
id, err := uuid.ParseString(a.Id)
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("failed to parse ID").Base(err).AtError()
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
protoID := protocol.NewID(id)
|
2021-04-29 13:28:52 +00:00
|
|
|
var AuthenticatedLength, NoTerminationSignal bool
|
2021-04-28 22:29:42 +00:00
|
|
|
if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
|
|
|
|
AuthenticatedLength = true
|
|
|
|
}
|
2021-04-29 13:28:52 +00:00
|
|
|
if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
|
|
|
|
NoTerminationSignal = true
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
return &MemoryAccount{
|
2021-04-28 22:29:42 +00:00
|
|
|
ID: protoID,
|
|
|
|
Security: a.SecuritySettings.GetSecurityType(),
|
|
|
|
AuthenticatedLengthExperiment: AuthenticatedLength,
|
2021-04-29 13:28:52 +00:00
|
|
|
NoTerminationSignal: NoTerminationSignal,
|
2020-11-25 11:01:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|