Add timestamp to scan results cache

This commit is contained in:
Chris Harris 2024-06-28 12:13:17 -04:00
parent 5f86f9a378
commit 6aeffc92f1
No known key found for this signature in database
GPG key ID: 9DAE9BAB4FBA0FEE
4 changed files with 17 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/OpenCHAMI/magellan/internal/db/sqlite" "github.com/OpenCHAMI/magellan/internal/db/sqlite"
@ -25,7 +26,7 @@ var listCmd = &cobra.Command{
fmt.Printf("%s\n", string(b)) fmt.Printf("%s\n", string(b))
} else { } else {
for _, r := range probeResults { for _, r := range probeResults {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) fmt.Printf("%s:%d (%s) @ %s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
} }
} }
}, },

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"time"
magellan "github.com/OpenCHAMI/magellan/internal" magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/db/sqlite" "github.com/OpenCHAMI/magellan/internal/db/sqlite"
@ -76,7 +77,7 @@ var scanCmd = &cobra.Command{
fmt.Printf("%s\n", string(b)) fmt.Printf("%s\n", string(b))
} else { } else {
for _, r := range probeStates { for _, r := range probeStates {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) fmt.Printf("%s:%d (%s) @ %s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
} }
} }
} }

View file

@ -15,6 +15,7 @@ func CreateProbeResultsIfNotExists(path string) (*sqlx.DB, error) {
port INTEGER NOT NULL, port INTEGER NOT NULL,
protocol TEXT, protocol TEXT,
state INTEGER, state INTEGER,
timestamp TIMESTAMP,
PRIMARY KEY (host, port) PRIMARY KEY (host, port)
); );
` `
@ -41,8 +42,8 @@ func InsertProbeResults(path string, states *[]magellan.ScannedResult) error {
// insert all probe states into db // insert all probe states into db
tx := db.MustBegin() tx := db.MustBegin()
for _, state := range *states { for _, state := range *states {
sql := `INSERT OR REPLACE INTO magellan_scanned_ports (host, port, protocol, state) sql := `INSERT OR REPLACE INTO magellan_scanned_ports (host, port, protocol, state, timestamp)
VALUES (:host, :port, :protocol, :state);` VALUES (:host, :port, :protocol, :state, :timestamp);`
_, err := tx.NamedExec(sql, &state) _, err := tx.NamedExec(sql, &state)
if err != nil { if err != nil {
fmt.Printf("failed toexecute transaction: %v\n", err) fmt.Printf("failed toexecute transaction: %v\n", err)

View file

@ -12,20 +12,22 @@ import (
) )
type ScannedResult struct { type ScannedResult struct {
Host string `json:"host"` Host string `json:"host"`
Port int `json:"port"` Port int `json:"port"`
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
State bool `json:"state"` State bool `json:"state"`
Timestamp time.Time `json:"timestamp"`
} }
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []ScannedResult { func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []ScannedResult {
results := []ScannedResult{} results := []ScannedResult{}
for _, p := range ports { for _, p := range ports {
result := ScannedResult{ result := ScannedResult{
Host: host, Host: host,
Port: p, Port: p,
Protocol: "tcp", Protocol: "tcp",
State: false, State: false,
Timestamp: time.Now(),
} }
t := time.Second * time.Duration(timeout) t := time.Second * time.Duration(timeout)
port := fmt.Sprint(p) port := fmt.Sprint(p)