From 1bd41cad439bf598b0f20ca67f51937fcb535454 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Tue, 26 Mar 2024 14:00:32 +0300 Subject: [PATCH] Add basic flags --- .gitignore | 4 ++-- TODO.md | 1 - auth.go | 1 + export.go | 2 +- flags.go | 22 ++++++++++++++++++++++ main.go | 1 + serve.go | 2 +- 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 flags.go diff --git a/.gitignore b/.gitignore index 54ee4f9..04e94c5 100644 --- a/.gitignore +++ b/.gitignore @@ -18,10 +18,10 @@ go.work # Executable -hibiscus +hibiscus-txt .idea/ # Because currently it's used in "prod" data/ -/config/log.txt \ No newline at end of file +config/log.txt \ No newline at end of file diff --git a/TODO.md b/TODO.md index 6ef9ea7..8b21df5 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,6 @@ List of things to add to this project * Check export function for improvements * Add missing frontend pages * More slog.Debug? -* Make the CLI multi-functional - export to path, edit file etc. * Think about timezones * Consider more secure auth methods * *Go* dependency-less? <-- this is a terrible idea \ No newline at end of file diff --git a/auth.go b/auth.go index 6c4a37e..33b44eb 100644 --- a/auth.go +++ b/auth.go @@ -19,6 +19,7 @@ type failedLogin struct { var failedLogins []failedLogin +// NoteLoginFail attempts to counteract bruteforce/spam attacks func NoteLoginFail(username string, password string, r *http.Request) { slog.Warn("failed auth", "username", username, "password", password, "address", r.RemoteAddr) NotifyTelegram(fmt.Sprintf("Failed auth attempt in hibiscus:\nusername=%s\npassword=%s\nremote=%s", username, password, r.RemoteAddr)) diff --git a/export.go b/export.go index af673a3..3c59659 100644 --- a/export.go +++ b/export.go @@ -23,7 +23,7 @@ func Export(filename string) error { defer w.Close() walker := func(path string, info os.FileInfo, err error) error { - if path == filename { // Do not add export .zip itself + if path == filename || filepath.Ext(path) == ".zip" { //Ignore export file itself and .zip archives return nil } slog.Debug("export crawling", "path", path) diff --git a/flags.go b/flags.go new file mode 100644 index 0000000..c76022b --- /dev/null +++ b/flags.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" +) + +func FlagInit() { + username := flag.String("user", "", "override username") + password := flag.String("pass", "", "override password") + port := flag.Int("port", 0, "override port") + + flag.Parse() + if *username != "" { + Cfg.Username = *username + } + if *password != "" { + Cfg.Password = *password + } + if *port != 0 { + Cfg.Port = *port + } +} diff --git a/main.go b/main.go index 0c5f4f4..e306884 100644 --- a/main.go +++ b/main.go @@ -4,5 +4,6 @@ var Cfg = ConfigInit() func main() { LogInit() + FlagInit() Serve() } diff --git a/serve.go b/serve.go index fac0a0f..61686e1 100644 --- a/serve.go +++ b/serve.go @@ -12,7 +12,7 @@ import ( func Serve() { r := chi.NewRouter() r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes) - r.Use(BasicAuth) // TODO: is this good enough? + r.Use(BasicAuth) // Is this good enough? Sure hope so r.NotFound(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) http.ServeFile(w, r, "./pages/error/404.html")