From 18d295c99372435ea145e8a2172f0c22a77fdcb3 Mon Sep 17 00:00:00 2001 From: nhyatt Date: Sat, 20 Jan 2024 20:59:56 -0600 Subject: [PATCH] add json method --- assets/html/robots.txt | 3 +++ assets/html/txt.tplt | 3 +++ internal/webserver/httpServer.go | 42 ++++++++++++++++++++++++++++++ internal/webserver/httpTemplate.go | 4 +++ 4 files changed, 52 insertions(+) create mode 100644 assets/html/txt.tplt diff --git a/assets/html/robots.txt b/assets/html/robots.txt index 7dd708a..60de74c 100644 --- a/assets/html/robots.txt +++ b/assets/html/robots.txt @@ -21,3 +21,6 @@ Disallow: / User-agent: anthropic-ai Disallow: / + +User-agent: * +Allow: / diff --git a/assets/html/txt.tplt b/assets/html/txt.tplt new file mode 100644 index 0000000..a44a5b3 --- /dev/null +++ b/assets/html/txt.tplt @@ -0,0 +1,3 @@ +{{- range .CVEs }} +{{ .CveID | ToUpper }} - {{ .Product }} - {{ .ShortDescription }} +{{- end }} \ No newline at end of file diff --git a/internal/webserver/httpServer.go b/internal/webserver/httpServer.go index 24a960c..34d7909 100644 --- a/internal/webserver/httpServer.go +++ b/internal/webserver/httpServer.go @@ -1,6 +1,7 @@ package webserver import ( + "encoding/json" "fmt" "log" "regexp" @@ -12,6 +13,7 @@ import ( "net/http" "istheinternetonfire.app/assets" + "istheinternetonfire.app/internal/cisa" "istheinternetonfire.app/internal/config" ) @@ -73,6 +75,8 @@ func Start() { IdleTimeout: time.Duration(config.Cfg.WebServerIdleTimeout) * time.Second, } + path.HandleFunc("/status.json", webJSON) + path.HandleFunc("/status.txt", webTXT) path.HandleFunc("/", webRoot) if err := connection.ListenAndServe(); err != nil { @@ -118,3 +122,41 @@ func webRoot(w http.ResponseWriter, r *http.Request) { } } } + +func webJSON(w http.ResponseWriter, r *http.Request) { + if strings.ToLower(r.Method) != "get" { + config.Cfg.Log.Debug("http invalid method", "url", r.URL.Path, "expected", "GET", "received", r.Method) + tmpltError(w, http.StatusBadRequest, "Invalid http method.") + return + } + + w.Header().Add("Content-Type", TYPE_APPLICATION_JSON) + + var ( + output []byte + num int = 3 + err error + ) + + cves := cisa.Read() + if len(cves.Vulnerabilities) == 0 { + w.Write([]byte("{}")) + return + } else if len(cves.Vulnerabilities) < 3 { + num = len(cves.Vulnerabilities) + } + + if output, err = json.MarshalIndent(cves.Vulnerabilities[len(cves.Vulnerabilities)-num:], "", " "); err != nil { + config.Cfg.Log.Debug("unable to convert CVES to JSON", "error", err, "url", r.URL.Path) + } + + w.Write(output) +} + +func webTXT(w http.ResponseWriter, r *http.Request) { + if strings.ToLower(r.Method) != "get" { + config.Cfg.Log.Debug("http invalid method", "url", r.URL.Path, "expected", "GET", "received", r.Method) + tmpltError(w, http.StatusBadRequest, "Invalid http method.") + return + } +} diff --git a/internal/webserver/httpTemplate.go b/internal/webserver/httpTemplate.go index 69f80e6..f290175 100644 --- a/internal/webserver/httpTemplate.go +++ b/internal/webserver/httpTemplate.go @@ -105,3 +105,7 @@ func tmpltStatusNotFound(w http.ResponseWriter, path string) { } w.Write(msgBuffer.Bytes()) //nolint:errcheck } + +func tmpltTXT(w http.ResponseWriter) { + +}