Better logging and cookie storage

This commit is contained in:
aunefyren 2023-10-24 16:36:21 +02:00
parent 160da683ec
commit 146344cfb0
5 changed files with 35 additions and 42 deletions

59
main.go
View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) {