Skip to content

Commit

Permalink
closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
leesoh committed Jul 13, 2022
1 parent b50462f commit 40d5ccf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
21 changes: 21 additions & 0 deletions internal/result/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (r *Result) Print() {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
r.hostPrinter(hh)
r.portPrinter(hh)
}
Expand All @@ -33,6 +36,9 @@ func (r *Result) PrintHost(h *Host) {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
if hh.IP.Equal(h.IP) {
r.hostPrinter(hh)
} else if hh.Name != "" && hh.Name == h.Name {
Expand Down Expand Up @@ -89,6 +95,9 @@ func (r *Result) PrintAlive() {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
if r.allPortsClosed(hh) {
r.Logger.Debugf("all ports closed: %v", hh.IP)
continue
Expand All @@ -111,6 +120,9 @@ func (r *Result) PrintByService(service string) {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
if r.allPortsClosed(hh) {
continue
}
Expand Down Expand Up @@ -142,6 +154,9 @@ func (r *Result) PrintServices() {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
for k, v := range hh.TCPPorts {
r.Logger.Debugf("matched: %v", hh.GetName())
s := r.formatService(hh.GetName(), k, v.Name)
Expand Down Expand Up @@ -169,6 +184,9 @@ func (r *Result) PrintByPort(port []int) {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
if r.allPortsClosed(hh) {
continue
}
Expand All @@ -192,6 +210,9 @@ func (r *Result) PrintPortSummary() {
if hh.IsExcluded(r.Exclude) {
continue
}
if hh.NotInCIDR(r.CIDR) {
continue
}
for k := range hh.TCPPorts {
p[k] = struct{}{}
}
Expand Down
20 changes: 19 additions & 1 deletion internal/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Result struct {
Logger *cli.Logger
Hosts []*Host
Exclude []string
CIDR *net.IPNet
}

func (r *Result) SortByIP() {
Expand All @@ -21,11 +22,24 @@ func (r *Result) SortByIP() {

}

func New(logger *cli.Logger, exclude []string) *Result {
func New(logger *cli.Logger, exclude []string, cidr string) *Result {
c, err := parseCIDR(cidr)
if err != nil {
logger.Errorf("error parsing CIDR: %s", err)
}
return &Result{
Logger: logger,
Exclude: exclude,
CIDR: c,
}
}

func parseCIDR(cidr string) (*net.IPNet, error) {
_, c, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
return c, nil
}

func (r *Result) AddHost(newHost *Host) {
Expand Down Expand Up @@ -65,6 +79,10 @@ func (h *Host) IsExcluded(el []string) bool {
return false
}

func (h *Host) NotInCIDR(c *net.IPNet) bool {
return !c.Contains(h.IP)
}

func (h *Host) updateName(hostname string) {
// Don't overwrite
if h.Name == "" && hostname != "" {
Expand Down
7 changes: 7 additions & 0 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"flag"
"strconv"
"strings"

"github.com/Masterminds/log-go"
"github.com/Masterminds/log-go/impl/cli"
)

type Options struct {
Exclude []string
JSON bool
Host string
Hosts bool
CIDR string
Path string
Port []int
Ports bool
Expand All @@ -21,10 +25,13 @@ type Options struct {

func ParseOptions() *Options {
options := &Options{}
logger := cli.NewStandard()
logger.Level = log.ErrorLevel
exclude := flag.String("exclude", "", "Exclude these hosts from output")
flag.BoolVar(&options.JSON, "json", false, "Display JSON output")
flag.StringVar(&options.Host, "host", "", "Show results for specified host")
flag.BoolVar(&options.Hosts, "hosts", false, "Print alive hosts")
flag.StringVar(&options.CIDR, "cidr", "0.0.0.0/0", "CIDR range to output")
flag.StringVar(&options.Path, "path", ".", "Path to scan file")
port := flag.String("port", "", "Display hosts with matching port(s)")
flag.BoolVar(&options.Ports, "ports", false, "Print all ports")
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New(options *Options) *Runner {

func (r *Runner) Run() {
r.GetScanFiles()
res := result.New(r.Logger, r.Options.Exclude)
res := result.New(r.Logger, r.Options.Exclude, r.Options.CIDR)
for _, ff := range r.Files {
r.Logger.Debugf("processing %v", ff)
// Get last modified time for timeline output
Expand Down

0 comments on commit 40d5ccf

Please sign in to comment.