From 557d7dbe17f90bfe5393ba2aa66e0906ac2b62b4 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 26 Apr 2018 12:41:18 +0200 Subject: [PATCH] completed lint driven refactoring --- .travis.yml | 1 + session/environment.go | 2 +- session/events.go | 2 +- session/module.go | 2 +- session/module_param.go | 17 +++-------------- session/prompt.go | 6 +++--- session/session.go | 31 +++++++++++-------------------- session/session_core_handlers.go | 13 ++++++------- 8 files changed, 27 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5924e887..1e7c725b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ sudo: false language: go go: + - 1.8.x - 1.9.x - 1.10.x - master diff --git a/session/environment.go b/session/environment.go index be3bbfab..e50dcc54 100644 --- a/session/environment.go +++ b/session/environment.go @@ -92,7 +92,7 @@ func (env *Environment) Set(name, value string) string { env.Lock() defer env.Unlock() - old, _ := env.Data[name] + old := env.Data[name] env.Data[name] = value if cb, hasCallback := env.cbs[name]; hasCallback { diff --git a/session/events.go b/session/events.go index 9d4da4d6..b919ade3 100644 --- a/session/events.go +++ b/session/events.go @@ -97,7 +97,7 @@ func (p *EventPool) Add(tag string, data interface{}) { } func (p *EventPool) Log(level int, format string, args ...interface{}) { - if level == core.DEBUG && p.debug == false { + if level == core.DEBUG && !p.debug { return } else if level < core.ERROR && p.silent { return diff --git a/session/module.go b/session/module.go index 1bc6b701..3d17995c 100644 --- a/session/module.go +++ b/session/module.go @@ -157,7 +157,7 @@ func (m *SessionModule) SetRunning(running bool, cb func()) error { }() select { - case _ = <-done: + case <-done: return nil case <-time.After(10 * time.Second): fmt.Printf("%s: Stopping module %s timed out.", core.Yellow(core.Bold("WARNING")), m.Name) diff --git a/session/module_param.go b/session/module_param.go index 98e6e0a2..489b692e 100644 --- a/session/module_param.go +++ b/session/module_param.go @@ -58,7 +58,7 @@ func NewIntParameter(name string, def_value string, desc string) *ModuleParam { func (p ModuleParam) Validate(value string) (error, interface{}) { if p.Validator != nil { - if p.Validator.MatchString(value) == false { + if !p.Validator.MatchString(value) { return fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", core.Bold(p.Name), value, p.Validator.String()), nil } } @@ -88,15 +88,7 @@ const ParamSubnet = "" const ParamRandomMAC = "" func (p ModuleParam) Get(s *Session) (error, interface{}) { - var v string - var found bool - var obj interface{} - var err error - - if found, v = s.Env.Get(p.Name); found == false { - v = "" - } - + _, v := s.Env.Get(p.Name) if v == ParamIfaceName { v = s.Interface.Name() } else if v == ParamIfaceAddress { @@ -108,10 +100,7 @@ func (p ModuleParam) Get(s *Session) (error, interface{}) { rand.Read(hw) v = net.HardwareAddr(hw).String() } - - err, obj = p.Validate(v) - return err, obj - + return p.Validate(v) } func (p ModuleParam) Dump(padding int) string { diff --git a/session/prompt.go b/session/prompt.go index b05ff45f..94f5a45a 100644 --- a/session/prompt.go +++ b/session/prompt.go @@ -39,7 +39,7 @@ var PromptCallbacks = map[string]func(s *Session) string{ }, } -var envRe = regexp.MustCompile("{env\\.([^}]+)}") +var envRe = regexp.MustCompile(`{env\.([^}]+)}`) type Prompt struct { } @@ -50,7 +50,7 @@ func NewPrompt() Prompt { func (p Prompt) Render(s *Session) string { found, prompt := s.Env.Get(PromptVariable) - if found == false { + if !found { prompt = DefaultPrompt } @@ -89,7 +89,7 @@ func (p Prompt) Render(s *Session) string { } // make sure an user error does not screw all terminal - if strings.HasPrefix(prompt, core.RESET) == false { + if !strings.HasPrefix(prompt, core.RESET) { prompt += core.RESET } diff --git a/session/session.go b/session/session.go index 8d2de5c3..7fc626e0 100644 --- a/session/session.go +++ b/session/session.go @@ -2,7 +2,6 @@ package session import ( "bufio" - "bytes" "errors" "fmt" "net" @@ -76,7 +75,7 @@ func ParseCommands(line string) []string { for _, c := range line { switch c { case ';': - if singleQuoted == false && doubleQuoted == false { + if !singleQuoted && !doubleQuoted { finish = true } else { buf += string(c) @@ -191,14 +190,13 @@ func (s *Session) setupReadline() error { } } - tree := make(map[string][]string, 0) - + tree := make(map[string][]string) for _, m := range s.Modules { for _, h := range m.Handlers() { parts := strings.Split(h.Name, " ") name := parts[0] - if _, found := tree[name]; found == false { + if _, found := tree[name]; !found { tree[name] = []string{} } @@ -222,7 +220,7 @@ func (s *Session) setupReadline() error { } history := "" - if *s.Options.NoHistory == false { + if !*s.Options.NoHistory { history, _ = core.ExpandPath(HistoryFile) } @@ -234,11 +232,7 @@ func (s *Session) setupReadline() error { } s.Input, err = readline.NewEx(&cfg) - if err != nil { - return err - } - - return nil + return err } func (s *Session) Close() { @@ -290,11 +284,11 @@ func (s *Session) startNetMon() { // keep reading network events in order to add / update endpoints go func() { for event := range s.Queue.Activities { - if s.Active == false { + if !s.Active { return } - if s.IsOn("net.recon") && event.Source == true { + if s.IsOn("net.recon") && event.Source { addr := event.IP.String() mac := event.MAC.String() @@ -329,7 +323,7 @@ func (s *Session) setupEnv() { s.Env.Set("gateway.address", s.Gateway.IpAddress) s.Env.Set("gateway.mac", s.Gateway.HwAddress) - if found, v := s.Env.Get(PromptVariable); found == false || v == "" { + if found, v := s.Env.Get(PromptVariable); !found || v == "" { s.Env.Set(PromptVariable, DefaultPrompt) } @@ -425,9 +419,9 @@ func (s *Session) Start() error { func (s *Session) Skip(ip net.IP) bool { if ip.IsLoopback() { return true - } else if bytes.Compare(ip, s.Interface.IP) == 0 { + } else if ip.Equal(s.Interface.IP) { return true - } else if bytes.Compare(ip, s.Gateway.IP) == 0 { + } else if ip.Equal(s.Gateway.IP) { return true } return false @@ -485,10 +479,7 @@ func (s *Session) isCapletCommand(line string) (is bool, filename string, argv [ } capspath := core.Trim(os.Getenv("CAPSPATH")) - for _, folder := range core.SepSplit(capspath, ":") { - paths = append(paths, folder) - } - + paths = append(paths, core.SepSplit(capspath, ":")...) file := core.Trim(line) parts := strings.Split(file, " ") argc := len(parts) diff --git a/session/session_core_handlers.go b/session/session_core_handlers.go index ce86b8a2..de2b2d67 100644 --- a/session/session_core_handlers.go +++ b/session/session_core_handlers.go @@ -37,7 +37,7 @@ func (s *Session) helpHandler(args []string, sess *Session) error { fmt.Printf(" "+core.Yellow(pad)+" : %s\n", h.Name, h.Description) } - fmt.Printf(core.Bold("\nModules\n\n")) + fmt.Println(core.Bold("\nModules\n")) maxLen = 0 for _, m := range s.Modules { @@ -85,13 +85,13 @@ func (s *Session) helpHandler(args []string, sess *Session) error { } for _, h := range handlers { - fmt.Printf(h.Help(maxLen)) + fmt.Print(h.Help(maxLen)) } fmt.Println() params := m.Parameters() if len(params) > 0 { - fmt.Printf(" Parameters\n\n") + fmt.Print(" Parameters\n\n") maxLen := 0 for _, h := range params { len := len(h.Name) @@ -101,7 +101,7 @@ func (s *Session) helpHandler(args []string, sess *Session) error { } for _, p := range params { - fmt.Printf(p.Help(maxLen)) + fmt.Print(p.Help(maxLen)) } fmt.Println() } @@ -112,7 +112,7 @@ func (s *Session) helpHandler(args []string, sess *Session) error { func (s *Session) activeHandler(args []string, sess *Session) error { for _, m := range s.Modules { - if m.Running() == false { + if !m.Running() { continue } @@ -343,8 +343,7 @@ func (s *Session) registerCoreHandlers() { prefix = "." } - files := []string{} - files, _ = filepath.Glob(prefix + "*") + files, _ := filepath.Glob(prefix + "*") return files })))