github.com/xraypb/xray-core@v1.6.6/proxy/vmess/account.go (about)

     1  package vmess
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/xraypb/xray-core/common/dice"
     7  	"github.com/xraypb/xray-core/common/protocol"
     8  	"github.com/xraypb/xray-core/common/uuid"
     9  )
    10  
    11  // MemoryAccount is an in-memory form of VMess account.
    12  type MemoryAccount struct {
    13  	// ID is the main ID of the account.
    14  	ID *protocol.ID
    15  	// AlterIDs are the alternative IDs of the account.
    16  	AlterIDs []*protocol.ID
    17  	// Security type of the account. Used for client connections.
    18  	Security protocol.SecurityType
    19  
    20  	AuthenticatedLengthExperiment bool
    21  	NoTerminationSignal           bool
    22  }
    23  
    24  // AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
    25  func (a *MemoryAccount) AnyValidID() *protocol.ID {
    26  	if len(a.AlterIDs) == 0 {
    27  		return a.ID
    28  	}
    29  	return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
    30  }
    31  
    32  // Equals implements protocol.Account.
    33  func (a *MemoryAccount) Equals(account protocol.Account) bool {
    34  	vmessAccount, ok := account.(*MemoryAccount)
    35  	if !ok {
    36  		return false
    37  	}
    38  	// TODO: handle AlterIds difference
    39  	return a.ID.Equals(vmessAccount.ID)
    40  }
    41  
    42  // AsAccount implements protocol.Account.
    43  func (a *Account) AsAccount() (protocol.Account, error) {
    44  	id, err := uuid.ParseString(a.Id)
    45  	if err != nil {
    46  		return nil, newError("failed to parse ID").Base(err).AtError()
    47  	}
    48  	protoID := protocol.NewID(id)
    49  	var AuthenticatedLength, NoTerminationSignal bool
    50  	if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
    51  		AuthenticatedLength = true
    52  	}
    53  	if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
    54  		NoTerminationSignal = true
    55  	}
    56  	return &MemoryAccount{
    57  		ID:                            protoID,
    58  		AlterIDs:                      protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
    59  		Security:                      a.SecuritySettings.GetSecurityType(),
    60  		AuthenticatedLengthExperiment: AuthenticatedLength,
    61  		NoTerminationSignal:           NoTerminationSignal,
    62  	}, nil
    63  }