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

Added Version to Scanner, fixed ScanRunner interface bug #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion nmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package nmap

import (
"bufio"
"bytes"
"context"
"encoding/xml"
Expand All @@ -15,9 +16,12 @@ import (
"golang.org/x/sync/errgroup"
)

var _ ScanRunner = (*Scanner)(nil)

// ScanRunner represents something that can run a scan.
type ScanRunner interface {
Run() (result *Run, warnings []string, err error)
Run() (result *Run, warnings *[]string, err error)
Version() (any, error)
}

// Scanner represents n Nmap scanner.
Expand Down Expand Up @@ -94,6 +98,55 @@ func (s *Scanner) Streamer(stream io.Writer) *Scanner {
return s
}

// Version returns the version of the installed nmap binary
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Version returns the version of the installed nmap binary
// Version returns the version of the used nmap binary.

This is more correct, since multiple nmap binaries can be installed, but only one is in use by the library at any given time.

func (s *Scanner) Version() (result any, err error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s *Scanner) Version() (result any, err error) {
func (s *Scanner) Version() (string, error) {

I recommend never using named return parameters when the function signature is implicitly clear enough without.

s.args = append(s.args, "-V")
args := s.args
cmd := exec.CommandContext(s.ctx, s.binaryPath, args...)

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}

err = cmd.Start()
if err != nil {
return nil, err
}

scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
m := scanner.Text()
splitted := strings.Split(m, " ")
if len(splitted) >= 2 {
Comment on lines +124 to +125
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
splitted := strings.Split(m, " ")
if len(splitted) >= 2 {
res:= strings.Split(m, " ")
if len(res) >= 2 {

result = splitted[2]
break
Comment on lines +126 to +127
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = splitted[2]
break
return res[2], nil

}
}

errScanner := bufio.NewScanner(stderr)
var errorOutput = []string{}
for errScanner.Scan() {
m := errScanner.Text()
errorOutput = append(errorOutput, m)
}

err = cmd.Wait()
if err != nil {
return
}

if len(errorOutput) > 0 {
return nil, fmt.Errorf("%v", errorOutput)
}
Comment on lines +131 to +145
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this code? To check that we can run the binary without errors on top of getting the version info? If yes, these should be two separate functions IMO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check all your comments and will make the proposed changes in the first calendar week of december.
I'm really busy at the moment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rush, take all the time you need! :) Thanks again!


return result, nil
}

// Run will run the Scanner with the enabled options.
// You need to create a Run struct and warnings array first so the function can parse it.
func (s *Scanner) Run() (result *Run, warnings *[]string, err error) {
Expand Down
8 changes: 4 additions & 4 deletions xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,20 @@ func TestFormatTableXML(t *testing.T) {

expectedXML := [][]byte{
[]byte(fmt.Sprintf(`<Table key="%s">`, table.Key)),
[]byte(fmt.Sprintf(`<table>`)),
[]byte(`<table>`),
[]byte(fmt.Sprintf(`<elem key="%s">%s</elem>`, table.Tables[0].Elements[0].Key, table.Tables[0].Elements[0].Value)),
[]byte(fmt.Sprintf(`<elem>%s</elem>`, table.Tables[0].Elements[1].Value)),
[]byte(fmt.Sprintf(`</table>`)),
[]byte(`</table>`),
[]byte(fmt.Sprintf(`<table key="%s">`, table.Tables[1].Key)),
[]byte(fmt.Sprintf(`<elem>%s</elem>`, table.Tables[1].Elements[0].Value)),
[]byte(fmt.Sprintf(`<elem>%s</elem>`, table.Tables[1].Elements[1].Value)),
[]byte(fmt.Sprintf(`</table>`)),
[]byte(`</table>`),
[]byte(fmt.Sprintf(`<elem key="%s">%s</elem>`, table.Elements[0].Key, table.Elements[0].Value)),
[]byte(fmt.Sprintf(`<elem key="%s">%s</elem>`, table.Elements[1].Key, table.Elements[1].Value)),
[]byte(fmt.Sprintf(`<elem key="%s">%s</elem>`, table.Elements[2].Key, table.Elements[2].Value)),
[]byte(fmt.Sprintf(`<elem key="%s">%s</elem>`, table.Elements[3].Key, table.Elements[3].Value)),
[]byte(fmt.Sprintf(`<elem>%s</elem>`, table.Elements[4].Value)),
[]byte(fmt.Sprintf(`</Table>`)),
[]byte(`</Table>`),
}

XML, err := xml.Marshal(table)
Expand Down