From ca9f4f09453554c64a5a4ec2f01e65f898d0e741 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Mon, 5 Feb 2018 18:09:59 +0100 Subject: [PATCH] refact: refactored several strings.Trim calls to core.Trim --- core/core.go | 10 +++++++++- firewall/firewall_darwin.go | 4 ++-- firewall/firewall_linux.go | 3 +-- modules/http_proxy_js_response.go | 4 +++- modules/wol.go | 3 +-- net/oui.go | 8 +++++--- session/module.go | 4 +++- session/session.go | 4 ++-- session/session_core_handlers.go | 14 +++++++------- session/targets.go | 4 ++-- 10 files changed, 35 insertions(+), 23 deletions(-) diff --git a/core/core.go b/core/core.go index e5ae7478..6bf39adb 100644 --- a/core/core.go +++ b/core/core.go @@ -9,6 +9,14 @@ import ( "strings" ) +func Trim(s string) string { + return strings.Trim(s, "\r\n\t ") +} + +func TrimRight(s string) string { + return strings.TrimRight(s, "\r\n\t ") +} + func Exec(executable string, args []string) (string, error) { path, err := exec.LookPath(executable) if err != nil { @@ -20,7 +28,7 @@ func Exec(executable string, args []string) (string, error) { fmt.Printf("ERROR: path=%s args=%s err=%s out='%s'\n", path, args, err, raw) return "", err } else { - return strings.Trim(string(raw), "\r\n\t "), nil + return Trim(string(raw)), nil } } diff --git a/firewall/firewall_darwin.go b/firewall/firewall_darwin.go index 9c3d3671..41433627 100644 --- a/firewall/firewall_darwin.go +++ b/firewall/firewall_darwin.go @@ -151,13 +151,13 @@ func (f PfFirewall) EnableRedirection(r *Redirection, enabled bool) error { lines := "" scanner := bufio.NewScanner(fd) for scanner.Scan() { - line := strings.Trim(scanner.Text(), "\r\n\t ") + line := core.Trim(scanner.Text()) if line != rule { lines += line + "\n" } } - if strings.Trim(lines, "\r\n\t ") == "" { + if core.Trim(lines) == "" { os.Remove(f.filename) f.enable(false) } else { diff --git a/firewall/firewall_linux.go b/firewall/firewall_linux.go index f77bfd34..b10e8a9a 100644 --- a/firewall/firewall_linux.go +++ b/firewall/firewall_linux.go @@ -4,7 +4,6 @@ import ( "fmt" "io/ioutil" "os" - "strings" "github.com/evilsocket/bettercap-ng/core" ) @@ -57,7 +56,7 @@ func (f LinuxFirewall) IsForwardingEnabled() bool { if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil { return false } else { - return strings.Trim(string(out), "\r\n\t ") == "1" + return core.Trim(string(out)) == "1" } } diff --git a/modules/http_proxy_js_response.go b/modules/http_proxy_js_response.go index b2ceaf57..aa45db98 100644 --- a/modules/http_proxy_js_response.go +++ b/modules/http_proxy_js_response.go @@ -6,6 +6,8 @@ import ( "strings" "github.com/elazarl/goproxy" + + "github.com/evilsocket/bettercap-ng/core" ) type JSResponse struct { @@ -47,7 +49,7 @@ func (j *JSResponse) ToResponse(req *http.Request) (resp *http.Response) { resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body) if j.Headers != "" { for _, header := range strings.Split(j.Headers, "\n") { - header = strings.Trim(header, "\n\r\t ") + header = core.Trim(header) if header == "" { continue } diff --git a/modules/wol.go b/modules/wol.go index eac69632..81759cce 100644 --- a/modules/wol.go +++ b/modules/wol.go @@ -4,7 +4,6 @@ import ( "fmt" "net" "regexp" - "strings" "github.com/evilsocket/bettercap-ng/core" "github.com/evilsocket/bettercap-ng/log" @@ -53,7 +52,7 @@ func NewWOL(s *session.Session) *WOL { func parseMAC(args []string) (string, error) { mac := "ff:ff:ff:ff:ff:ff" if len(args) == 1 { - tmp := strings.Trim(args[0], "\r\n\t ") + tmp := core.Trim(args[0]) if tmp != "" { if reMAC.MatchString(tmp) == false { return "", fmt.Errorf("%s is not a valid MAC address.", tmp) diff --git a/net/oui.go b/net/oui.go index 7ef5219d..26095f8f 100644 --- a/net/oui.go +++ b/net/oui.go @@ -2,6 +2,8 @@ package net import ( "strings" + + "github.com/evilsocket/bettercap-ng/core" ) var ( @@ -18,7 +20,7 @@ func OuiInit() { lines := strings.Split(data, "\n") for _, line := range lines { - line = strings.Trim(line, " \n\r\t") + line = core.Trim(line) if len(line) == 0 || line[0] == '#' { continue } @@ -28,8 +30,8 @@ func OuiInit() { continue } - prefix := strings.ToLower(strings.Trim(parts[0], " \n\r\t")) - vendor := strings.Trim(parts[1], " \n\r\t") + prefix := strings.ToLower(core.Trim(parts[0])) + vendor := core.Trim(parts[1]) oui[prefix] = vendor } diff --git a/session/module.go b/session/module.go index 871b9973..acf0f9ce 100644 --- a/session/module.go +++ b/session/module.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "sync" + + "github.com/evilsocket/bettercap-ng/core" ) type Module interface { @@ -62,7 +64,7 @@ func (m SessionModule) ListParam(name string) (err error, values []string) { } else { parts := strings.Split(list, ",") for _, part := range parts { - part = strings.Trim(part, "\t\n\r ") + part = core.Trim(part) if part != "" { values = append(values, part) } diff --git a/session/session.go b/session/session.go index 087cbafa..d9a4ffb2 100644 --- a/session/session.go +++ b/session/session.go @@ -52,7 +52,7 @@ type Session struct { func ParseCommands(buffer string) []string { cmds := make([]string, 0) for _, cmd := range strings.Split(buffer, ";") { - cmd = strings.Trim(cmd, "\r\n\t ") + cmd = core.Trim(cmd) if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') { cmds = append(cmds, cmd) } @@ -338,7 +338,7 @@ func (s *Session) RunCaplet(filename string) error { } func (s *Session) Run(line string) error { - line = strings.TrimRight(line, " ") + line = core.TrimRight(line) for _, h := range s.CoreHandlers { if parsed, args := h.Parse(line); parsed == true { return h.Exec(args, s) diff --git a/session/session_core_handlers.go b/session/session_core_handlers.go index 2e076be9..336e2147 100644 --- a/session/session_core_handlers.go +++ b/session/session_core_handlers.go @@ -15,7 +15,7 @@ import ( func (s *Session) helpHandler(args []string, sess *Session) error { filter := "" if len(args) == 2 { - filter = strings.Trim(args[1], "\r\n\t ") + filter = core.Trim(args[1]) } if filter == "" { @@ -181,7 +181,7 @@ func (s *Session) shHandler(args []string, sess *Session) error { func (s *Session) aliasHandler(args []string, sess *Session) error { mac := args[0] - alias := strings.Trim(args[1], "\r\n\t ") + alias := core.Trim(args[1]) if s.Targets.SetAliasFor(mac, alias) == true { return nil @@ -201,7 +201,7 @@ func (s *Session) registerCoreHandlers() { "List available commands or show module specific help if no module name is provided.", s.helpHandler), readline.PcItem("help", readline.PcItemDynamic(func(prefix string) []string { - prefix = strings.Trim(prefix[4:], "\t\r\n ") + prefix = core.Trim(prefix[4:]) modNames := []string{""} for _, m := range s.Modules { if prefix == "" || strings.HasPrefix(m.Name(), prefix) == true { @@ -234,7 +234,7 @@ func (s *Session) registerCoreHandlers() { "Get the value of variable NAME, use * for all.", s.getHandler), readline.PcItem("get", readline.PcItemDynamic(func(prefix string) []string { - prefix = strings.Trim(prefix[3:], "\t\r\n ") + prefix = core.Trim(prefix[3:]) varNames := []string{""} for key := range s.Env.Storage { if prefix == "" || strings.HasPrefix(key, prefix) == true { @@ -249,7 +249,7 @@ func (s *Session) registerCoreHandlers() { "Set the VALUE of variable NAME.", s.setHandler), readline.PcItem("set", readline.PcItemDynamic(func(prefix string) []string { - prefix = strings.Trim(prefix[3:], "\t\r\n ") + prefix = core.Trim(prefix[3:]) varNames := []string{""} for key, _ := range s.Env.Storage { if prefix == "" || strings.HasPrefix(key, prefix) == true { @@ -270,7 +270,7 @@ func (s *Session) registerCoreHandlers() { "Load and run this caplet in the current session.", s.includeHandler), readline.PcItem("include", readline.PcItemDynamic(func(prefix string) []string { - prefix = strings.Trim(prefix[8:], "\t\r\n ") + prefix = core.Trim(prefix[8:]) if prefix == "" { prefix = "." } @@ -291,7 +291,7 @@ func (s *Session) registerCoreHandlers() { "Assign an alias to a given endpoint given its MAC address.", s.aliasHandler), readline.PcItem("alias", readline.PcItemDynamic(func(prefix string) []string { - prefix = strings.Trim(prefix[5:], "\t\r\n ") + prefix = core.Trim(prefix[5:]) macs := []string{""} for mac, _ := range s.Targets.Targets { if prefix == "" || strings.HasPrefix(mac, prefix) == true { diff --git a/session/targets.go b/session/targets.go index 2d193999..b4326df3 100644 --- a/session/targets.go +++ b/session/targets.go @@ -71,8 +71,8 @@ func (tp *Targets) loadAliases() error { for scanner.Scan() { line := scanner.Text() parts := strings.SplitN(line, " ", 2) - mac := strings.Trim(parts[0], "\r\n\t ") - alias := strings.Trim(parts[1], "\r\n\t ") + mac := core.Trim(parts[0]) + alias := core.Trim(parts[1]) tp.Session.Events.Log(core.DEBUG, " aliases[%s] = '%s'", mac, alias) tp.Aliases[mac] = alias }