2021-05-17 07:50:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/csv"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-05-17 15:59:48 -06:00
|
|
|
"regexp"
|
2021-05-17 08:01:31 -06:00
|
|
|
"strings"
|
2021-05-17 07:50:45 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
"suah.dev/protect"
|
|
|
|
)
|
|
|
|
|
|
|
|
var twFile = "empty-5.1.23.html"
|
|
|
|
|
|
|
|
//go:embed empty-5.1.23.html
|
|
|
|
var tiddly embed.FS
|
|
|
|
|
|
|
|
var (
|
|
|
|
davDir string
|
|
|
|
listen string
|
2021-05-18 16:34:49 -06:00
|
|
|
auth bool
|
2021-05-17 07:50:45 -06:00
|
|
|
passPath string
|
|
|
|
users map[string]string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
users = make(map[string]string)
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.")
|
2021-05-17 07:50:45 -06:00
|
|
|
flag.StringVar(&listen, "http", "127.0.0.1:8080", "Listen on")
|
|
|
|
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
|
2021-05-18 16:34:49 -06:00
|
|
|
flag.BoolVar(&auth, "auth", true, "Enable HTTP Basic Authentication.")
|
2021-05-17 07:50:45 -06:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// These are OpenBSD specific protections used to prevent unnecessary file access.
|
|
|
|
_ = protect.Unveil(passPath, "r")
|
|
|
|
_ = protect.Unveil(davDir, "rwc")
|
|
|
|
_ = protect.Unveil("/etc/ssl/cert.pem", "r")
|
|
|
|
_ = protect.Unveil("/etc/resolv.conf", "r")
|
|
|
|
_ = protect.Pledge("stdio wpath rpath cpath inet dns")
|
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
_, fErr := os.Stat(passPath)
|
|
|
|
if os.IsNotExist(fErr) {
|
|
|
|
if auth {
|
|
|
|
fmt.Println("No .htpasswd file found!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p, err := os.Open(passPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer p.Close()
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
ht := csv.NewReader(p)
|
|
|
|
ht.Comma = ':'
|
|
|
|
ht.Comment = '#'
|
|
|
|
ht.TrimLeadingSpace = true
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
entries, err := ht.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
for _, parts := range entries {
|
|
|
|
users[parts[0]] = parts[1]
|
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authenticate(user string, pass string) bool {
|
|
|
|
htpass, exists := users[user]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func logger(f http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
n := time.Now()
|
|
|
|
fmt.Printf("%s (%s) [%s] \"%s %s\" %03d\n",
|
|
|
|
r.RemoteAddr,
|
|
|
|
n.Format(time.RFC822Z),
|
|
|
|
r.Method,
|
|
|
|
r.URL.Path,
|
|
|
|
r.Proto,
|
|
|
|
r.ContentLength,
|
|
|
|
)
|
|
|
|
f(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEmpty(path string) error {
|
|
|
|
_, fErr := os.Stat(path)
|
|
|
|
if os.IsNotExist(fErr) {
|
|
|
|
log.Printf("creating %q\n", path)
|
|
|
|
twData, _ := tiddly.ReadFile(twFile)
|
|
|
|
wErr := ioutil.WriteFile(path, []byte(twData), 0600)
|
|
|
|
if wErr != nil {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
wdav := &webdav.Handler{
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
FileSystem: webdav.Dir(davDir),
|
|
|
|
}
|
|
|
|
|
|
|
|
idxHandler := http.FileServer(http.Dir(davDir))
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) {
|
2021-05-17 15:59:48 -06:00
|
|
|
|
2021-05-17 08:01:31 -06:00
|
|
|
if strings.Contains(r.URL.Path, ".htpasswd") {
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
if auth {
|
|
|
|
user, pass, ok := r.BasicAuth()
|
|
|
|
if !(ok && authenticate(user, pass)) {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:59:48 -06:00
|
|
|
//wdav.Prefix = user
|
|
|
|
|
2021-05-17 07:50:45 -06:00
|
|
|
fp := path.Join(davDir, r.URL.Path)
|
2021-05-17 15:59:48 -06:00
|
|
|
/*
|
|
|
|
fp := path.Join(davDir, user, r.URL.Path)
|
|
|
|
_, dErr := os.Stat(user)
|
|
|
|
if os.IsNotExist(dErr) {
|
|
|
|
mErr := os.Mkdir(path.Join(davDir, user), 0700)
|
|
|
|
if mErr != nil {
|
|
|
|
http.Error(w, mErr.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
isHTML, err := regexp.Match(`\.html$`, []byte(r.URL.Path))
|
2021-05-17 07:50:45 -06:00
|
|
|
if err != nil {
|
2021-05-17 15:59:48 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-05-17 07:50:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:59:48 -06:00
|
|
|
if isHTML {
|
|
|
|
// HTML files will be created or sent back
|
|
|
|
err := createEmpty(fp)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wdav.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
// Everything else is browsable
|
2021-05-17 07:50:45 -06:00
|
|
|
idxHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
s := http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", listen)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:57:33 -06:00
|
|
|
log.Printf("Listening for HTTP on 'http://%s'", listen)
|
2021-05-17 07:50:45 -06:00
|
|
|
log.Panic(s.Serve(lis))
|
|
|
|
}
|