Files

123 lines
3.5 KiB
Go

// Package export writes scan results as JSON or CSV.
package export
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"nscan/internal/scanner"
)
// Record is one device in an export.
type Record struct {
Name string `json:"name,omitempty"`
IP string `json:"ip"`
MAC string `json:"mac,omitempty"`
Vendor string `json:"vendor,omitempty"`
Model string `json:"model,omitempty"`
Online bool `json:"online"`
RTTms float64 `json:"rtt_ms,omitempty"`
MDNSName string `json:"mdns_name,omitempty"`
NetBIOSName string `json:"netbios_name,omitempty"`
DNSName string `json:"dns_name,omitempty"`
Services []scanner.Service `json:"services,omitempty"`
OpenPorts []scanner.PortResult `json:"open_ports,omitempty"`
Tracked bool `json:"tracked,omitempty"`
Whitelisted bool `json:"whitelisted,omitempty"`
FirstSeen time.Time `json:"first_seen,omitzero"`
LastSeen time.Time `json:"last_seen,omitzero"`
}
// FromHost fills the scan-derived fields of a record.
func FromHost(h scanner.Host) Record {
r := Record{
Name: h.Name(),
IP: h.IP.String(),
Vendor: h.Maker(),
Model: h.Model,
MDNSName: h.MDNSName,
NetBIOSName: h.NetBIOSName,
DNSName: h.DNSName,
Services: h.Services,
}
if len(h.MAC) > 0 {
r.MAC = h.MAC.String()
}
if h.RTT > 0 {
r.RTTms = float64(h.RTT.Microseconds()) / 1000
}
return r
}
// WriteJSON writes the records as an indented JSON array.
func WriteJSON(w io.Writer, recs []Record) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(recs)
}
// WriteCSV writes one row per record.
func WriteCSV(w io.Writer, recs []Record) error {
cw := csv.NewWriter(w)
cw.Write([]string{"name", "ip", "mac", "vendor", "model", "online", "rtt_ms", "mdns_name",
"netbios_name", "dns_name", "services", "open_ports", "tracked", "whitelisted", "first_seen", "last_seen"})
for _, r := range recs {
var svcs, ports []string
for _, s := range r.Services {
svcs = append(svcs, fmt.Sprintf("%s:%d", s.Short(), s.Port))
}
for _, p := range r.OpenPorts {
ports = append(ports, strconv.Itoa(p.Port))
}
rtt := ""
if r.RTTms > 0 {
rtt = strconv.FormatFloat(r.RTTms, 'f', 2, 64)
}
cw.Write([]string{r.Name, r.IP, r.MAC, r.Vendor, r.Model, strconv.FormatBool(r.Online), rtt, r.MDNSName,
r.NetBIOSName, r.DNSName, strings.Join(svcs, " "), strings.Join(ports, " "),
strconv.FormatBool(r.Tracked), strconv.FormatBool(r.Whitelisted), timeOrEmpty(r.FirstSeen), timeOrEmpty(r.LastSeen)})
}
cw.Flush()
return cw.Error()
}
func timeOrEmpty(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.RFC3339)
}
// Files writes the records as <dir>/nscan-<timestamp>.json and .csv and
// returns both paths.
func Files(dir string, recs []Record, now time.Time) ([]string, error) {
base := filepath.Join(dir, "nscan-"+now.Format("20060102-150405"))
var paths []string
for _, f := range []struct {
ext string
write func(io.Writer, []Record) error
}{{".json", WriteJSON}, {".csv", WriteCSV}} {
path := base + f.ext
file, err := os.Create(path)
if err != nil {
return paths, err
}
err = f.write(file, recs)
if cerr := file.Close(); err == nil {
err = cerr
}
if err != nil {
return paths, err
}
paths = append(paths, path)
}
return paths, nil
}