76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using AmtScanner.Api.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace AmtScanner.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class ScanController : ControllerBase
|
|
{
|
|
private readonly IAmtScannerService _scannerService;
|
|
private readonly IHubContext<ScanProgressHub> _hubContext;
|
|
private readonly ILogger<ScanController> _logger;
|
|
|
|
public ScanController(
|
|
IAmtScannerService scannerService,
|
|
IHubContext<ScanProgressHub> hubContext,
|
|
ILogger<ScanController> logger)
|
|
{
|
|
_scannerService = scannerService;
|
|
_hubContext = hubContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost("start")]
|
|
public async Task<IActionResult> StartScan([FromBody] ScanRequest request)
|
|
{
|
|
var taskId = Guid.NewGuid().ToString();
|
|
|
|
_logger.LogInformation("Starting scan task: {TaskId}", taskId);
|
|
|
|
// Start scan in background
|
|
_ = Task.Run(async () =>
|
|
{
|
|
var progress = new Progress<ScanProgress>(async p =>
|
|
{
|
|
await _hubContext.Clients.All.SendAsync("ReceiveScanProgress", p);
|
|
});
|
|
|
|
try
|
|
{
|
|
await _scannerService.ScanNetworkAsync(
|
|
taskId,
|
|
request.NetworkSegment,
|
|
request.SubnetMask,
|
|
progress
|
|
);
|
|
|
|
// Send completion notification
|
|
_logger.LogInformation("Scan task {TaskId} completed", taskId);
|
|
await _hubContext.Clients.All.SendAsync("ScanCompleted", new { taskId });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in scan task {TaskId}", taskId);
|
|
await _hubContext.Clients.All.SendAsync("ScanError", new { taskId, error = ex.Message });
|
|
}
|
|
});
|
|
|
|
return Ok(new { taskId });
|
|
}
|
|
|
|
[HttpPost("cancel/{taskId}")]
|
|
public IActionResult CancelScan(string taskId)
|
|
{
|
|
_scannerService.CancelScan(taskId);
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
public class ScanRequest
|
|
{
|
|
public string NetworkSegment { get; set; } = string.Empty;
|
|
public string SubnetMask { get; set; } = string.Empty;
|
|
}
|