151 lines
5.0 KiB
C#
151 lines
5.0 KiB
C#
using AmtScanner.Api.Data;
|
|
using AmtScanner.Api.Models;
|
|
using AmtScanner.Api.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AmtScanner.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/amt-network")]
|
|
[Authorize]
|
|
public class AmtNetworkController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
private readonly IAmtNetworkService _networkService;
|
|
private readonly ILogger<AmtNetworkController> _logger;
|
|
|
|
public AmtNetworkController(AppDbContext db, IAmtNetworkService networkService, ILogger<AmtNetworkController> logger)
|
|
{
|
|
_db = db;
|
|
_networkService = networkService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取设备网络配置
|
|
/// </summary>
|
|
[HttpGet("{uuid}/config")]
|
|
public async Task<IActionResult> GetNetworkConfig(string uuid)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(device.AmtUsername) || string.IsNullOrEmpty(device.AmtPassword))
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "设备未配置 AMT 凭据"));
|
|
}
|
|
|
|
try
|
|
{
|
|
var config = await _networkService.GetNetworkConfigAsync(
|
|
device.IpAddress, device.AmtUsername, device.AmtPassword);
|
|
|
|
if (config == null)
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "无法获取网络配置"));
|
|
}
|
|
|
|
return Ok(ApiResponse<object>.Success(config));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取网络配置失败: {Uuid}", uuid);
|
|
return BadRequest(ApiResponse<object>.Fail(400, $"获取网络配置失败: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置设备静态 IP
|
|
/// </summary>
|
|
[HttpPost("{uuid}/static-ip")]
|
|
public async Task<IActionResult> SetStaticIp(string uuid, [FromBody] SetStaticIpRequest request)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(device.AmtUsername) || string.IsNullOrEmpty(device.AmtPassword))
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "设备未配置 AMT 凭据"));
|
|
}
|
|
|
|
try
|
|
{
|
|
var success = await _networkService.SetStaticIpAsync(
|
|
device.IpAddress, device.AmtUsername, device.AmtPassword,
|
|
request.IpAddress, request.SubnetMask, request.Gateway, request.PrimaryDns, request.SecondaryDns);
|
|
|
|
if (!success)
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "设置静态 IP 失败"));
|
|
}
|
|
|
|
// 更新数据库中的 IP 信息
|
|
device.IpAddress = request.IpAddress;
|
|
device.SubnetMask = request.SubnetMask;
|
|
device.Gateway = request.Gateway;
|
|
device.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(ApiResponse<object>.Success(null, "静态 IP 设置成功"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "设置静态 IP 失败: {Uuid}", uuid);
|
|
return BadRequest(ApiResponse<object>.Fail(400, $"设置静态 IP 失败: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置设备为 DHCP 模式
|
|
/// </summary>
|
|
[HttpPost("{uuid}/dhcp")]
|
|
public async Task<IActionResult> SetDhcp(string uuid)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(device.AmtUsername) || string.IsNullOrEmpty(device.AmtPassword))
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "设备未配置 AMT 凭据"));
|
|
}
|
|
|
|
try
|
|
{
|
|
var success = await _networkService.SetDhcpModeAsync(
|
|
device.IpAddress, device.AmtUsername, device.AmtPassword);
|
|
|
|
if (!success)
|
|
{
|
|
return BadRequest(ApiResponse<object>.Fail(400, "设置 DHCP 模式失败"));
|
|
}
|
|
|
|
return Ok(ApiResponse<object>.Success(null, "DHCP 模式设置成功"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "设置 DHCP 模式失败: {Uuid}", uuid);
|
|
return BadRequest(ApiResponse<object>.Fail(400, $"设置 DHCP 模式失败: {ex.Message}"));
|
|
}
|
|
}
|
|
}
|
|
|
|
public class SetStaticIpRequest
|
|
{
|
|
public string IpAddress { get; set; } = string.Empty;
|
|
public string SubnetMask { get; set; } = string.Empty;
|
|
public string? Gateway { get; set; }
|
|
public string? PrimaryDns { get; set; }
|
|
public string? SecondaryDns { get; set; }
|
|
}
|