refact: refactored core constants into swag.go

This commit is contained in:
evilsocket 2018-01-11 12:19:36 +01:00
commit 2cda9c8c67
5 changed files with 49 additions and 61 deletions

View file

@ -25,6 +25,34 @@ const (
const ON = GREEN + "✔" + RESET const ON = GREEN + "✔" + RESET
const OFF = RED + "✘" + 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 // W for Wrap
func W(e, s string) string { func W(e, s string) string {
return e + s + RESET return e + s + RESET

View file

@ -1,25 +1,26 @@
package log package log
import ( import (
"github.com/evilsocket/bettercap-ng/core"
"github.com/evilsocket/bettercap-ng/session" "github.com/evilsocket/bettercap-ng/session"
) )
func Debug(format string, args ...interface{}) { 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{}) { 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{}) { 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{}) { 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{}) { func Fatal(format string, args ...interface{}) {
session.I.Events.Log(session.FATAL, format, args...) session.I.Events.Log(core.FATAL, format, args...)
} }

View file

@ -5,50 +5,8 @@ import (
"os" "os"
"sync" "sync"
"time" "time"
)
const ( "github.com/evilsocket/bettercap-ng/core"
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,
}
) )
type Event struct { type Event struct {
@ -72,9 +30,9 @@ func NewEvent(tag string, data interface{}) Event {
func (e Event) Label() string { func (e Event) Label() string {
log := e.Data.(LogMessage) log := e.Data.(LogMessage)
label := labels[log.Level] label := core.LogLabels[log.Level]
color := colors[log.Level] color := core.LogColors[log.Level]
return color + label + RESET return color + label + core.RESET
} }
type EventPool struct { 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{}) { func (p *EventPool) Log(level int, format string, args ...interface{}) {
if level == DEBUG && p.debug == false { if level == core.DEBUG && p.debug == false {
return return
} else if level < ERROR && p.silent == true { } else if level < core.ERROR && p.silent == true {
return return
} }
@ -118,7 +76,7 @@ func (p *EventPool) Log(level int, format string, args ...interface{}) {
message, message,
}) })
if level == FATAL { if level == core.FATAL {
fmt.Fprintf(os.Stderr, "%s\n", message) fmt.Fprintf(os.Stderr, "%s\n", message)
os.Exit(1) os.Exit(1)
} }

View file

@ -2,10 +2,11 @@ package session
import ( import (
"fmt" "fmt"
"github.com/evilsocket/bettercap-ng/core"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/evilsocket/bettercap-ng/core"
) )
type ParamType int type ParamType int

View file

@ -170,7 +170,7 @@ func (s *Session) Start() error {
} }
if s.Gateway, err = net.FindGateway(s.Interface); err != nil { 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 { if s.Gateway == nil || s.Gateway.IpAddress == s.Interface.IpAddress {
@ -212,7 +212,7 @@ func (s *Session) Start() error {
go func() { go func() {
<-c <-c
fmt.Println() fmt.Println()
s.Events.Log(WARNING, "Got SIGTERM") s.Events.Log(core.WARNING, "Got SIGTERM")
s.Close() s.Close()
os.Exit(0) os.Exit(0)
}() }()
@ -224,12 +224,12 @@ func (s *Session) Start() error {
} }
func (s *Session) ReadLine() (string, error) { func (s *Session) ReadLine() (string, error) {
prompt := FG_WHITE + BG_YELLOW + " " + s.Interface.CIDR() + prompt := core.FG_WHITE + core.BG_YELLOW + " " + s.Interface.CIDR() +
FG_BLACK + core.FG_BLACK +
" > " + " > " +
s.Interface.IpAddress + s.Interface.IpAddress +
" " + core.RESET + " " + core.RESET +
BOLD + " » " + RESET core.BOLD + " » " + core.RESET
s.Input.SetPrompt(prompt) s.Input.SetPrompt(prompt)
s.Input.Refresh() s.Input.Refresh()
@ -237,7 +237,7 @@ func (s *Session) ReadLine() (string, error) {
} }
func (s *Session) RunCaplet(filename 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) input, err := os.Open(filename)
if err != nil { if err != nil {