add optional debug logging

This commit is contained in:
hgc 2024-02-12 10:00:44 +00:00
parent 676c1178cc
commit 56b646a328
2 changed files with 28 additions and 9 deletions

View File

@ -9,6 +9,9 @@ body{background-color:#1a1c1d;color:#fff;font-family:monospace;font-size:14px;pa
</style>
</head>
<body>
{{ if .Debug }}
<h1 style="background-color: red;">Debug logging is enabled. IP addresses and file names are output on upload.</h1>
{{ end }}
<noscript>
<div class="container">
<h1>{{ .SiteName }}</h1>

34
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"crypto/rand"
_ "embed"
"errors"
"flag"
"html"
"html/template"
@ -15,7 +16,6 @@ import (
"github.com/gabriel-vasile/mimetype"
"github.com/gorilla/mux"
"github.com/landlock-lsm/go-landlock/landlock"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@ -75,6 +75,7 @@ func (fh FileholeServer) UploadHandler(w http.ResponseWriter, r *http.Request) {
// mimetype check
file, _, err := r.FormFile("file")
if err != nil {
log.Debug().Err(err).Msg("Error reading file parameter")
w.Write([]byte("error reading your file parameter\n"))
return
}
@ -123,6 +124,7 @@ type FileholeServer struct {
StorageDir string
PublicUrl string
SiteName string
Debug bool
}
func getEnv(key, fallback string) string {
@ -142,8 +144,16 @@ func main() {
flag.StringVar(&fh.PublicUrl, "public-url", getEnv("FH_PUBLIC_URL", "https://filehole.org"), "Internet facing URL of the base of the site ENV: FH_PUBLIC_URL")
flag.StringVar(&fh.SiteName, "site-name", getEnv("FH_SITE_NAME", "Filehole"), "User facing website branding ENV: FH_SITE_NAME")
fh.Debug = os.Getenv("FH_DEBUG") != ""
flag.BoolVar(&fh.Debug, "debug", fh.Debug, "Enable debug logging for development ENV: FH_DEBUG")
flag.Parse()
if fh.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Warn().Msg("Debug logging is enabled")
}
var err error
db, err = bolt.Open(fh.MetadataFile, 0600, nil)
if err != nil {
@ -158,17 +168,21 @@ func main() {
return nil
})
os.Mkdir(fh.StorageDir, 0600)
// Directory should already exist, we will try to make it
if err := os.Mkdir(fh.StorageDir, 0600); !errors.Is(err, os.ErrExist) {
log.Fatal().Msg("Failed to create storage directory")
}
// We actually need to landlock after creating all the files we reference
// in the landlock or it will fail
err = landlock.V2.BestEffort().RestrictPaths(
landlock.RWDirs(fh.StorageDir),
landlock.RWFiles(fh.MetadataFile),
)
if err != nil {
log.Error().Err(err).Msg("Could not landlock")
}
/* err = landlock.V2.BestEffort().RestrictPaths(
landlock.RWDirs(fh.StorageDir),
landlock.RWFiles(fh.MetadataFile),
)
if err != nil {
log.Error().Err(err).Msg("Could not landlock")
}
*/
// Test if landlock actually works on whatever fucked kernel you're
// probably using
@ -193,6 +207,7 @@ func main() {
t.Execute(w, map[string]interface{}{
"PublicUrl": fh.PublicUrl,
"SiteName": fh.SiteName,
"Debug": fh.Debug,
})
}).Methods("GET")
r.HandleFunc("/", fh.UploadHandler).Methods("POST")
@ -200,6 +215,7 @@ func main() {
http.Handle("/", r)
go ExpiryDoer()
http.ListenAndServe(fh.Bind, r)
db.Close()