Notes/main.go

673 lines
14 KiB
Go
Raw Normal View History

2023-06-15 07:24:23 +02:00
package main
import (
// Standard
2023-06-21 23:52:21 +02:00
"crypto/md5"
"encoding/hex"
"path/filepath"
2023-06-15 07:24:23 +02:00
"flag"
"fmt"
"html/template"
2023-06-21 23:52:21 +02:00
"io"
2023-06-15 07:24:23 +02:00
"log"
"net/http"
"os"
"path"
"regexp"
"strings"
2023-06-22 06:52:27 +02:00
"strconv"
2023-06-17 09:11:14 +02:00
"time"
2023-06-18 22:05:10 +02:00
_ "embed"
2023-06-15 07:24:23 +02:00
)
2023-07-20 07:27:44 +02:00
const VERSION = "v10";
2023-06-15 07:24:23 +02:00
const LISTEN_HOST = "0.0.0.0";
2023-07-20 07:27:29 +02:00
const DB_SCHEMA = 12
2023-06-15 07:24:23 +02:00
var (
flagPort int
connectionManager ConnectionManager
static http.Handler
config Config
)
func init() {// {{{
flag.IntVar(
&flagPort,
"port",
1371,
"TCP port to listen on",
)
flag.Parse()
2023-06-17 09:11:14 +02:00
if false {
time.Sleep(time.Second*1)
}
2023-06-15 07:24:23 +02:00
}// }}}
func main() {// {{{
var err error
log.Printf("\x1b[32mNotes\x1b[0m %s\n", VERSION)
config, err = ConfigRead(os.Getenv("HOME")+"/.config/notes.yaml")
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
2023-06-18 22:05:10 +02:00
if err = dbInit(); err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
2023-06-15 07:24:23 +02:00
connectionManager = NewConnectionManager()
go connectionManager.BroadcastLoop()
2023-06-21 23:52:21 +02:00
static = http.FileServer(http.Dir(config.Application.Directories.Static))
2023-06-15 07:24:23 +02:00
http.HandleFunc("/css_updated", cssUpdateHandler)
http.HandleFunc("/session/create", sessionCreate)
2023-06-16 07:11:27 +02:00
http.HandleFunc("/session/retrieve", sessionRetrieve)
http.HandleFunc("/session/authenticate", sessionAuthenticate)
http.HandleFunc("/node/tree", nodeTree)
2023-06-17 09:11:14 +02:00
http.HandleFunc("/node/retrieve", nodeRetrieve)
2023-06-18 20:13:35 +02:00
http.HandleFunc("/node/create", nodeCreate)
http.HandleFunc("/node/update", nodeUpdate)
2023-06-18 22:05:10 +02:00
http.HandleFunc("/node/rename", nodeRename)
http.HandleFunc("/node/delete", nodeDelete)
2023-06-21 23:52:21 +02:00
http.HandleFunc("/node/upload", nodeUpload)
2023-06-22 16:48:31 +02:00
http.HandleFunc("/node/download", nodeDownload)
2023-07-19 10:00:36 +02:00
http.HandleFunc("/node/search", nodeSearch)
2023-07-12 22:35:38 +02:00
http.HandleFunc("/key/retrieve", keyRetrieve)
http.HandleFunc("/key/create", keyCreate)
http.HandleFunc("/key/counter", keyCounter)
2023-06-15 07:24:23 +02:00
http.HandleFunc("/ws", websocketHandler)
http.HandleFunc("/", staticHandler)
listen := fmt.Sprintf("%s:%d", LISTEN_HOST, flagPort)
log.Printf("\x1b[32mNotes\x1b[0m Listening on %s\n", listen)
log.Printf("\x1b[32mNotes\x1b[0m Answer for domains %s\n", strings.Join(config.Websocket.Domains, ", "))
http.ListenAndServe(listen, nil)
}// }}}
func cssUpdateHandler(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("[BROADCAST] CSS updated")
connectionManager.Broadcast(struct{ Ok bool; ID string; Op string }{ Ok: true, Op: "css_reload" })
}// }}}
2023-06-17 09:11:14 +02:00
func websocketHandler(w http.ResponseWriter, r *http.Request) {// {{{
var err error
_, err = connectionManager.NewConnection(w, r)
if err != nil {
log.Printf("[Connection] %s\n", err)
return
}
}// }}}
func staticHandler(w http.ResponseWriter, r *http.Request) {// {{{
data := struct{
VERSION string
}{
VERSION: VERSION,
}
// URLs with pattern /(css|images)/v1.0.0/foobar are stripped of the version.
// To get rid of problems with cached content in browser on a new version release,
// while also not disabling cache altogether.
log.Printf("static: %s", r.URL.Path)
if r.URL.Path == "/favicon.ico" {
static.ServeHTTP(w, r)
return
}
rxp := regexp.MustCompile("^/(css|images|js|fonts)/v[0-9]+/(.*)$")
2023-06-17 09:11:14 +02:00
if comp := rxp.FindStringSubmatch(r.URL.Path); comp != nil {
r.URL.Path = fmt.Sprintf("/%s/%s", comp[1], comp[2])
static.ServeHTTP(w, r)
return
}
// Everything else is run through the template system.
// For now to get VERSION into files to fix caching.
log.Printf("template: %s", r.URL.Path)
tmpl, err := newTemplate(r.URL.Path)
if err != nil {
if os.IsNotExist(err) {
w.WriteHeader(404)
}
w.Write([]byte(err.Error()))
return
}
if err = tmpl.Execute(w, data); err != nil {
w.Write([]byte(err.Error()))
}
}// }}}
2023-06-15 07:24:23 +02:00
func sessionCreate(w http.ResponseWriter, r *http.Request) {// {{{
2023-06-17 09:11:14 +02:00
log.Println("/session/create")
session, err := CreateSession()
2023-06-15 07:24:23 +02:00
if err != nil {
2023-06-15 14:12:35 +02:00
responseError(w, err)
2023-06-16 07:11:27 +02:00
return
2023-06-15 07:24:23 +02:00
}
2023-06-15 14:12:35 +02:00
responseData(w, map[string]interface{}{
"OK": true,
"Session": session,
})
2023-06-15 07:24:23 +02:00
}// }}}
2023-06-16 07:11:27 +02:00
func sessionRetrieve(w http.ResponseWriter, r *http.Request) {// {{{
2023-06-17 09:11:14 +02:00
log.Println("/session/retrieve")
2023-06-16 07:11:27 +02:00
var err error
2023-06-17 09:11:14 +02:00
var found bool
var session Session
2023-06-16 07:11:27 +02:00
2023-06-17 09:11:14 +02:00
if session, found, err = ValidateSession(r, false); err != nil {
2023-06-16 07:11:27 +02:00
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
2023-06-17 09:11:14 +02:00
"Valid": found,
2023-06-16 07:11:27 +02:00
"Session": session,
})
}// }}}
func sessionAuthenticate(w http.ResponseWriter, r *http.Request) {// {{{
2023-06-17 09:11:14 +02:00
log.Println("/session/authenticate")
2023-06-16 07:11:27 +02:00
var err error
2023-06-17 09:11:14 +02:00
var session Session
var authenticated bool
2023-06-16 07:11:27 +02:00
2023-06-17 09:11:14 +02:00
// Validate session
if session, _, err = ValidateSession(r, true); err != nil {
2023-06-16 07:11:27 +02:00
responseError(w, err)
return
}
req := struct{ Username string; Password string }{}
2023-06-17 09:11:14 +02:00
if err = parseRequest(r, &req); err != nil {
2023-06-16 07:11:27 +02:00
responseError(w, err)
return
}
2023-06-17 09:11:14 +02:00
if authenticated, err = session.Authenticate(req.Username, req.Password); err != nil {
2023-06-16 07:11:27 +02:00
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
2023-06-17 09:11:14 +02:00
"Authenticated": authenticated,
2023-06-16 07:11:27 +02:00
"Session": session,
})
}// }}}
2023-06-17 09:11:14 +02:00
func nodeTree(w http.ResponseWriter, r *http.Request) {// {{{
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct { StartNodeID int }{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
nodes, err := session.NodeTree(req.StartNodeID)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Nodes": nodes,
})
}// }}}
2023-06-17 09:11:14 +02:00
func nodeRetrieve(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/node/retrieve")
2023-06-15 07:24:23 +02:00
var err error
2023-06-17 09:11:14 +02:00
var session Session
2023-06-15 07:24:23 +02:00
2023-06-17 09:11:14 +02:00
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
2023-06-15 07:24:23 +02:00
return
}
2023-06-17 09:11:14 +02:00
req := struct { ID int }{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
2023-06-15 07:24:23 +02:00
return
}
2023-06-17 09:11:14 +02:00
node, err := session.Node(req.ID)
2023-06-15 07:24:23 +02:00
if err != nil {
2023-06-17 09:11:14 +02:00
responseError(w, err)
2023-06-15 07:24:23 +02:00
return
}
2023-06-17 09:11:14 +02:00
responseData(w, map[string]interface{}{
"OK": true,
"Node": node,
})
2023-06-15 07:24:23 +02:00
}// }}}
2023-06-18 20:13:35 +02:00
func nodeCreate(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/node/create")
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
Name string
ParentID int
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
node, err := session.CreateNode(req.ParentID, req.Name)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Node": node,
})
}// }}}
func nodeUpdate(w http.ResponseWriter, r *http.Request) {// {{{
2023-07-14 16:17:37 +02:00
log.Println("/node/update")
2023-06-18 20:13:35 +02:00
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
NodeID int
Content string
2023-07-12 22:35:38 +02:00
CryptoKeyID int
2023-06-18 20:13:35 +02:00
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
2023-07-12 22:35:38 +02:00
err = session.UpdateNode(req.NodeID, req.Content, req.CryptoKeyID)
2023-06-18 20:13:35 +02:00
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
})
}// }}}
2023-06-18 22:05:10 +02:00
func nodeRename(w http.ResponseWriter, r *http.Request) {// {{{
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
NodeID int
Name string
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
err = session.RenameNode(req.NodeID, req.Name)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
})
}// }}}
func nodeDelete(w http.ResponseWriter, r *http.Request) {// {{{
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
NodeID int
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
err = session.DeleteNode(req.NodeID)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
})
}// }}}
2023-06-21 23:52:21 +02:00
func nodeUpload(w http.ResponseWriter, r *http.Request) {// {{{
2023-06-22 06:52:27 +02:00
log.Println("/node/upload")
2023-06-21 23:52:21 +02:00
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 10 MB files.
2023-06-22 06:52:27 +02:00
r.Body = http.MaxBytesReader(w, r.Body, 128<<20+512)
r.ParseMultipartForm(128 << 20)
2023-06-21 23:52:21 +02:00
// FormFile returns the first file for the given key `myFile`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("file")
if err != nil {
responseError(w, err)
return
}
defer file.Close()
2023-06-22 06:52:27 +02:00
// Read metadata of file for database, and also file contents
// for MD5, which is used to store the file on disk.
2023-06-21 23:52:21 +02:00
fileBytes, err := io.ReadAll(file)
if err != nil {
responseError(w, err)
return
}
md5sumBytes := md5.Sum(fileBytes)
md5sum := hex.EncodeToString(md5sumBytes[:])
2023-06-22 06:52:27 +02:00
var nodeID int
if nodeID, err = strconv.Atoi(r.PostFormValue("NodeID")); err != nil {
responseError(w, err)
return
}
2023-06-21 23:52:21 +02:00
nodeFile := File{
2023-06-22 06:52:27 +02:00
NodeID: nodeID,
2023-06-21 23:52:21 +02:00
Filename: handler.Filename,
Size: handler.Size,
MIME: handler.Header.Get("Content-Type"),
MD5: md5sum,
}
if err = session.AddFile(&nodeFile); err != nil {
responseError(w, err)
return
}
2023-06-22 06:52:27 +02:00
// Files are stored in a directory structure composed of
// the first three characters in the md5sum, which is statistically
// distributed by design, making sure there aren't too many files in
// a single directory.
2023-06-21 23:52:21 +02:00
path := filepath.Join(
config.Application.Directories.Upload,
md5sum[0:1],
md5sum[1:2],
md5sum[2:3],
)
if err = os.MkdirAll(path, 0755); err != nil {
responseError(w, err)
return
}
path = filepath.Join(
config.Application.Directories.Upload,
md5sum[0:1],
md5sum[1:2],
md5sum[2:3],
md5sum,
)
if err = os.WriteFile(path, fileBytes, 0644); err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
2023-06-22 06:52:27 +02:00
"File": nodeFile,
})
}// }}}
2023-06-22 16:48:31 +02:00
func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/node/download")
var err error
var session Session
var files []File
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
NodeID int
FileID int
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
files, err = session.Files(req.NodeID, req.FileID)
if err != nil {
responseError(w, err)
return
}
if len(files) != 1 {
responseError(w, fmt.Errorf("File not found"))
return
}
var file *os.File
fname := filepath.Join(
config.Application.Directories.Upload,
files[0].MD5[0:1],
files[0].MD5[1:2],
files[0].MD5[2:3],
files[0].MD5,
)
file, err = os.Open(fname)
if err != nil {
responseError(w, err)
return
}
var finfo os.FileInfo
finfo, err = file.Stat()
if err != nil {
responseError(w, err)
return
}
2023-06-22 17:42:34 +02:00
w.Header().Add("Content-Type", files[0].MIME)
2023-06-27 14:44:36 +02:00
w.Header().Add("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, files[0].Filename))
2023-06-22 17:42:34 +02:00
w.Header().Add("Content-Length", strconv.Itoa(int(finfo.Size())))
2023-06-22 16:48:31 +02:00
read := 1
var buf []byte
for read > 0 {
buf = make([]byte, 65536)
read, err = file.Read(buf)
if read > 0 {
w.Write(buf[0:read])
}
}
}// }}}
2023-06-22 06:52:27 +02:00
func nodeFiles(w http.ResponseWriter, r *http.Request) {// {{{
var err error
var session Session
var files []File
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
NodeID int
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
2023-06-22 16:48:31 +02:00
files, err = session.Files(req.NodeID, 0)
2023-06-22 06:52:27 +02:00
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Files": files,
2023-06-21 23:52:21 +02:00
})
}// }}}
2023-07-19 10:00:36 +02:00
func nodeSearch(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/node/search")
var err error
var session Session
var nodes []Node
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
Search string
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
nodes, err = session.SearchNodes(req.Search)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Nodes": nodes,
})
}// }}}
2023-06-15 07:24:23 +02:00
2023-07-01 20:33:26 +02:00
func keyRetrieve(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/key/retrieve")
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
keys, err := session.Keys()
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Keys": keys,
})
}// }}}
2023-07-12 22:35:38 +02:00
func keyCreate(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/key/create")
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
req := struct {
Description string
Key string
}{}
if err = parseRequest(r, &req); err != nil {
responseError(w, err)
return
}
key, err := session.KeyCreate(req.Description, req.Key)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Key": key,
})
}// }}}
func keyCounter(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/key/counter")
var err error
var session Session
if session, _, err = ValidateSession(r, true); err != nil {
responseError(w, err)
return
}
counter, err := session.KeyCounter()
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
// Javascript uses int32, thus getting a string counter for Javascript BigInt to parse.
"Counter": strconv.FormatInt(counter, 10),
})
}// }}}
2023-07-01 20:33:26 +02:00
2023-06-15 07:24:23 +02:00
func newTemplate(requestPath string) (tmpl *template.Template, err error) {// {{{
// Append index.html if needed for further reading of the file
p := requestPath
if p[len(p)-1] == '/' {
p += "index.html"
}
2023-06-21 23:52:21 +02:00
p = config.Application.Directories.Static + p
2023-06-15 07:24:23 +02:00
base := path.Base(p)
if tmpl, err = template.New(base).ParseFiles(p); err != nil { return }
return
}// }}}
// vim: foldmethod=marker