feat: 使用静态arp处理通信
This commit is contained in:
@@ -433,8 +433,38 @@ public static class Arp
|
||||
return Environment.UserName == "root" || (Environment.GetEnvironmentVariable("USER") == "root");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定 IP 是否存在对应的 MAC,如果不存在则删除原有 ARP 记录并新增
|
||||
/// </summary>
|
||||
/// <param name="ipAddress">IP 地址</param>
|
||||
/// <param name="macAddress">MAC 地址</param>
|
||||
/// <param name="interfaceName">网络接口名称(可选)</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static async Task<bool> CheckOrAddAsync(string ipAddress, string macAddress, string? interfaceName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||
if (string.IsNullOrWhiteSpace(macAddress))
|
||||
throw new ArgumentException("MAC 地址不能为空", nameof(macAddress));
|
||||
|
||||
var entry = await GetArpEntryAsync(ipAddress);
|
||||
if (entry != null && string.Equals(entry.MacAddress, macAddress, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// 已存在且 MAC 匹配,无需操作
|
||||
return true;
|
||||
}
|
||||
|
||||
// 若存在但 MAC 不匹配,先删除
|
||||
if (entry != null)
|
||||
{
|
||||
await DeleteArpEntryAsync(ipAddress, interfaceName);
|
||||
}
|
||||
|
||||
// 新增 ARP 记录
|
||||
return await AddArpEntryAsync(ipAddress, macAddress, interfaceName);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// ARP 记录条目
|
||||
/// </summary>
|
||||
|
@@ -207,7 +207,7 @@ public class DataController : ControllerBase
|
||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetAvailableBoard(int durationHours = 1)
|
||||
public async ValueTask<IActionResult> GetAvailableBoard(int durationHours = 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -227,7 +227,13 @@ public class DataController : ControllerBase
|
||||
if (!boardOpt.HasValue)
|
||||
return NotFound("没有可用的实验板");
|
||||
|
||||
return Ok(boardOpt.Value);
|
||||
var boardInfo = boardOpt.Value;
|
||||
if (!(await Arp.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr)))
|
||||
{
|
||||
logger.Error($"无法配置ARP,实验板可能会无法连接");
|
||||
}
|
||||
|
||||
return Ok(boardInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -278,7 +284,7 @@ public class DataController : ControllerBase
|
||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetBoardByID(Guid id)
|
||||
public async Task<IActionResult> GetBoardByID(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -288,7 +294,14 @@ public class DataController : ControllerBase
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!ret.Value.HasValue)
|
||||
return NotFound("未找到对应的实验板");
|
||||
return Ok(ret.Value.Value);
|
||||
|
||||
var boardInfo = ret.Value.Value;
|
||||
if (!(await Arp.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr)))
|
||||
{
|
||||
logger.Error($"无法配置ARP,实验板可能会无法连接");
|
||||
}
|
||||
|
||||
return Ok(boardInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -372,5 +385,59 @@ public class DataController : ControllerBase
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新板卡名称(管理员权限)
|
||||
/// </summary>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("UpdateBoardName")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult UpdateBoardName(Guid boardId, string newName)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板子Guid不能为空");
|
||||
if (string.IsNullOrWhiteSpace(newName))
|
||||
return BadRequest("新名称不能为空");
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.UpdateBoardName(boardId, newName);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "更新板卡名称时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新板卡状态(管理员权限)
|
||||
/// </summary>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("UpdateBoardStatus")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult UpdateBoardStatus(Guid boardId, Database.Board.BoardStatus newStatus)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板子Guid不能为空");
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.UpdateBoardStatus(boardId, newStatus);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "更新板卡状态时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -33,6 +33,10 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.GetHostIP();
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -63,6 +67,10 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.GetBoardIP();
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -93,6 +101,10 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.GetHostMAC();
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -123,6 +135,10 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.GetBoardMAC();
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -153,6 +169,10 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
|
||||
var hostIPResult = await netConfig.GetHostIP();
|
||||
var boardIPResult = await netConfig.GetBoardIP();
|
||||
@@ -237,6 +257,10 @@ public class NetConfigController : ControllerBase
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetHostIP(hostIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -276,6 +300,10 @@ public class NetConfigController : ControllerBase
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetBoardIP(newIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -316,6 +344,10 @@ public class NetConfigController : ControllerBase
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -367,6 +399,10 @@ public class NetConfigController : ControllerBase
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机IP地址");
|
||||
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetHostIP(ip);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -411,6 +447,10 @@ public class NetConfigController : ControllerBase
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -451,6 +491,10 @@ public class NetConfigController : ControllerBase
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
if (!(await netConfig.Init()))
|
||||
{
|
||||
throw new Exception($"无法配置ARP");
|
||||
}
|
||||
var result = await netConfig.SetBoardMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
|
@@ -26,6 +26,8 @@ public class NetConfig
|
||||
readonly string address;
|
||||
private IPEndPoint ep;
|
||||
|
||||
const string DEFAULT_BOARD_MAC = "12:34:56:78:9a:bc";
|
||||
|
||||
/// <summary>
|
||||
/// Initialize NetConfig client
|
||||
/// </summary>
|
||||
@@ -44,6 +46,15 @@ public class NetConfig
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async ValueTask<bool> Init()
|
||||
{
|
||||
return await Arp.CheckOrAddAsync(this.address, DEFAULT_BOARD_MAC);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set host IP address
|
||||
/// </summary>
|
||||
@@ -55,11 +66,11 @@ public class NetConfig
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ipBytes = ip.GetAddressBytes();
|
||||
|
||||
@@ -106,11 +117,11 @@ public class NetConfig
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ipBytes = ip.GetAddressBytes();
|
||||
|
||||
@@ -160,13 +171,13 @@ public class NetConfig
|
||||
|
||||
// 清除UDP服务器接收缓冲区
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, macAddress, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
@@ -214,13 +225,13 @@ public class NetConfig
|
||||
|
||||
// 清除UDP服务器接收缓冲区
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, macAddress, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
@@ -262,13 +273,13 @@ public class NetConfig
|
||||
{
|
||||
// 清除UDP服务器接收缓冲区
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_IP, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
@@ -296,13 +307,13 @@ public class NetConfig
|
||||
{
|
||||
// 清除UDP服务器接收缓冲区
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_IP, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
@@ -332,11 +343,11 @@ public class NetConfig
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
@@ -364,13 +375,13 @@ public class NetConfig
|
||||
{
|
||||
// 清除UDP服务器接收缓冲区
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
|
||||
// 刷新ARP
|
||||
var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
if (!refrshRet)
|
||||
{
|
||||
logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
}
|
||||
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||
// if (!refrshRet)
|
||||
// {
|
||||
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||
// }
|
||||
|
||||
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
|
Reference in New Issue
Block a user