diff --git a/README.md b/README.md index 524ebe2..6e5a94c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ A user can be added under the `[users]` section like so: passwordHash = "$2a$10$w00dzQ1PP6nwXLhuzV2pFOUU6m8bcZXtDX3UVxpOYq3fTSwVMqPge" showPublic = true ``` -`passwordHash` should be a bcrypt hash of the desired password with a cost of 10 (default) +`passwordHash` should be a bcrypt hash of the desired password with a cost of 10 (default). `simpledash --hash ` can be used to get a suitable hash + `showPublic` should be a boolean denoting whether public cards should be displayed while signed in #### Cards diff --git a/main.go b/main.go index ddc56e5..3b0dbbb 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/Masterminds/sprig" "github.com/gorilla/mux" @@ -9,6 +10,7 @@ import ( "github.com/rs/zerolog/log" flag "github.com/spf13/pflag" "github.com/wader/gormstore/v2" + "golang.org/x/crypto/bcrypt" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" @@ -39,9 +41,20 @@ func main() { addr := flag.IPP("addr", "a", net.ParseIP("0.0.0.0"), "Bind address for HTTP server") port := flag.IntP("port", "p", 8080, "Bind port for HTTP server") config := flag.StringP("config", "c", "simpledash.toml", "TOML config file") + hash := flag.String("hash", "", "Generate new bcrypt password hash") + flag.ErrHelp = errors.New("simpledash: help requested") // Parse flags flag.Parse() + if *hash != "" { + hash, err := bcrypt.GenerateFromPassword([]byte(*hash), bcrypt.DefaultCost) + if err != nil { + Log.Fatal().Err(err).Msg("Error creating bcrypt hash") + } + fmt.Println(string(hash)) + os.Exit(0) + } + // Create new router router := mux.NewRouter().StrictSlash(true) diff --git a/resources/templates/cards/api.html b/resources/templates/cards/api.html index 7c85a25..b4987e3 100644 --- a/resources/templates/cards/api.html +++ b/resources/templates/cards/api.html @@ -1,5 +1,5 @@ {{- $format := splitList "\n" (trim .Data.format) -}} -{{- $title := replace " " "" .Title -}} +{{- $randID := randAlphaNum 10 -}}
{{if ne .Icon ""}} @@ -9,10 +9,9 @@
-

Loading...

- {{range $_, $accessStr := $format}} - {{- $id := printf `%s_%s` $title (b64enc $accessStr) -}} -

+

Loading...

+ {{range $index, $fmtStr := $format}} +
{{end}}
{{if .Data.footer}} @@ -25,10 +24,9 @@ request.open('GET', "{{proxy .URL}}", true) request.onload = function () { const data = JSON.parse(this.response) - document.getElementById("{{$title}}LoadingText").classList.add("is-hidden") - {{range $_, $accessStr := $format}} - {{- $id := printf `%s_%s` $title (b64enc $accessStr) -}} - document.getElementById("{{$id}}").innerHTML = `{{unescJS (trim $accessStr)}}` + document.getElementById("APILoadingText_{{$randID}}").classList.add("is-hidden") + {{range $index, $fmtStr := $format}} + document.getElementById("{{printf `APIElement%d_%s` $index $randID}}").innerHTML = `{{unescJS (trim $fmtStr)}}` {{end}} } request.send() diff --git a/resources/templates/cards/status.html b/resources/templates/cards/status.html index 8bc8f40..6ff4a0c 100644 --- a/resources/templates/cards/status.html +++ b/resources/templates/cards/status.html @@ -1,3 +1,4 @@ +{{- $randID := randAlphaNum 10 -}}
{{if ne .Icon ""}} @@ -8,7 +9,7 @@

Status

-

Loading...

+

Loading...

@@ -26,13 +27,13 @@ request.onload = function () { var data = JSON.parse(this.response) if (data.down === true || parseInt(data.code) > 500 && parseInt(data.code) < 600 ) { - document.getElementById('{{.Title}}Status').classList.remove("is-warning") - document.getElementById('{{.Title}}Status').classList.add("is-danger") - document.getElementById('{{.Title}}Status').innerHTML = "Offline" + document.getElementById('StatusTag_{{$randID}}').classList.remove("is-warning") + document.getElementById('StatusTag_{{$randID}}').classList.add("is-danger") + document.getElementById('StatusTag_{{$randID}}').innerHTML = "Offline" } else { - document.getElementById('{{.Title}}Status').classList.remove("is-warning") - document.getElementById('{{.Title}}Status').classList.add("is-success") - document.getElementById('{{.Title}}Status').innerHTML = "Online" + document.getElementById('StatusTag_{{$randID}}').classList.remove("is-warning") + document.getElementById('StatusTag_{{$randID}}').classList.add("is-success") + document.getElementById('StatusTag_{{$randID}}').innerHTML = "Online" } } request.send() diff --git a/resources/templates/cards/weather.html b/resources/templates/cards/weather.html index 177c1a0..94f5cf6 100644 --- a/resources/templates/cards/weather.html +++ b/resources/templates/cards/weather.html @@ -1,43 +1,44 @@ +{{$randID := randAlphaNum 10}}

{{.Title}}

-

Loading...

+

Loading...

- +
-

+

-

-

-

-

-

-

-

+

+

+

+

+

+

+

diff --git a/template.go b/template.go index 87f8cdb..496de4a 100644 --- a/template.go +++ b/template.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "fmt" "html/template" + "regexp" ) // Function to dynamically execute template and return results @@ -33,6 +34,14 @@ func unescapeJS(s string) template.JS { return template.JS(s) } +// Remove all non-alphanumeric characters +func toAlphaNum(s string) string { + // Create regex matching everything but alphanumeric + regex := regexp.MustCompile(`[^a-zA-Z0-9]+`) + // Remove all matched characters in string, then return + return regex.ReplaceAllString(s, "") +} + // Function to get template function map func getFuncMap() template.FuncMap { // Return function map with template functions @@ -40,5 +49,6 @@ func getFuncMap() template.FuncMap { "dyn_template": dynamicTemplate, "proxy": wrapProxy, "unescJS": unescapeJS, - } + "toAlphaNum": toAlphaNum, + } }