Add support for plain text and curl

This commit is contained in:
Locria Cyber 2023-10-20 08:25:18 +00:00
parent 3985a69934
commit 55d28230cc
Signed by: iacore
GPG key ID: F8C16E5157A63006
4 changed files with 46 additions and 8 deletions

2
go.mod
View file

@ -11,6 +11,7 @@ require (
) )
require ( require (
github.com/elnormous/contenttype v1.0.4
github.com/google/uuid v1.3.1 // indirect github.com/google/uuid v1.3.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
@ -19,6 +20,7 @@ require (
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect github.com/tidwall/sjson v1.2.5 // indirect
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09
go.mau.fi/util v0.1.0 // indirect go.mau.fi/util v0.1.0 // indirect
golang.org/x/crypto v0.13.0 // indirect golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect

4
go.sum
View file

@ -4,6 +4,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elnormous/contenttype v1.0.4 h1:FjmVNkvQOGqSX70yvocph7keC8DtmJaLzTTq6ZOQCI8=
github.com/elnormous/contenttype v1.0.4/go.mod h1:5KTOW8m1kdX1dLMiUJeN9szzR2xkngiv2K+RVZwWBbI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -40,6 +42,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09 h1:QVxbx5l/0pzciWYOynixQMtUhPYC3YKD6EcUlOsgGqw=
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09/go.mod h1:Uy/Rnv5WKuOO+PuDhuYLEpUiiKIZtss3z519uk67aF0=
go.mau.fi/util v0.1.0 h1:BwIFWIOEeO7lsiI2eWKFkWTfc5yQmoe+0FYyOFVyaoE= go.mau.fi/util v0.1.0 h1:BwIFWIOEeO7lsiI2eWKFkWTfc5yQmoe+0FYyOFVyaoE=
go.mau.fi/util v0.1.0/go.mod h1:AxuJUMCxpzgJ5eV9JbPWKRH8aAJJidxetNdUj7qcb84= go.mau.fi/util v0.1.0/go.mod h1:AxuJUMCxpzgJ5eV9JbPWKRH8aAJJidxetNdUj7qcb84=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=

47
main.go
View file

@ -17,6 +17,7 @@ import (
"git.exozy.me/exozyme/status/core" "git.exozy.me/exozyme/status/core"
"git.exozy.me/exozyme/status/scanner" "git.exozy.me/exozyme/status/scanner"
"github.com/cbroglie/mustache" "github.com/cbroglie/mustache"
"github.com/timewasted/go-accept-headers"
) )
//go:embed all:public //go:embed all:public
@ -46,7 +47,7 @@ func parseAndRenderTemplate(templateName string, data interface{}) (string, erro
return res, nil return res, nil
} }
func rootPage(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { if r.Method != "GET" {
return return
} }
@ -77,6 +78,31 @@ func rootPage(w http.ResponseWriter, r *http.Request) {
// don't uncomment this in production. verbose // don't uncomment this in production. verbose
// log.Printf("%v", data) // log.Printf("%v", data)
ua := r.UserAgent()
prefer_text := strings.HasPrefix(ua, "curl/")
header_accept := accept.Parse(r.Header.Get("Accept"))
accept_text := header_accept.Accepts("text/plain")
accept_html := header_accept.Accepts("text/html")
if (!accept_html || prefer_text) && accept_text {
indexForVT100(w, r, data)
} else if accept_html {
indexForBrowser(w, r, data)
} else { // when no valid content type is requested
// not to-spec. still sends plain text anyway
indexForVT100(w, r, data)
}
}
func indexForVT100(w http.ResponseWriter, r *http.Request, data scanner.ServiceStatusBatch) {
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
parsedTemplate := formatPlainText(data)
w.Write([]byte(parsedTemplate))
}
func indexForBrowser(w http.ResponseWriter, r *http.Request, data scanner.ServiceStatusBatch) {
parsedTemplate, err := parseAndRenderTemplate("templates/index.html", data) parsedTemplate, err := parseAndRenderTemplate("templates/index.html", data)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
@ -113,7 +139,7 @@ func main() {
log.Panic(err) log.Panic(err)
} }
http.HandleFunc("/", rootPage) http.HandleFunc("/", index)
// serve public folder from embedfs // serve public folder from embedfs
htmlContent, err := fs.Sub(fs.FS(staticAssets), "public") htmlContent, err := fs.Sub(fs.FS(staticAssets), "public")
@ -230,11 +256,8 @@ func routineCheck(timeout time.Duration, onlyShowDown bool) error {
return nil return nil
} }
func notify(updates []scanner.ServiceStatus) error { func formatPlainText(updates []scanner.ServiceStatus) string {
if len(updates) == 0 { result := ""
return nil
}
messageBody := ""
for _, v := range updates { for _, v := range updates {
var upOrDown string var upOrDown string
if v.Ok { if v.Ok {
@ -243,8 +266,16 @@ func notify(updates []scanner.ServiceStatus) error {
upOrDown = "DOWN" upOrDown = "DOWN"
} }
serviceType := fmt.Sprintf("[%s]", v.Type) serviceType := fmt.Sprintf("[%s]", v.Type)
messageBody += fmt.Sprintf("%-4s | %-24s %-7s %s\n", upOrDown, v.Name, serviceType, v.Status) result += fmt.Sprintf("%-4s | %-24s %-7s %s\n", upOrDown, v.Name, serviceType, v.Status)
} }
return result
}
func notify(updates []scanner.ServiceStatus) error {
if len(updates) == 0 {
return nil
}
messageBody := formatPlainText(updates)
_, err := client.SendNoticeHTML( _, err := client.SendNoticeHTML(
"```\n"+messageBody+"```", "```\n"+messageBody+"```",
"<pre><code>"+strings.TrimRight(messageBody, "\n")+"</code></pre>") "<pre><code>"+strings.TrimRight(messageBody, "\n")+"</code></pre>")

View file

@ -34,6 +34,7 @@ Services
- [x] persist data on disk - [x] persist data on disk
- [x] clean up old checkpoints - [x] clean up old checkpoints
- [x] Matrix notification - [x] Matrix notification
- [x] serves plain text to curl
## Environment Variables ## Environment Variables