Add TCP port scan persistence and API endpoint.
Probe configured ports for alive hosts, persist port states, and expose scan port results via REST. Made-with: Cursor
This commit is contained in:
@@ -315,3 +315,46 @@ limit $2`
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *PostgresStore) SaveOpenPortResult(scanID string, result OpenPortResult) error {
|
||||
query := `
|
||||
insert into scan_ports (scan_id, ip, port, is_open, checked_at)
|
||||
values ($1, $2, $3, $4, $5)`
|
||||
_, err := s.db.ExecContext(
|
||||
context.Background(),
|
||||
query,
|
||||
scanID,
|
||||
result.IP,
|
||||
result.Port,
|
||||
result.IsOpen,
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListOpenPortResults(scanID string, limit int) []OpenPortResult {
|
||||
if limit <= 0 {
|
||||
limit = 1000
|
||||
}
|
||||
query := `
|
||||
select ip::text, port, is_open, checked_at
|
||||
from scan_ports
|
||||
where scan_id = $1
|
||||
order by checked_at desc
|
||||
limit $2`
|
||||
rows, err := s.db.QueryContext(context.Background(), query, scanID, limit)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]OpenPortResult, 0, limit)
|
||||
for rows.Next() {
|
||||
var p OpenPortResult
|
||||
if err = rows.Scan(&p.IP, &p.Port, &p.IsOpen, &p.CheckedAt); err != nil {
|
||||
return nil
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user