Files
v2ray_simple/utils/strings_unix.go
2022-12-02 08:54:42 +08:00

46 lines
768 B
Go

//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd
package utils
import (
"io"
"math/rand"
"os"
"strings"
)
var words []string
var getWordListFailed bool
func GetRandomWord() (result string) {
if len(words) == 0 && !getWordListFailed {
words = readAvailableDictionary()
}
if theLen := len(words); theLen == 0 {
getWordListFailed = true
result = GenerateRandomString()
} else {
result = words[rand.Int()%theLen]
}
return
}
func readAvailableDictionary() (words []string) {
file, err := os.Open("/usr/share/dict/words")
if err != nil {
return
}
bytes, err := io.ReadAll(file)
if err != nil {
return
}
words = strings.Split(string(bytes), "\n")
return
}