Refactored flags and added xnames to add endpoint to hms-smd

This commit is contained in:
David J. Allen 2023-09-05 14:26:53 -06:00
parent de07b119b5
commit 2edb9fdbb0
18 changed files with 270 additions and 175 deletions

View file

@ -5,15 +5,13 @@ import (
"net"
"sync"
"time"
"github.com/jmoiron/sqlx"
)
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []bmcProbeResult {
results := []bmcProbeResult{}
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []BMCProbeResult {
results := []BMCProbeResult{}
for _, p := range ports {
result := bmcProbeResult{
result := BMCProbeResult{
Host: host,
Port: p,
Protocol: "tcp",
@ -53,8 +51,8 @@ func GenerateHosts(subnet string, begin uint8, end uint8) []string {
return hosts
}
func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []bmcProbeResult {
states := make([]bmcProbeResult, 0, len(hosts))
func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []BMCProbeResult {
states := make([]BMCProbeResult, 0, len(hosts))
done := make(chan struct{}, threads+1)
chanHost := make(chan string, threads+1)
// chanPort := make(chan int, threads+1)
@ -92,57 +90,6 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []bmcP
return states
}
func StoreStates(path string, states *[]bmcProbeResult) error {
if states == nil {
return fmt.Errorf("states == nil")
}
// create database if it doesn't already exist
schema := `
CREATE TABLE IF NOT EXISTS magellan_scanned_ports (
host TEXT PRIMARY KEY NOT NULL,
port INTEGER,
protocol TEXT,
state INTEGER
);
`
db, err := sqlx.Open("sqlite3", path)
if err != nil {
return fmt.Errorf("could not open database: %v", err)
}
db.MustExec(schema)
// insert all probe states into db
tx := db.MustBegin()
for _, state := range *states {
sql := `INSERT OR REPLACE INTO magellan_scanned_ports (host, port, protocol, state)
VALUES (:host, :port, :protocol, :state);`
_, err := tx.NamedExec(sql, &state)
if err != nil {
fmt.Printf("could not execute transaction: %v\n", err)
}
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("could not commit transaction: %v", err)
}
return nil
}
func GetStates(path string) ([]bmcProbeResult, error) {
db, err := sqlx.Open("sqlite3", path)
if err != nil {
return nil, fmt.Errorf("could not open database: %v", err)
}
results := []bmcProbeResult{}
err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC")
if err != nil {
return nil, fmt.Errorf("could not retrieve probes: %v", err)
}
return results, nil
}
func GetDefaultPorts() []int {
return []int{SSH_PORT, TLS_PORT, IPMI_PORT, REDFISH_PORT}
}