Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nmap error handling #21

Merged
merged 15 commits into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions nmap.go
Expand Up @@ -4,7 +4,6 @@ package nmap
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
Expand Down Expand Up @@ -83,11 +82,14 @@ func (s *Scanner) Run() (*Run, error) {
return nil, ErrScanTimeout
case <-done:
// Scan finished before timeout.
var nmapErrors []NmapErrors
Ullaakut marked this conversation as resolved.
Show resolved Hide resolved
if stderr.Len() > 0 {
return nil, errors.New(strings.Trim(stderr.String(), ".\n"))
for _, v := range RemoveDuplicatesFromSlice(strings.Split(strings.Trim(stderr.String(), "\n"), "\n")) {
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
nmapErrors = append(nmapErrors, NmapErrors{Error: v})
}
}

result, err := Parse(stdout.Bytes())
result, err := Parse(stdout.Bytes(), nmapErrors)
if err != nil {
return nil, fmt.Errorf("unable to parse nmap output: %v", err)
}
Expand Down
18 changes: 18 additions & 0 deletions utils.go
@@ -0,0 +1,18 @@
package nmap

// https://siongui.github.io/2018/04/14/go-remove-duplicates-from-slice-or-array/
func RemoveDuplicatesFromSlice(s []string) []string {
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
m := make(map[string]bool)
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
for _, item := range s {
if _, ok := m[item]; ok {
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
} else {
m[item] = true
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
}
}

var result []string
for item, _ := range m {
Ullaakut marked this conversation as resolved.
Show resolved Hide resolved
result = append(result, item)
}
return result
}
12 changes: 9 additions & 3 deletions xml.go
Expand Up @@ -32,7 +32,12 @@ type Run struct {
TaskProgress []TaskProgress `xml:"taskprogress" json:"task_progress"`
TaskEnd []Task `xml:"taskend" json:"task_end"`

rawXML []byte
NmapErrors []NmapErrors
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
rawXML []byte
}

type NmapErrors struct {
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
Error string
}

// ToFile writes a Run as XML into the specified file path.
Expand Down Expand Up @@ -418,9 +423,10 @@ func (t *Timestamp) UnmarshalXMLAttr(attr xml.Attr) (err error) {

// Parse takes a byte array of nmap xml data and unmarshals it into a
// Run struct.
func Parse(content []byte) (*Run, error) {
func Parse(content []byte, nmapErrors []NmapErrors) (*Run, error) {
TerminalFi marked this conversation as resolved.
Show resolved Hide resolved
r := &Run{
rawXML: content,
rawXML: content,
NmapErrors: nmapErrors,
}

err := xml.Unmarshal(content, r)
Expand Down