Initial scaffold and technical specification
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nettopo-go/internal/scans"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store scans.Store
|
||||
}
|
||||
|
||||
func NewHandler(store scans.Store) *Handler {
|
||||
return &Handler{store: store}
|
||||
}
|
||||
|
||||
func (h *Handler) Routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /health", h.health)
|
||||
mux.HandleFunc("POST /api/scans", h.createScan)
|
||||
mux.HandleFunc("GET /api/scans/{id}", h.getScan)
|
||||
return withJSON(mux)
|
||||
}
|
||||
|
||||
func (h *Handler) health(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"status": "ok",
|
||||
"time": time.Now().UTC(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) createScan(w http.ResponseWriter, r *http.Request) {
|
||||
var req scans.CreateScanRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid json body")
|
||||
return
|
||||
}
|
||||
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
req.Name = "manual-scan"
|
||||
}
|
||||
|
||||
job, err := h.store.CreateScan(req)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, map[string]any{
|
||||
"scan_id": job.ID,
|
||||
"status": job.Status,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) getScan(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
writeError(w, http.StatusBadRequest, "scan id is required")
|
||||
return
|
||||
}
|
||||
|
||||
job, ok := h.store.GetScan(id)
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotFound, "scan not found")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, job)
|
||||
}
|
||||
|
||||
func withJSON(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, payload any) {
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
writeJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
type Config struct {
|
||||
HTTPAddr string
|
||||
}
|
||||
|
||||
func FromEnv() Config {
|
||||
addr := os.Getenv("HTTP_ADDR")
|
||||
if addr == "" {
|
||||
addr = ":8080"
|
||||
}
|
||||
|
||||
return Config{
|
||||
HTTPAddr: addr,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package scans
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreateScanRequest struct {
|
||||
Name string `json:"name"`
|
||||
CIDRs []string `json:"cidrs"`
|
||||
ExcludeIPs []string `json:"exclude_ips"`
|
||||
SNMPCredentialsID string `json:"snmp_credentials_id"`
|
||||
Options ScanOptions `json:"options"`
|
||||
}
|
||||
|
||||
type ScanOptions struct {
|
||||
PingTimeoutMS int `json:"ping_timeout_ms"`
|
||||
PingRetries int `json:"ping_retries"`
|
||||
MaxParallelHosts int `json:"max_parallel_hosts"`
|
||||
PortScanEnabled bool `json:"port_scan_enabled"`
|
||||
Ports []int `json:"ports"`
|
||||
}
|
||||
|
||||
type ScanJob struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
CIDRs []string `json:"cidrs"`
|
||||
ExcludeIPs []string `json:"exclude_ips"`
|
||||
SNMPCredentialsID string `json:"snmp_credentials_id"`
|
||||
Options ScanOptions `json:"options"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
StartedAt time.Time `json:"started_at,omitempty"`
|
||||
FinishedAt time.Time `json:"finished_at,omitempty"`
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
CreateScan(CreateScanRequest) (ScanJob, error)
|
||||
GetScan(id string) (ScanJob, bool)
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
jobs: make(map[string]ScanJob),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) CreateScan(req CreateScanRequest) (ScanJob, error) {
|
||||
if len(req.CIDRs) == 0 {
|
||||
return ScanJob{}, errors.New("cidrs must not be empty")
|
||||
}
|
||||
|
||||
for _, cidr := range req.CIDRs {
|
||||
if _, _, err := net.ParseCIDR(cidr); err != nil {
|
||||
return ScanJob{}, errors.New("invalid cidr: " + cidr)
|
||||
}
|
||||
}
|
||||
|
||||
for _, ip := range req.ExcludeIPs {
|
||||
if net.ParseIP(ip) == nil {
|
||||
return ScanJob{}, errors.New("invalid exclude ip: " + ip)
|
||||
}
|
||||
}
|
||||
|
||||
applyDefaultOptions(&req.Options)
|
||||
|
||||
id, err := newID()
|
||||
if err != nil {
|
||||
return ScanJob{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
job := ScanJob{
|
||||
ID: id,
|
||||
Name: req.Name,
|
||||
Status: "queued",
|
||||
CIDRs: req.CIDRs,
|
||||
ExcludeIPs: req.ExcludeIPs,
|
||||
SNMPCredentialsID: req.SNMPCredentialsID,
|
||||
Options: req.Options,
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
s.jobs[id] = job
|
||||
s.mu.Unlock()
|
||||
|
||||
return job, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetScan(id string) (ScanJob, bool) {
|
||||
s.mu.RLock()
|
||||
job, ok := s.jobs[id]
|
||||
s.mu.RUnlock()
|
||||
return job, ok
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
}
|
||||
if opts.PingRetries < 0 {
|
||||
opts.PingRetries = 0
|
||||
}
|
||||
if opts.MaxParallelHosts <= 0 {
|
||||
opts.MaxParallelHosts = 128
|
||||
}
|
||||
if len(opts.Ports) == 0 {
|
||||
opts.Ports = []int{22, 80, 443, 161}
|
||||
}
|
||||
}
|
||||
|
||||
func newID() (string, error) {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b[:]), nil
|
||||
}
|
||||
Reference in New Issue
Block a user