43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package global
|
|
|
|
import (
|
|
crand "crypto/rand" // Probably better than math/rand for this
|
|
"log"
|
|
"math/rand"
|
|
)
|
|
|
|
type SnowcloakConfigurationBase struct {
|
|
Jwt string
|
|
}
|
|
|
|
func GenerateRandomString(length int) string {
|
|
// C# had optional parameters that allowed lowercase for chardata and gpose lobbies, Go doesn't.
|
|
// We can probably get away with just uppercase.
|
|
allowedArray := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
random := make([]byte, length)
|
|
_, err := crand.Read(random)
|
|
if err != nil {
|
|
log.Fatalf("Couldn't generate secure slice: %s", err)
|
|
}
|
|
allowedLength := len(allowedArray)
|
|
i := 0
|
|
charArray := make([]rune, length)
|
|
for i < length {
|
|
charArray[i] = allowedArray[int(random[i])%allowedLength] // Casting random[i] due to type mismatch
|
|
i++
|
|
}
|
|
return string(charArray)
|
|
}
|
|
|
|
func PhasedStartupString() string {
|
|
// Redis is used for a lot of stuff, which means the Postgres DB gets HAMMERED on a fresh startup. Use
|
|
// this to generate a shuffled string that can be used to only allow e.g. UIDs starting with [x] to connect
|
|
// for the first however many seconds and incrementally expand access so that data doesn't need to be loaded all at
|
|
// once
|
|
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
rand.Shuffle(len(chars), func(i, j int) {
|
|
chars[i], chars[j] = chars[j], chars[i]
|
|
})
|
|
return string(chars)
|
|
}
|