add ScannedIPv4Addrs and ScannedIPv6Addrs (#764)

This commit is contained in:
Tomoya Amachi
2019-01-19 22:19:06 +09:00
committed by Kota Kanbe
parent 7c4831d2d1
commit 967c56909d
2 changed files with 42 additions and 0 deletions

View File

@@ -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"`

View File

@@ -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)