mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Added locking
Former-commit-id: 2034389d03058ea5ba79125f8694bbb5e9712ec7 [formerly d1feaa08d9130b4cda3c529de51578f2a5320b61] [formerly 5362d1c477e2aa27c48804c5633880fdc794df95 [formerly e935f561818adc4fed08ab4bf7e399d8e7359629 [formerlyd8052bdb7a
]]] Former-commit-id: 07f6bc6cc11c9147e6a23354df6364c61d90a076 [formerly 27b9029d60a9a9003436a6e1f85332c989db0ce7] Former-commit-id: 6cf5171e6af74ebb249005a931f360476b9e2d4e Former-commit-id:0ac24a9e12
This commit is contained in:
parent
405e9dcf09
commit
a0f8dbf1ff
42
db.go
42
db.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
@ -40,9 +41,46 @@ type CowyoData struct {
|
|||||||
Timestamps []string
|
Timestamps []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CowyoData) load(title string) error {
|
func hasPassword(title string) (bool, error) {
|
||||||
|
title = strings.ToLower(title)
|
||||||
if !open {
|
if !open {
|
||||||
return fmt.Errorf("db must be opened before saving!")
|
return false, fmt.Errorf("db must be opened before loading!")
|
||||||
|
}
|
||||||
|
hasPassword := false
|
||||||
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
var err error
|
||||||
|
b := tx.Bucket([]byte("datas"))
|
||||||
|
if b == nil {
|
||||||
|
return fmt.Errorf("db must be opened before loading!")
|
||||||
|
}
|
||||||
|
k := []byte(title)
|
||||||
|
val := b.Get(k)
|
||||||
|
if val == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var p CowyoData
|
||||||
|
err = p.decode(val)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, line := range strings.Split(p.CurrentText, "\n") {
|
||||||
|
if strings.Contains(line, "<") == true && strings.Contains(line, ">") == true && strings.Contains(line, "user") == true && strings.Contains(line, "password") == true && strings.Contains(line, "public") == true {
|
||||||
|
hasPassword = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Could not get CowyoData: %s", err)
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return hasPassword, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *CowyoData) load(title string) error {
|
||||||
|
title = strings.ToLower(title)
|
||||||
|
if !open {
|
||||||
|
return fmt.Errorf("db must be opened before loading!")
|
||||||
}
|
}
|
||||||
err := db.View(func(tx *bolt.Tx) error {
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
var err error
|
var err error
|
||||||
|
@ -24,12 +24,17 @@ func editNote(c *gin.Context) {
|
|||||||
wshandler(c.Writer, c.Request)
|
wshandler(c.Writer, c.Request)
|
||||||
} else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
|
} else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
|
||||||
c.Redirect(302, "/about/view")
|
c.Redirect(302, "/about/view")
|
||||||
|
} else {
|
||||||
|
locked, _ := hasPassword(title)
|
||||||
|
if locked {
|
||||||
|
c.Redirect(302, "/"+title+"/view")
|
||||||
} else {
|
} else {
|
||||||
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||||
"Title": title,
|
"Title": title,
|
||||||
"ExternalIP": RuntimeArgs.ExternalIP,
|
"ExternalIP": RuntimeArgs.ExternalIP,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func everythingElse(c *gin.Context) {
|
func everythingElse(c *gin.Context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user