From 967c56909d69a2d1cf764d5f350b2de617aad9a1 Mon Sep 17 00:00:00 2001 From: Tomoya Amachi Date: Sat, 19 Jan 2019 22:19:06 +0900 Subject: [PATCH] add ScannedIPv4Addrs and ScannedIPv6Addrs (#764) --- models/scanresults.go | 2 ++ scan/serverapi.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/models/scanresults.go b/models/scanresults.go index 61b7bd24..caa27713 100644 --- a/models/scanresults.go +++ b/models/scanresults.go @@ -51,6 +51,8 @@ type ScanResult struct { ScannedVersion string `json:"scannedVersion"` ScannedRevision string `json:"scannedRevision"` ScannedBy string `json:"scannedBy"` + ScannedIPv4Addrs []string `json:"scannedIpv4Addrs"` + ScannedIPv6Addrs []string `json:"scannedIpv6Addrs"` ReportedAt time.Time `json:"reportedAt"` ReportedVersion string `json:"reportedVersion"` ReportedRevision string `json:"reportedRevision"` diff --git a/scan/serverapi.go b/scan/serverapi.go index 1a0e4626..b684d998 100644 --- a/scan/serverapi.go +++ b/scan/serverapi.go @@ -20,6 +20,7 @@ package scan import ( "errors" "fmt" + "net" "net/http" "os" "path/filepath" @@ -595,12 +596,19 @@ func scanVulns(jsonDir string, scannedAt time.Time, timeoutSec int) error { }, timeoutSec) hostname, _ := os.Hostname() + ipv4s, ipv6s, err := ip() + if err != nil { + util.Log.Errorf("Failed to fetch scannedIPs: %s", err) + } + for _, s := range append(servers, errServers...) { r := s.convertToModel() r.ScannedAt = scannedAt r.ScannedVersion = config.Version r.ScannedRevision = config.Revision r.ScannedBy = hostname + r.ScannedIPv4Addrs = ipv4s + r.ScannedIPv6Addrs = ipv6s r.Config.Scan = config.Conf results = append(results, r) } @@ -619,6 +627,38 @@ func scanVulns(jsonDir string, scannedAt time.Time, timeoutSec int) error { return nil } +// ip returns scanner network ip addresses +func ip() (ipv4Addrs []string, ipv6Addrs []string, err error) { + ifaces, err := net.Interfaces() + if err != nil { + return nil, nil, err + } + for _, i := range ifaces { + addrs, _ := i.Addrs() + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + + // only global unicast address + if !ip.IsGlobalUnicast() { + continue + } + + if ok := ip.To4(); ok != nil { + ipv4Addrs = append(ipv4Addrs, ip.String()) + } else { + ipv6Addrs = append(ipv6Addrs, ip.String()) + } + } + } + return ipv4Addrs, ipv6Addrs, nil +} + // EnsureResultDir ensures the directory for scan results func EnsureResultDir(scannedAt time.Time) (currentDir string, err error) { jsonDirName := scannedAt.Format(time.RFC3339)