2017-03-22 06:51:52 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2017-03-22 20:19:39 +03:00
|
|
|
"strings"
|
2017-03-22 17:16:16 +03:00
|
|
|
"time"
|
2017-03-22 06:51:52 +03:00
|
|
|
|
2017-03-22 22:03:00 +03:00
|
|
|
// "github.com/gin-contrib/static"
|
|
|
|
"github.com/gin-contrib/multitemplate"
|
2017-03-22 06:51:52 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2017-03-22 17:26:34 +03:00
|
|
|
func serve(port string) {
|
2017-03-22 22:19:00 +03:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2017-03-22 06:51:52 +03:00
|
|
|
router := gin.Default()
|
2017-03-22 22:03:00 +03:00
|
|
|
router.HTMLRender = loadTemplates("index.tmpl")
|
|
|
|
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
|
2017-03-22 06:51:52 +03:00
|
|
|
router.GET("/", func(c *gin.Context) {
|
|
|
|
c.Redirect(302, "/"+randomAlliterateCombo())
|
|
|
|
})
|
|
|
|
router.GET("/:page", func(c *gin.Context) {
|
|
|
|
page := c.Param("page")
|
|
|
|
c.Redirect(302, "/"+page+"/edit")
|
|
|
|
})
|
2017-03-22 22:03:00 +03:00
|
|
|
router.GET("/:page/*command", handlePageRequest)
|
2017-03-22 06:51:52 +03:00
|
|
|
router.POST("/update", handlePageUpdate)
|
|
|
|
router.POST("/prime", handlePrime)
|
|
|
|
router.POST("/lock", handleLock)
|
|
|
|
router.POST("/encrypt", handleEncrypt)
|
2017-03-22 20:19:39 +03:00
|
|
|
router.DELETE("/listitem", deleteListItem)
|
2017-03-22 06:51:52 +03:00
|
|
|
|
2017-03-22 17:26:34 +03:00
|
|
|
router.Run(":" + port)
|
2017-03-22 06:51:52 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 22:03:00 +03:00
|
|
|
func loadTemplates(list ...string) multitemplate.Render {
|
|
|
|
r := multitemplate.New()
|
|
|
|
|
|
|
|
for _, x := range list {
|
|
|
|
templateString, err := Asset("templates/" + x)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmplMessage, err := template.New(x).Parse(string(templateString))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Add(x, tmplMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-03-22 06:51:52 +03:00
|
|
|
func handlePageRequest(c *gin.Context) {
|
|
|
|
page := c.Param("page")
|
|
|
|
command := c.Param("command")
|
2017-03-22 22:03:00 +03:00
|
|
|
|
|
|
|
// Serve static content from memory
|
|
|
|
if page == "static" {
|
|
|
|
filename := page + command
|
|
|
|
data, err := Asset(filename)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Could not find data")
|
|
|
|
}
|
|
|
|
c.Data(http.StatusOK, contentType(filename), data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-22 06:51:52 +03:00
|
|
|
version := c.DefaultQuery("version", "ajksldfjl")
|
|
|
|
p := Open(page)
|
2017-03-22 17:09:09 +03:00
|
|
|
if p.IsPrimedForSelfDestruct && !p.IsLocked && !p.IsEncrypted {
|
2017-03-22 06:51:52 +03:00
|
|
|
p.Update("*This page has now self-destructed.*\n\n" + p.Text.GetCurrent())
|
|
|
|
p.Erase()
|
|
|
|
}
|
2017-03-22 22:42:51 +03:00
|
|
|
if command == "/erase" {
|
|
|
|
if !p.IsLocked && !p.IsEncrypted {
|
|
|
|
p.Erase()
|
|
|
|
c.Redirect(302, "/"+page+"/edit")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.Redirect(302, "/"+page+"/view")
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 06:51:52 +03:00
|
|
|
}
|
|
|
|
rawText := p.Text.GetCurrent()
|
|
|
|
rawHTML := p.RenderedPage
|
|
|
|
|
|
|
|
// Check to see if an old version is requested
|
|
|
|
versionInt, versionErr := strconv.Atoi(version)
|
|
|
|
if versionErr == nil && versionInt > 0 {
|
|
|
|
versionText, err := p.Text.GetPreviousByTimestamp(int64(versionInt))
|
|
|
|
if err == nil {
|
|
|
|
rawText = versionText
|
2017-03-22 17:09:09 +03:00
|
|
|
rawHTML = GithubMarkdownToHTML(rawText)
|
2017-03-22 06:51:52 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 16:22:14 +03:00
|
|
|
versionsInt64 := p.Text.GetMajorSnapshots(60) // get snapshots 60 seconds apart
|
2017-03-22 17:16:16 +03:00
|
|
|
versionsText := make([]string, len(versionsInt64))
|
|
|
|
for i, v := range versionsInt64 {
|
|
|
|
versionsText[i] = time.Unix(v/1000000000, 0).String()
|
|
|
|
}
|
2017-03-22 17:21:49 +03:00
|
|
|
|
2017-03-22 22:03:00 +03:00
|
|
|
if command == "/raw" {
|
2017-03-22 17:21:49 +03:00
|
|
|
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")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
c.Data(200, contentType(p.Name), []byte(rawText))
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 22:03:00 +03:00
|
|
|
log.Debug(command)
|
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
"EditPage": command == "/edit",
|
|
|
|
"ViewPage": command == "/view",
|
|
|
|
"ListPage": command == "/list",
|
|
|
|
"HistoryPage": command == "/history",
|
2017-03-22 06:51:52 +03:00
|
|
|
"Page": p.Name,
|
|
|
|
"RenderedPage": template.HTML([]byte(rawHTML)),
|
|
|
|
"RawPage": rawText,
|
2017-03-22 17:16:16 +03:00
|
|
|
"Versions": versionsInt64,
|
|
|
|
"VersionsText": versionsText,
|
2017-03-22 06:51:52 +03:00
|
|
|
"IsLocked": p.IsLocked,
|
|
|
|
"IsEncrypted": p.IsEncrypted,
|
2017-03-22 20:19:39 +03:00
|
|
|
"ListItems": renderList(rawText),
|
2017-03-22 06:51:52 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePageUpdate(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
NewText string `json:"new_text"`
|
|
|
|
}
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
2017-03-22 17:21:49 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(json.NewText) > 100000 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Too much"})
|
2017-03-22 06:51:52 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("Update: %v", json)
|
|
|
|
p := Open(json.Page)
|
|
|
|
var message string
|
|
|
|
if p.IsLocked {
|
|
|
|
message = "Locked"
|
|
|
|
} else if p.IsEncrypted {
|
|
|
|
message = "Encrypted"
|
|
|
|
} else {
|
|
|
|
p.Update(json.NewText)
|
|
|
|
p.Save()
|
|
|
|
message = "Saved"
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": message})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePrime(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
}
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("Update: %v", json)
|
|
|
|
p := Open(json.Page)
|
2017-03-22 17:09:09 +03:00
|
|
|
if p.IsLocked {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
|
|
|
|
return
|
|
|
|
} else if p.IsEncrypted {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 06:51:52 +03:00
|
|
|
p.IsPrimedForSelfDestruct = true
|
|
|
|
p.Save()
|
2017-03-22 17:09:09 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Primed"})
|
2017-03-22 06:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleLock(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
Passphrase string `json:"passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
2017-03-22 22:42:51 +03:00
|
|
|
if p.IsEncrypted {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 06:51:52 +03:00
|
|
|
var message string
|
|
|
|
if p.IsLocked {
|
|
|
|
err2 := CheckPasswordHash(json.Passphrase, p.PassphraseToUnlock)
|
|
|
|
if err2 != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Can't unlock"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.IsLocked = false
|
|
|
|
message = "Unlocked"
|
|
|
|
} else {
|
|
|
|
p.IsLocked = true
|
|
|
|
p.PassphraseToUnlock = HashPassword(json.Passphrase)
|
|
|
|
message = "Locked"
|
|
|
|
}
|
|
|
|
p.Save()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleEncrypt(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
Passphrase string `json:"passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
2017-03-22 22:37:59 +03:00
|
|
|
if p.IsLocked {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 17:09:09 +03:00
|
|
|
q := Open(json.Page)
|
2017-03-22 06:51:52 +03:00
|
|
|
var message string
|
|
|
|
if p.IsEncrypted {
|
|
|
|
decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase)
|
|
|
|
if err2 != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"})
|
|
|
|
return
|
|
|
|
}
|
2017-03-22 17:09:09 +03:00
|
|
|
q.Erase()
|
|
|
|
q = Open(json.Page)
|
|
|
|
q.Update(decrypted)
|
|
|
|
q.IsEncrypted = false
|
|
|
|
q.IsLocked = p.IsLocked
|
|
|
|
q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct
|
2017-03-22 06:51:52 +03:00
|
|
|
message = "Decrypted"
|
|
|
|
} else {
|
|
|
|
currentText := p.Text.GetCurrent()
|
|
|
|
encrypted, _ := EncryptString(currentText, json.Passphrase)
|
2017-03-22 17:09:09 +03:00
|
|
|
q.Erase()
|
|
|
|
q = Open(json.Page)
|
|
|
|
q.Update(encrypted)
|
|
|
|
q.IsEncrypted = true
|
|
|
|
q.IsLocked = p.IsLocked
|
|
|
|
q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct
|
2017-03-22 06:51:52 +03:00
|
|
|
message = "Encrypted"
|
|
|
|
}
|
2017-03-22 17:09:09 +03:00
|
|
|
q.Save()
|
2017-03-22 06:51:52 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
|
|
|
}
|
2017-03-22 20:19:39 +03:00
|
|
|
|
|
|
|
func deleteListItem(c *gin.Context) {
|
|
|
|
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
|
|
|
|
page := c.Query("page") // shortcut for c.Request.URL.Query().Get("lastname")
|
|
|
|
if err == nil {
|
|
|
|
p := Open(page)
|
|
|
|
|
|
|
|
_, listItems := reorderList(p.Text.GetCurrent())
|
|
|
|
newText := p.Text.GetCurrent()
|
|
|
|
for i, lineString := range listItems {
|
|
|
|
// fmt.Println(i, lineString, lineNum)
|
|
|
|
if i+1 == lineNum {
|
|
|
|
// fmt.Println("MATCHED")
|
|
|
|
if strings.Contains(lineString, "~~") == false {
|
|
|
|
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
|
|
|
|
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+strings.TrimSpace(lineString[2:])+"~~"+"\n", 1)
|
|
|
|
} else {
|
|
|
|
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
|
|
|
|
}
|
|
|
|
p.Update(newText)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "Done.",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|