1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Add -lock for automatic locking

Fixes #90
This commit is contained in:
Zack Scholl 2017-11-04 04:41:56 -06:00
parent d481145c5f
commit de03d2b547
3 changed files with 31 additions and 3 deletions

View File

@ -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})
}

View File

@ -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",

View File

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