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 1da494f commit 382dd0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Protest/Http/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@ private void ListenerCallback(IAsyncResult result) {
ctx.Response.Close();
return;
}


if (String.Equals(path, "/api", StringComparison.Ordinal)) {

if (String.Equals(ctx.Request.Url?.LocalPath, "/api", StringComparison.Ordinal)) {
Api.HandleApiCall(ctx);
ctx.Response.Close();
return;
Expand Down
44 changes: 25 additions & 19 deletions Protest/Tools/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

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

namespace Protest.Tools;
internal static class Api {
Expand All @@ -18,6 +18,8 @@ internal static class Api {
private static readonly ConcurrentDictionary<string, ulong> traffic;
private static readonly JsonSerializerOptions apiLinksSerializerOptions;

private static ConcurrentDictionary<string, Link> links;

public enum Permissions : byte {
Users = 0x01,
Devices = 0x02,
Expand All @@ -40,12 +42,15 @@ static Api() {

apiLinksSerializerOptions = new JsonSerializerOptions();
apiLinksSerializerOptions.Converters.Add(new ApiJsonConverter());

Api.links = new ConcurrentDictionary<string, Link>(Load().ToDictionary(link => link.apikey));
}

internal static void HandleApiCall(HttpListenerContext ctx) {
Dictionary<string, string> parameters = Listener.ParseQuery(ctx.Request.Url.Query);

if (!parameters.TryGetValue("apikey", out string apiKey)) {
if (!parameters.TryGetValue("key", out string apiKey)
|| !Api.links.ContainsKey(apiKey)) {
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
Expand All @@ -70,16 +75,15 @@ internal static Link[] Load() {
Link[] profiles = JsonSerializer.Deserialize<Link[]>(plain, apiLinksSerializerOptions);
return profiles;
}
catch {
catch (Exception ex){
Logger.Error(ex);
return Array.Empty<Link>();
}
}

internal static byte[] List() {
Link[] links = Load();

var data = new {
data = links.ToDictionary(
return JsonSerializer.SerializeToUtf8Bytes(new {
data = Api.links.Values.ToDictionary(
link => link.guid.ToString(),
link => new {
guid = new { v = link.guid },
Expand All @@ -89,37 +93,39 @@ internal static byte[] List() {
permissions = new { v = link.permissions }
}
),
length = links.Length
};

return JsonSerializer.SerializeToUtf8Bytes(data);
length = Api.links.Count()
});
}

internal static byte[] Save(HttpListenerContext ctx, string origin) {
using StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding);
string payload = reader.ReadToEnd();

try {
Link[] links = JsonSerializer.Deserialize<Link[]>(payload, apiLinksSerializerOptions);
Api.links.Clear();

for (int i = 0; i < links.Length; i++) {
if (links[i].guid == default(Guid)) {
links[i].guid = Guid.NewGuid();
try {
Link[] temp = JsonSerializer.Deserialize<Link[]>(payload, apiLinksSerializerOptions);
for (int i = 0; i < temp.Length; i++) {
if (temp[i].guid == default(Guid)) {
temp[i].guid = Guid.NewGuid();
}
Api.links.TryAdd(temp[i].guid.ToString(), temp[i]);
}

byte[] plain = JsonSerializer.SerializeToUtf8Bytes(links, apiLinksSerializerOptions);
byte[] plain = JsonSerializer.SerializeToUtf8Bytes(temp, apiLinksSerializerOptions);
byte[] cipher = Cryptography.Encrypt(plain, Configuration.DB_KEY, Configuration.DB_KEY_IV);
lock (mutex) {
File.WriteAllBytes(Data.FILE_API_LINKS, cipher);
}

Logger.Action(origin, $"Modify API links");
}
catch (JsonException) {
catch (JsonException ex) {
Logger.Error(ex);
return Data.CODE_INVALID_ARGUMENT.Array;
}
catch (Exception) {
catch (Exception ex) {
Logger.Error(ex);
return Data.CODE_FAILED.Array;
}

Expand Down

0 comments on commit 382dd0f

Please sign in to comment.