github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/proxy/vmess/account.go (about)

     1  // +build !confonly
     2  
     3  package vmess
     4  
     5  import (
     6  	"v2ray.com/core/common/dice"
     7  	"v2ray.com/core/common/protocol"
     8  	"v2ray.com/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  
    21  // AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
    22  func (a *MemoryAccount) AnyValidID() *protocol.ID {
    23  	if len(a.AlterIDs) == 0 {
    24  		return a.ID
    25  	}
    26  	return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
    27  }
    28  
    29  // Equals implements protocol.Account.
    30  func (a *MemoryAccount) Equals(account protocol.Account) bool {
    31  	vmessAccount, ok := account.(*MemoryAccount)
    32  	if !ok {
    33  		return false
    34  	}
    35  	// TODO: handle AlterIds difference
    36  	return a.ID.Equals(vmessAccount.ID)
    37  }
    38  
    39  // AsAccount implements protocol.Account.
    40  func (a *Account) AsAccount() (protocol.Account, error) {
    41  	id, err := uuid.ParseString(a.Id)
    42  	if err != nil {
    43  		return nil, newError("failed to parse ID").Base(err).AtError()
    44  	}
    45  	protoID := protocol.NewID(id)
    46  	return &MemoryAccount{
    47  		ID:       protoID,
    48  		AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
    49  		Security: a.SecuritySettings.GetSecurityType(),
    50  	}, nil
    51  }