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

@ -5,11 +5,12 @@ import (
"encoding/base64"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"github.com/Dreamacro/clash/common/pool"
"github.com/zhangyunhao116/fastrand"
)
// HTTPObfs is shadowsocks http simple-obfs implementation
@ -63,9 +64,9 @@ func (ho *HTTPObfs) Read(b []byte) (int, error) {
func (ho *HTTPObfs) Write(b []byte) (int, error) {
if ho.firstRequest {
randBytes := make([]byte, 16)
rand.Read(randBytes)
fastrand.Read(randBytes)
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", rand.Int()%54, rand.Int()%2))
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", fastrand.Int()%54, fastrand.Int()%2))
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
req.Host = ho.host

View File

@ -4,16 +4,13 @@ import (
"bytes"
"encoding/binary"
"io"
"math/rand"
"net"
"time"
"github.com/Dreamacro/clash/common/pool"
)
func init() {
rand.Seed(time.Now().Unix())
}
"github.com/zhangyunhao116/fastrand"
)
const (
chunkSize = 1 << 14 // 2 ** 14 == 16 * 1024
@ -130,8 +127,8 @@ func NewTLSObfs(conn net.Conn, server string) net.Conn {
func makeClientHelloMsg(data []byte, server string) []byte {
random := make([]byte, 28)
sessionID := make([]byte, 32)
rand.Read(random)
rand.Read(sessionID)
fastrand.Read(random)
fastrand.Read(sessionID)
buf := &bytes.Buffer{}