Xray-core/proxy/vmess/account.go

68 lines
1.8 KiB
Go
Raw Normal View History

2020-11-25 14:01:53 +03:00
package vmess
import (
"google.golang.org/protobuf/proto"
"strings"
"github.com/xtls/xray-core/common/errors"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/uuid"
2020-11-25 14:01:53 +03: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
AuthenticatedLengthExperiment bool
NoTerminationSignal bool
2020-11-25 14:01:53 +03: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)
}
func (a *MemoryAccount) ToProto() proto.Message {
var test = ""
if a.AuthenticatedLengthExperiment {
test = "AuthenticatedLength|"
}
if a.NoTerminationSignal {
test = test + "NoTerminationSignal"
}
return &Account{
Id: a.ID.String(),
TestsEnabled: test,
SecuritySettings: &protocol.SecurityConfig{Type: a.Security},
}
}
2020-11-25 14:01:53 +03:00
// AsAccount implements protocol.Account.
func (a *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(a.Id)
if err != nil {
return nil, errors.New("failed to parse ID").Base(err).AtError()
2020-11-25 14:01:53 +03:00
}
protoID := protocol.NewID(id)
var AuthenticatedLength, NoTerminationSignal bool
if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
AuthenticatedLength = true
}
if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
NoTerminationSignal = true
}
2020-11-25 14:01:53 +03:00
return &MemoryAccount{
ID: protoID,
Security: a.SecuritySettings.GetSecurityType(),
AuthenticatedLengthExperiment: AuthenticatedLength,
NoTerminationSignal: NoTerminationSignal,
2020-11-25 14:01:53 +03:00
}, nil
}