From 04fc3794796946747f1453a50ec40968a504a0d0 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Tue, 3 Sep 2024 21:51:36 -0300 Subject: [PATCH] feat: easier setup and more transparent customization --- .gitignore | 2 +- abyss.go | 9 +++++++-- generate_config.sh | 30 ++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ handlers.go | 11 ++++------- 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100755 generate_config.sh create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index 003d3cf..18ca02e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ files/ -.key abyss +.env diff --git a/abyss.go b/abyss.go index 1fe9efe..4026fc2 100644 --- a/abyss.go +++ b/abyss.go @@ -5,19 +5,24 @@ import ( "net/http" "os" "time" + + "github.com/joho/godotenv" ) const ( filesDir = "./files" - port = ":8080" + port = ":8999" ) func main() { app := new(Application) + godotenv.Load() + app.auth.username = os.Getenv("AUTH_USERNAME") app.auth.password = os.Getenv("AUTH_PASSWORD") - app.url = os.Getenv("URL") + app.url = os.Getenv("ABYSS_URL") + app.key = os.Getenv("UPLOAD_KEY") if app.auth.username == "" { log.Fatal("basic auth username must be provided") diff --git a/generate_config.sh b/generate_config.sh new file mode 100755 index 0000000..8fa1693 --- /dev/null +++ b/generate_config.sh @@ -0,0 +1,30 @@ +#!/bin/sh +echo "Press enter to use the default value '[value]' or use a custom value" + +read -p "Server domain name - this is the end url of where abyss will be hosted []: " -e ABYSS_URL + +read -p "Upload key - this is the key you will need to have to be able to make uploads to the server []: " -e UPLOAD_KEY + +read -p "Auth username - this is the username to access /tree (show all uploaded files) [admin]: " -e AUTH_USERNAME +if [ -z $AUTH_USERNAME ]; then + AUTH_USERNAME="admin" +fi + +read -p "Auth password - this is the password to access /tree (show all uploaded files) [admin]: " -e AUTH_PASSWORD +if [ -z $AUTH_PASSWORD ]; then + AUTH_PASSWORD="admin" +fi + +cat << EOF > .env +# This is the full name of the final domain for the server. Example: paste.abyss.dev +ABYSS_URL=$ABYSS_URL + +# This is the username of the user for accessing /tree +AUTH_USERNAME=$AUTH_USERNAME + +# This is the password of the user for accessing /tree +AUTH_PASSWORD=$AUTH_PASSWORD + +# This is the key needed to make uploads. Include it as X-Auth in curl. +UPLOAD_KEY=$UPLOAD_KEY +EOF diff --git a/go.mod b/go.mod index eec50d0..617b6d1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/jabuxas/abyss go 1.22.6 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/handlers.go b/handlers.go index a381c93..955e32c 100644 --- a/handlers.go +++ b/handlers.go @@ -17,6 +17,7 @@ type Application struct { password string } url string + key string } func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { @@ -50,7 +51,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { return } - if !checkAuth(w, r) { + if !app.checkAuth(r) { http.Error(w, "You're not authorized.", http.StatusBadRequest) return } @@ -91,12 +92,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { } } -func checkAuth(w http.ResponseWriter, r *http.Request) bool { - authKey, err := os.ReadFile(".key") - if err != nil { - http.Error(w, "Couldn't find your .key", http.StatusNotFound) - } - return r.Header.Get("X-Auth")+"\n" == string(authKey) +func (app *Application) checkAuth(r *http.Request) bool { + return r.Header.Get("X-Auth") == string(app.key) } func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {