diff --git a/arc.service b/arc.service index 3fc5fa4..978fcde 100644 --- a/arc.service +++ b/arc.service @@ -7,7 +7,7 @@ After=network.target [Service] Type=simple PermissionsStartOnly=true -ExecStart=/usr/local/bin/arc -config /usr/local/etc/arc/config.json -log-file /var/log/arc.log +ExecStart=/usr/local/bin/arc -config /usr/local/etc/arc/config.json -log /var/log/arc.log Restart=always RestartSec=30 diff --git a/backup/backup.go b/backup/backup.go index 8ea04d8..e934990 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -10,8 +10,9 @@ package backup import ( "fmt" "github.com/evilsocket/arc/db" - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "os/exec" "path" "runtime" @@ -22,18 +23,18 @@ func worker(secs int, folder string, cmd string) { period := time.Duration(secs) * time.Second filename := path.Join(folder, "arc-backup.tar") - log.Debugf("Backup task started with a %v period to %s", period, filename) + log.Debug("Backup task started with a %v period to %s", period, filename) for { started := time.Now() - log.Infof("Backupping database to %s ...", filename) + log.Info("Backupping database to %s ...", filename) if err := db.Export(filename); err != nil { - log.Errorf("Error while creating the backup file: %s.", err) + log.Error("Error while creating the backup file: %s.", err) } else { - log.Infof("Backupped %s of data to %s in %s.", utils.FormatBytes(db.Size), log.Bold(filename), time.Since(started)) + log.Info("Backupped %s of data to %s in %s.", utils.FormatBytes(db.Size), tui.Bold(filename), time.Since(started)) if cmd != "" { - log.Infof("Running %s ...", log.Bold(cmd)) + log.Info("Running %s ...", tui.Bold(cmd)) var timer *time.Timer var c *exec.Cmd @@ -43,7 +44,7 @@ func worker(secs int, folder string, cmd string) { timer = time.AfterFunc(period, func() { timer.Stop() if c != nil { - log.Warningf("Command timed out, killing.") + log.Warning("Command timed out, killing.") c.Process.Kill() } }) @@ -60,14 +61,14 @@ func worker(secs int, folder string, cmd string) { output, err := c.CombinedOutput() if err != nil { - log.Errorf("Error: %s", err) + log.Error("Error: %s", err) } if output != nil && len(output) > 0 { - log.Infof("Output: %s", log.Bold(string(output))) + log.Info("Output: %s", tui.Bold(string(output))) } - log.Infof("Command ran in %s.", time.Since(started)) + log.Info("Command ran in %s.", time.Since(started)) } } diff --git a/cmd/arc/main.go b/cmd/arc/main.go index 98ac1b9..13902b7 100644 --- a/cmd/arc/main.go +++ b/cmd/arc/main.go @@ -5,7 +5,8 @@ import ( "fmt" "github.com/evilsocket/arc/config" "github.com/evilsocket/arc/db" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "golang.org/x/crypto/bcrypt" "os" "os/signal" @@ -18,7 +19,7 @@ func arcSignalHandler() { signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) s := <-signals log.Raw("\n") - log.Importantf("RECEIVED SIGNAL: %s", s) + log.Warning("RECEIVED SIGNAL: %s", s) db.Flush() os.Exit(1) } @@ -30,7 +31,7 @@ func main() { if len(os.Args) == 4 { n, err := strconv.Atoi(os.Args[3]) if err != nil { - log.Fatal(err) + log.Fatal("%v", err) } cost = n } @@ -44,10 +45,10 @@ func main() { setupLogging() - log.Infof("%s (%s %s) is starting ...", log.Bold(config.APP_NAME+" v"+config.APP_VERSION), runtime.GOOS, runtime.GOARCH) + log.Info("%s (%s %s) is starting ...", tui.Bold(config.APP_NAME+" v"+config.APP_VERSION), runtime.GOOS, runtime.GOARCH) if confFile != "" { if err := config.Load(confFile); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } } @@ -63,8 +64,8 @@ func main() { address = "0.0.0.0" + address } - log.Infof("Running on %s ...", log.Bold("https://"+address+"/")) + log.Info("Running on %s ...", tui.Bold("https://"+address+"/")) if err := router.RunTLS(address, config.Conf.Certificate, config.Conf.Key); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } } diff --git a/cmd/arc/setup.go b/cmd/arc/setup.go index c9bdde3..2215302 100644 --- a/cmd/arc/setup.go +++ b/cmd/arc/setup.go @@ -8,13 +8,14 @@ import ( "github.com/evilsocket/arc/controllers" "github.com/evilsocket/arc/db" "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/middlewares" "github.com/evilsocket/arc/scheduler" "github.com/evilsocket/arc/tls" "github.com/evilsocket/arc/updater" "github.com/evilsocket/arc/utils" "github.com/evilsocket/arc/webui" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "github.com/gin-gonic/contrib/static" "github.com/gin-gonic/gin" "net/http" @@ -44,9 +45,8 @@ func init() { flag.BoolVar(&noAuth, "no-auth", noAuth, "Disable authentication.") flag.BoolVar(&noUpdates, "no-updates", noUpdates, "Disable updates check.") - flag.BoolVar(&debug, "log-debug", debug, "Enable debug logs.") - flag.StringVar(&logfile, "log-file", logfile, "Log messages to this file instead of standard error.") - flag.BoolVar(&noColors, "log-colors-off", noColors, "Disable colored output.") + flag.BoolVar(&debug, "debug", debug, "Enable debug logs.") + flag.StringVar(&logfile, "log", logfile, "Log messages to this file instead of standard error.") flag.StringVar(&importFrom, "import", importFrom, "Import stores from this TAR export file.") flag.BoolVar(&export, "export", export, "Export store to a TAR archive, requires --output parameter.") @@ -54,23 +54,23 @@ func init() { } func setupLogging() { - var err error - - log.WithColors = !noColors - if logfile != "" { - log.Output, err = os.Create(logfile) - if err != nil { - log.Fatal(err) - } - - defer log.Output.Close() + log.Output = logfile } if debug == true { - log.MinLevel = log.DEBUG + log.Level = log.DEBUG } else { - log.MinLevel = log.INFO + log.Level = log.INFO + } + + log.DateFormat = "06-Jan-02" + log.TimeFormat = "15:04:05" + log.DateTimeFormat = "2006-01-02 15:04:05" + log.Format = "{datetime} {level:color}{level:name}{reset} {message}" + + if err := log.Open(); err != nil { + panic(err) } } @@ -78,22 +78,22 @@ func setupDatabase() { var err error if dbIsNew, err = db.Setup(); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } if export == true { started := time.Now() if err = db.Export(output); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Infof("Archived %s of data in %s to %s.", utils.FormatBytes(db.Size), time.Since(started), log.Bold(output)) + log.Info("Archived %s of data in %s to %s.", utils.FormatBytes(db.Size), time.Since(started), tui.Bold(output)) os.Exit(0) } else if importFrom != "" { started := time.Now() if err = db.Import(importFrom); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Infof("Imported %s of data in %s.", utils.FormatBytes(db.Size), time.Since(started)) + log.Info("Imported %s of data in %s.", utils.FormatBytes(db.Size), time.Since(started)) os.Exit(0) } } @@ -101,22 +101,22 @@ func setupDatabase() { func setupScheduler() { if config.Conf.Scheduler.Enabled { if err := events.Setup(); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Debugf("Starting scheduler with a period of %ds ...", config.Conf.Scheduler.Period) + log.Debug("Starting scheduler with a period of %ds ...", config.Conf.Scheduler.Period) scheduler.Start(config.Conf.Scheduler.Period) } else { - log.Importantf("Scheduler is disabled.") + log.Warning("Scheduler is disabled.") } } func setupBackups() { if config.Conf.Backups.Enabled { - log.Debugf("Starting backup task with a period of %ds ...", config.Conf.Backups.Period) + log.Debug("Starting backup task with a period of %ds ...", config.Conf.Backups.Period) backup.Start(config.Conf.Backups.Period, config.Conf.Backups.Folder, config.Conf.Backups.Run) } else { - log.Importantf("Backups are disabled.") + log.Warning("Backups are disabled.") } } @@ -130,25 +130,25 @@ func setupTLS() { var err error if config.Conf.Certificate, err = utils.ExpandPath(config.Conf.Certificate); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } else if config.Conf.Key, err = utils.ExpandPath(config.Conf.Key); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } if utils.Exists(config.Conf.Certificate) == false || utils.Exists(config.Conf.Key) == false { - log.Importantf("TLS certificate files not found, generating new ones ...") + log.Warning("TLS certificate files not found, generating new ones ...") if err = tls.Generate(&config.Conf); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Infof("New RSA key and certificate have been generated, remember to add them as exceptions to your browser!") + log.Info("New RSA key and certificate have been generated, remember to add them as exceptions to your browser!") } tlsFingerprint, err = tls.Fingerprint(config.Conf.Certificate) if err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Importantf("TLS certificate fingerprint is %s", log.Bold(tlsFingerprint)) + log.Warning("TLS certificate fingerprint is %s", tui.Bold(tlsFingerprint)) } type binaryFileSystem struct { @@ -171,9 +171,9 @@ func (b *binaryFileSystem) Exists(prefix string, filepath string) bool { func BinaryFileSystem(root string) *binaryFileSystem { fs := &assetfs.AssetFS{ - Asset: webui.Asset, - AssetDir: webui.AssetDir, - Prefix: root} + Asset: webui.Asset, + AssetDir: webui.AssetDir, + Prefix: root} return &binaryFileSystem{ fs, } @@ -196,7 +196,7 @@ func setupRouter() *gin.Engine { if noAuth == false { api.Use(middlewares.AuthHandler()) } else { - log.Importantf("API authentication is disabled.") + log.Warning("API authentication is disabled.") } api.GET("/status", controllers.GetStatus) diff --git a/config/config.go b/config/config.go index 8776ea4..9f1155d 100644 --- a/config/config.go +++ b/config/config.go @@ -10,11 +10,11 @@ package config import ( "encoding/json" "errors" - "io/ioutil" - - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "golang.org/x/crypto/bcrypt" + "io/ioutil" ) const ( @@ -125,7 +125,7 @@ var Conf = Configuration{ // Load function convert a loaded JSON config file to a config struct // return err if secret param is empty func Load(filename string) error { - log.Infof("Loading configuration from %s ...", log.Bold(filename)) + log.Info("Loading configuration from %s ...", tui.Bold(filename)) raw, err := ioutil.ReadFile(filename) if err != nil { return err @@ -152,7 +152,7 @@ func Load(filename string) error { func (c Configuration) HashPassword(password string, cost int) string { hash, err := bcrypt.GenerateFromPassword([]byte(password), cost) if err != nil { - log.Fatal(err) + log.Fatal("%v", err) } return string(hash) } diff --git a/controllers/auth.go b/controllers/auth.go index f1a7d09..6d1e594 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -10,9 +10,10 @@ package controllers import ( "github.com/evilsocket/arc/config" "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/middlewares" "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "github.com/gin-gonic/gin" "strings" ) @@ -58,7 +59,7 @@ func Auth(c *gin.Context) { if err := SafeBind(c, &auth); err != nil { utils.BadRequest(c) } else if config.Conf.Auth(auth.Username, auth.Password) == false { - log.Warningf("Invalid login credentials provided.") + log.Warning("Invalid login credentials provided.") events.Add( events.Login(false, strings.Split(c.Request.RemoteAddr, ":")[0], @@ -75,7 +76,7 @@ func Auth(c *gin.Context) { auth.Username, auth.Password, )) - log.Api(log.INFO, c, "User %s requested new API token", log.Bold(auth.Username)) + utils.Api(log.INFO, c, "User %s requested new API token", tui.Bold(auth.Username)) c.JSON(200, gin.H{"token": token}) } } diff --git a/controllers/main.go b/controllers/main.go index c425c3b..ded475c 100644 --- a/controllers/main.go +++ b/controllers/main.go @@ -22,15 +22,15 @@ package controllers import ( "encoding/json" - "io" - "time" - "github.com/evilsocket/arc/config" "github.com/evilsocket/arc/db" "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" + "io" + "time" ) type Status struct { @@ -74,7 +74,7 @@ func SafeBind(c *gin.Context, obj interface{}) error { // Responses: // 200: Status func GetStatus(c *gin.Context) { - // log.Api(log.DEBUG, c, "Requested status.") + // utils.Api(log.DEBUG, c, "Requested status.") c.JSON(200, ServerStatus) } @@ -93,6 +93,6 @@ func ClearEvents(c *gin.Context) { // Responses: // 200: Configuration func GetConfig(c *gin.Context) { - log.Api(log.DEBUG, c, "Requested configuration.") + utils.Api(log.DEBUG, c, "Requested configuration.") c.JSON(200, config.Conf) } diff --git a/controllers/record.go b/controllers/record.go index cde892e..9cb226c 100644 --- a/controllers/record.go +++ b/controllers/record.go @@ -12,7 +12,7 @@ import ( "fmt" "github.com/evilsocket/arc/config" "github.com/evilsocket/arc/db" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/evilsocket/arc/utils" "github.com/gin-gonic/gin" ) @@ -23,7 +23,7 @@ func ListRecords(c *gin.Context) { if err != nil { utils.NotFound(c) } else { - log.Api(log.DEBUG, c, "Requested records of store %s.", store_id) + utils.Api(log.DEBUG, c, "Requested records of store %s.", store_id) c.JSON(200, records) } } @@ -46,7 +46,7 @@ func CreateRecord(c *gin.Context) { raw := c.Request.Form.Get("meta") nbytes := int64(len(raw)) if nbytes > config.Conf.MaxReqSize { - log.Warningf("Request meta field is %d bytes, while max request size is %d.", nbytes, config.Conf.MaxReqSize) + log.Warning("Request meta field is %d bytes, while max request size is %d.", nbytes, config.Conf.MaxReqSize) utils.BadRequest(c) return } @@ -62,7 +62,7 @@ func CreateRecord(c *gin.Context) { utils.ServerError(c, err) } - log.Api(log.DEBUG, c, + utils.Api(log.DEBUG, c, "Created record %d (store %s) with %s of %s encrypted data.", record.Id(), store_id, @@ -78,7 +78,7 @@ func GetRecord(c *gin.Context) { if err != nil { utils.NotFound(c) } else { - log.Api(log.DEBUG, c, "Requested record %d of store %s.", record.Id, store_id) + utils.Api(log.DEBUG, c, "Requested record %d of store %s.", record.Id, store_id) c.JSON(200, record) } } @@ -102,7 +102,7 @@ func GetRecordBuffer(c *gin.Context) { desc = "compressed " } - log.Debugf("Streaming %s (%d b) of %sbuffer to %s.", utils.FormatBytes(meta.Size), meta.Size, desc, c.Request.RemoteAddr) + log.Debug("Streaming %s (%d b) of %sbuffer to %s.", utils.FormatBytes(meta.Size), meta.Size, desc, c.Request.RemoteAddr) // Let the client handle the decompression :P if meta.Compressed { @@ -132,7 +132,7 @@ func DeleteRecord(c *gin.Context) { } else if _, err = store.Del(id); err != nil { utils.NotFound(c) } else { - log.Api(log.DEBUG, c, "Deleted record %s of store %s.", record_id, store_id) + utils.Api(log.DEBUG, c, "Deleted record %s of store %s.", record_id, store_id) c.JSON(200, gin.H{"msg": "Record deleted."}) } } @@ -158,7 +158,7 @@ func UpdateRecord(c *gin.Context) { raw := c.Request.Form.Get("meta") nbytes := int64(len(raw)) if nbytes > config.Conf.MaxReqSize { - log.Warningf("Request meta field is %d bytes, while max request size is %d.", nbytes, config.Conf.MaxReqSize) + log.Warning("Request meta field is %d bytes, while max request size is %d.", nbytes, config.Conf.MaxReqSize) utils.BadRequest(c) return } @@ -184,6 +184,6 @@ func UpdateRecord(c *gin.Context) { store, _ := db.GetStore(store_id) store.MarkUpdated() - log.Api(log.DEBUG, c, "Updated record %s of store %s.", record_id, store_id) + utils.Api(log.DEBUG, c, "Updated record %s of store %s.", record_id, store_id) c.JSON(200, record.Meta()) } diff --git a/controllers/store.go b/controllers/store.go index 862d99e..a5b110d 100644 --- a/controllers/store.go +++ b/controllers/store.go @@ -9,7 +9,7 @@ package controllers import ( "github.com/evilsocket/arc/db" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/evilsocket/arc/utils" "github.com/gin-gonic/gin" ) @@ -19,7 +19,7 @@ func ListStores(c *gin.Context) { if err != nil { utils.NotFound(c) } else { - log.Api(log.DEBUG, c, "Requested stores.") + utils.Api(log.DEBUG, c, "Requested stores.") c.JSON(200, stores) } } @@ -31,7 +31,7 @@ func CreateStore(c *gin.Context) { } else if store, err := db.Create(&meta); err != nil { utils.ServerError(c, err) } else { - log.Api(log.DEBUG, c, "Created store %d.", store.Id) + utils.Api(log.DEBUG, c, "Created store %d.", store.Id) c.JSON(200, store) } } @@ -41,7 +41,7 @@ func GetStore(c *gin.Context) { if err != nil { utils.NotFound(c) } else { - log.Api(log.DEBUG, c, "Requested store %s.", c.Params.ByName("id")) + utils.Api(log.DEBUG, c, "Requested store %s.", c.Params.ByName("id")) c.JSON(200, store.Meta()) } } @@ -53,7 +53,7 @@ func DeleteStore(c *gin.Context) { } else if err := db.Delete(store); err != nil { utils.ServerError(c, err) } else { - log.Api(log.DEBUG, c, "Deleted store %s.", c.Params.ByName("id")) + utils.Api(log.DEBUG, c, "Deleted store %s.", c.Params.ByName("id")) c.JSON(200, gin.H{"msg": "Store deleted."}) } } @@ -68,7 +68,7 @@ func UpdateStore(c *gin.Context) { } else if err := store.Update(&meta); err != nil { utils.ServerError(c, err) } else { - log.Api(log.DEBUG, c, "Updated store %s.", c.Params.ByName("id")) + utils.Api(log.DEBUG, c, "Updated store %s.", c.Params.ByName("id")) c.JSON(200, gin.H{"msg": "Store updated."}) } } diff --git a/db/db.go b/db/db.go index 9f7e484..970128d 100644 --- a/db/db.go +++ b/db/db.go @@ -8,7 +8,7 @@ package db import ( - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "sort" "time" ) @@ -160,11 +160,11 @@ func PrunableRecords() (records []*Record, err error) { } func Flush() { - log.Infof("Flushing database ...") + log.Info("Flushing database ...") start := time.Now() dbIndex.Flush() elapsed := time.Since(start) - log.Infof("Database flushed in %s.", elapsed) + log.Info("Database flushed in %s.", elapsed) } diff --git a/db/export.go b/db/export.go index fe998b6..6a81821 100644 --- a/db/export.go +++ b/db/export.go @@ -9,7 +9,7 @@ package db import ( "archive/tar" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "io" "os" "path/filepath" @@ -20,7 +20,7 @@ func Export(filename string) error { Lock() defer Unlock() - log.Importantf("Exporting %d stores from %s ...", dbIndex.NumRecords(), dbIndex.path) + log.Warning("Exporting %d stores from %s ...", dbIndex.NumRecords(), dbIndex.path) out, err := os.Create(filename) if err != nil { @@ -54,7 +54,7 @@ func Export(filename string) error { return nil } - log.Debugf("Writing contents for %s ...", file) + log.Debug("Writing contents for %s ...", file) f, err := os.Open(file) if err != nil { return err diff --git a/db/import.go b/db/import.go index a6dbb49..8db51e7 100644 --- a/db/import.go +++ b/db/import.go @@ -9,14 +9,14 @@ package db import ( "archive/tar" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "io" "os" "path/filepath" ) func Import(filename string) error { - log.Infof("Importing %s into %s ...", filename, dbIndex.path) + log.Info("Importing %s into %s ...", filename, dbIndex.path) in, err := os.Open(filename) if err != nil { @@ -41,7 +41,7 @@ func Import(filename string) error { // the target location where the dir/file should be created target := filepath.Join(dbIndex.path, header.Name) - log.Infof("Creating %s ...", target) + log.Info("Creating %s ...", target) // check the file type switch header.Typeflag { diff --git a/db/index.go b/db/index.go index 0abe1f5..a208352 100644 --- a/db/index.go +++ b/db/index.go @@ -8,7 +8,7 @@ package db import ( - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/evilsocket/arc/utils" "github.com/theckman/go-flock" "path/filepath" @@ -22,7 +22,7 @@ type Index struct { } func LoadIndex(path string) (i *Index, err error) { - log.Debugf("Loading index from '%s' ...", path) + log.Debug("Loading index from '%s' ...", path) i = &Index{ path: path, @@ -35,7 +35,7 @@ func LoadIndex(path string) (i *Index, err error) { matches, err := filepath.Glob(filepath.Join(path, "*")) if err != nil { - log.Errorf("Error while globbing folder %s: %s", path, err) + log.Error("Error while globbing folder %s: %s", path, err) return i, err } @@ -46,11 +46,11 @@ func LoadIndex(path string) (i *Index, err error) { if err == nil { meta_path := filepath.Join(folder, "meta.json") if utils.Exists(meta_path) { - log.Debugf("Loading record from %s ...", folder) + log.Debug("Loading record from %s ...", folder) if child, err := OpenRecord(folder); err == nil { i.records[id] = child } else { - log.Errorf("Error while loading record from %s: %s", folder, err) + log.Error("Error while loading record from %s: %s", folder, err) } } } diff --git a/db/meta.go b/db/meta.go index 090c189..b0efc6e 100644 --- a/db/meta.go +++ b/db/meta.go @@ -9,7 +9,7 @@ package db import ( "encoding/json" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/theckman/go-flock" "io/ioutil" "path/filepath" @@ -43,16 +43,16 @@ func (m *Meta) Unlock() error { } func (m *Meta) FlushNoLock() error { - log.Debugf("Flushing meta file '%s'.", m.path) + log.Debug("Flushing meta file '%s'.", m.path) buff, err := json.Marshal(m) if err != nil { - log.Errorf("Error while serializing meta file: %s", err) + log.Error("Error while serializing meta file: %s", err) return err } if err = ioutil.WriteFile(m.path, buff, 0644); err != nil { - log.Errorf("Error while writing meta file to %s: %s", m.path, err) + log.Error("Error while writing meta file to %s: %s", m.path, err) return err } @@ -83,7 +83,7 @@ func (m *Meta) Update(values *Meta) (err error) { func (m *Meta) Close() { if err := m.Flush(); err != nil { - log.Errorf("Error while flushing meta file '%s': %s", m.path, err) + log.Error("Error while flushing meta file '%s': %s", m.path, err) } } @@ -110,10 +110,10 @@ func CreateMeta(path string, values *Meta) (meta *Meta, err error) { } if err = meta.SetPath(path); err != nil { - log.Errorf("Error setting path %s: %s", path, err) + log.Error("Error setting path %s: %s", path, err) return nil, err } else if err = meta.Flush(); err != nil { - log.Errorf("Error flushing meta file: %s", err) + log.Error("Error flushing meta file: %s", err) return nil, err } @@ -121,24 +121,24 @@ func CreateMeta(path string, values *Meta) (meta *Meta, err error) { } func OpenMeta(path string) (meta *Meta, err error) { - log.Debugf("Opening meta from '%s' ...", path) + log.Debug("Opening meta from '%s' ...", path) buff, err := ioutil.ReadFile(path) if err != nil { - log.Errorf("Error opening %s: %s", path, err) + log.Error("Error opening %s: %s", path, err) return nil, err } var m Meta if err = json.Unmarshal(buff, &m); err != nil { - log.Errorf("Error while parsing json from buffer '%s': %s", string(buff), err) + log.Error("Error while parsing json from buffer '%s': %s", string(buff), err) return nil, err } m.SetPath(path) meta = &m - // log.Debugf("%+v", meta) + // log.Debug("%+v", meta) return meta, nil } diff --git a/db/record.go b/db/record.go index 45ec830..aa7a093 100644 --- a/db/record.go +++ b/db/record.go @@ -11,7 +11,7 @@ import ( "compress/gzip" "fmt" "github.com/evilsocket/arc/config" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/evilsocket/arc/utils" "io" "os" @@ -26,10 +26,10 @@ type Record struct { } func CreateRecord(root_path string, meta *Meta, reader *io.Reader) (record *Record, err error) { - log.Debugf("Creating record %d:'%s' on '%s' with reader %v...", meta.Id, meta.Title, root_path, reader) + log.Debug("Creating record %d:'%s' on '%s' with reader %v...", meta.Id, meta.Title, root_path, reader) if root_path, err = utils.ExpandPath(root_path); err != nil { - log.Errorf("Error expanding %s: %s", root_path, err) + log.Error("Error expanding %s: %s", root_path, err) return nil, err } @@ -37,9 +37,9 @@ func CreateRecord(root_path string, meta *Meta, reader *io.Reader) (record *Reco meta_path := filepath.Join(record_path, "meta.json") if utils.Exists(record_path) == false { - log.Debugf("Creating record path '%s' ...", record_path) + log.Debug("Creating record path '%s' ...", record_path) if err = os.MkdirAll(record_path, os.ModePerm); err != nil { - log.Errorf("Error creating folder %s: %s", record_path, err) + log.Error("Error creating folder %s: %s", record_path, err) return nil, err } } @@ -63,7 +63,7 @@ func CreateRecord(root_path string, meta *Meta, reader *io.Reader) (record *Reco if reader != nil { err = record.UpdateBuffer(*reader) if err != nil { - log.Errorf("Error while updating the buffer: %s", err) + log.Error("Error while updating the buffer: %s", err) return nil, err } } @@ -72,10 +72,10 @@ func CreateRecord(root_path string, meta *Meta, reader *io.Reader) (record *Reco } func OpenRecord(path string) (record *Record, err error) { - log.Debugf("Opening record from path '%s' ...", path) + log.Debug("Opening record from path '%s' ...", path) if path, err = utils.ExpandPath(path); err != nil { - log.Errorf("Error expanding %s: %s", path, err) + log.Error("Error expanding %s: %s", path, err) return nil, err } @@ -161,39 +161,39 @@ func (r *Record) Unlock() error { } func (r *Record) Update(meta *Meta) (err error) { - log.Debugf("Updating record '%s' meta.", r.path) + log.Debug("Updating record '%s' meta.", r.path) return r.meta.Update(meta) } func (r *Record) compress() (err error) { datapath := r.DataPath() - log.Debugf("Compressing buffer %s ...", datapath) + log.Debug("Compressing buffer %s ...", datapath) start := time.Now() reader, err := os.Open(datapath) if err != nil { - log.Errorf("Error while opening %s: %s.", datapath, err) + log.Error("Error while opening %s: %s.", datapath, err) return err } tmp_filename := datapath + ".tmp.gz" writer, err := os.Create(tmp_filename) if err != nil { - log.Errorf("Error while creating %s: %s.", tmp_filename, err) + log.Error("Error while creating %s: %s.", tmp_filename, err) return err } defer writer.Close() gzipper, err := gzip.NewWriterLevel(writer, gzip.BestCompression) if err != nil { - log.Errorf("Error while creating gzipper: %s.", err) + log.Error("Error while creating gzipper: %s.", err) return err } _, err = io.Copy(gzipper, reader) if err != nil { - log.Errorf("Error while compressing %s: %s.", tmp_filename, err) + log.Error("Error while compressing %s: %s.", tmp_filename, err) return err } @@ -205,7 +205,7 @@ func (r *Record) compress() (err error) { err = os.Rename(tmp_filename, datapath) if err != nil { - log.Errorf("Error while renaming %s to %s: %s.", tmp_filename, datapath, err) + log.Error("Error while renaming %s to %s: %s.", tmp_filename, datapath, err) return err } @@ -219,7 +219,7 @@ func (r *Record) compress() (err error) { Size += r.meta.Size bps := uint64(float64(r.meta.Size) / elapsed.Seconds()) - log.Infof("Compressed %s in %s (%s/s).", utils.FormatBytes(r.meta.Size), elapsed, utils.FormatBytes(bps)) + log.Info("Compressed %s in %s (%s/s).", utils.FormatBytes(r.meta.Size), elapsed, utils.FormatBytes(bps)) return nil } @@ -234,17 +234,17 @@ func (r *Record) UpdateBuffer(reader io.Reader) (err error) { Size -= uint64(stats.Size()) } - log.Debugf("Writing buffer to %s ...", datapath) + log.Debug("Writing buffer to %s ...", datapath) start := time.Now() writer, err := os.Create(datapath) if err != nil { - log.Errorf("Error while creating %s: %s.", datapath, err) + log.Error("Error while creating %s: %s.", datapath, err) return err } written, err := io.Copy(writer, reader) if err != nil { - log.Errorf("Error while writing to %s: %s.", datapath, err) + log.Error("Error while writing to %s: %s.", datapath, err) writer.Close() return err } @@ -258,13 +258,13 @@ func (r *Record) UpdateBuffer(reader io.Reader) (err error) { r.meta.FlushNoLock() Size += r.meta.Size - log.Debugf("Wrote %s (%d b) in %s ...", utils.FormatBytes(r.meta.Size), r.meta.Size, elapsed) + log.Debug("Wrote %s (%d b) in %s ...", utils.FormatBytes(r.meta.Size), r.meta.Size, elapsed) if config.Conf.Compression && r.meta.Size > 1024 { go func() { err := r.compress() if err != nil { - log.Errorf("Error while compressing %s: %s.", datapath, err) + log.Error("Error while compressing %s: %s.", datapath, err) } }() } @@ -283,7 +283,7 @@ func (r *Record) New(meta *Meta, reader io.Reader) (child *Record, err error) { r.Lock() defer r.Unlock() - log.Debugf("Creating new record '%s' for parent %s.", meta.Title, r.path) + log.Debug("Creating new record '%s' for parent %s.", meta.Title, r.path) meta.Id = r.meta.NextId @@ -331,7 +331,7 @@ func (r *Record) Get(id uint64) *Record { } func (r *Record) Close() { - log.Debugf("Closing record %s ...", r.path) + log.Debug("Closing record %s ...", r.path) r.meta.Close() } @@ -339,7 +339,7 @@ func (r *Record) Delete() error { for _, child := range r.children.records { child.Delete() } - log.Debugf("Deleting record %s ...", r.path) + log.Debug("Deleting record %s ...", r.path) Size -= r.Size() return os.RemoveAll(r.path) } diff --git a/db/setup.go b/db/setup.go index ed817b6..1d9833e 100644 --- a/db/setup.go +++ b/db/setup.go @@ -9,8 +9,9 @@ package db import ( "github.com/evilsocket/arc/config" - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "os" "time" ) @@ -31,10 +32,10 @@ func Setup() (created bool, err error) { if _, err = os.Stat(config.Conf.Database); os.IsNotExist(err) { created = true - log.Warningf("Creating database %s ...", log.Bold(config.Conf.Database)) + log.Warning("Creating database %s ...", tui.Bold(config.Conf.Database)) } else { created = false - log.Infof("Loading database %s ...", log.Bold(config.Conf.Database)) + log.Info("Loading database %s ...", tui.Bold(config.Conf.Database)) } dbIndex, err = LoadIndex(config.Conf.Database) @@ -50,8 +51,8 @@ func Setup() (created bool, err error) { elapsed := time.Since(started) - log.Debugf(" dbNextId=%d", dbNextId) - log.Infof("%s of records loaded in %s.", utils.FormatBytes(Size), elapsed) + log.Debug(" dbNextId=%d", dbNextId) + log.Info("%s of records loaded in %s.", utils.FormatBytes(Size), elapsed) return created, nil } diff --git a/events/main.go b/events/main.go index fc4036a..cdebf5d 100644 --- a/events/main.go +++ b/events/main.go @@ -11,7 +11,7 @@ import ( "crypto/tls" "fmt" "github.com/evilsocket/arc/config" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/evilsocket/arc/pgp" "github.com/evilsocket/arc/utils" "gopkg.in/gomail.v2" @@ -58,7 +58,7 @@ func rateLimit(event Event) bool { func Report(event Event) { if rateLimit(event) == true { - log.Importantf("Dropping event '%s' because of rate limiting.", event.Title) + log.Warning("Dropping event '%s' because of rate limiting.", event.Title) return } @@ -67,7 +67,7 @@ func Report(event Event) { repotype = "PGP encrypted" } - log.Infof("Reporting %s event '%s' to %s ...", repotype, event.Title, config.Conf.Scheduler.Reports.To) + log.Info("Reporting %s event '%s' to %s ...", repotype, event.Title, config.Conf.Scheduler.Reports.To) smtp := config.Conf.Scheduler.Reports.SMTP d := gomail.NewDialer(smtp.Address, smtp.Port, smtp.Username, smtp.Password) @@ -84,14 +84,14 @@ func Report(event Event) { if pgpConf.Enabled { ctype = "text/plain" if err, body = pgp.Encrypt(body); err != nil { - log.Errorf("Could not PGP encrypt the message: %s.", err) + log.Error("Could not PGP encrypt the message: %s.", err) } } m.SetBody(ctype, body) if err := d.DialAndSend(m); err != nil { - log.Errorf("Error: %s.", err) + log.Error("Error: %s.", err) } } @@ -99,7 +99,7 @@ func Add(event Event) { lock.Lock() defer lock.Unlock() Pool = append([]Event{event}, Pool...) - log.Infof("New event (Pool size is %d): %s.", len(Pool), event) + log.Info("New event (Pool size is %d): %s.", len(Pool), event) if config.Conf.Scheduler.Reports.Enabled && utils.InSlice(event.Name, config.Conf.Scheduler.Reports.Filter) == true { go Report(event) @@ -110,7 +110,7 @@ func Clear() { lock.Lock() defer lock.Unlock() Pool = make([]Event, 0) - log.Debugf("Events Pool has been cleared.") + log.Debug("Events Pool has been cleared.") } func AddNew(name, title, description string) Event { diff --git a/events/templates.go b/events/templates.go index a68c210..5699e40 100644 --- a/events/templates.go +++ b/events/templates.go @@ -9,7 +9,7 @@ package events import ( "bytes" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "html/template" ) @@ -21,7 +21,7 @@ func T(name, value string) *template.Template { func Populate(t *template.Template, data interface{}) string { var b bytes.Buffer if err := t.Execute(&b, data); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } return b.String() } diff --git a/go.mod b/go.mod index a8de182..50fefc7 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.13 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/elazarl/go-bindata-assetfs v1.0.0 + github.com/evilsocket/islazy v1.10.6 github.com/gin-gonic/contrib v0.0.0-20190923054218-35076c1b2bea github.com/gin-gonic/gin v1.4.0 github.com/jteeuwen/go-bindata v3.0.7+incompatible // indirect diff --git a/go.sum b/go.sum index 6e841c7..d30b467 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk= github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= +github.com/evilsocket/islazy v1.10.6 h1:MFq000a1ByoumoJWlytqg0qon0KlBeUfPsDjY0hK0bo= +github.com/evilsocket/islazy v1.10.6/go.mod h1:OrwQGYg3DuZvXUfmH+KIZDjwTCbrjy48T24TUpGqVVw= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-gonic/contrib v0.0.0-20190923054218-35076c1b2bea h1:tPQfr1S0mubDv/jvdbS1xbKOJzDgvIHi7db/MYr4EKg= diff --git a/log/log.go b/log/log.go deleted file mode 100644 index 1f52459..0000000 --- a/log/log.go +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Arc - Copyleft of Simone 'evilsocket' Margaritelli. - * evilsocket at protonmail dot com - * https://www.evilsocket.net/ - * - * See LICENSE. - */ -package log - -import ( - "fmt" - "github.com/gin-gonic/gin" - "os" - "strings" - "sync" - "time" -) - -type Handler func(format string, args ...interface{}) - -// https://misc.flogisoft.com/bash/tip_colors_and_formatting -const ( - BOLD = "\033[1m" - DIM = "\033[2m" - - FG_BLACK = "\033[30m" - FG_WHITE = "\033[97m" - - BG_DGRAY = "\033[100m" - BG_RED = "\033[41m" - BG_GREEN = "\033[42m" - BG_YELLOW = "\033[43m" - BG_LBLUE = "\033[104m" - - RESET = "\033[0m" -) - -const ( - DEBUG = iota - INFO - IMPORTANT - WARNING - ERROR - FATAL -) - -var ( - WithColors = true - Output = os.Stderr - DateFormat = "2006-01-02 15:04:05" - MinLevel = DEBUG - - mutex = &sync.Mutex{} - labels = map[int]string{ - DEBUG: "DBG", - INFO: "INF", - IMPORTANT: "IMP", - WARNING: "WAR", - ERROR: "ERR", - FATAL: "!!!", - } - colors = map[int]string{ - DEBUG: DIM + FG_BLACK + BG_DGRAY, - INFO: FG_WHITE + BG_GREEN, - IMPORTANT: FG_WHITE + BG_LBLUE, - WARNING: FG_WHITE + BG_YELLOW, - ERROR: FG_WHITE + BG_RED, - FATAL: FG_WHITE + BG_RED + BOLD, - } -) - -func Wrap(s, effect string) string { - if WithColors == true { - s = effect + s + RESET - } - return s -} - -func Dim(s string) string { - return Wrap(s, DIM) -} - -func Bold(s string) string { - return Wrap(s, BOLD) -} - -func Raw(s string) { - mutex.Lock() - defer mutex.Unlock() - fmt.Fprintf(Output, "%s", s) -} - -func Log(level int, format string, args ...interface{}) { - if level >= MinLevel { - mutex.Lock() - defer mutex.Unlock() - - label := labels[level] - color := colors[level] - when := time.Now().UTC().Format(DateFormat) - - what := fmt.Sprintf(format, args...) - if strings.HasSuffix(what, "\n") == false { - what += "\n" - } - - l := Dim("[%s]") - r := Wrap(" %s ", color) + " %s" - - fmt.Fprintf(Output, l+" "+r, when, label, what) - } -} - -func Debugf(format string, args ...interface{}) { - Log(DEBUG, format, args...) -} - -func Infof(format string, args ...interface{}) { - Log(INFO, format, args...) -} - -func Importantf(format string, args ...interface{}) { - Log(IMPORTANT, format, args...) -} - -func Warningf(format string, args ...interface{}) { - Log(WARNING, format, args...) -} - -func Errorf(format string, args ...interface{}) { - Log(ERROR, format, args...) -} - -func Fatal(err error) { - Log(FATAL, "%s", err) - os.Exit(1) -} - -func Api(level int, c *gin.Context, format string, args ...interface{}) { - who := strings.Split(c.Request.RemoteAddr, ":")[0] - req := fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path) - format = fmt.Sprintf("%s '%s' > %s", who, req, format) - Log(level, format, args...) -} diff --git a/middlewares/auth.go b/middlewares/auth.go index 8b3d3e3..44a90f9 100644 --- a/middlewares/auth.go +++ b/middlewares/auth.go @@ -11,8 +11,8 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/evilsocket/arc/config" "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" "github.com/evilsocket/arc/utils" + "github.com/evilsocket/islazy/log" "github.com/gin-gonic/gin" "regexp" "strings" @@ -62,7 +62,7 @@ func AuthHandler() gin.HandlerFunc { token := m[1] valid, err := ValidateToken(token, config.Conf.Secret) if err != nil { - log.Api(log.WARNING, c, "Error while validating bearer token: %s", err) + utils.Api(log.WARNING, c, "Error while validating bearer token: %s", err) events.Add(events.InvalidToken(strings.Split(c.Request.RemoteAddr, ":")[0], token, err)) diff --git a/middlewares/security.go b/middlewares/security.go index 51edc7c..5758c34 100644 --- a/middlewares/security.go +++ b/middlewares/security.go @@ -9,7 +9,7 @@ package middlewares import ( "fmt" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "github.com/gin-gonic/gin" "gopkg.in/unrolled/secure.v1" "strings" @@ -29,7 +29,7 @@ func Security(tlsFingerprint string) gin.HandlerFunc { if err != nil { who := strings.Split(c.Request.RemoteAddr, ":")[0] req := fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path) - log.Warningf("%s > %s | Security exception: %s", who, req, err) + log.Warning("%s > %s | Security exception: %s", who, req, err) c.Abort() return } diff --git a/middlewares/static.go b/middlewares/static.go index 33e12d6..e065102 100644 --- a/middlewares/static.go +++ b/middlewares/static.go @@ -10,7 +10,7 @@ package middlewares // This middleware is a variation of github.com/gin-gonic/contrib/static // created because of this https://github.com/evilsocket/arc/issues/64 import ( - // "github.com/evilsocket/arc/log" + // "github.com/evilsocket/islazy/log" "github.com/gin-gonic/gin" "net/http" "os" @@ -54,7 +54,7 @@ func (l *localFileSystem) Exists(prefix string, filepath string) bool { } func ServeStatic(url, root, index string) gin.HandlerFunc { - // log.Debugf("Creating static middleware for path %s (index=%s)", log.Bold(root), index) + // log.Debug("Creating static middleware for path %s (index=%s)", tui.Bold(root), index) return Serve(url, Static(root, index)) } @@ -68,7 +68,7 @@ func Serve(url string, fs ServeFileSystem) gin.HandlerFunc { path := c.Request.URL.Path // Fixes https://github.com/evilsocket/arc/issues/64 if path == "/" { - // log.Debugf("Fixing request path / to %s.", log.Bold(IndexFile)) + // log.Debug("Fixing request path / to %s.", tui.Bold(IndexFile)) path = IndexFile } diff --git a/pgp/keys.go b/pgp/keys.go index 6a211de..35fbca7 100644 --- a/pgp/keys.go +++ b/pgp/keys.go @@ -12,7 +12,8 @@ import ( "crypto/rsa" _ "crypto/sha256" "fmt" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/packet" @@ -37,7 +38,7 @@ func LoadKey(filename string, private bool) error { desc = "private" } - log.Infof("Loading PGP %s key from %s ...", desc, log.Bold(filename)) + log.Info("Loading PGP %s key from %s ...", desc, tui.Bold(filename)) in, err := os.Open(filename) if err != nil { @@ -104,7 +105,7 @@ func SaveKey(out io.Writer, key *rsa.PrivateKey, private bool) (err error) { } func GenerateKeys(private, public string) error { - log.Warningf("Generating %d bits RSA key (this may take a few seconds) ...", RSA_BITS) + log.Warning("Generating %d bits RSA key (this may take a few seconds) ...", RSA_BITS) key, err := rsa.GenerateKey(rand.Reader, RSA_BITS) if err != nil { @@ -127,13 +128,13 @@ func GenerateKeys(private, public string) error { return err } - log.Infof("RSA private key saved to %s.", log.Bold(private)) + log.Info("RSA private key saved to %s.", tui.Bold(private)) if err := SaveKey(pub, key, false); err != nil { return err } - log.Infof("RSA public key saved to %s.", log.Bold(public)) + log.Info("RSA public key saved to %s.", tui.Bold(public)) return nil } diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 60f09f2..79b1035 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -10,14 +10,14 @@ package scheduler import ( "github.com/evilsocket/arc/db" "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "time" ) func worker(secs int) { period := time.Duration(secs) * time.Second - log.Debugf("Scheduler started with a %v period.", period) + log.Debug("Scheduler started with a %v period.", period) for { time.Sleep(period) @@ -34,9 +34,9 @@ func worker(secs int) { } if meta.Prune { - log.Infof("Pruning record %d ( %s ) ...", meta.Id, meta.Title) + log.Info("Pruning record %d ( %s ) ...", meta.Id, meta.Title) if _, err := store.Del(meta.Id); err != nil { - log.Errorf("Error while deleting record %d: %s.", meta.Id, err) + log.Error("Error while deleting record %d: %s.", meta.Id, err) } } } diff --git a/tls/cert.go b/tls/cert.go index 4cc6633..261bf00 100644 --- a/tls/cert.go +++ b/tls/cert.go @@ -17,7 +17,8 @@ import ( "encoding/pem" "errors" "github.com/evilsocket/arc/config" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" + "github.com/evilsocket/islazy/tui" "io/ioutil" "math/big" "os" @@ -37,14 +38,14 @@ func Generate(conf *config.Configuration) error { } defer certfile.Close() - log.Debugf("Generating RSA key ...") + log.Debug("Generating RSA key ...") priv, err := rsa.GenerateKey(rand.Reader, 4096) if err != nil { return err } - log.Debugf("Creating X509 certificate ...") + log.Debug("Creating X509 certificate ...") notBefore := time.Now() notAfter := notBefore.Add(time.Duration(24*365) * time.Hour) @@ -73,12 +74,12 @@ func Generate(conf *config.Configuration) error { return err } - log.Importantf("Saving key to %s ...", log.Bold(conf.Key)) + log.Warning("Saving key to %s ...", tui.Bold(conf.Key)) if err := pem.Encode(keyfile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil { return err } - log.Importantf("Saving certificate to %s ...", log.Bold(conf.Certificate)) + log.Warning("Saving certificate to %s ...", tui.Bold(conf.Certificate)) return pem.Encode(certfile, &pem.Block{Type: "CERTIFICATE", Bytes: cert_raw}) } diff --git a/updater/updater.go b/updater/updater.go index fe5a17b..21ab652 100644 --- a/updater/updater.go +++ b/updater/updater.go @@ -9,7 +9,7 @@ package updater import ( "github.com/evilsocket/arc/events" - "github.com/evilsocket/arc/log" + "github.com/evilsocket/islazy/log" "net/http" "regexp" "time" @@ -26,35 +26,35 @@ func worker(currVersion string) { } for { - log.Debugf("Checking for newer versions ...") + log.Debug("Checking for newer versions ...") req, _ := http.NewRequest("GET", "https://github.com/evilsocket/arc/releases/latest", nil) resp, err := client.Do(req) if err != nil { if err := events.Setup(); err != nil { - log.Fatal(err) + log.Fatal("%v", err) } - log.Errorf("Error while checking latest version: %s.", err) + log.Error("Error while checking latest version: %s.", err) return } defer resp.Body.Close() location := resp.Header.Get("Location") - log.Debugf("Location header = '%s'", location) + log.Debug("Location header = '%s'", location) m := versionParser.FindStringSubmatch(location) if len(m) == 2 { latest := m[1] - log.Debugf("Latest version is '%s'", latest) + log.Debug("Latest version is '%s'", latest) if currVersion != latest { - log.Importantf("Update to %s available at %s.", latest, location) + log.Warning("Update to %s available at %s.", latest, location) events.Add(events.UpdateAvailable(currVersion, latest, location)) } else { - log.Debugf("No updates available.") + log.Debug("No updates available.") } } else { - log.Warningf("Unexpected location header: '%s'.", location) + log.Warning("Unexpected location header: '%s'.", location) } time.Sleep(interval) diff --git a/utils/json.go b/utils/json.go index 4d4b025..e3422e6 100644 --- a/utils/json.go +++ b/utils/json.go @@ -8,8 +8,10 @@ package utils import ( - "github.com/evilsocket/arc/log" + "fmt" + "github.com/evilsocket/islazy/log" "github.com/gin-gonic/gin" + "strings" ) // ErrorResponse is used when sending a HTTP status response different than 200 @@ -25,8 +27,20 @@ type ErrorResponse struct { Message string `json:"message"` } -func jError(level int, c *gin.Context, code int, message string) { - log.Api(level, c, "[%d] %s", code, message) +func Api(level log.Verbosity, c *gin.Context, format string, args ...interface{}) { + who := strings.Split(c.Request.RemoteAddr, ":")[0] + req := fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path) + format = fmt.Sprintf("%s '%s' > %s", who, req, format) + if level == log.WARNING { + log.Warning(format, args...) + + } else { + log.Error(format, args...) + } +} + +func jError(level log.Verbosity, c *gin.Context, code int, message string) { + Api(level, c, "[%d] %s", code, message) c.JSON(code, ErrorResponse{ Code: code, Message: message,