mirror of
https://github.com/nadoo/glider.git
synced 2025-02-24 01:45:39 +08:00
vmess: add CmdKey in user struct
This commit is contained in:
parent
385f5d7790
commit
14cded4665
@ -129,29 +129,12 @@ func (c *Client) EncodeRequest() ([]byte, error) {
|
|||||||
fnv1a.Write(buf.Bytes())
|
fnv1a.Write(buf.Bytes())
|
||||||
buf.Write(fnv1a.Sum(nil))
|
buf.Write(fnv1a.Sum(nil))
|
||||||
|
|
||||||
// AES-128-CFB crypt the request:
|
block, err := aes.NewCipher(c.user.CmdKey[:])
|
||||||
// Key:MD5(UUID + []byte('c48619fe-8f02-49e0-b9e9-edf763e17e21'))
|
|
||||||
// IV:MD5(X + X + X + X),X = []byte(timestamp.now) (8 bytes, Big Endian)
|
|
||||||
md5hash := md5.New()
|
|
||||||
md5hash.Write(c.user.UUID[:])
|
|
||||||
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
|
|
||||||
key := md5hash.Sum(nil)
|
|
||||||
|
|
||||||
md5hash.Reset()
|
|
||||||
ts := make([]byte, 8)
|
|
||||||
binary.BigEndian.PutUint64(ts, uint64(time.Now().UTC().Unix()))
|
|
||||||
md5hash.Write(ts)
|
|
||||||
md5hash.Write(ts)
|
|
||||||
md5hash.Write(ts)
|
|
||||||
md5hash.Write(ts)
|
|
||||||
iv := md5hash.Sum(nil)
|
|
||||||
|
|
||||||
block, err := aes.NewCipher(key)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := cipher.NewCFBEncrypter(block, iv)
|
stream := cipher.NewCFBEncrypter(block, TimestampHash(time.Now().UTC()))
|
||||||
stream.XORKeyStream(buf.Bytes(), buf.Bytes())
|
stream.XORKeyStream(buf.Bytes(), buf.Bytes())
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package vmess
|
package vmess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User of vmess client
|
// User of vmess client
|
||||||
type User struct {
|
type User struct {
|
||||||
UUID [16]byte
|
UUID [16]byte
|
||||||
|
CmdKey [16]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUser .
|
// NewUser .
|
||||||
@ -18,9 +22,8 @@ func NewUser(uuidStr string) (*User, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
u := &User{
|
u := &User{UUID: uuid}
|
||||||
UUID: uuid,
|
copy(u.CmdKey[:], GetKey(uuid))
|
||||||
}
|
|
||||||
|
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
@ -35,3 +38,26 @@ func StrToUUID(s string) (uuid [16]byte, err error) {
|
|||||||
_, err = hex.Decode(uuid[:], b)
|
_, err = hex.Decode(uuid[:], b)
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
@ -90,9 +90,7 @@ func (s *VMess) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rc, err = client.NewConn(rc)
|
return client.NewConn(rc)
|
||||||
|
|
||||||
return rc, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP connects to the given address via the proxy.
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
Loading…
Reference in New Issue
Block a user