NAS: Add a profanity filter

This commit is contained in:
MikeIsAStar 2024-02-12 03:00:00 -05:00
parent f429fb557e
commit bea2bb8886
3 changed files with 70 additions and 8 deletions

View File

@ -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,
}
}

View File

@ -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)))
}

39
nas/profanity.go Normal file
View File

@ -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
}