-
Notifications
You must be signed in to change notification settings - Fork 102
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||
package nmap | ||||||||||
|
||||||||||
import ( | ||||||||||
"bufio" | ||||||||||
"bytes" | ||||||||||
"context" | ||||||||||
"encoding/xml" | ||||||||||
|
@@ -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. | ||||||||||
|
@@ -94,6 +98,55 @@ func (s *Scanner) Streamer(stream io.Writer) *Scanner { | |||||||||
return s | ||||||||||
} | ||||||||||
|
||||||||||
// Version returns the version of the installed nmap binary | ||||||||||
func (s *Scanner) Version() (result any, err error) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
result = splitted[2] | ||||||||||
break | ||||||||||
Comment on lines
+126
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more correct, since multiple nmap binaries can be installed, but only one is in use by the library at any given time.