NAS: Return HTTP error codes

This commit is contained in:
mkwcat 2023-11-24 23:13:05 -05:00
parent 8804352d29
commit 6834661ad1
No known key found for this signature in database
GPG Key ID: 7A505679CE9E7AA9
2 changed files with 28 additions and 2 deletions

View File

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

View File

@ -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 := "<html>\n"
response += "<head><title>" + errorString + "</title></head>\n"
response += "<body>\n"
response += "<center><h1>" + errorString + "</h1></center>\n"
response += "<hr><center>WiiLink</center>\n"
response += "</body>\n"
response += "</html>\n"
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.WriteHeader(errorCode)
w.Write([]byte(response))
}