Skip to content

Commit

Permalink
Adds Json Output (#7)
Browse files Browse the repository at this point in the history
* Adding Json object return
  • Loading branch information
JustinTimperio authored Mar 25, 2021
1 parent a2a8eb0 commit c724121
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 19 deletions.
104 changes: 91 additions & 13 deletions gomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package gomap

import (
"bytes"
"encoding/json"
"fmt"
"net"
)

// IPScanResult contains the results of a scan on a single ip
type IPScanResult struct {
hostname string
ip []net.IP
results []portResult
Hostname string
IP []net.IP
Results []portResult
}

type portResult struct {
Expand Down Expand Up @@ -72,12 +73,12 @@ func ScanRange(proto string, fastscan bool, stealth bool) (RangeScanResult, erro
// String with the results of a single scanned IP
func (results *IPScanResult) String() string {
b := bytes.NewBuffer(nil)
ip := results.ip[len(results.ip)-1]
ip := results.IP[len(results.IP)-1]

fmt.Fprintf(b, "\nHost: %s (%s)\n", results.hostname, ip)
fmt.Fprintf(b, "\nHost: %s (%s)\n", results.Hostname, ip)

active := false
for _, r := range results.results {
for _, r := range results.Results {
if r.State {
active = true
break
Expand All @@ -86,12 +87,12 @@ func (results *IPScanResult) String() string {
if active {
fmt.Fprintf(b, "\t| %s %s\n", "Port", "Service")
fmt.Fprintf(b, "\t| %s %s\n", "----", "-------")
for _, v := range results.results {
for _, v := range results.Results {
if v.State {
fmt.Fprintf(b, "\t|---- %d %s\n", v.Port, v.Service)
}
}
} else if results.hostname != "Unknown" {
} else if results.Hostname != "Unknown" {
fmt.Fprintf(b, "\t|---- %s\n", "No Open Ports Found")
}
return b.String()
Expand All @@ -101,12 +102,12 @@ func (results *IPScanResult) String() string {
func (results RangeScanResult) String() string {
b := bytes.NewBuffer(nil)
for _, r := range results {
ip := r.ip[len(r.ip)-1]
ip := r.IP[len(r.IP)-1]

fmt.Fprintf(b, "\nHost: %s (%s)\n", r.hostname, ip)
fmt.Fprintf(b, "\nHost: %s (%s)\n", r.Hostname, ip)
active := false

for _, r := range r.results {
for _, r := range r.Results {
if r.State {
active = true
break
Expand All @@ -115,15 +116,92 @@ func (results RangeScanResult) String() string {
if active {
fmt.Fprintf(b, "\t| %s %s\n", "Port", "Service")
fmt.Fprintf(b, "\t| %s %s\n", "----", "-------")
for _, v := range r.results {
for _, v := range r.Results {
if v.State {
fmt.Fprintf(b, "\t|---- %d %s\n", v.Port, v.Service)
}
}
} else if r.hostname != "Unknown" {
} else if r.Hostname != "Unknown" {
fmt.Fprintf(b, "\t|---- %s\n", "No Open Ports Found")
}
}

return b.String()
}

type jsonRange struct {
results []jsonIP
}

type jsonIP struct {
IP string
Hostname string
Active bool
Ports []string
}

func (results *IPScanResult) Json() (string, error) {
var ipdata jsonIP
fmt.Println(results.IP)
ipdata.IP = fmt.Sprintf("%s", results.IP[len(results.IP)-1])
ipdata.Hostname = results.Hostname

active := false
for _, r := range results.Results {
if r.State {
active = true
break
}
}
ipdata.Active = active

if active {
for _, v := range results.Results {
if v.State {
entry := fmt.Sprintf("%d: %s", v.Port, v.Service)
ipdata.Ports = append(ipdata.Ports, entry)
}
}
}

j, err := json.MarshalIndent(ipdata, "", " ")
if err != nil {
return "", err
}
return string(j), nil
}

func (results RangeScanResult) Json() (string, error) {
var data jsonRange

for _, r := range results {
var ipdata jsonIP
ipdata.IP = fmt.Sprintf("%s", r.IP[len(r.IP)-1])
ipdata.Hostname = r.Hostname

active := false
for _, r := range r.Results {
if r.State {
active = true
break
}
}
ipdata.Active = active

if active {
for _, v := range r.Results {
if v.State {
entry := fmt.Sprintf("%d: %s", v.Port, v.Service)
ipdata.Ports = append(ipdata.Ports, entry)
}
}
}
data.results = append(data.results, ipdata)
}

j, err := json.MarshalIndent(data.results, "", " ")
if err != nil {
return "", err
}
return string(j), nil
}
6 changes: 3 additions & 3 deletions gomap_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func scanIPPorts(hostname string, laddr string, proto string, fastscan bool, ste
}

return &IPScanResult{
hostname: hname[0],
ip: addr,
results: results,
Hostname: hname[0],
IP: addr,
Results: results,
}, nil
}

Expand Down
13 changes: 10 additions & 3 deletions gomap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import (
func TestMain(m *testing.M) {
var (
proto = "tcp"
fastscan = false
fastscan = true
stealth = false
)

// results, err := gomap.ScanIP("192.168.1.120", proto, fastscan, stealth)
// results, err := gomap.ScanIP("192.168.1.1", proto, fastscan, stealth)
results, err := gomap.ScanRange(proto, fastscan, stealth)
if err != nil {
panic(err)
} else {
fmt.Println(results.String())
}

j, err := results.Json()
if err != nil {
panic(err)
} else {
fmt.Println(j)
}
fmt.Println(results.String())
}

0 comments on commit c724121

Please sign in to comment.