mirror of https://github.com/cbeuw/Cloak
Move random utilities to common package
This commit is contained in:
parent
3b449b64b3
commit
392fc41de8
|
|
@ -1,16 +1,11 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
cryptoRand "crypto/rand"
|
||||
"github.com/cbeuw/Cloak/internal/common"
|
||||
utls "github.com/refraction-networking/utls"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cbeuw/Cloak/internal/common"
|
||||
)
|
||||
|
||||
const appDataMaxLength = 16401
|
||||
|
|
@ -40,26 +35,12 @@ var topLevelDomains = []string{"com", "net", "org", "it", "fr", "me", "ru", "cn"
|
|||
// https://github.com/ProtonVPN/wireguard-go/commit/bcf344b39b213c1f32147851af0d2a8da9266883
|
||||
func randomServerName() string {
|
||||
charNum := int('z') - int('a') + 1
|
||||
size := 3 + randInt(10)
|
||||
size := 3 + common.RandInt(10)
|
||||
name := make([]byte, size)
|
||||
for i := range name {
|
||||
name[i] = byte(int('a') + randInt(charNum))
|
||||
name[i] = byte(int('a') + common.RandInt(charNum))
|
||||
}
|
||||
return string(name) + "." + randItem(topLevelDomains)
|
||||
}
|
||||
|
||||
func randItem(list []string) string {
|
||||
return list[randInt(len(list))]
|
||||
}
|
||||
|
||||
func randInt(n int) int {
|
||||
size, err := cryptoRand.Int(cryptoRand.Reader, big.NewInt(int64(n)))
|
||||
if err == nil {
|
||||
return int(size.Int64())
|
||||
}
|
||||
//goland:noinspection GoDeprecation
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return rand.Intn(n)
|
||||
return string(name) + "." + common.RandItem(topLevelDomains)
|
||||
}
|
||||
|
||||
func buildClientHello(browser browser, fields clientHelloFields) ([]byte, error) {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"crypto/rand"
|
||||
"errors"
|
||||
"io"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
|
@ -52,8 +53,8 @@ func CryptoRandRead(buf []byte) {
|
|||
RandRead(rand.Reader, buf)
|
||||
}
|
||||
|
||||
func RandRead(randSource io.Reader, buf []byte) {
|
||||
_, err := randSource.Read(buf)
|
||||
func backoff(f func() error) {
|
||||
err := f()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -61,12 +62,36 @@ func RandRead(randSource io.Reader, buf []byte) {
|
|||
100 * time.Millisecond, 300 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second,
|
||||
3 * time.Second, 5 * time.Second}
|
||||
for i := 0; i < 10; i++ {
|
||||
log.Errorf("Failed to get random bytes: %v. Retrying...", err)
|
||||
_, err = randSource.Read(buf)
|
||||
log.Errorf("Failed to get random: %v. Retrying...", err)
|
||||
err = f()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(waitDur[i])
|
||||
}
|
||||
log.Fatal("Cannot get random bytes after 10 retries")
|
||||
log.Fatal("Cannot get random after 10 retries")
|
||||
}
|
||||
|
||||
func RandRead(randSource io.Reader, buf []byte) {
|
||||
backoff(func() error {
|
||||
_, err := randSource.Read(buf)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func RandItem[T any](list []T) T {
|
||||
return list[RandInt(len(list))]
|
||||
}
|
||||
|
||||
func RandInt(n int) int {
|
||||
s := new(int)
|
||||
backoff(func() error {
|
||||
size, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*s = int(size.Int64())
|
||||
return nil
|
||||
})
|
||||
return *s
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue