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