From 333c79701d2a04603be12dd7852679fcd4353729 Mon Sep 17 00:00:00 2001 From: MikeIsAStar <99037623+MikeIsAStar@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:05:00 -0500 Subject: [PATCH] DLS1: Implement the 'count' action --- common/strings.go | 19 +++++++++++++++++++ dlc/README.md | 3 +++ nas/auth.go | 30 +++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 dlc/README.md diff --git a/common/strings.go b/common/strings.go index f342c61..f7b3a78 100644 --- a/common/strings.go +++ b/common/strings.go @@ -34,3 +34,22 @@ func GetString(buf []byte) string { nullTerminator := bytes.IndexByte(buf, 0) return string(buf[:nullTerminator]) } + +// Checks if the given string is composed exclusively of uppercase alphanumeric characters. +func IsUppercaseAlphanumeric(str string) bool { + strLength := len(str) + + if strLength == 0 { + return false + } + + for i := 0; i < strLength; i++ { + c := str[i] + + if (c < '0' || c > '9') && (c < 'A' || c > 'Z') { + return false + } + } + + return true +} diff --git a/dlc/README.md b/dlc/README.md new file mode 100644 index 0000000..28fccfa --- /dev/null +++ b/dlc/README.md @@ -0,0 +1,3 @@ +# Downloadable Content + +Create a folder in this directory using the game's game code as the folder name. Afterwards, place downloadable content into the aforementioned folder. diff --git a/nas/auth.go b/nas/auth.go index 141d58c..3accfe9 100644 --- a/nas/auth.go +++ b/nas/auth.go @@ -5,6 +5,8 @@ import ( "github.com/logrusorgru/aurora/v3" "net/http" "net/url" + "os" + "path/filepath" "strconv" "strings" "time" @@ -13,6 +15,10 @@ import ( "wwfc/logging" ) +var ( + dlcDir = "./dlc" +) + func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { @@ -87,6 +93,13 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request return } + rhgamecd, ok := fields["rhgamecd"] + if !ok || !isValidRhgamecd(rhgamecd) { + logging.Error(moduleName, "Missing or invalid rhgamecd") + replyHTTPError(w, 400, "400 Bad Request") + return + } + switch action { case "count": response = []byte(dlsCount(moduleName, fields)) @@ -210,7 +223,22 @@ func handleProfanity(moduleName string, fields map[string]string) map[string]str } func dlsCount(moduleName string, fields map[string]string) string { - return "0" + dlcFolder := filepath.Join(dlcDir, fields["rhgamecd"]) + + dir, ok := os.ReadDir(dlcFolder) + if ok != nil { + return "0" + } + + return strconv.Itoa(len(dir)) +} + +func isValidRhgamecd(rhgamecd string) bool { + if len(rhgamecd) != 4 { + return false + } + + return common.IsUppercaseAlphanumeric(rhgamecd) } func getDateTime() string {