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"))