2016-02-03 05:58:42 -05:00
|
|
|
package protocol
|
2015-09-05 11:48:38 -04:00
|
|
|
|
|
|
|
import (
|
2016-02-25 15:50:10 -05:00
|
|
|
"crypto/hmac"
|
2015-09-07 17:48:37 -04:00
|
|
|
"crypto/md5"
|
2016-02-25 15:50:10 -05:00
|
|
|
"hash"
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/uuid"
|
2015-09-05 11:48:38 -04:00
|
|
|
)
|
|
|
|
|
2015-09-14 12:19:17 -04:00
|
|
|
const (
|
|
|
|
IDBytesLen = 16
|
|
|
|
)
|
|
|
|
|
2016-02-25 15:50:10 -05:00
|
|
|
type IDHash func(key []byte) hash.Hash
|
|
|
|
|
|
|
|
func DefaultIDHash(key []byte) hash.Hash {
|
|
|
|
return hmac.New(md5.New, key)
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:52:16 -04:00
|
|
|
// The ID of en entity, in the form of a UUID.
|
2015-09-14 12:19:17 -04:00
|
|
|
type ID struct {
|
2018-01-18 17:25:48 -05:00
|
|
|
uuid uuid.UUID
|
2015-09-26 16:32:45 -04:00
|
|
|
cmdKey [IDBytesLen]byte
|
2015-09-14 12:19:17 -04:00
|
|
|
}
|
|
|
|
|
2016-12-07 07:13:48 -05:00
|
|
|
// Equals returns true if this ID equals to the other one.
|
2017-09-27 15:09:47 -04:00
|
|
|
func (id *ID) Equals(another *ID) bool {
|
2018-01-18 17:25:48 -05:00
|
|
|
return id.uuid.Equals(&(another.uuid))
|
2016-01-20 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:09:47 -04:00
|
|
|
func (id *ID) Bytes() []byte {
|
|
|
|
return id.uuid.Bytes()
|
2015-12-12 15:40:16 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:09:47 -04:00
|
|
|
func (id *ID) String() string {
|
|
|
|
return id.uuid.String()
|
2015-12-12 15:40:16 -05:00
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2018-01-18 17:25:48 -05:00
|
|
|
func (id *ID) UUID() uuid.UUID {
|
2017-09-27 15:09:47 -04:00
|
|
|
return id.uuid
|
2016-01-08 18:10:57 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:09:47 -04:00
|
|
|
func (id ID) CmdKey() []byte {
|
|
|
|
return id.cmdKey[:]
|
2016-01-12 07:39:02 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:09:47 -04:00
|
|
|
// NewID returns an ID with given UUID.
|
2018-01-18 17:25:48 -05:00
|
|
|
func NewID(uuid uuid.UUID) *ID {
|
2016-01-12 07:39:02 -05:00
|
|
|
id := &ID{uuid: uuid}
|
2015-09-15 18:06:22 -04:00
|
|
|
md5hash := md5.New()
|
2017-09-27 15:09:47 -04:00
|
|
|
common.Must2(md5hash.Write(uuid.Bytes()))
|
|
|
|
common.Must2(md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")))
|
2016-01-20 11:45:50 -05:00
|
|
|
md5hash.Sum(id.cmdKey[:0])
|
2016-01-12 07:39:02 -05:00
|
|
|
return id
|
2015-09-07 17:48:19 -04:00
|
|
|
}
|
2016-05-07 15:07:46 -04:00
|
|
|
|
2018-07-12 17:38:10 -04:00
|
|
|
func nextID(u *uuid.UUID) uuid.UUID {
|
2018-04-14 15:14:35 -04:00
|
|
|
md5hash := md5.New()
|
|
|
|
common.Must2(md5hash.Write(u.Bytes()))
|
|
|
|
common.Must2(md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81")))
|
|
|
|
var newid uuid.UUID
|
|
|
|
for {
|
|
|
|
md5hash.Sum(newid[:0])
|
|
|
|
if !newid.Equals(u) {
|
|
|
|
return newid
|
|
|
|
}
|
|
|
|
common.Must2(md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 15:07:46 -04:00
|
|
|
func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID {
|
|
|
|
alterIDs := make([]*ID, alterIDCount)
|
|
|
|
prevID := primary.UUID()
|
|
|
|
for idx := range alterIDs {
|
2018-07-12 17:38:10 -04:00
|
|
|
newid := nextID(&prevID)
|
2016-05-07 15:07:46 -04:00
|
|
|
alterIDs[idx] = NewID(newid)
|
|
|
|
prevID = newid
|
|
|
|
}
|
|
|
|
return alterIDs
|
|
|
|
}
|