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
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package scans
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@@ -70,11 +71,22 @@ func (r *Runner) run(job ScanJob) {
|
||||
defer wg.Done()
|
||||
for ip := range workCh {
|
||||
isUp := probeHost(ip, job.Options.PingTimeoutMS)
|
||||
checkedAt := time.Now().UTC()
|
||||
_ = r.store.SaveHostResult(job.ID, HostResult{
|
||||
IP: ip,
|
||||
IsUp: isUp,
|
||||
CheckedAt: time.Now().UTC(),
|
||||
CheckedAt: checkedAt,
|
||||
})
|
||||
if isUp && job.Options.PortScanEnabled {
|
||||
for _, p := range job.Options.Ports {
|
||||
_ = r.store.SaveOpenPortResult(job.ID, OpenPortResult{
|
||||
IP: ip,
|
||||
Port: p,
|
||||
IsOpen: probeTCPPort(ip, p, job.Options.PingTimeoutMS),
|
||||
CheckedAt: checkedAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
done++
|
||||
@@ -166,6 +178,23 @@ func probeHost(ip string, timeoutMS int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func probeTCPPort(ip string, port int, timeoutMS int) bool {
|
||||
if timeoutMS <= 0 {
|
||||
timeoutMS = 700
|
||||
}
|
||||
addr, err := netip.ParseAddr(ip)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
target := net.JoinHostPort(addr.String(), strconv.Itoa(port))
|
||||
conn, err := net.DialTimeout("tcp", target, time.Duration(timeoutMS)*time.Millisecond)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_ = conn.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
func dupIP(ip net.IP) net.IP {
|
||||
out := make(net.IP, len(ip))
|
||||
copy(out, ip)
|
||||
|
||||
@@ -61,6 +61,8 @@ type Store interface {
|
||||
UpdateScan(id string, update ScanUpdate) (ScanJob, bool)
|
||||
SaveHostResult(scanID string, host HostResult) error
|
||||
ListHostResults(scanID string, limit int) []HostResult
|
||||
SaveOpenPortResult(scanID string, result OpenPortResult) error
|
||||
ListOpenPortResults(scanID string, limit int) []OpenPortResult
|
||||
}
|
||||
|
||||
type HostResult struct {
|
||||
@@ -69,16 +71,25 @@ type HostResult struct {
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type OpenPortResult struct {
|
||||
IP string `json:"ip"`
|
||||
Port int `json:"port"`
|
||||
IsOpen bool `json:"is_open"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
hosts map[string][]HostResult
|
||||
ports map[string][]OpenPortResult
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
jobs: make(map[string]ScanJob),
|
||||
hosts: make(map[string][]HostResult),
|
||||
ports: make(map[string][]OpenPortResult),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,6 +211,35 @@ func (s *MemoryStore) ListHostResults(scanID string, limit int) []HostResult {
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *MemoryStore) SaveOpenPortResult(scanID string, result OpenPortResult) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, ok := s.jobs[scanID]; !ok {
|
||||
return errors.New("scan not found")
|
||||
}
|
||||
s.ports[scanID] = append(s.ports[scanID], result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListOpenPortResults(scanID string, limit int) []OpenPortResult {
|
||||
if limit <= 0 {
|
||||
limit = 1000
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
items := s.ports[scanID]
|
||||
if len(items) <= limit {
|
||||
out := make([]OpenPortResult, len(items))
|
||||
copy(out, items)
|
||||
return out
|
||||
}
|
||||
out := make([]OpenPortResult, limit)
|
||||
copy(out, items[:limit])
|
||||
return out
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
|
||||
Reference in New Issue
Block a user