Files
IT-Nexus/agent-cs/Services/ApiService.cs

108 lines
4.1 KiB
C#
Raw Normal View History

2026-06-01 20:49:07 +02:00
using ITNexusAgent.Models;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
namespace ITNexusAgent.Services;
public class ApiService(string serverUrl, string agentKey)
{
private readonly HttpClient _http = new() { Timeout = TimeSpan.FromSeconds(30) };
private readonly string _baseUrl = serverUrl.TrimEnd('/');
private string _agentKey = agentKey;
// Nach erfolgreichem Enrollment wird der geteilte Bootstrap-Key durch den individuellen
// Per-Device-Key ersetzt — alle nachfolgenden Requests dieser Instanz nutzen ab dann den neuen Key.
public void UpdateKey(string newKey) => _agentKey = newKey;
public async Task<string?> EnrollAsync(string hostname)
{
try
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/enroll", new { hostname });
var resp = await _http.SendAsync(req);
if (!resp.IsSuccessStatusCode) return null;
var body = await resp.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeAnonymousType(body, new { status = "", data = new { agent_key = "" } });
return result?.data?.agent_key;
}
catch { return null; }
}
2026-06-01 20:49:07 +02:00
private HttpRequestMessage BuildRequest(HttpMethod method, string path, object? body = null)
{
var req = new HttpRequestMessage(method, $"{_baseUrl}{path}");
req.Headers.Add("X-Agent-Key", _agentKey);
if (body != null)
{
var json = JsonConvert.SerializeObject(body);
req.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
return req;
}
public async Task<CheckinResponse> CheckinAsync(CheckinPayload payload)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/checkin", payload);
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
var body = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CheckinResponse>(body) ?? new CheckinResponse();
}
public async Task<List<Announcement>> PollAnnouncementsAsync(string hostname)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/announcements-poll",
new { hostname });
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
var body = await resp.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeAnonymousType(body, new { announcements = new List<Announcement>() });
return result?.announcements ?? [];
}
public async Task AckAnnouncementAsync(int id, string hostname)
{
var req = BuildRequest(HttpMethod.Post, $"/api/announcements/{id}/ack-agent",
new { hostname });
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
}
public async Task ReportCommandResultAsync(CommandResult result)
{
var req = BuildRequest(HttpMethod.Post, "/api/patch/commands/result", result);
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
}
public async Task<byte[]> DownloadAgentAsync()
{
var req = BuildRequest(HttpMethod.Get, "/api/monitoring/agent-script");
var resp = await _http.SendAsync(req);
return await resp.Content.ReadAsByteArrayAsync();
}
public async Task<byte[]> DownloadSetupAsync(string hostname)
2026-06-01 20:49:07 +02:00
{
var req = BuildRequest(HttpMethod.Get, $"/api/monitoring/agent-setup?hostname={Uri.EscapeDataString(hostname)}");
2026-06-01 20:49:07 +02:00
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
return await resp.Content.ReadAsByteArrayAsync();
}
public async Task SendMessageAsync(string hostname, string message)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/message",
new { hostname, message });
await _http.SendAsync(req);
}
public async Task SendRebootRequestAsync(string hostname)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/reboot-request",
new { hostname });
await _http.SendAsync(req);
}
}