chore: use fastrand to replace math/rand

This commit is contained in:
wwqgtxx
2023-03-06 18:10:14 +08:00
parent ad6336231c
commit 6a97ab9ecb
26 changed files with 109 additions and 111 deletions

View File

@ -11,17 +11,13 @@ import (
"errors"
"hash/fnv"
"io"
"math/rand"
"net"
"time"
"github.com/zhangyunhao116/fastrand"
"golang.org/x/crypto/chacha20poly1305"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Conn wrapper a net.Conn with vmess protocol
type Conn struct {
net.Conn
@ -76,7 +72,7 @@ func (vc *Conn) sendRequest() error {
buf.WriteByte(vc.respV)
buf.WriteByte(OptionChunkStream)
p := rand.Intn(16)
p := fastrand.Intn(16)
// P Sec Reserve Cmd
buf.WriteByte(byte(p<<4) | byte(vc.security))
buf.WriteByte(0)
@ -94,7 +90,7 @@ func (vc *Conn) sendRequest() error {
// padding
if p > 0 {
padding := make([]byte, p)
rand.Read(padding)
fastrand.Read(padding)
buf.Write(padding)
}
@ -200,7 +196,7 @@ func hashTimestamp(t time.Time) []byte {
// newConn return a Conn instance
func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security, isAead bool) (*Conn, error) {
randBytes := make([]byte, 33)
rand.Read(randBytes)
fastrand.Read(randBytes)
reqBodyIV := make([]byte, 16)
reqBodyKey := make([]byte, 16)
copy(reqBodyIV[:], randBytes[:16])