Improve: use crypto/rand

This commit is contained in:
xjasonlyu
2023-02-04 14:26:41 +08:00
parent 059f661862
commit 4a8bf64cb1
3 changed files with 14 additions and 7 deletions
+2 -2
View File
@@ -2,10 +2,10 @@ package obfs
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net"
"net/http"
@@ -65,7 +65,7 @@ func (ho *HTTPObfs) Write(b []byte) (int, error) {
randBytes := make([]byte, 16)
rand.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", randInt()%54, randInt()%2))
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
req.Host = ho.host
+11
View File
@@ -1,4 +1,15 @@
// Package obfs provides obfuscation functionality for Shadowsocks protocol.
package obfs
import (
"crypto/rand"
"math"
"math/big"
)
// Ref: github.com/Dreamacro/clash/component/simple-obfs
func randInt() int {
n, _ := rand.Int(rand.Reader, big.NewInt(math.MaxInt))
return int(n.Int64())
}
+1 -5
View File
@@ -2,19 +2,15 @@ package obfs
import (
"bytes"
"crypto/rand"
"encoding/binary"
"io"
"math/rand"
"net"
"time"
"github.com/xjasonlyu/tun2socks/v2/common/pool"
)
func init() {
rand.Seed(time.Now().Unix())
}
const (
chunkSize = 1 << 14 // 2 ** 14 == 16 * 1024
)