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

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"math"
"math/rand"
"net"
"strconv"
"strings"
@ -12,6 +11,8 @@ import (
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/ssr/tools"
"github.com/zhangyunhao116/fastrand"
)
type (
@ -64,7 +65,7 @@ func (a *authAES128) initUserData() {
}
if len(a.userKey) == 0 {
a.userKey = a.Key
rand.Read(a.userID[:])
fastrand.Read(a.userID[:])
}
}
@ -198,7 +199,7 @@ func (a *authAES128) packData(poolBuf *bytes.Buffer, data []byte, fullDataLength
}
func trapezoidRandom(max int, d float64) int {
base := rand.Float64()
base := fastrand.Float64()
if d-0 > 1e-6 {
a := 1 - d
base = (math.Sqrt(a*a+4*d*base) - a) / (2 * d)
@ -219,10 +220,10 @@ func (a *authAES128) getRandDataLengthForPackData(dataLength, fullDataLength int
if revLength > -1460 {
return trapezoidRandom(revLength+1460, -0.3)
}
return rand.Intn(32)
return fastrand.Intn(32)
}
if dataLength > 900 {
return rand.Intn(revLength)
return fastrand.Intn(revLength)
}
return trapezoidRandom(revLength, -0.3)
}
@ -247,7 +248,7 @@ func (a *authAES128) packAuthData(poolBuf *bytes.Buffer, data []byte) {
copy(macKey, a.iv)
copy(macKey[len(a.iv):], a.Key)
poolBuf.WriteByte(byte(rand.Intn(256)))
poolBuf.WriteByte(byte(fastrand.Intn(256)))
poolBuf.Write(a.hmac(macKey, poolBuf.Bytes())[:6])
poolBuf.Write(a.userID[:])
err := a.authData.putEncryptedData(poolBuf, a.userKey, [2]int{packedAuthDataLength, randDataLength}, a.salt)
@ -263,9 +264,9 @@ func (a *authAES128) packAuthData(poolBuf *bytes.Buffer, data []byte) {
func (a *authAES128) getRandDataLengthForPackAuthData(size int) int {
if size > 400 {
return rand.Intn(512)
return fastrand.Intn(512)
}
return rand.Intn(1024)
return fastrand.Intn(1024)
}
func (a *authAES128) packRandData(poolBuf *bytes.Buffer, size int) {

View File

@ -5,11 +5,12 @@ import (
"encoding/binary"
"hash/adler32"
"hash/crc32"
"math/rand"
"net"
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/transport/ssr/tools"
"github.com/zhangyunhao116/fastrand"
)
func init() {
@ -176,7 +177,7 @@ func (a *authSHA1V4) getRandDataLength(size int) int {
return 0
}
if size > 400 {
return rand.Intn(256)
return fastrand.Intn(256)
}
return rand.Intn(512)
return fastrand.Intn(512)
}

View File

@ -6,13 +6,14 @@ import (
"crypto/cipher"
"encoding/base64"
"encoding/binary"
"math/rand"
"sync"
"time"
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/shadowsocks/core"
"github.com/zhangyunhao116/fastrand"
)
type Base struct {
@ -37,8 +38,8 @@ func (a *authData) next() *authData {
a.mutex.Lock()
defer a.mutex.Unlock()
if a.connectionID > 0xff000000 || a.connectionID == 0 {
rand.Read(a.clientID[:])
a.connectionID = rand.Uint32() & 0xffffff
fastrand.Read(a.clientID[:])
a.connectionID = fastrand.Uint32() & 0xffffff
}
a.connectionID++
copy(r.clientID[:], a.clientID[:])

View File

@ -4,8 +4,9 @@ import (
"bytes"
"errors"
"fmt"
"math/rand"
"net"
"github.com/zhangyunhao116/fastrand"
)
var (
@ -68,7 +69,7 @@ func getHeadSize(b []byte, defaultValue int) int {
func getDataLength(b []byte) int {
bLength := len(b)
dataLength := getHeadSize(b, 30) + rand.Intn(32)
dataLength := getHeadSize(b, 30) + fastrand.Intn(32)
if bLength < dataLength {
return bLength
}