github.com/EagleQL/Xray-core@v1.4.3/proxy/vmess/account.go (about)

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