vmess: add CmdKey in user struct

This commit is contained in:
nadoo committed 2018-07-03 01:07:28 +08:00
1 parent 385f5d7790
commit 14cded4665
3 files changed
+33 -26

No files matched your search

+30 -4
View File
@@ -1,14 +1,18 @@
package vmess
import (
"crypto/md5"
"encoding/binary"
"encoding/hex"
"errors"
"strings"
"time"
)
// User of vmess client
type User struct {
UUID [16]byte
UUID [16]byte
CmdKey [16]byte
}
// NewUser .
@@ -18,9 +22,8 @@ func NewUser(uuidStr string) (*User, error) {
return nil, err
}
u := &User{
UUID: uuid,
}
u := &User{UUID: uuid}
copy(u.CmdKey[:], GetKey(uuid))
return u, nil
}
@@ -35,3 +38,26 @@ func StrToUUID(s string) (uuid [16]byte, err error) {
_, err = hex.Decode(uuid[:], b)
return
}
// GetKey returns the key of AES-128-CFB encrypter
// Key:MD5(UUID + []byte('c48619fe-8f02-49e0-b9e9-edf763e17e21'))
func GetKey(uuid [16]byte) []byte {
md5hash := md5.New()
md5hash.Write(uuid[:])
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
return md5hash.Sum(nil)
}
// TimestampHash returns the iv of AES-128-CFB encrypter
// IV:MD5(X + X + X + X),X = []byte(timestamp.now) (8 bytes, Big Endian)
func TimestampHash(t time.Time) []byte {
md5hash := md5.New()
ts := make([]byte, 8)
binary.BigEndian.PutUint64(ts, uint64(t.UTC().Unix()))
md5hash.Write(ts)
md5hash.Write(ts)
md5hash.Write(ts)
md5hash.Write(ts)
return md5hash.Sum(nil)
}