Migration: go1.20

This commit is contained in:
Dreamacro
2023-02-16 21:43:40 +08:00
parent fbf2f26516
commit 8173d6681b
15 changed files with 65 additions and 69 deletions

View File

@ -1,9 +1,10 @@
package obfs
import (
"crypto/rand"
"encoding/binary"
"hash/crc32"
"math/rand"
mathRand "math/rand"
"net"
"github.com/Dreamacro/clash/common/pool"
@ -53,7 +54,7 @@ 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 := mathRand.Intn(96) + 4
buf := pool.Get(dataLength + 4)
defer pool.Put(buf)
rand.Read(buf[:dataLength])

View File

@ -3,8 +3,9 @@ package obfs
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"encoding/binary"
"math/rand"
mathRand "math/rand"
"net"
"strings"
"time"
@ -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 := mathRand.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 * (mathRand.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[mathRand.Intn(len(hosts))]
return host
}

View File

@ -2,9 +2,10 @@ package protocol
import (
"bytes"
"crypto/rand"
"encoding/binary"
"math"
"math/rand"
mathRand "math/rand"
"net"
"strconv"
"strings"
@ -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 := mathRand.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 mathRand.Intn(32)
}
if dataLength > 900 {
return rand.Intn(revLength)
return mathRand.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(mathRand.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 mathRand.Intn(512)
}
return rand.Intn(1024)
return mathRand.Intn(1024)
}
func (a *authAES128) packRandData(poolBuf *bytes.Buffer, size int) {

View File

@ -4,9 +4,10 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/binary"
"math/rand"
mathRand "math/rand"
"sync"
"time"
@ -38,7 +39,7 @@ func (a *authData) next() *authData {
defer a.mutex.Unlock()
if a.connectionID > 0xff000000 || a.connectionID == 0 {
rand.Read(a.clientID[:])
a.connectionID = rand.Uint32() & 0xffffff
a.connectionID = mathRand.Uint32() & 0xffffff
}
a.connectionID++
copy(r.clientID[:], a.clientID[:])