refact: refactored several strings.Trim calls to core.Trim

This commit is contained in:
evilsocket 2018-02-05 18:09:59 +01:00
parent 5189068cd3
commit ca9f4f0945
10 changed files with 35 additions and 23 deletions

View file

@ -9,6 +9,14 @@ import (
"strings" "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) { func Exec(executable string, args []string) (string, error) {
path, err := exec.LookPath(executable) path, err := exec.LookPath(executable)
if err != nil { 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) fmt.Printf("ERROR: path=%s args=%s err=%s out='%s'\n", path, args, err, raw)
return "", err return "", err
} else { } else {
return strings.Trim(string(raw), "\r\n\t "), nil return Trim(string(raw)), nil
} }
} }

View file

@ -151,13 +151,13 @@ func (f PfFirewall) EnableRedirection(r *Redirection, enabled bool) error {
lines := "" lines := ""
scanner := bufio.NewScanner(fd) scanner := bufio.NewScanner(fd)
for scanner.Scan() { for scanner.Scan() {
line := strings.Trim(scanner.Text(), "\r\n\t ") line := core.Trim(scanner.Text())
if line != rule { if line != rule {
lines += line + "\n" lines += line + "\n"
} }
} }
if strings.Trim(lines, "\r\n\t ") == "" { if core.Trim(lines) == "" {
os.Remove(f.filename) os.Remove(f.filename)
f.enable(false) f.enable(false)
} else { } else {

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/evilsocket/bettercap-ng/core" "github.com/evilsocket/bettercap-ng/core"
) )
@ -57,7 +56,7 @@ func (f LinuxFirewall) IsForwardingEnabled() bool {
if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil { if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil {
return false return false
} else { } else {
return strings.Trim(string(out), "\r\n\t ") == "1" return core.Trim(string(out)) == "1"
} }
} }

View file

