2024-10-29 23:08:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-11-08 15:00:17 +00:00
|
|
|
"errors"
|
2024-12-16 21:31:50 +00:00
|
|
|
"io/fs"
|
2024-10-29 23:08:11 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-11-30 10:01:38 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-10-30 09:33:03 +00:00
|
|
|
"time"
|
2024-10-29 23:08:11 +00:00
|
|
|
|
|
|
|
"git.1e99.eu/1e99/passed/routes"
|
|
|
|
"git.1e99.eu/1e99/passed/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed static/*
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
|
|
|
func run() error {
|
2024-11-08 16:08:02 +00:00
|
|
|
store, err := newStore()
|
2024-10-29 23:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-16 21:31:50 +00:00
|
|
|
staticFS, err := newStaticFS()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
var address string
|
|
|
|
var logRequests bool
|
|
|
|
var maxPasswordLength int
|
2024-11-30 10:01:38 +00:00
|
|
|
env("PASSED_ADDRESS", &address, ":3000")
|
|
|
|
env("PASSED_LOG_REQUESTS", &logRequests, "true")
|
|
|
|
env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288")
|
2024-10-29 23:21:25 +00:00
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-11-08 15:00:17 +00:00
|
|
|
handler := http.Handler(mux)
|
|
|
|
|
2024-12-16 21:31:50 +00:00
|
|
|
mux.Handle("GET /", http.FileServerFS(staticFS))
|
2024-11-09 10:14:19 +00:00
|
|
|
mux.Handle("POST /api/password", routes.CreatePassword(store, maxPasswordLength))
|
2024-11-08 16:08:02 +00:00
|
|
|
mux.Handle("GET /api/password/{id}", routes.GetPassword(store))
|
|
|
|
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(store))
|
2024-10-29 23:08:11 +00:00
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
if logRequests {
|
2024-11-09 11:59:42 +00:00
|
|
|
handler = routes.Logger(handler)
|
2024-10-29 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
log.Printf("Listening on %s.", address)
|
|
|
|
err = http.ListenAndServe(address, handler)
|
2024-10-29 23:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-16 21:31:50 +00:00
|
|
|
func newStaticFS() (sfs fs.FS, err error) {
|
|
|
|
var fsType string
|
|
|
|
env("PASSED_STATIC_TYPE", &fsType, "embed")
|
|
|
|
|
|
|
|
switch fsType {
|
|
|
|
case "embed":
|
|
|
|
sfs, err = fs.Sub(embedFS, "static")
|
|
|
|
return
|
|
|
|
case "dir", "directory":
|
|
|
|
var path string
|
|
|
|
env("PASSED_STATIC_DIR_PATH", &path, "static")
|
|
|
|
|
|
|
|
sfs = os.DirFS(path)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
err = errors.New("unkown fs type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
func newStore() (store storage.Store, err error) {
|
|
|
|
var storeType string
|
|
|
|
var clearInterval int
|
2024-11-30 10:01:38 +00:00
|
|
|
env("PASSED_STORE_TYPE", &storeType, "ram")
|
|
|
|
env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30")
|
2024-11-08 15:00:17 +00:00
|
|
|
|
|
|
|
switch storeType {
|
|
|
|
case "ram":
|
|
|
|
store = storage.NewRamStore()
|
|
|
|
case "dir", "directory":
|
|
|
|
var path string
|
2024-11-30 10:01:38 +00:00
|
|
|
env("PASSED_STORE_DIR_PATH", &path, "passwords")
|
|
|
|
|
|
|
|
err = os.MkdirAll(path, os.ModePerm)
|
|
|
|
if err != nil {
|
2024-12-15 09:03:43 +00:00
|
|
|
return
|
2024-11-30 10:01:38 +00:00
|
|
|
}
|
2024-11-08 15:00:17 +00:00
|
|
|
|
|
|
|
store = storage.NewDirStore(path)
|
|
|
|
default:
|
|
|
|
err = errors.New("unknown storage type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.Tick(time.Duration(clearInterval) * time.Second)
|
|
|
|
for {
|
|
|
|
<-ticker
|
|
|
|
|
|
|
|
err := store.ClearExpired()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to clear expired passwords: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Cleared expired passwords.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
func main() {
|
|
|
|
err := run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
2024-11-30 10:01:38 +00:00
|
|
|
|
|
|
|
func env(name string, out any, def string) {
|
|
|
|
raw := os.Getenv(name)
|
|
|
|
if raw == "" {
|
|
|
|
raw = def
|
|
|
|
log.Printf("No \"%s\" provided, defaulting to \"%s\".", name, def)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch value := out.(type) {
|
|
|
|
case *int:
|
|
|
|
i, err := strconv.ParseInt(raw, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("\"%s\" is not a number (\"%s\").", name, raw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = int(i)
|
|
|
|
|
|
|
|
case *bool:
|
|
|
|
switch raw {
|
|
|
|
case "true", "TRUE", "1":
|
|
|
|
*value = true
|
|
|
|
case "false", "FALSE", "0":
|
|
|
|
*value = false
|
|
|
|
}
|
|
|
|
|
|
|
|
case *string:
|
|
|
|
*value = raw
|
|
|
|
}
|
|
|
|
}
|