Skip to content

Commit

Permalink
Better logging and cookie storage
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Oct 24, 2023
1 parent 160da68 commit 146344c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
59 changes: 34 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions routes/admin_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"aunefyren/wrapperr/utilities"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions routes/no_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"aunefyren/wrapperr/utilities"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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

Expand Down
4 changes: 0 additions & 4 deletions routes/user_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"aunefyren/wrapperr/utilities"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 146344c

Please sign in to comment.