From 4bbb96663419071f4abc63ffaa3526081035c403 Mon Sep 17 00:00:00 2001 From: MikeIsAStar <99037623+MikeIsAStar@users.noreply.github.com> Date: Sun, 21 Apr 2024 22:40:00 -0400 Subject: [PATCH] Allow for console output to be saved to a file --- .gitignore | 4 +++- common/config.go | 5 +++++ config_example.xml | 19 ++++++++++++++++--- logging/log.go | 36 +++++++++++++++++++++++++++++++++++- main.go | 3 +++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index efe55bd..ce06abd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,11 @@ salt.bin *.der *.pem +# Logs +logs + # Payload data payload # Editor files .vscode - diff --git a/common/config.go b/common/config.go index 7ce75fe..268fd75 100644 --- a/common/config.go +++ b/common/config.go @@ -20,6 +20,7 @@ type Config struct { EnableHTTPSExploitWii *bool `xml:"enableHttpsExploitWii,omitempty"` EnableHTTPSExploitDS *bool `xml:"enableHttpsExploitDS,omitempty"` LogLevel *int `xml:"logLevel"` + LogOutput string `xml:"logOutput"` CertPath string `xml:"certPath"` KeyPath string `xml:"keyPath"` CertPathWii string `xml:"certDerPathWii"` @@ -78,5 +79,9 @@ func GetConfig() Config { config.LogLevel = &level } + if config.LogOutput == "" { + config.LogOutput = "StdOutAndFile" + } + return config } diff --git a/config_example.xml b/config_example.xml index 02a44f3..4e48094 100644 --- a/config_example.xml +++ b/config_example.xml @@ -33,12 +33,25 @@ username password - + 127.0.0.1 wwfc - - + + + 4 + + StdOutAndFile hQ3f57b3tW2WnjJH3v diff --git a/logging/log.go b/logging/log.go index d651cf6..688bb4d 100644 --- a/logging/log.go +++ b/logging/log.go @@ -1,18 +1,52 @@ package logging import ( + "errors" "fmt" + "io" "log" + "os" + "time" "github.com/logrusorgru/aurora/v3" ) -var logLevel = 0 +var ( + logDir = "./logs" + logLevel = 0 +) func SetLevel(level int) { logLevel = level } +func SetOutput(output string) error { + switch output { + case "None": + log.SetOutput(io.Discard) + case "StdOut": + log.SetOutput(os.Stdout) + case "StdOutAndFile": + if err := os.MkdirAll(logDir, 0700); err != nil { + return err + } + + time := time.Now() + logFilePath := time.Format(logDir + "/2006-01-02-15-04-05.log") + + file, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE, 0400) + if err != nil { + return err + } + + log.SetOutput(io.MultiWriter(os.Stdout, file)) + default: + return errors.New("invalid output value provided") + } + + return nil +} + func Notice(module string, arguments ...any) { if logLevel < 1 { return diff --git a/main.go b/main.go index 6fde69c..c40f80c 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,9 @@ import ( func main() { config := common.GetConfig() logging.SetLevel(*config.LogLevel) + if err := logging.SetOutput(config.LogOutput); err != nil { + logging.Error("MAIN", err) + } wg := &sync.WaitGroup{} actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer}