Skip to content

Commit

Permalink
API, back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Sep 26, 2024
1 parent 382dd0f commit fc42e16
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
14 changes: 3 additions & 11 deletions Protest/Front/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 ()=> {
Expand All @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions Protest/Http/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
10 changes: 8 additions & 2 deletions Protest/Protocols/Icmp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ public static byte[] BulkPing(Dictionary<string, string> 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<Task<int>> tasks = new List<Task<int>>();
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 {
Expand Down
67 changes: 64 additions & 3 deletions Protest/Tools/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading.Tasks;

using Protest.Http;
using static Protest.Tools.Api;

namespace Protest.Tools;
internal static class Api {
Expand All @@ -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 {
Expand Down Expand Up @@ -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<string, string> 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<string, string> 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<string, string> 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() {
Expand Down

0 comments on commit fc42e16

Please sign in to comment.