github.com/yaling888/clash@v1.53.0/transport/vmess/user.go (about)

     1  package vmess
     2  
     3  import (
     4  	"crypto/md5"
     5  
     6  	"github.com/yaling888/clash/common/uuid"
     7  )
     8  
     9  // ID cmdKey length
    10  const (
    11  	IDBytesLen = 16
    12  )
    13  
    14  // The ID of en entity, in the form of a UUID.
    15  type ID struct {
    16  	UUID   uuid.UUID
    17  	CmdKey []byte
    18  }
    19  
    20  // newID returns an ID with given UUID.
    21  func newID(uuid uuid.UUID) *ID {
    22  	id := &ID{UUID: uuid, CmdKey: make([]byte, IDBytesLen)}
    23  	md5hash := md5.New()
    24  	md5hash.Write(uuid.Bytes())
    25  	md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
    26  	md5hash.Sum(id.CmdKey[:0])
    27  	return id
    28  }
    29  
    30  func nextID(u uuid.UUID) uuid.UUID {
    31  	md5hash := md5.New()
    32  	md5hash.Write(u.Bytes())
    33  	md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
    34  	var buf [IDBytesLen]byte
    35  	for {
    36  		md5hash.Sum(buf[:0])
    37  		if newId := uuid.FromStd(buf[:]); newId.IsValid() && newId != u {
    38  			return newId
    39  		}
    40  		md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
    41  	}
    42  }
    43  
    44  func newAlterIDs(primary *ID, alterIDCount uint16) []*ID {
    45  	alterIDs := make([]*ID, alterIDCount)
    46  	prevID := primary.UUID
    47  	for idx := range alterIDs {
    48  		newId := nextID(prevID)
    49  		alterIDs[idx] = &ID{UUID: newId, CmdKey: primary.CmdKey[:]}
    50  		prevID = newId
    51  	}
    52  	alterIDs = append(alterIDs, primary)
    53  	return alterIDs
    54  }