using MikrotikAPI.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Net.Http.Headers; using System.Text; namespace MikrotikAPI { public class APIWrapper { private string MT_IP { get; set; } private string MT_USER { get; set; } private string MT_PASS { get; set; } public APIWrapper(string IP, string User, string Password) { MT_IP = IP; MT_USER = User; MT_PASS = Password; } public async Task> GetLogsAsync() { string json = await SendGetRequestAsync(Endpoints.Log); return json.ToModel>(); } public async Task> GetServersAsync() { string json = await SendGetRequestAsync(Endpoints.Wireguard); return json.ToModel>(); } public async Task GetServer(string Name) { var servers = await GetServersAsync(); return servers.Find(s => s.Name == Name); } public async Task> GetServersTraffic() { var json = await SendRequestBase(RequestMethod.GET, Endpoints.Interface, "{\"stats\", {\".proplist\":\"name, type, rx-byte, tx-byte\"}}"); return json.ToModel>(); } public async Task GetIPAddresses() { var json = await SendGetRequestAsync(Endpoints.IPAddress); return json; } public async Task CreateIPAddress(IPAddressCreateModel ipAddress) { return await CreateItem(Endpoints.IPAddress, ipAddress); } public async Task UpdateIPAddress(IPAddressUpdateModel ipAddress) { var itemJson = JObject.FromObject(ipAddress, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }); return await UpdateItem(Endpoints.IPAddress, itemJson, ipAddress.Id); } public async Task> GetServerIPAddress(string Interface) { var json = await SendGetRequestAsync(Endpoints.IPAddress + "?interface=" + Interface); return json.ToModel>(); } public async Task> GetUsersAsync() { string json = await SendGetRequestAsync(Endpoints.WireguardPeers); return json.ToModel>(); } public async Task GetUser(string id) { var users = await GetUsersAsync(); return users.Find(u => u.Id == id); } public async Task GetUserHandshake(string id) { var json = await SendRequestBase(RequestMethod.GET, Endpoints.WireguardPeers + $"/{id}?.proplist=last-handshake"); return json.ToModel(); } public async Task GetInfo() { var json = await SendGetRequestAsync(Endpoints.SystemResource); return json.ToModel(); } public async Task GetName() { var json = await SendGetRequestAsync(Endpoints.SystemIdentity); return json.ToModel(); } public async Task SetName(MTIdentityUpdateModel identity) // Create Model { var itemJson = JObject.FromObject(identity, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }).ToString(); var json = await SendPostRequestAsync(Endpoints.SystemIdentity + "/set", itemJson); return json == "[]" ? new() { Success = true, Item = await GetName() } : json.ToModel(); } public async Task TryConnectAsync() { var connection = await SendGetRequestAsync(Endpoints.Empty, true); return connection.ToModel(); } public async Task> GetActiveSessions() { var json = await SendGetRequestAsync($"{Endpoints.ActiveUsers}?name=" + MT_USER); return json.ToModel>(); } public async Task> GetJobs() { var json = await SendGetRequestAsync(Endpoints.Jobs); return json.ToModel>(); } public async Task GetDNS() { var json = await SendGetRequestAsync(Endpoints.DNS); return json.ToModel(); } public async Task SetDNS(MTDNSUpdateModel dns) // Create Model { var itemJson = JObject.FromObject(dns, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }).ToString(); var json = await SendPostRequestAsync(Endpoints.DNS + "/set", itemJson); return json == "[]" ? new() { Success = true, Item = await GetDNS() } : json.ToModel(); } public async Task KillJob(string JobID) { return await SendDeleteRequestAsync($"{Endpoints.Jobs}/" + JobID); } public async Task CreateServer(WGServerCreateModel server) { return await CreateItem(Endpoints.Wireguard, server); } public async Task CreateUser(WGPeerCreateModel user) { return await CreateItem(Endpoints.WireguardPeers, user); } public async Task UpdateServer(WGServerUpdateModel server) { var itemJson = JObject.FromObject(server, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }); return await UpdateItem(Endpoints.Wireguard, itemJson, server.Id); } public async Task UpdateUser(WGPeerUpdateModel user) { var itemJson = JObject.FromObject(user, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }); return await UpdateItem(Endpoints.WireguardPeers, itemJson, user.Id); } public async Task SetServerEnabled(WGEnability enability) { return await UpdateItem(Endpoints.Wireguard, new { disabled = enability.Disabled }, enability.ID); } public async Task SetUserEnabled(WGEnability enability) { return await UpdateItem(Endpoints.WireguardPeers, new { disabled = enability.Disabled }, enability.ID); } public async Task DeleteServer(string id) { return await DeleteItem(Endpoints.Wireguard, id); } public async Task DeleteUser(string id) { return await DeleteItem(Endpoints.WireguardPeers, id); } public async Task GetTrafficSpeed() { return await SendPostRequestAsync(Endpoints.MonitorTraffic, "{\"interface\":\"ether1\",\"duration\":\"3s\"}"); } public async Task> GetScripts() { var json = await SendGetRequestAsync(Endpoints.Scripts); return json.ToModel>(); } public async Task CreateScript(ScriptCreateModel script) { return await CreateItem