| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- [ApiController, AllowAnonymous, NonUnify]
- [Route("api/v1/edge")]
- public sealed class MdpEdgeDeviceController(MdpEdgeDeviceService devices) : ControllerBase
- {
- [HttpPost("register"), RequestSizeLimit(12288)]
- public Task<IActionResult> Register(EdgeRegistration input, CancellationToken token) =>
- Execute(async () => Ok(await devices.RegisterAsync(input, token)));
- [HttpPost("certificate/renew"), RequestSizeLimit(12288)]
- public Task<IActionResult> Renew(EdgeRenewal input, CancellationToken token) =>
- Execute(async () => Ok(await devices.RenewAsync(await DeviceCertificate(token), input, token)));
- [HttpPost("certificate/confirm"), RequestSizeLimit(1024)]
- public Task<IActionResult> Confirm(CancellationToken token) => Execute(async () =>
- {
- await devices.ConfirmCertificateAsync(await DeviceCertificate(token), token);
- return NoContent();
- });
- [HttpPost("heartbeat"), RequestSizeLimit(4096)]
- public Task<IActionResult> Poll(EdgeReport report, CancellationToken token) =>
- Execute(async () => Ok(await devices.PollAsync(await DeviceCertificate(token), report)));
- [HttpGet("batches/{id:guid}")]
- public Task<IActionResult> Status(Guid id, CancellationToken token) => Execute(async () =>
- {
- var receipt = await devices.FindReceiptAsync(await DeviceCertificate(token), id);
- return receipt is null ? NotFound() : Ok(receipt);
- });
- [HttpPost("batches"), RequestSizeLimit(1500000)]
- public Task<IActionResult> Upload(EdgeBatch batch, CancellationToken token) =>
- Execute(async () => Ok(await devices.ReceiveAsync(await DeviceCertificate(token), batch, token)));
- private async Task<System.Security.Cryptography.X509Certificates.X509Certificate2> DeviceCertificate(CancellationToken token) =>
- await HttpContext.Connection.GetClientCertificateAsync(token) ?? throw new EdgeContractException("EDGE_DEVICE_FORBIDDEN");
- private async Task<IActionResult> Execute(Func<Task<IActionResult>> action)
- {
- if (!Request.IsHttps) return StatusCode(403, new { code = "EDGE_TLS_REQUIRED" });
- Response.Headers.CacheControl = "no-store";
- try { return await action(); }
- catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException
- or System.Data.Common.DbException or SqlSugarException)
- {
- return EdgeFailure(error);
- }
- }
- internal static ObjectResult EdgeFailure(Exception error)
- {
- var code = error switch
- {
- EdgeContractException edge => edge.Code,
- MdpIngestHttpException http => http.Code,
- IngestContractException ingest => ingest.Code.ToString(),
- _ => "EDGE_STORE_UNAVAILABLE"
- };
- var status = error is MdpIngestHttpException custom ? custom.StatusCode : code switch
- {
- "EDGE_DISABLED" or "EDGE_DEVICE_NOT_FOUND" => 404,
- "EDGE_DEVICE_FORBIDDEN" or "EDGE_ACTIVATION_INVALID" or "EDGE_WRITER_FORBIDDEN" => 403,
- "EDGE_REPORT_INVALID" or "EDGE_CERT_REQUEST_INVALID" or "EDGE_BATCH_INVALID" or "EDGE_TASK_INVALID" => 400,
- "EDGE_STORE_UNAVAILABLE" or "EDGE_CA_UNAVAILABLE" => 503,
- _ => 409
- };
- return new ObjectResult(new { code }) { StatusCode = status };
- }
- }
|