From 146344cfb08498d48d09d52492897ba2a42d1f50 Mon Sep 17 00:00:00 2001 From: aunefyren Date: Tue, 24 Oct 2023 16:36:21 +0200 Subject: [PATCH] Better logging and cookie storage --- main.go | 59 +++++++++++++++++++++++++------------------- routes/admin_auth.go | 2 -- routes/no_auth.go | 10 -------- routes/user_auth.go | 4 --- web/js/functions.js | 2 +- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/main.go b/main.go index ef573cc..1a9214d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "context" "flag" "fmt" + "io" "log" "net/http" "os" @@ -24,55 +25,66 @@ func main() { utilities.PrintASCII() // Create and define file for logging - file, err := os.OpenFile("config/wrapperr.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + logFile, err := os.OpenFile("config/wrapperr.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { - fmt.Println("Failed to load configuration file. Error: " + err.Error()) - + fmt.Println("Failed to load log file. Error: " + err.Error()) os.Exit(1) } + // Set log file as log destination + log.SetOutput(logFile) + fmt.Println("Log file set.") + + // Load config file config, err := files.GetConfig() if err != nil { fmt.Println("Failed to load configuration file. Error: " + err.Error()) - os.Exit(1) } + var mw io.Writer + // Set log file is logging is enabled + if config.UseLogs { + out := os.Stdout + mw = io.MultiWriter(out, logFile) + } else { + out := os.Stdout + mw = io.MultiWriter(out) + } + // Get pipe reader and writer | writes to pipe writer come out pipe reader + _, w, _ := os.Pipe() + + // Replace stdout,stderr with pipe writer | all writes to stdout, stderr will go through pipe instead (log.print, log) + os.Stdout = w + os.Stderr = w + + // writes with log.Print should also write to mw + log.SetOutput(mw) + // Set time zone from config if it is not empty if config.Timezone != "" { loc, err := time.LoadLocation(config.Timezone) if err != nil { - fmt.Println("Failed to set time zone from config. Error: " + err.Error()) - fmt.Println("Removing value...") + log.Println("Failed to set time zone from config. Error: " + err.Error()) + log.Println("Removing value...") config.Timezone = "" err = files.SaveConfig(config) if err != nil { - fmt.Println("Failed to set new time zone in the config. Error: " + err.Error()) - fmt.Println("Exiting...") + log.Println("Failed to set new time zone in the config. Error: " + err.Error()) + log.Println("Exiting...") os.Exit(1) } } else { time.Local = loc - fmt.Println("Timezone set to " + config.Timezone + ".") + log.Println("Timezone set to " + config.Timezone + ".") } } - // Set log file is logging is enabled - if config.UseLogs { - log.SetOutput(file) - - fmt.Println("Log file enabled.") - log.Println("Log file enabled.") - } - // Print current version - fmt.Println("Running version " + config.WrapperrVersion + ".") - if config.UseLogs { - log.Println("Running version " + config.WrapperrVersion + ".") - } + log.Println("Running version " + config.WrapperrVersion + ".") // Define port variable with the port from the config file as default var port int @@ -82,10 +94,7 @@ func main() { flag.Parse() // Alert what port is in use - fmt.Println("Starting Wrapperr on port " + strconv.Itoa(port) + ".") - if config.UseLogs { - log.Println("Starting Wrapperr on port " + strconv.Itoa(port) + ".") - } + log.Println("Starting Wrapperr on port " + strconv.Itoa(port) + ".") // Assign routes router := mux.NewRouter().StrictSlash(true) diff --git a/routes/admin_auth.go b/routes/admin_auth.go index 77b1ae9..6fdc364 100644 --- a/routes/admin_auth.go +++ b/routes/admin_auth.go @@ -7,7 +7,6 @@ import ( "aunefyren/wrapperr/utilities" "encoding/json" "errors" - "fmt" "io/ioutil" "log" "net/http" @@ -254,7 +253,6 @@ func ApiUpdateAdmin(w http.ResponseWriter, r *http.Request) { } log.Println("New admin account created. Server is now claimed.") - fmt.Println("New admin account created. Server is now claimed.") utilities.RespondDefaultOkay(w, r, "Admin created.") return diff --git a/routes/no_auth.go b/routes/no_auth.go index 7375bee..16c1a1d 100644 --- a/routes/no_auth.go +++ b/routes/no_auth.go @@ -7,7 +7,6 @@ import ( "aunefyren/wrapperr/utilities" "encoding/json" "errors" - "fmt" "io/ioutil" "log" "net/http" @@ -93,7 +92,6 @@ func ApiGetFunctions(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println(err) log.Println("Failed to load configuration file.") - fmt.Println("Failed to load configuration file.") return } @@ -120,7 +118,6 @@ func ApiCreateAdmin(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println(err) log.Println("Failed to load admin state.") - fmt.Println("Failed to load admin state.") return } @@ -169,7 +166,6 @@ func ApiCreateAdmin(w http.ResponseWriter, r *http.Request) { } log.Println("New admin account created. Server is now claimed.") - fmt.Println("New admin account created. Server is now claimed.") utilities.RespondDefaultOkay(w, r, "Admin created.") return @@ -214,14 +210,12 @@ func ApiLogInAdmin(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println(err) log.Println("Failed to load admin state.") - fmt.Println("Failed to load admin state.") return } config, err := files.GetConfig() if err != nil { log.Println("Failed to load configuration file. Error: " + err.Error()) - fmt.Println("Failed to load configuration file.") return } @@ -235,7 +229,6 @@ func ApiLogInAdmin(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println(err) log.Println("Failed to load admin config.") - fmt.Println("Failed to load admin config.") return } @@ -283,7 +276,6 @@ func ApiLogInAdmin(w http.ResponseWriter, r *http.Request) { if !password_validity || admin_config.AdminUsername != username { ip_string := utilities.GetOriginIPString(w, r) log.Println("Admin login failed. Incorrect admin username or password." + ip_string) - fmt.Println("Admin login failed. Incorrect admin username or password." + ip_string) utilities.RespondDefaultError(w, r, errors.New("Login failed. Username or password is incorrect."), 401) return } @@ -305,8 +297,6 @@ func ApiLogInAdmin(w http.ResponseWriter, r *http.Request) { log.Println("Created and retrieved admin login JWT Token." + ip_string) - fmt.Println("Created and retrieved admin login JWT Token." + ip_string) - utilities.RespondWithJSON(w, http.StatusOK, string_reply) return diff --git a/routes/user_auth.go b/routes/user_auth.go index dcf6eae..8c72943 100644 --- a/routes/user_auth.go +++ b/routes/user_auth.go @@ -7,7 +7,6 @@ import ( "aunefyren/wrapperr/utilities" "encoding/json" "errors" - "fmt" "io/ioutil" "log" "net/http" @@ -86,7 +85,6 @@ func ApiGetLoginURL(w http.ResponseWriter, r *http.Request) { ip_string := utilities.GetOriginIPString(w, r) log.Println("Created and retrieved Plex Auth login URL." + ip_string) - fmt.Println("Created and retrieved Plex Auth login URL." + ip_string) utilities.RespondWithJSON(w, http.StatusOK, url_reply) return @@ -166,8 +164,6 @@ func ApiLoginPlexAuth(w http.ResponseWriter, r *http.Request) { log.Println("Created and retrieved Plex Auth login JWT Token." + ip_string) - fmt.Println("Created and retrieved Plex Auth login JWT Token." + ip_string) - utilities.RespondWithJSON(w, http.StatusOK, string_reply) return diff --git a/web/js/functions.js b/web/js/functions.js index 36c0a60..71ce176 100644 --- a/web/js/functions.js +++ b/web/js/functions.js @@ -2,7 +2,7 @@ function set_cookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); - document.cookie = cname + "=" + cvalue + ";" + expires + ";" + "samesite=strict" + ";"; + document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/; samesite=strict; secure;"; } function get_cookie(cname) {