Move random utilities to common package

pull/242/head
Andy Wang 1 month ago
parent 3b449b64b3
commit 392fc41de8
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374

@ -1,16 +1,11 @@
package client package client
import ( import (
cryptoRand "crypto/rand" "github.com/cbeuw/Cloak/internal/common"
utls "github.com/refraction-networking/utls" utls "github.com/refraction-networking/utls"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"math/big"
"math/rand"
"net" "net"
"strings" "strings"
"time"
"github.com/cbeuw/Cloak/internal/common"
) )
const appDataMaxLength = 16401 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 // https://github.com/ProtonVPN/wireguard-go/commit/bcf344b39b213c1f32147851af0d2a8da9266883
func randomServerName() string { func randomServerName() string {
charNum := int('z') - int('a') + 1 charNum := int('z') - int('a') + 1
size := 3 + randInt(10) size := 3 + common.RandInt(10)
name := make([]byte, size) name := make([]byte, size)
for i := range name { 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 return string(name) + "." + common.RandItem(topLevelDomains)
rand.Seed(time.Now().UnixNano())
return rand.Intn(n)
} }
func buildClientHello(browser browser, fields clientHelloFields) ([]byte, error) { func buildClientHello(browser browser, fields clientHelloFields) ([]byte, error) {

@ -6,6 +6,7 @@ import (
"crypto/rand" "crypto/rand"
"errors" "errors"
"io" "io"
"math/big"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -52,8 +53,8 @@ func CryptoRandRead(buf []byte) {
RandRead(rand.Reader, buf) RandRead(rand.Reader, buf)
} }
func RandRead(randSource io.Reader, buf []byte) { func backoff(f func() error) {
_, err := randSource.Read(buf) err := f()
if err == nil { if err == nil {
return return
} }
@ -61,12 +62,36 @@ func RandRead(randSource io.Reader, buf []byte) {
100 * time.Millisecond, 300 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second, 100 * time.Millisecond, 300 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second,
3 * time.Second, 5 * time.Second} 3 * time.Second, 5 * time.Second}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
log.Errorf("Failed to get random bytes: %v. Retrying...", err) log.Errorf("Failed to get random: %v. Retrying...", err)
_, err = randSource.Read(buf) err = f()
if err == nil { if err == nil {
return return
} }
time.Sleep(waitDur[i]) 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…
Cancel
Save