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