From 2cda9c8c6704688140864692fa54012f24c30831 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 11 Jan 2018 12:19:36 +0100 Subject: [PATCH] refact: refactored core constants into swag.go --- core/swag.go | 28 +++++++++++++++++++++ log/log.go | 11 ++++---- session/events.go | 56 ++++++----------------------------------- session/module_param.go | 3 ++- session/session.go | 12 ++++----- 5 files changed, 49 insertions(+), 61 deletions(-) diff --git a/core/swag.go b/core/swag.go index 175de86d..dca94d09 100644 --- a/core/swag.go +++ b/core/swag.go @@ -25,6 +25,34 @@ const ( const ON = GREEN + "✔" + RESET const OFF = RED + "✘" + RESET +const ( + DEBUG = iota + INFO + IMPORTANT + WARNING + ERROR + FATAL +) + +var ( + LogLabels = map[int]string{ + DEBUG: "dbg", + INFO: "inf", + IMPORTANT: "imp", + WARNING: "war", + ERROR: "err", + FATAL: "!!!", + } + LogColors = 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, + } +) + // W for Wrap func W(e, s string) string { return e + s + RESET diff --git a/log/log.go b/log/log.go index c788d35e..712f1382 100644 --- a/log/log.go +++ b/log/log.go @@ -1,25 +1,26 @@ package log import ( + "github.com/evilsocket/bettercap-ng/core" "github.com/evilsocket/bettercap-ng/session" ) func Debug(format string, args ...interface{}) { - session.I.Events.Log(session.DEBUG, format, args...) + session.I.Events.Log(core.DEBUG, format, args...) } func Info(format string, args ...interface{}) { - session.I.Events.Log(session.INFO, format, args...) + session.I.Events.Log(core.INFO, format, args...) } func Warning(format string, args ...interface{}) { - session.I.Events.Log(session.WARNING, format, args...) + session.I.Events.Log(core.WARNING, format, args...) } func Error(format string, args ...interface{}) { - session.I.Events.Log(session.ERROR, format, args...) + session.I.Events.Log(core.ERROR, format, args...) } func Fatal(format string, args ...interface{}) { - session.I.Events.Log(session.FATAL, format, args...) + session.I.Events.Log(core.FATAL, format, args...) } diff --git a/session/events.go b/session/events.go index 9f1bc657..0ad40d4b 100644 --- a/session/events.go +++ b/session/events.go @@ -5,50 +5,8 @@ import ( "os" "sync" "time" -) -const ( - DEBUG = iota - INFO - IMPORTANT - WARNING - ERROR - FATAL -) - -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" -) - -var ( - 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, - } + "github.com/evilsocket/bettercap-ng/core" ) type Event struct { @@ -72,9 +30,9 @@ func NewEvent(tag string, data interface{}) Event { func (e Event) Label() string { log := e.Data.(LogMessage) - label := labels[log.Level] - color := colors[log.Level] - return color + label + RESET + label := core.LogLabels[log.Level] + color := core.LogColors[log.Level] + return color + label + core.RESET } type EventPool struct { @@ -105,9 +63,9 @@ func (p *EventPool) Add(tag string, data interface{}) { } func (p *EventPool) Log(level int, format string, args ...interface{}) { - if level == DEBUG && p.debug == false { + if level == core.DEBUG && p.debug == false { return - } else if level < ERROR && p.silent == true { + } else if level < core.ERROR && p.silent == true { return } @@ -118,7 +76,7 @@ func (p *EventPool) Log(level int, format string, args ...interface{}) { message, }) - if level == FATAL { + if level == core.FATAL { fmt.Fprintf(os.Stderr, "%s\n", message) os.Exit(1) } diff --git a/session/module_param.go b/session/module_param.go index 2b4461eb..58adb0af 100644 --- a/session/module_param.go +++ b/session/module_param.go @@ -2,10 +2,11 @@ package session import ( "fmt" - "github.com/evilsocket/bettercap-ng/core" "regexp" "strconv" "strings" + + "github.com/evilsocket/bettercap-ng/core" ) type ParamType int diff --git a/session/session.go b/session/session.go index 29a1db6f..1b7d3dd2 100644 --- a/session/session.go +++ b/session/session.go @@ -170,7 +170,7 @@ func (s *Session) Start() error { } if s.Gateway, err = net.FindGateway(s.Interface); err != nil { - s.Events.Log(WARNING, "%s", err.Error()) + s.Events.Log(core.WARNING, "%s", err.Error()) } if s.Gateway == nil || s.Gateway.IpAddress == s.Interface.IpAddress { @@ -212,7 +212,7 @@ func (s *Session) Start() error { go func() { <-c fmt.Println() - s.Events.Log(WARNING, "Got SIGTERM") + s.Events.Log(core.WARNING, "Got SIGTERM") s.Close() os.Exit(0) }() @@ -224,12 +224,12 @@ func (s *Session) Start() error { } func (s *Session) ReadLine() (string, error) { - prompt := FG_WHITE + BG_YELLOW + " " + s.Interface.CIDR() + - FG_BLACK + + prompt := core.FG_WHITE + core.BG_YELLOW + " " + s.Interface.CIDR() + + core.FG_BLACK + " > " + s.Interface.IpAddress + " " + core.RESET + - BOLD + " » " + RESET + core.BOLD + " » " + core.RESET s.Input.SetPrompt(prompt) s.Input.Refresh() @@ -237,7 +237,7 @@ func (s *Session) ReadLine() (string, error) { } func (s *Session) RunCaplet(filename string) error { - s.Events.Log(INFO, "Reading from caplet %s ...", filename) + s.Events.Log(core.INFO, "Reading from caplet %s ...", filename) input, err := os.Open(filename) if err != nil {