From 6834661ad104d49c8f331ae50e908c83c8f9c363 Mon Sep 17 00:00:00 2001 From: mkwcat Date: Fri, 24 Nov 2023 23:13:05 -0500 Subject: [PATCH] NAS: Return HTTP error codes --- nas/auth.go | 10 ++++++++++ nas/main.go | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nas/auth.go b/nas/auth.go index 58dad6b..138f175 100644 --- a/nas/auth.go +++ b/nas/auth.go @@ -15,6 +15,7 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request err := r.ParseForm() if err != nil { logging.Error(moduleName, "Failed to parse form") + replyHTTPError(w, 400, "400 Bad Request") return } @@ -28,6 +29,7 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request parsed, err := common.Base64DwcEncoding.DecodeString(values[0]) if err != nil { logging.Error(moduleName, "Invalid POST form value:", aurora.Cyan(key).String()+":", aurora.Cyan(values[0])) + replyHTTPError(w, 400, "400 Bad Request") return } logging.Info(moduleName, aurora.Cyan(key).String()+":", aurora.Cyan(string(parsed))) @@ -37,6 +39,7 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request action, ok := fields["action"] if !ok || action == "" { logging.Error(moduleName, "No action in form") + replyHTTPError(w, 400, "400 Bad Request") return } @@ -50,6 +53,13 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request case "acctcreate": reply = acctcreate(moduleName, fields) break + + default: + reply = map[string]string{ + "retry": "0", + "returncd": "109", + } + break } param := url.Values{} diff --git a/nas/main.go b/nas/main.go index 6e5871c..a29574e 100644 --- a/nas/main.go +++ b/nas/main.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/jackc/pgx/v4/pgxpool" "github.com/logrusorgru/aurora/v3" - "log" "net/http" "regexp" "strconv" @@ -40,7 +39,7 @@ func StartServer() { address := config.Address + ":" + config.Port logging.Notice("NAS", "Starting HTTP server on", address) - log.Fatal(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) + panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) } var regexSakeHost = regexp.MustCompile(`^([a-z\-]+\.)?sake\.gs\.`) @@ -86,4 +85,21 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { downloadStage1(moduleName, w, r, val) return } + + replyHTTPError(w, 404, "404 Not Found") +} + +func replyHTTPError(w http.ResponseWriter, errorCode int, errorString string) { + response := "\n" + response += "" + errorString + "\n" + response += "\n" + response += "

" + errorString + "

\n" + response += "
WiiLink
\n" + response += "\n" + response += "\n" + + w.Header().Set("Content-Type", "text/html") + w.Header().Set("Content-Length", strconv.Itoa(len(response))) + w.WriteHeader(errorCode) + w.Write([]byte(response)) }