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,12 +4,13 @@ import (
"bytes"
"encoding/hex"
"io"
"math/rand"
"net"
"strconv"
"strings"
"github.com/Dreamacro/clash/common/pool"
"github.com/zhangyunhao116/fastrand"
)
func init() {
@ -81,7 +82,7 @@ func (c *httpConn) Write(b []byte) (int, error) {
bLength := len(b)
headDataLength := bLength
if bLength-headLength > 64 {
headDataLength = headLength + rand.Intn(65)
headDataLength = headLength + fastrand.Intn(65)
}
headData := b[:headDataLength]
b = b[headDataLength:]
@ -99,7 +100,7 @@ func (c *httpConn) Write(b []byte) (int, error) {
}
}
hosts := strings.Split(host, ",")
host = hosts[rand.Intn(len(hosts))]
host = hosts[fastrand.Intn(len(hosts))]
buf := pool.GetBuffer()
defer pool.PutBuffer(buf)
@ -118,7 +119,7 @@ func (c *httpConn) Write(b []byte) (int, error) {
buf.WriteString(body + "\r\n\r\n")
} else {
buf.WriteString("User-Agent: ")
buf.WriteString(userAgent[rand.Intn(len(userAgent))])
buf.WriteString(userAgent[fastrand.Intn(len(userAgent))])
buf.WriteString("\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.8\r\nAccept-Encoding: gzip, deflate\r\n")
if c.post {
packBoundary(buf)
@ -146,7 +147,7 @@ func packBoundary(buf *bytes.Buffer) {
buf.WriteString("Content-Type: multipart/form-data; boundary=")
set := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i := 0; i < 32; i++ {
buf.WriteByte(set[rand.Intn(62)])
buf.WriteByte(set[fastrand.Intn(62)])
}
buf.WriteString("\r\n")
}

View File

@ -3,10 +3,11 @@ package obfs
import (
"encoding/binary"
"hash/crc32"
"math/rand"
"net"
"github.com/Dreamacro/clash/common/pool"
"github.com/zhangyunhao116/fastrand"
)
func init() {
@ -53,10 +54,10 @@ func (c *randomHeadConn) Write(b []byte) (int, error) {
c.buf = append(c.buf, b...)
if !c.hasSentHeader {
c.hasSentHeader = true
dataLength := rand.Intn(96) + 4
dataLength := fastrand.Intn(96) + 4
buf := pool.Get(dataLength + 4)
defer pool.Put(buf)
rand.Read(buf[:dataLength])
fastrand.Read(buf[:dataLength])
binary.LittleEndian.PutUint32(buf[dataLength:], 0xffffffff-crc32.ChecksumIEEE(buf[:dataLength]))
_, err := c.Conn.Write(buf)
return len(b), err

View File

@ -4,13 +4,14 @@ import (
"bytes"
"crypto/hmac"
"encoding/binary"
"math/rand"
"net"
"strings"
"time"
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/transport/ssr/tools"
"github.com/zhangyunhao116/fastrand"
)
func init() {
@ -25,7 +26,7 @@ type tls12Ticket struct {
func newTLS12Ticket(b *Base) Obfs {
r := &tls12Ticket{Base: b, authData: &authData{}}
rand.Read(r.clientID[:])
fastrand.Read(r.clientID[:])
return r
}
@ -90,7 +91,7 @@ func (c *tls12TicketConn) Write(b []byte) (int, error) {
buf := pool.GetBuffer()
defer pool.PutBuffer(buf)
for len(b) > 2048 {
size := rand.Intn(4096) + 100
size := fastrand.Intn(4096) + 100
if len(b) < size {
size = len(b)
}
@ -196,7 +197,7 @@ func packSNIData(buf *bytes.Buffer, u string) {
}
func (c *tls12TicketConn) packTicketBuf(buf *bytes.Buffer, u string) {
length := 16 * (rand.Intn(17) + 8)
length := 16 * (fastrand.Intn(17) + 8)
buf.Write([]byte{0, 0x23})
binary.Write(buf, binary.BigEndian, uint16(length))
tools.AppendRandBytes(buf, length)
@ -221,6 +222,6 @@ func (t *tls12Ticket) getHost() string {
host = ""
}
hosts := strings.Split(host, ",")
host = hosts[rand.Intn(len(hosts))]
host = hosts[fastrand.Intn(len(hosts))]
return host
}