chore: use fastrand to replace math/rand
This commit is contained in:
@ -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])
|
||||
|
@ -2,11 +2,11 @@ package vmess
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/zhangyunhao116/fastrand"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
@ -26,7 +26,7 @@ type H2Config struct {
|
||||
func (hc *h2Conn) establishConn() error {
|
||||
preader, pwriter := io.Pipe()
|
||||
|
||||
host := hc.cfg.Hosts[rand.Intn(len(hc.cfg.Hosts))]
|
||||
host := hc.cfg.Hosts[fastrand.Intn(len(hc.cfg.Hosts))]
|
||||
path := hc.cfg.Path
|
||||
// TODO: connect use VMess Host instead of H2 Host
|
||||
req := http.Request{
|
||||
|
@ -4,10 +4,11 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
|
||||
"github.com/zhangyunhao116/fastrand"
|
||||
)
|
||||
|
||||
type httpConn struct {
|
||||
@ -51,16 +52,16 @@ func (hc *httpConn) Write(b []byte) (int, error) {
|
||||
return hc.Conn.Write(b)
|
||||
}
|
||||
|
||||
path := hc.cfg.Path[rand.Intn(len(hc.cfg.Path))]
|
||||
path := hc.cfg.Path[fastrand.Intn(len(hc.cfg.Path))]
|
||||
host := hc.cfg.Host
|
||||
if header := hc.cfg.Headers["Host"]; len(header) != 0 {
|
||||
host = header[rand.Intn(len(header))]
|
||||
host = header[fastrand.Intn(len(header))]
|
||||
}
|
||||
|
||||
u := fmt.Sprintf("http://%s%s", host, path)
|
||||
req, _ := http.NewRequest("GET", u, bytes.NewBuffer(b))
|
||||
for key, list := range hc.cfg.Headers {
|
||||
req.Header.Set(key, list[rand.Intn(len(list))])
|
||||
req.Header.Set(key, list[fastrand.Intn(len(list))])
|
||||
}
|
||||
req.ContentLength = int64(len(b))
|
||||
if err := req.Write(hc.Conn); err != nil {
|
||||
|
@ -2,12 +2,13 @@ package vmess
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
"math/rand"
|
||||
"net"
|
||||
"runtime"
|
||||
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/zhangyunhao116/fastrand"
|
||||
)
|
||||
|
||||
// Version of vmess
|
||||
@ -77,7 +78,7 @@ type Config struct {
|
||||
|
||||
// StreamConn return a Conn with net.Conn and DstAddr
|
||||
func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
||||
r := rand.Intn(len(c.user))
|
||||
r := fastrand.Intn(len(c.user))
|
||||
return newConn(conn, c.user[r], dst, c.security, c.isAead)
|
||||
}
|
||||
|
||||
|
@ -8,9 +8,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"io"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -22,7 +20,9 @@ import (
|
||||
"github.com/Dreamacro/clash/common/buf"
|
||||
N "github.com/Dreamacro/clash/common/net"
|
||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/zhangyunhao116/fastrand"
|
||||
)
|
||||
|
||||
type websocketConn struct {
|
||||
@ -120,7 +120,7 @@ func (wsc *websocketConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||
binary.BigEndian.PutUint64(header[2:], uint64(dataLen))
|
||||
}
|
||||
|
||||
maskKey := rand.Uint32()
|
||||
maskKey := fastrand.Uint32()
|
||||
binary.LittleEndian.PutUint32(header[1+payloadBitLength:], maskKey)
|
||||
N.MaskWebSocket(maskKey, data)
|
||||
|
||||
|
Reference in New Issue
Block a user