From 5ae5c919451b16fe5bc09cf8d77988760c71002d Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 2 Nov 2017 06:27:39 -0600 Subject: [PATCH 1/4] Trying to make a /read --- Makefile | 5 +++++ handlers.go | 18 ++++++++++++++---- templates/index.tmpl | 37 ++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 401ad47..81f69cb 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,11 @@ build: go-bindata static/... templates/... go build ${LDFLAGS} +.PHONY: quick +quick: + go-bindata static/... templates/... + go build + .PHONY: linuxarm linuxarm: env GOOS=linux GOARCH=arm go build ${LDFLAGS} -o dist/cowyo_linux_arm diff --git a/handlers.go b/handlers.go index f8193cc..70e38ed 100755 --- a/handlers.go +++ b/handlers.go @@ -149,6 +149,7 @@ func generateSiteMap() (sitemap string) { sitemap += "" return } + func handlePageRequest(c *gin.Context) { page := c.Param("page") command := c.Param("command") @@ -174,13 +175,19 @@ func handlePageRequest(c *gin.Context) { c.Data(http.StatusOK, contentType(filename), data) return } + p := Open(page) + fmt.Println(command) if len(command) < 2 { - c.Redirect(302, "/"+page+"/edit") + fmt.Println(p.IsPublished) + if p.IsPublished { + c.Redirect(302, "/"+page+"/read") + } else { + c.Redirect(302, "/"+page+"/edit") + } return } version := c.DefaultQuery("version", "ajksldfjl") - p := Open(page) // Disallow anything but viewing locked/encrypted pages if (p.IsEncrypted || p.IsLocked) && @@ -232,7 +239,7 @@ func handlePageRequest(c *gin.Context) { versionsChangeSums = reverseSliceInt(versionsChangeSums) } - if command[0:2] == "/r" { + if command[0:3] == "/ra" { c.Writer.Header().Set("Content-Type", contentType(p.Name)) c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Max-Age", "86400") @@ -246,7 +253,8 @@ func handlePageRequest(c *gin.Context) { log.Debug("%v", command[0:2] != "/e" && command[0:2] != "/v" && command[0:2] != "/l" && - command[0:2] != "/h") + command[0:2] != "/h" && + command[0:2] != "/r") var FileNames, FileLastEdited []string var FileSizes, FileNumChanges []int @@ -260,6 +268,7 @@ func handlePageRequest(c *gin.Context) { "ViewPage": command[0:2] == "/v", // /view "ListPage": command[0:2] == "/l", // /list "HistoryPage": command[0:2] == "/h", // /history + "ReadPage": command[0:2] == "/r", // /history "DontKnowPage": command[0:2] != "/e" && command[0:2] != "/v" && command[0:2] != "/l" && @@ -282,6 +291,7 @@ func handlePageRequest(c *gin.Context) { "HasDotInName": strings.Contains(page, "."), "RecentlyEdited": getRecentlyEdited(page, c), "IsPublished": p.IsPublished, + "CustomCSS": "", }) } diff --git a/templates/index.tmpl b/templates/index.tmpl index d2ada86..314a417 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -22,15 +22,17 @@ - - - - - - - +{{ if and .CustomCSS .IsPublished }} +{{ else }} + + + + + + + - - + +{{ end }} {{ .Page }} @@ -478,9 +479,18 @@ body#pad textarea { -
+ + {{ if .ReadPage }} +
+
+ {{ .RenderedPage }} +
+
+ {{ else }} + + -
+{{ end }} + From 2040dbaaa5f662e029eef77a2663160f65871030 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 4 Nov 2017 04:18:05 -0600 Subject: [PATCH 2/4] Add -css option for custom CSS on the /read page --- handlers.go | 33 ++++++++++++++++++++++++++++----- main.go | 7 ++++++- templates/index.tmpl | 3 ++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/handlers.go b/handlers.go index 70e38ed..445c7c6 100755 --- a/handlers.go +++ b/handlers.go @@ -17,7 +17,9 @@ import ( "github.com/schollz/cowyo/encrypt" ) -func serve(host, port, crt_path, key_path string, TLS bool) { +var customCSS []byte + +func serve(host, port, crt_path, key_path string, TLS bool, cssFile string) { gin.SetMode(gin.ReleaseMode) router := gin.Default() store := sessions.NewCookieStore([]byte("secret")) @@ -45,6 +47,17 @@ func serve(host, port, crt_path, key_path string, TLS bool) { // start long-processes as threads go thread_SiteMap() + // collect custom CSS + if len(cssFile) > 0 { + var errRead error + customCSS, errRead = ioutil.ReadFile(cssFile) + if errRead != nil { + fmt.Println(errRead.Error()) + return + } + fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS)) + } + if TLS { http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) } else { @@ -166,11 +179,21 @@ func handlePageRequest(c *gin.Context) { data, _ := Asset("/static/img/cowyo/favicon.ico") c.Data(http.StatusOK, contentType("/static/img/cowyo/favicon.ico"), data) return + } else if page == "/static/css/custom.css" { + c.Data(http.StatusOK, contentType("custom.css"), customCSS) + return } else if page == "static" { filename := page + command - data, err := Asset(filename) - if err != nil { - c.String(http.StatusInternalServerError, "Could not find data") + var data []byte + fmt.Println(filename) + if filename == "static/css/custom.css" { + data = customCSS + } else { + var errAssset error + data, errAssset = Asset(filename) + if errAssset != nil { + c.String(http.StatusInternalServerError, "Could not find data") + } } c.Data(http.StatusOK, contentType(filename), data) return @@ -291,7 +314,7 @@ func handlePageRequest(c *gin.Context) { "HasDotInName": strings.Contains(page, "."), "RecentlyEdited": getRecentlyEdited(page, c), "IsPublished": p.IsPublished, - "CustomCSS": "", + "CustomCSS": len(customCSS) > 0, }) } diff --git a/main.go b/main.go index 54fb1cd..1c46a93 100755 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } else { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS) + serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css")) return nil } app.Flags = []cli.Flag{ @@ -72,6 +72,11 @@ func main() { Value: "", Usage: "absolute path to SSL private key", }, + cli.StringFlag{ + Name: "css", + Value: "", + Usage: "use a custom CSS file", + }, cli.BoolFlag{ Name: "debug, d", Usage: "turn on debugging", diff --git a/templates/index.tmpl b/templates/index.tmpl index 314a417..287f324 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -22,7 +22,8 @@ -{{ if and .CustomCSS .IsPublished }} +{{ if and .CustomCSS .ReadPage }} + {{ else }} From d481145c5f71ab9ce4bcb886569b6ab124fa845b Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 4 Nov 2017 04:21:51 -0600 Subject: [PATCH 3/4] Add default page Fixes #91 --- handlers.go | 8 ++++++-- main.go | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 445c7c6..6020a59 100755 --- a/handlers.go +++ b/handlers.go @@ -19,7 +19,7 @@ import ( var customCSS []byte -func serve(host, port, crt_path, key_path string, TLS bool, cssFile string) { +func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string) { gin.SetMode(gin.ReleaseMode) router := gin.Default() store := sessions.NewCookieStore([]byte("secret")) @@ -27,7 +27,11 @@ func serve(host, port, crt_path, key_path string, TLS bool, cssFile string) { router.HTMLRender = loadTemplates("index.tmpl") // router.Use(static.Serve("/static/", static.LocalFile("./static", true))) router.GET("/", func(c *gin.Context) { - c.Redirect(302, "/"+randomAlliterateCombo()) + if defaultPage != "" { + c.Redirect(302, "/"+defaultPage+"/read") + } else { + c.Redirect(302, "/"+randomAlliterateCombo()) + } }) router.GET("/:page", func(c *gin.Context) { page := c.Param("page") diff --git a/main.go b/main.go index 1c46a93..4bbe4b3 100755 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } else { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css")) + serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css"), c.GlobalString("default-page")) return nil } app.Flags = []cli.Flag{ @@ -77,6 +77,11 @@ func main() { Value: "", Usage: "use a custom CSS file", }, + cli.StringFlag{ + Name: "default-page", + Value: "", + Usage: "show default-page/read instead of editing", + }, cli.BoolFlag{ Name: "debug, d", Usage: "turn on debugging", From de03d2b54737943c38c6c7e2f3eaa523a2b4d823 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 4 Nov 2017 04:41:56 -0600 Subject: [PATCH 4/4] Add -lock for automatic locking Fixes #90 --- handlers.go | 21 ++++++++++++++++++++- main.go | 9 +++++++-- page.go | 4 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 6020a59..1ff50be 100755 --- a/handlers.go +++ b/handlers.go @@ -18,8 +18,9 @@ import ( ) var customCSS []byte +var defaultLock string -func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string) { +func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string, defaultPassword string) { gin.SetMode(gin.ReleaseMode) router := gin.Default() store := sessions.NewCookieStore([]byte("secret")) @@ -62,6 +63,11 @@ func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defa fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS)) } + if defaultPassword != "" { + fmt.Println("running with locked pages") + defaultLock = HashPassword(defaultPassword) + } + if TLS { http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) } else { @@ -216,9 +222,16 @@ func handlePageRequest(c *gin.Context) { version := c.DefaultQuery("version", "ajksldfjl") + // use the default lock + if defaultLock != "" && p.IsNew() { + p.IsLocked = true + p.PassphraseToUnlock = defaultLock + } + // Disallow anything but viewing locked/encrypted pages if (p.IsEncrypted || p.IsLocked) && (command[0:2] != "/v" && command[0:2] != "/r") { + fmt.Println("IS LOCKED") c.Redirect(302, "/"+page+"/view") return } @@ -456,6 +469,11 @@ func handleLock(c *gin.Context) { return } p := Open(json.Page) + if defaultLock != "" && p.IsNew() { + p.IsLocked = true + p.PassphraseToUnlock = defaultLock + } + if p.IsEncrypted { c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"}) return @@ -474,6 +492,7 @@ func handleLock(c *gin.Context) { p.PassphraseToUnlock = HashPassword(json.Passphrase) message = "Locked" } + fmt.Println(p) p.Save() c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) } diff --git a/main.go b/main.go index 4bbe4b3..b884339 100755 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } else { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css"), c.GlobalString("default-page")) + serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css"), c.GlobalString("default-page"), c.GlobalString("lock")) return nil } app.Flags = []cli.Flag{ @@ -80,7 +80,12 @@ func main() { cli.StringFlag{ Name: "default-page", Value: "", - Usage: "show default-page/read instead of editing", + Usage: "show default-page/read instead of editing (default: show random editing)", + }, + cli.StringFlag{ + Name: "lock", + Value: "", + Usage: "password to lock editing all files (default: all pages unlocked)", }, cli.BoolFlag{ Name: "debug, d", diff --git a/page.go b/page.go index 89fa832..5cc1fd1 100755 --- a/page.go +++ b/page.go @@ -102,6 +102,10 @@ func (p *Page) Save() error { return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644) } +func (p *Page) IsNew() bool { + return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json")) +} + func (p *Page) Erase() error { log.Trace("Erasing " + p.Name) return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))