@ -6,6 +6,8 @@ import (
"strings" "strings"
"github.com/elazarl/goproxy" "github.com/elazarl/goproxy"
"github.com/evilsocket/bettercap-ng/core"
) )
type JSResponse struct { 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) resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body)
if j.Headers != "" { if j.Headers != "" {
for _, header := range strings.Split(j.Headers, "\n") { for _, header := range strings.Split(j.Headers, "\n") {
header = strings.Trim(header, "\n\r\t ") header = core.Trim(header)
if header == "" { if header == "" {
continue continue
} }

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"net" "net"
"regexp" "regexp"
"strings"
"github.com/evilsocket/bettercap-ng/core" "github.com/evilsocket/bettercap-ng/core"
"github.com/evilsocket/bettercap-ng/log" "github.com/evilsocket/bettercap-ng/log"
@ -53,7 +52,7 @@ func NewWOL(s *session.Session) *WOL {
func parseMAC(args []string) (string, error) { func parseMAC(args []string) (string, error) {
mac := "ff:ff:ff:ff:ff:ff" mac := "ff:ff:ff:ff:ff:ff"
if len(args) == 1 { if len(args) == 1 {
tmp := strings.Trim(args[0], "\r\n\t ") tmp := core.Trim(args[0])
if tmp != "" { if tmp != "" {
if reMAC.MatchString(tmp) == false { if reMAC.MatchString(tmp) == false {
return "", fmt.Errorf("%s is not a valid MAC address.", tmp) return "", fmt.Errorf("%s is not a valid MAC address.", tmp)

View file

@ -2,6 +2,8 @@ package net
import ( import (
"strings" "strings"
"github.com/evilsocket/bettercap-ng/core"
) )
var ( var (
@ -18,7 +20,7 @@ func OuiInit() {
lines := strings.Split(data, "\n") lines := strings.Split(data, "\n")
for _, line := range lines { for _, line := range lines {
line = strings.Trim(line, " \n\r\t") line = core.Trim(line)
if len(line) == 0 || line[0] == '#' { if len(line) == 0 || line[0] == '#' {
continue continue
} }
@ -28,8 +30,8 @@ func OuiInit() {
continue continue
} }
prefix := strings.ToLower(strings.Trim(parts[0], " \n\r\t")) prefix := strings.ToLower(core.Trim(parts[0]))
vendor := strings.Trim(parts[1], " \n\r\t") vendor := core.Trim(parts[1])
oui[prefix] = vendor oui[prefix] = vendor
} }

View file

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"sync" "sync"
"github.com/evilsocket/bettercap-ng/core"
) )
type Module interface { type Module interface {
@ -62,7 +64,7 @@ func (m SessionModule) ListParam(name string) (err error, values []string) {
} else { } else {
parts := strings.Split(list, ",") parts := strings.Split(list, ",")
for _, part := range parts { for _, part := range parts {
part = strings.Trim(part, "\t\n\r ") part = core.Trim(part)
if part != "" { if part != "" {
values = append(values, part) values = append(values, part)
} }

View file

@ -52,7 +52,7 @@ type Session struct {
func ParseCommands(buffer string) []string { func ParseCommands(buffer string) []string {
cmds := make([]string, 0) cmds := make([]string, 0)
for _, cmd := range strings.Split(buffer, ";") { for _, cmd := range strings.Split(buffer, ";") {
cmd = strings.Trim(cmd, "\r\n\t ") cmd = core.Trim(cmd)
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') { if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
} }
@ -338,7 +338,7 @@ func (s *Session) RunCaplet(filename string) error {
} }
func (s *Session) Run(line string) error { func (s *Session) Run(line string) error {
line = strings.TrimRight(line, " ") line = core.TrimRight(line)
for _, h := range s.CoreHandlers { for _, h := range s.CoreHandlers {
if parsed, args := h.Parse(line); parsed == true { if parsed, args := h.Parse(line); parsed == true {
return h.Exec(args, s) return h.Exec(args, s)

View file

@ -15,7 +15,7 @@ import (
func (s *Session) helpHandler(args []string, sess *Session) error { func (s *Session) helpHandler(args []string, sess *Session) error {
filter := "" filter := ""
if len(args) == 2 { if len(args) == 2 {
filter = strings.Trim(args[1], "\r\n\t ") filter = core.Trim(args[1])
} }
if filter == "" { if filter == "" {
@ -181,7 +181,7 @@ func (s *Session) shHandler(args []string, sess *Session) error {
func (s *Session) aliasHandler(args []string, sess *Session) error { func (s *Session) aliasHandler(args []string, sess *Session) error {
mac := args[0] mac := args[0]
alias := strings.Trim(args[1], "\r\n\t ") alias := core.Trim(args[1])
if s.Targets.SetAliasFor(mac, alias) == true { if s.Targets.SetAliasFor(mac, alias) == true {
return nil return nil
@ -201,7 +201,7 @@ func (s *Session) registerCoreHandlers() {
"List available commands or show module specific help if no module name is provided.", "List available commands or show module specific help if no module name is provided.",
s.helpHandler), s.helpHandler),
readline.PcItem("help", readline.PcItemDynamic(func(prefix string) []string { readline.PcItem("help", readline.PcItemDynamic(func(prefix string) []string {
prefix = strings.Trim(prefix[4:], "\t\r\n ") prefix = core.Trim(prefix[4:])
modNames := []string{""} modNames := []string{""}
for _, m := range s.Modules { for _, m := range s.Modules {
if prefix == "" || strings.HasPrefix(m.Name(), prefix) == true { 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.", "Get the value of variable NAME, use * for all.",
s.getHandler), s.getHandler),
readline.PcItem("get", readline.PcItemDynamic(func(prefix string) []string { readline.PcItem("get", readline.PcItemDynamic(func(prefix string) []string {
prefix = strings.Trim(prefix[3:], "\t\r\n ") prefix = core.Trim(prefix[3:])
varNames := []string{""} varNames := []string{""}
for key := range s.Env.Storage { for key := range s.Env.Storage {
if prefix == "" || strings.HasPrefix(key, prefix) == true { if prefix == "" || strings.HasPrefix(key, prefix) == true {
@ -249,7 +249,7 @@ func (s *Session) registerCoreHandlers() {
"Set the VALUE of variable NAME.", "Set the VALUE of variable NAME.",
s.setHandler), s.setHandler),
readline.PcItem("set", readline.PcItemDynamic(func(prefix string) []string { readline.PcItem("set", readline.PcItemDynamic(func(prefix string) []string {
prefix = strings.Trim(prefix[3:], "\t\r\n ") prefix = core.Trim(prefix[3:])
varNames := []string{""} varNames := []string{""}
for key, _ := range s.Env.Storage { for key, _ := range s.Env.Storage {
if prefix == "" || strings.HasPrefix(key, prefix) == true { if prefix == "" || strings.HasPrefix(key, prefix) == true {
@ -270,7 +270,7 @@ func (s *Session) registerCoreHandlers() {
"Load and run this caplet in the current session.", "Load and run this caplet in the current session.",
s.includeHandler), s.includeHandler),
readline.PcItem("include", readline.PcItemDynamic(func(prefix string) []string { readline.PcItem("include", readline.PcItemDynamic(func(prefix string) []string {
prefix = strings.Trim(prefix[8:], "\t\r\n ") prefix = core.Trim(prefix[8:])
if prefix == "" { if prefix == "" {
prefix = "." prefix = "."
} }
@ -291,7 +291,7 @@ func (s *Session) registerCoreHandlers() {
"Assign an alias to a given endpoint given its MAC address.", "Assign an alias to a given endpoint given its MAC address.",
s.aliasHandler), s.aliasHandler),
readline.PcItem("alias", readline.PcItemDynamic(func(prefix string) []string { readline.PcItem("alias", readline.PcItemDynamic(func(prefix string) []string {
prefix = strings.Trim(prefix[5:], "\t\r\n ") prefix = core.Trim(prefix[5:])
macs := []string{""} macs := []string{""}
for mac, _ := range s.Targets.Targets { for mac, _ := range s.Targets.Targets {
if prefix == "" || strings.HasPrefix(mac, prefix) == true { if prefix == "" || strings.HasPrefix(mac, prefix) == true {

View file

@ -71,8 +71,8 @@ func (tp *Targets) loadAliases() error {
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
parts := strings.SplitN(line, " ", 2) parts := strings.SplitN(line, " ", 2)
mac := strings.Trim(parts[0], "\r\n\t ") mac := core.Trim(parts[0])
alias := strings.Trim(parts[1], "\r\n\t ") alias := core.Trim(parts[1])
tp.Session.Events.Log(core.DEBUG, " aliases[%s] = '%s'", mac, alias) tp.Session.Events.Log(core.DEBUG, " aliases[%s] = '%s'", mac, alias)
tp.Aliases[mac] = alias tp.Aliases[mac] = alias
} }