新增车辆表及OTA权限与逻辑优化
新增车辆任务与报警表,完善实体与DbContext配置。细化OTA权限校验,增强回传会话IP安全。优化OTA上传与设置面板,调度器支持重启恢复。报警采集逻辑支持历史分段。
This commit is contained in:
@@ -48,7 +48,10 @@ public class OtaController : ControllerBase
|
||||
if (string.Equals(Scope, "Platform", StringComparison.OrdinalIgnoreCase)) return true;
|
||||
var ops = User.FindFirst("ops")?.Value ?? "";
|
||||
var set = ops.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
return set.Contains("*") || set.Any(o => o.StartsWith("ops.ota", StringComparison.OrdinalIgnoreCase));
|
||||
return set.Contains("*")
|
||||
|| set.Contains("ops.ota")
|
||||
|| set.Contains("ops.ota.write")
|
||||
|| set.Any(o => o.StartsWith("ops.ota.", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private bool DenyWrite(out ActionResult denied)
|
||||
@@ -147,12 +150,32 @@ public class OtaController : ControllerBase
|
||||
var receiveBase = $"{baseUrl.TrimEnd('/')}/api/ota/receive";
|
||||
var time = DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
await _wd.TriggerPullAsync(car.Ip, receiveBase, time, ct);
|
||||
// 等待文件落盘
|
||||
// 等待文件落盘:绝对超时 + 收到文件后的空闲窗口,避免首个组件到达就清会话导致半包
|
||||
await Task.Delay(1500, ct);
|
||||
for (var i = 0; i < 40; i++)
|
||||
var deadline = DateTime.UtcNow.AddSeconds(90);
|
||||
var idleAfterReceive = TimeSpan.FromSeconds(8);
|
||||
var lastCount = 0;
|
||||
DateTime? lastProgressAt = null;
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var info = _store.ScanPackage(pkgId);
|
||||
if (info.Components.Count > 0) break;
|
||||
if (info.Components.Count > lastCount)
|
||||
{
|
||||
lastCount = info.Components.Count;
|
||||
lastProgressAt = DateTime.UtcNow;
|
||||
}
|
||||
else if (_store.TryGetLastReceiveAt(car.Ip, out var recvAt) &&
|
||||
(lastProgressAt == null || recvAt > lastProgressAt.Value))
|
||||
{
|
||||
lastProgressAt = recvAt;
|
||||
}
|
||||
|
||||
if (lastCount > 0 &&
|
||||
lastProgressAt != null &&
|
||||
DateTime.UtcNow - lastProgressAt.Value >= idleAfterReceive)
|
||||
break;
|
||||
|
||||
await Task.Delay(500, ct);
|
||||
}
|
||||
_store.ClearActivePull(car.Ip);
|
||||
|
||||
@@ -8,11 +8,14 @@ namespace MiGu.Server.Controllers;
|
||||
/// WatchDog 回传包接收端。
|
||||
/// WatchDog 写死 POST 到 http://{config.serverIP}:8000/upload-mdcs/{routeKey},
|
||||
/// 必须与参考 Electron Express :8000 路径一致;/api/ota/receive/* 仅作兼容别名。
|
||||
/// 会话校验使用 TCP 对端 IP(见 Program 中 TcpRemoteIp),忽略可伪造的 X-Forwarded-For。
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class OtaReceiveController : ControllerBase
|
||||
{
|
||||
public const string TcpRemoteIpItemKey = "TcpRemoteIp";
|
||||
|
||||
private readonly OtaStore _store;
|
||||
private readonly ILogger<OtaReceiveController> _log;
|
||||
|
||||
@@ -40,15 +43,15 @@ public class OtaReceiveController : ControllerBase
|
||||
[RequestSizeLimit(512_000_000)]
|
||||
public async Task<IActionResult> UploadHistory(string routeKey, CancellationToken ct)
|
||||
{
|
||||
var ip = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown";
|
||||
var ip = ResolveTcpRemoteIp();
|
||||
if (!_store.TryGetActivePullId(ip, out _))
|
||||
{
|
||||
_log.LogWarning("OTA history rejected without active pull session from {Ip}", ip);
|
||||
_log.LogWarning("OTA history rejected without active pull session from {Ip}", ip ?? "unknown");
|
||||
return BadRequest("no active pull session");
|
||||
}
|
||||
|
||||
var day = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
var dir = Path.Combine(_store.HistoryDir, day, SafeFileName(ip, "unknown"));
|
||||
var dir = Path.Combine(_store.HistoryDir, day, SafeFileName(ip ?? "unknown", "unknown"));
|
||||
Directory.CreateDirectory(dir);
|
||||
var file = await ReadFirstFileAsync(ct);
|
||||
if (file == null || file.Length == 0) return BadRequest("empty");
|
||||
@@ -58,6 +61,7 @@ public class OtaReceiveController : ControllerBase
|
||||
var path = Path.Combine(dir, safeName);
|
||||
await using var fs = System.IO.File.Create(path);
|
||||
await file.CopyToAsync(fs, ct);
|
||||
_store.NotePullReceive(ip);
|
||||
_log.LogInformation("OTA history receive {Route} -> {Path} ({Len})", routeKey, path, file.Length);
|
||||
return Ok(new { ok = true });
|
||||
}
|
||||
@@ -66,7 +70,7 @@ public class OtaReceiveController : ControllerBase
|
||||
{
|
||||
try
|
||||
{
|
||||
var clientIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
var clientIp = ResolveTcpRemoteIp();
|
||||
if (!_store.TryGetActivePullId(clientIp, out _))
|
||||
{
|
||||
_log.LogWarning("OTA mdcs rejected without active pull session from {Ip}", clientIp ?? "unknown");
|
||||
@@ -80,8 +84,9 @@ public class OtaReceiveController : ControllerBase
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
|
||||
await using (var fs = System.IO.File.Create(dest))
|
||||
await file.CopyToAsync(fs, ct);
|
||||
_store.NotePullReceive(clientIp);
|
||||
_log.LogInformation("OTA mdcs receive {Route} -> {Dest} ({Len})", routeKey, dest, file.Length);
|
||||
return Ok(new { ok = true, path = dest });
|
||||
return Ok(new { ok = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -90,6 +95,14 @@ public class OtaReceiveController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>优先取 ForwardedHeaders 之前写入的 TCP 对端 IP,避免 X-Forwarded-For 投毒。</summary>
|
||||
private string? ResolveTcpRemoteIp()
|
||||
{
|
||||
if (HttpContext.Items.TryGetValue(TcpRemoteIpItemKey, out var boxed) && boxed is string s && !string.IsNullOrWhiteSpace(s))
|
||||
return s;
|
||||
return HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
}
|
||||
|
||||
private async Task<IFormFile?> ReadFirstFileAsync(CancellationToken ct)
|
||||
{
|
||||
if (!Request.HasFormContentType) return null;
|
||||
|
||||
@@ -33,6 +33,8 @@ public class RbacController : ControllerBase
|
||||
new("ops.task.cancel", "任务 · 取消"),
|
||||
new("ops.task.reassign", "任务 · 改派"),
|
||||
new("ops.task.boostPriority", "任务 · 提升优先级"),
|
||||
new("ops.ota", "OTA · 运维读写"),
|
||||
new("ops.ota.write", "OTA · 写操作"),
|
||||
new("monitor.note.write", "监控 · 写运营备注"),
|
||||
new("auth.manage", "系统 · 权限与角色管理"),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user