From bea2bb8886f73d7422ea74aba2cf945fa6974a26 Mon Sep 17 00:00:00 2001 From: MikeIsAStar <99037623+MikeIsAStar@users.noreply.github.com> Date: Mon, 12 Feb 2024 03:00:00 -0500 Subject: [PATCH] NAS: Add a profanity filter --- nas/auth.go | 37 +++++++++++++++++++++++++++++-------- nas/main.go | 2 ++ nas/profanity.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 nas/profanity.go diff --git a/nas/auth.go b/nas/auth.go index 29f11b7..3e27899 100644 --- a/nas/auth.go +++ b/nas/auth.go @@ -63,7 +63,7 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request return } - if key == "ingamesn" || key == "devname" { + if key == "ingamesn" || key == "devname" || key == "words" { // Special handling required for the UTF-16 string var utf16String []uint16 if unitcdString == "0" { @@ -249,8 +249,15 @@ func login(moduleName string, fields map[string]string, isLocalhost bool) map[st return param } - var authToken, challenge string + hasProfaneName := false + ingamesn, ok := fields["ingamesn"] + if ok { + if hasProfaneName, _ = IsBadWord(ingamesn); hasProfaneName { + logging.Info(moduleName, aurora.Cyan(strconv.FormatUint(userId, 10)), "has a profane name ("+aurora.Red(ingamesn).String()+")") + } + } + var authToken, challenge string switch unitcdInt { // ds case 0: @@ -303,7 +310,11 @@ func login(moduleName string, fields map[string]string, isLocalhost bool) map[st logging.Notice(moduleName, "Login (Wii)", aurora.Cyan(strconv.FormatUint(userId, 10)), aurora.Cyan(gsbrcd), "ingamesn:", aurora.Cyan(fields["ingamesn"])) } - param["returncd"] = "001" + if hasProfaneName { + param["returncd"] = "040" + } else { + param["returncd"] = "001" + } param["challenge"] = challenge param["token"] = authToken @@ -341,14 +352,24 @@ func svcloc(fields map[string]string) map[string]string { } func handleProfanity(fields map[string]string) map[string]string { - prwords := "" - wordCount := strings.Count(fields["words"], "\t") + 1 - for i := 0; i < wordCount; i++ { - prwords += "0" + var prwords string + for _, word := range strings.Split(fields["words"], "\t") { + if isBadWord, _ := IsBadWord(word); isBadWord { + prwords += "1" + } else { + prwords += "0" + } + } + + var returncd string + if strings.Contains(prwords, "1") { + returncd = "040" + } else { + returncd = "000" } return map[string]string{ - "returncd": "000", + "returncd": returncd, "prwords": prwords, } } diff --git a/nas/main.go b/nas/main.go index 78f95d9..e2f03fe 100644 --- a/nas/main.go +++ b/nas/main.go @@ -49,6 +49,8 @@ func StartServer() { go startHTTPSProxy(config) } + CacheProfanityFile() + logging.Notice("NAS", "Starting HTTP server on", address) panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) } diff --git a/nas/profanity.go b/nas/profanity.go new file mode 100644 index 0000000..587bee1 --- /dev/null +++ b/nas/profanity.go @@ -0,0 +1,39 @@ +package nas + +import ( + "errors" + "os" + "strings" +) + +var profanityFilePath = "./profanity.txt" +var profanityFileLines []string = nil + +func CacheProfanityFile() bool { + contents, err := os.ReadFile(profanityFilePath) + if err != nil { + return false + } + + lines := strings.Split(string(contents), "\n") + profanityFileLines = lines + return true +} + +func IsBadWord(word string) (bool, error) { + if !isProfanityFileCached() { + return false, errors.New("the file '" + profanityFilePath + "' has not been cached") + } + + for _, line := range profanityFileLines { + if strings.EqualFold(line, word) { + return true, nil + } + } + + return false, nil +} + +func isProfanityFileCached() bool { + return profanityFileLines != nil +}