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"
)
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
}
}

View file

@ -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 {

View file

@ -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"
}
}

View file

@ -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
}

View file

@ -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)

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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 {

View file

@ -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
}