diff --git a/Protest/Front/api.js b/Protest/Front/api.js index ede73a33..65b75502 100644 --- a/Protest/Front/api.js +++ b/Protest/Front/api.js @@ -143,7 +143,7 @@ class Api extends List { } EditDialog(object=null) { - const dialog = this.DialogBox("400px"); + const dialog = this.DialogBox("360px"); if (dialog === null) return; const {okButton, innerBox} = dialog; @@ -155,7 +155,7 @@ class Api extends List { innerBox.style.padding = "16px 32px"; innerBox.style.display = "grid"; innerBox.style.gridTemplateColumns = "auto 160px 275px 44px 72px auto"; - innerBox.style.gridTemplateRows = "repeat(3, 38px) 12px repeat(5, 32px)"; + innerBox.style.gridTemplateRows = "repeat(3, 38px) 12px repeat(4, 32px)"; innerBox.style.alignItems = "center"; let counter = 0; @@ -242,12 +242,7 @@ class Api extends List { lifelineLabel.style.backgroundImage = "url(mono/lifeline.svg)"; lifelineLabel.style.backgroundSize = "20px"; lifelineLabel.style.backgroundRepeat = "no-repeat"; - - const [utilitiesLabel, utilitiesInput] = AddParameter("Network utilities", "input", "toggle"); - utilitiesLabel.style.paddingLeft = "24px"; - utilitiesLabel.style.backgroundImage = "url(mono/portscan.svg)"; - utilitiesLabel.style.backgroundSize = "20px"; - utilitiesLabel.style.backgroundRepeat = "no-repeat"; + lifelineInput.disabled = true; setTimeout(()=>nameInput.focus(), 200); @@ -284,7 +279,6 @@ class Api extends List { usersInput.checked = permissions & 0x01; devicesInput.checked = permissions & 0x02; lifelineInput.checked = permissions & 0x04; - utilitiesInput.checked = permissions & 0x80; } okButton.onclick = async ()=> { @@ -309,8 +303,6 @@ class Api extends List { if (usersInput.checked) permissions |= 0x01; if (devicesInput.checked) permissions |= 0x02; if (lifelineInput.checked) permissions |= 0x04; - if (utilitiesInput.checked) permissions |= 0x80; - if (object === null) { const guid = UI.GenerateUuid(); diff --git a/Protest/Http/Listener.cs b/Protest/Http/Listener.cs index 637aaec1..785fde68 100644 --- a/Protest/Http/Listener.cs +++ b/Protest/Http/Listener.cs @@ -276,8 +276,6 @@ private void ListenerCallback(IAsyncResult result) { ctx.Response.Close(); return; } - - if (String.Equals(ctx.Request.Url?.LocalPath, "/api", StringComparison.Ordinal)) { Api.HandleApiCall(ctx); @@ -377,6 +375,10 @@ private bool CacheHandler(HttpListenerContext ctx, string path) { string acceptEncoding = ctx.Request.Headers.Get("Accept-Encoding")?.ToLower() ?? String.Empty; bool acceptGZip = acceptEncoding.Contains("gzip"); +#if DEFLATE + bool acceptDeflate = acceptEncoding.Contains("deflate"); +#endif + byte[] buffer; #if BROTLI bool acceptBrotli = acceptEncoding.Contains("br"); @@ -387,7 +389,6 @@ private bool CacheHandler(HttpListenerContext ctx, string path) { else #endif #if DEFLATE - bool acceptDeflate = acceptEncoding.Contains("deflate"); if (acceptDeflate && entry.deflate is not null) { //deflate buffer = entry.deflate; ctx.Response.AddHeader("Content-Encoding", "deflate"); diff --git a/Protest/Protocols/Icmp.cs b/Protest/Protocols/Icmp.cs index 7a704dc9..5bf103b9 100644 --- a/Protest/Protocols/Icmp.cs +++ b/Protest/Protocols/Icmp.cs @@ -26,14 +26,20 @@ public static byte[] BulkPing(Dictionary parameters) { parameters.TryGetValue("query", out string query); if (String.IsNullOrEmpty(query)) { return null; } + if (!parameters.TryGetValue("timeout", out string timeoutString) || !Int32.TryParse(timeoutString, out int timeout)) { + timeout = 1000; + } + + if (timeout < 50) { timeout = 50; } + string[] queryArray = query.Split(';'); List> tasks = new List>(); foreach (string host in queryArray) { - tasks.Add(Task.Run(async () => { + tasks.Add(Task.Run(async ()=> { using Ping p = new Ping(); try { - PingReply reply = await p.SendPingAsync(host, 1000, ICMP_PAYLOAD); + PingReply reply = await p.SendPingAsync(host, timeout, ICMP_PAYLOAD); return reply.Status == IPStatus.Success ? (int)reply.RoundtripTime : -1; } catch { diff --git a/Protest/Tools/Api.cs b/Protest/Tools/Api.cs index caaa3164..6f388efe 100644 --- a/Protest/Tools/Api.cs +++ b/Protest/Tools/Api.cs @@ -9,7 +9,6 @@ using System.Threading.Tasks; using Protest.Http; -using static Protest.Tools.Api; namespace Protest.Tools; internal static class Api { @@ -23,8 +22,7 @@ internal static class Api { public enum Permissions : byte { Users = 0x01, Devices = 0x02, - Lifeline = 0x04, - NetUtilities = 0x80 + Lifeline = 0x04 } public record Link { @@ -55,9 +53,72 @@ internal static void HandleApiCall(HttpListenerContext ctx) { return; } + if (!Api.links.TryGetValue(apiKey, out Link link)) { + ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return; + } + + if (!parameters.TryGetValue("call", out string call)) { + ctx.Response.StatusCode = (int)HttpStatusCode.NotFound; + return; + } + + byte[] buffer; + + switch (call) { + case "devices" : buffer = HandleDevicesCall(ctx, parameters, link); break; + case "users" : buffer = HandleUsersCall(ctx, parameters, link); break; + case "lifeline" : buffer = HandleLifelineCall(ctx, parameters, link); break; + + default: + buffer = null; + ctx.Response.StatusCode = (int)HttpStatusCode.NotFound; + return; + } + + if (buffer is not null) { + ctx.Response.AddHeader("Length", buffer?.Length.ToString() ?? "0"); + ctx.Response.OutputStream.Write(buffer, 0, buffer.Length); + } + } + + internal static byte[] HandleDevicesCall(HttpListenerContext ctx, Dictionary parameters, Link link) { + if ((link.permissions & (byte)Permissions.Devices) == 0x00) { + ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return null; + } + + byte[] buffer = null; + //TODO: + + ctx.Response.StatusCode = (int)HttpStatusCode.OK; + return buffer; + } + + internal static byte[] HandleUsersCall(HttpListenerContext ctx, Dictionary parameters, Link link) { + if ((link.permissions & (byte)Permissions.Users) == 0x00) { + ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return null; + } + + byte[] buffer = null; + //TODO: + + ctx.Response.StatusCode = (int)HttpStatusCode.OK; + return buffer; + } + + internal static byte[] HandleLifelineCall(HttpListenerContext ctx, Dictionary parameters, Link link) { + if ((link.permissions & (byte)Permissions.Lifeline) == 0x00) { + ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return null; + } + + byte[] buffer = null; //TODO: ctx.Response.StatusCode = (int)HttpStatusCode.OK; + return buffer; } internal static Link[] Load() {