From 77a609758bb80bac9497d2e3988550f8be578407 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 23 Feb 2026 02:03:20 +0100 Subject: Initial commit --- .gitignore | 6 + ReferenceClientProxyImplementation.slnx | 3 + ReferenceClientProxyImplementation/.gitignore | 9 ++ .../Configuration/AssetCacheConfig.cs | 15 ++ .../Configuration/ProxyConfiguration.cs | 8 + .../Configuration/TestClientConfig.cs | 13 ++ .../Configuration/TestClientDebug.cs | 7 + .../Configuration/TestClientPatchOptions.cs | 7 + .../Controllers/AssetsControllers.cs | 72 +++++++++ .../Controllers/AssetsControllers.cs.bak | 124 +++++++++++++++ .../Controllers/ErrorReportingProxy.cs | 34 ++++ .../Controllers/FrontendController.cs | 77 +++++++++ .../Controllers/StaticController.cs | 15 ++ .../Helpers/HtmlUtils.cs | 16 ++ .../Helpers/Resolvers.cs | 80 ++++++++++ .../Patches/Constants/Constant.cs | 6 + .../Patches/IPatch.cs | 8 + .../Patches/Implementations/FormatFilePatch.cs | 91 +++++++++++ .../Patches/Implementations/FormatHtmlCssPatch.cs | 72 +++++++++ .../Implementations/HTMLPatches/GlobalEnvPatch.cs | 22 +++ .../HTMLPatches/StripNoncesPatch.cs | 36 +++++ .../Implementations/JSPatches/ApiProtocolPatch.cs | 32 ++++ .../Implementations/JSPatches/BooleanPatch.cs | 21 +++ .../JSPatches/BooleanPropagationPatch.cs | 21 +++ .../JSPatches/DisableSciencePatch.cs | 26 ++++ .../JSPatches/ExpandUnicodeEscapesPatch.cs | 26 ++++ .../Implementations/JSPatches/IsStaffPatch.cs | 33 ++++ .../JSPatches/JsonParseMultilinePatch.cs | 79 ++++++++++ .../JSPatches/KnownConstantsPatch.cs | 16 ++ .../Implementations/JSPatches/LegacyJsPatches.cs | 32 ++++ .../JSPatches/LogErrorContextPatch.cs | 34 ++++ .../JSPatches/NullCoalescingPatch.cs | 32 ++++ .../JSPatches/PrefetchAssetsPatch.cs | 56 +++++++ .../Implementations/JSPatches/Void0Patch.cs | 27 ++++ .../Implementations/JSPatches/WhileTruePatch.cs | 19 +++ .../Patches/Implementations/PatchSet.cs | 26 ++++ ReferenceClientProxyImplementation/Program.cs | 107 +++++++++++++ .../Properties/launchSettings.json | 32 ++++ .../ReferenceClientProxyImplementation.csproj | 34 ++++ .../Resources/Assets/checkLocale.js | 47 ++++++ .../Assets/dff87c953f43b561d71fbcfe8a93a79a.png | 0 .../Resources/Assets/endpoints.json | 115 ++++++++++++++ .../Resources/Assets/features.json | 26 ++++ .../Resources/Assets/fosscord-login.css | 71 +++++++++ .../Resources/Assets/fosscord.css | 44 ++++++ .../Resources/Assets/plugins/.gitkeep | 0 .../Assets/preload-plugins/autoRegister.js | 62 ++++++++ .../Resources/Assets/tools/rightsCalculator.js | 17 ++ .../Resources/Assets/user.css | 1 + .../Resources/Assets/widget/banner1.png | Bin 0 -> 5950 bytes .../Resources/Assets/widget/banner2.png | Bin 0 -> 3756 bytes .../Resources/Assets/widget/banner3.png | Bin 0 -> 5342 bytes .../Resources/Assets/widget/banner4.png | Bin 0 -> 13105 bytes .../Resources/Assets/widget/shield.png | Bin 0 -> 726 bytes .../Resources/Pages/developers.html | 52 +++++++ .../Resources/Pages/index-template.html | 75 +++++++++ .../Resources/Pages/index.html | 130 ++++++++++++++++ .../Private/Injections/WebSocketDataLog.html | 7 + .../Private/Injections/WebSocketDumper.html | 22 +++ .../Services/BuildDownloadService.cs | 76 +++++++++ .../Services/ClientStoreService.cs | 62 ++++++++ .../Services/ModernAssetLocator.cs | 3 + .../Services/TemporaryTestJob.cs | 14 ++ .../Tasks/Startup/BuildClientTask.cs | 39 +++++ .../Tasks/Startup/InitClientStoreTask.cs | 173 +++++++++++++++++++++ .../Tasks/Startup/PatchClientAssetsTask.cs | 37 +++++ .../Tasks/Startup/SanitiseConfigPathsTask.cs | 15 ++ ReferenceClientProxyImplementation/Tasks/Tasks.cs | 28 ++++ .../appsettings.Development.json | 62 ++++++++ .../appsettings.json | 9 ++ ReferenceClientProxyImplementation/formatCache.sh | 5 + 71 files changed, 2566 insertions(+) create mode 100644 .gitignore create mode 100644 ReferenceClientProxyImplementation.slnx create mode 100644 ReferenceClientProxyImplementation/.gitignore create mode 100644 ReferenceClientProxyImplementation/Configuration/AssetCacheConfig.cs create mode 100644 ReferenceClientProxyImplementation/Configuration/ProxyConfiguration.cs create mode 100644 ReferenceClientProxyImplementation/Configuration/TestClientConfig.cs create mode 100644 ReferenceClientProxyImplementation/Configuration/TestClientDebug.cs create mode 100644 ReferenceClientProxyImplementation/Configuration/TestClientPatchOptions.cs create mode 100644 ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs create mode 100644 ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs.bak create mode 100644 ReferenceClientProxyImplementation/Controllers/ErrorReportingProxy.cs create mode 100644 ReferenceClientProxyImplementation/Controllers/FrontendController.cs create mode 100644 ReferenceClientProxyImplementation/Controllers/StaticController.cs create mode 100644 ReferenceClientProxyImplementation/Helpers/HtmlUtils.cs create mode 100644 ReferenceClientProxyImplementation/Helpers/Resolvers.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Constants/Constant.cs create mode 100644 ReferenceClientProxyImplementation/Patches/IPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/FormatFilePatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/FormatHtmlCssPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/GlobalEnvPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/StripNoncesPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ApiProtocolPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPropagationPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/DisableSciencePatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ExpandUnicodeEscapesPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/IsStaffPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/JsonParseMultilinePatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/KnownConstantsPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LegacyJsPatches.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LogErrorContextPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/NullCoalescingPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/Void0Patch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/WhileTruePatch.cs create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/PatchSet.cs create mode 100644 ReferenceClientProxyImplementation/Program.cs create mode 100644 ReferenceClientProxyImplementation/Properties/launchSettings.json create mode 100644 ReferenceClientProxyImplementation/ReferenceClientProxyImplementation.csproj create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/checkLocale.js create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/dff87c953f43b561d71fbcfe8a93a79a.png create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/endpoints.json create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/features.json create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/fosscord-login.css create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/fosscord.css create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/plugins/.gitkeep create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/preload-plugins/autoRegister.js create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/tools/rightsCalculator.js create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/user.css create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/widget/banner1.png create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/widget/banner2.png create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/widget/banner3.png create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/widget/banner4.png create mode 100644 ReferenceClientProxyImplementation/Resources/Assets/widget/shield.png create mode 100644 ReferenceClientProxyImplementation/Resources/Pages/developers.html create mode 100644 ReferenceClientProxyImplementation/Resources/Pages/index-template.html create mode 100644 ReferenceClientProxyImplementation/Resources/Pages/index.html create mode 100644 ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDataLog.html create mode 100644 ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDumper.html create mode 100644 ReferenceClientProxyImplementation/Services/BuildDownloadService.cs create mode 100644 ReferenceClientProxyImplementation/Services/ClientStoreService.cs create mode 100644 ReferenceClientProxyImplementation/Services/ModernAssetLocator.cs create mode 100644 ReferenceClientProxyImplementation/Services/TemporaryTestJob.cs create mode 100644 ReferenceClientProxyImplementation/Tasks/Startup/BuildClientTask.cs create mode 100644 ReferenceClientProxyImplementation/Tasks/Startup/InitClientStoreTask.cs create mode 100644 ReferenceClientProxyImplementation/Tasks/Startup/PatchClientAssetsTask.cs create mode 100644 ReferenceClientProxyImplementation/Tasks/Startup/SanitiseConfigPathsTask.cs create mode 100644 ReferenceClientProxyImplementation/Tasks/Tasks.cs create mode 100644 ReferenceClientProxyImplementation/appsettings.Development.json create mode 100644 ReferenceClientProxyImplementation/appsettings.json create mode 100755 ReferenceClientProxyImplementation/formatCache.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f400df3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +.idea/ \ No newline at end of file diff --git a/ReferenceClientProxyImplementation.slnx b/ReferenceClientProxyImplementation.slnx new file mode 100644 index 0000000..dc01632 --- /dev/null +++ b/ReferenceClientProxyImplementation.slnx @@ -0,0 +1,3 @@ + + + diff --git a/ReferenceClientProxyImplementation/.gitignore b/ReferenceClientProxyImplementation/.gitignore new file mode 100644 index 0000000..0219682 --- /dev/null +++ b/ReferenceClientProxyImplementation/.gitignore @@ -0,0 +1,9 @@ +downloadCache +server-cs-d9282bd218a35e2399d27e98cdd6f7a0a8552bb7 +cache +cache_formatted +clientRepository +error_reports +prettier +node +biome \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Configuration/AssetCacheConfig.cs b/ReferenceClientProxyImplementation/Configuration/AssetCacheConfig.cs new file mode 100644 index 0000000..7360a0a --- /dev/null +++ b/ReferenceClientProxyImplementation/Configuration/AssetCacheConfig.cs @@ -0,0 +1,15 @@ +namespace ReferenceClientProxyImplementation.Configuration; + +public class AssetCacheConfig { + public bool MemoryCache { get; set; } = true; + public bool DiskCache { get; set; } = true; + public string DiskCachePath { get; set; } = "cache"; + public bool WipeOnStartup { get; set; } = false; + public string DiskCacheBaseDirectory { get; set; } = "./clientRepository"; + public List> ExecOnRevisionChange { get; set; } = []; + public bool DitchPatchedOnStartup { get; set; } = false; + public string BiomePath { get; set; } = "biome"; + public string PrettierPath { get; set; } = "prettier"; + + public string NodePath { get; set; } = "node"; +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Configuration/ProxyConfiguration.cs b/ReferenceClientProxyImplementation/Configuration/ProxyConfiguration.cs new file mode 100644 index 0000000..84afacd --- /dev/null +++ b/ReferenceClientProxyImplementation/Configuration/ProxyConfiguration.cs @@ -0,0 +1,8 @@ +namespace ReferenceClientProxyImplementation.Configuration; + +public class ProxyConfiguration { + public ProxyConfiguration(IConfiguration configuration) => configuration.GetRequiredSection("ProxyConfiguration").Bind(this); + + public TestClientConfig TestClient { get; set; } = new(); + public AssetCacheConfig AssetCache { get; set; } = new(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Configuration/TestClientConfig.cs b/ReferenceClientProxyImplementation/Configuration/TestClientConfig.cs new file mode 100644 index 0000000..1776eef --- /dev/null +++ b/ReferenceClientProxyImplementation/Configuration/TestClientConfig.cs @@ -0,0 +1,13 @@ +namespace ReferenceClientProxyImplementation.Configuration; + +public class TestClientConfig { + public TestClientDebug DebugOptions = new(); + public bool Enabled { get; set; } = true; + // public bool UseLatest = true; + public string Revision { get; set; } = "canary"; + public Dictionary GlobalEnv { get; set; } = new(); + + // internal + public string RevisionPath { get; set; } = null!; + public string RevisionBaseUrl { get; set; } = null!; +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Configuration/TestClientDebug.cs b/ReferenceClientProxyImplementation/Configuration/TestClientDebug.cs new file mode 100644 index 0000000..b7370b5 --- /dev/null +++ b/ReferenceClientProxyImplementation/Configuration/TestClientDebug.cs @@ -0,0 +1,7 @@ +namespace ReferenceClientProxyImplementation.Configuration; + +public class TestClientDebug { + public bool DumpWebsocketTraffic = false; + public bool DumpWebsocketTrafficToBrowserConsole = false; + public TestClientPatchOptions PatchOptions = new(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Configuration/TestClientPatchOptions.cs b/ReferenceClientProxyImplementation/Configuration/TestClientPatchOptions.cs new file mode 100644 index 0000000..e95dfa5 --- /dev/null +++ b/ReferenceClientProxyImplementation/Configuration/TestClientPatchOptions.cs @@ -0,0 +1,7 @@ +namespace ReferenceClientProxyImplementation.Configuration; + +public class TestClientPatchOptions { + public bool GatewayImmediateReconnect = false; + public bool GatewayPlaintext = true; + public bool NoXssWarning = true; +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs b/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs new file mode 100644 index 0000000..8bd78bc --- /dev/null +++ b/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs @@ -0,0 +1,72 @@ +//#define MEMCACHE +using System.Collections.Frozen; +using ArcaneLibs.Extensions.Streams; +using Microsoft.AspNetCore.Mvc; +using ReferenceClientProxyImplementation.Configuration; +using ReferenceClientProxyImplementation.Services; + +namespace ReferenceClientProxyImplementation.Controllers; + +[Controller] +[Route("/")] +public class AssetsController(ProxyConfiguration proxyConfiguration, ClientStoreService clientStore) : Controller { +#if MEMCACHE + private static FrozenDictionary> memCache = new Dictionary>().ToFrozenDictionary(); +#endif + + [HttpGet("/assets/{*res:required}")] + public async Task Asset(string res) { + if (res is "version.staging.json" or "version.internal.json") + res = $"version.{proxyConfiguration.TestClient.Revision}.json"; + else if (res.EndsWith(".map")) { + return NotFound(); + } + + var ext = res.Split(".").Last(); + var contentType = ext switch { + //text types + "html" => "text/html", + "js" => "text/javascript", + "css" => "text/css", + "txt" => "text/plain", + "csv" => "text/csv", + "json" => "application/json", + //image types + "apng" => "image/apng", + "gif" => "image/gif", + "jpg" => "image/jpeg", + "png" => "image/png", + "svg" => "image/svg+xml", + "webp" => "image/webp", + "ico" => "image/x-icon", + //script types + "wasm" => "application/wasm", + _ => "application/octet-stream" + }; + // Response.Headers.ContentType = contentType; +#if MEMCACHE + if (memCache.TryGetValue(res, out var value)) { + // value.Position = 0; + var cms = new MemoryStream(value.ToArray()); + // await value.CopyToAsync(cms); + // ms.Position = 0; + + return new FileStreamResult(cms, contentType); + } +#endif + +#if MEMCACHE + var stream = await clientStore.GetPatchedClientAsset("assets/" + res); + stream.Position = 0; + // var ms = new MemoryStream(stream.ReadToEnd().ToArray(), false); + // memCache = memCache.Append(new KeyValuePair(res, ms)).ToFrozenDictionary(); + // return new FileStreamResult(ms, contentType); + var mem = new ReadOnlyMemory(stream.ReadToEnd().ToArray()); + memCache = memCache.Append(new KeyValuePair>(res, mem)).ToFrozenDictionary(); + return new FileStreamResult(new MemoryStream(mem.ToArray()), contentType); +#else + return new FileStreamResult(await clientStore.GetPatchedClientAsset("assets/" + res), contentType); +#endif + // return ; + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs.bak b/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs.bak new file mode 100644 index 0000000..ea5909a --- /dev/null +++ b/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs.bak @@ -0,0 +1,124 @@ +//using System.Collections.Concurrent; +//using System.Text; +//using Microsoft.AspNetCore.Mvc; +//using ReferenceClientProxyImplementation.Configuration; +//using ReferenceClientProxyImplementation.Services; +//using Spacebar.API.Helpers; +// +//namespace ReferenceClientProxyImplementation.Controllers; +// +//[Controller] +//[Route("/")] +//public class AssetsController(ProxyConfiguration proxyConfiguration, ClientStoreService clientStore) : Controller { +// private static readonly ConcurrentDictionary cache = new(); +// +// [HttpGet("/assets/{*res:required}")] +// public async Task Asset(string res) { +// var ext = res.Split(".").Last(); +// var contentType = ext switch { +// //text types +// "html" => "text/html", +// "js" => "text/javascript", +// "css" => "text/css", +// "txt" => "text/plain", +// "csv" => "text/csv", +// //image types +// "apng" => "image/apng", +// "gif" => "image/gif", +// "jpg" => "image/jpeg", +// "png" => "image/png", +// "svg" => "image/svg+xml", +// "webp" => "image/webp", +// "ico" => "image/x-icon", +// _ => "application/octet-stream" +// }; +// if (cache.ContainsKey(res)) +// return File(cache[res], contentType); +// +// if (System.IO.File.Exists("./Resources/Assets/" + res)) +// cache.TryAdd(res, await System.IO.File.ReadAllBytesAsync("./Resources/Assets/" + res)); +// else if (System.IO.File.Exists("./cache_formatted/" + res)) +// cache.TryAdd(res, await System.IO.File.ReadAllBytesAsync("./cache_formatted/" + res)); +// else if (System.IO.File.Exists("./cache/" + res)) +// cache.TryAdd(res, await System.IO.File.ReadAllBytesAsync($"{proxyConfiguration.AssetCache.DiskCachePath}/{res}")); +// else { +// if (!Directory.Exists(proxyConfiguration.AssetCache.DiskCachePath)) Directory.CreateDirectory(proxyConfiguration.AssetCache.DiskCachePath); +// if (res.EndsWith(".map")) return NotFound(); +// Console.WriteLine($"[Asset cache] Downloading {"https://discord.com/assets/" + res} -> {proxyConfiguration.AssetCache.DiskCachePath}/{res}"); +// try { +// using (var hc = new HttpClient()) { +// var resp = await hc.GetAsync("https://discord.com/assets/" + res); +// +// if (!resp.IsSuccessStatusCode) return NotFound(); +// //save to file +// var bytes = await resp.Content.ReadAsByteArrayAsync(); +// //check if cloudflare +// if (bytes.Length == 0) { +// Console.WriteLine( +// $"[Asset cache] Cloudflare detected, retrying {"https://discord.com/assets/" + res} -> {proxyConfiguration.AssetCache.DiskCachePath}/{res}"); +// await Task.Delay(1000); +// resp = await hc.GetAsync("https://discord.com/assets/" + res); +// if (!resp.IsSuccessStatusCode) return NotFound(); +// bytes = await resp.Content.ReadAsByteArrayAsync(); +// } +// +// //check if cloudflare html +// /*if (bytes.Length < 1000 && bytes.ToList().Contains(Encoding.UTF8.GetBytes("Cloudflare"))) +// { +// Console.WriteLine($"[Asset cache] Cloudflare detected, retrying {"https://discord.com/assets/" + res} -> ./cache/{res}"); +// await Task.Delay(1000); +// resp = await hc.GetAsync("https://discord.com/assets/" + res); +// if (!resp.IsSuccessStatusCode) return NotFound(); +// bytes = await resp.Content.ReadAsByteArrayAsync(); +// }*/ +// if (res.EndsWith(".js") || res.EndsWith(".css")) { +// //remove sourcemap +// var str = Encoding.UTF8.GetString(bytes); +// str = PatchClient(str); +// bytes = Encoding.UTF8.GetBytes(str); +// } +// +// if (proxyConfiguration.AssetCache.DiskCache) await System.IO.File.WriteAllBytesAsync($"{proxyConfiguration.AssetCache.DiskCachePath}/{res}", bytes); +// cache.TryAdd(res, bytes); +// } +// //await new WebClient().DownloadFileTaskAsync("https://discord.com/assets/" + res, "./cache/" + res); +// //cache.TryAdd(res, await System.IO.File.ReadAllBytesAsync("./cache/" + res)); +// } +// catch (Exception e) { +// Console.WriteLine(e); +// return NotFound(); +// } +// } +// +// if (cache.ContainsKey(res)) { +// var result = cache[res]; +// if (!proxyConfiguration.AssetCache.MemoryCache) cache.TryRemove(res, out _); +// return File(result, contentType); +// } +// +// return NotFound(); +// } +// +// public string PatchClient(string str) { +// var patchOptions = proxyConfiguration.TestClient.DebugOptions.PatchOptions; +// str = str.Replace("//# sourceMappingURL=", "//# disabledSourceMappingURL="); +// str = str.Replace("https://fa97a90475514c03a42f80cd36d147c4@sentry.io/140984", "https://6bad92b0175d41a18a037a73d0cff282@sentry.thearcanebrony.net/12"); +// if (patchOptions.GatewayPlaintext) +// str = str.Replace("e.isDiscordGatewayPlaintextSet=function(){0;return!1};", "e.isDiscordGatewayPlaintextSet = function() { return true };"); +// +// if (patchOptions.NoXssWarning) { +// str = str.Replace("console.log(\"%c\"+n.SELF_XSS_", "console.valueOf(n.SELF_XSS_"); +// str = str.Replace("console.log(\"%c\".concat(n.SELF_XSS_", "console.valueOf(console.valueOf(n.SELF_XSS_"); +// } +// +// if (patchOptions.GatewayImmediateReconnect) str = str.Replace("nextReconnectIsImmediate=!1", "nextReconnectIsImmediate = true"); +// +// return str; +// } +// +// [HttpGet("/robots.txt")] +// public object Robots() => Resolvers.ReturnFile("./Resources/robots.txt"); +// +// [HttpGet("/favicon.ico")] +// public object Favicon() => Resolvers.ReturnFile("./Resources/RunData/favicon.png"); +//} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Controllers/ErrorReportingProxy.cs b/ReferenceClientProxyImplementation/Controllers/ErrorReportingProxy.cs new file mode 100644 index 0000000..acc186b --- /dev/null +++ b/ReferenceClientProxyImplementation/Controllers/ErrorReportingProxy.cs @@ -0,0 +1,34 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Mvc; + +namespace ReferenceClientProxyImplementation.Controllers; + +[Controller] +[Route("/")] +public class ErrorReportingProxy : Controller { + [HttpPost("/error-reporting-proxy/web")] + public async Task HandleErrorReport() { + // read body as string + var body = await new StreamReader(Request.Body).ReadToEndAsync(); + var lines = body.Split('\n'); + var data = new JsonObject() { + ["eventInfo"] = JsonSerializer.Deserialize(lines[0]), + ["typeInfo"] = JsonSerializer.Deserialize(lines[1]), + ["stackTrace"] = JsonSerializer.Deserialize(lines[2]), + }; + + if (lines.Length > 3) + for (var i = 3; i < lines.Length; i++) { + data[$"unk_line_{i}"] = JsonSerializer.Deserialize(lines[i]); + } + + if (!System.IO.Directory.Exists("error_reports")) + System.IO.Directory.CreateDirectory("error_reports"); + await System.IO.File.WriteAllTextAsync($"error_reports/web_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}.json", data.ToJsonString(new JsonSerializerOptions { + WriteIndented = true + })); + + return NoContent(); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Controllers/FrontendController.cs b/ReferenceClientProxyImplementation/Controllers/FrontendController.cs new file mode 100644 index 0000000..b12996d --- /dev/null +++ b/ReferenceClientProxyImplementation/Controllers/FrontendController.cs @@ -0,0 +1,77 @@ +using Microsoft.AspNetCore.Mvc; +using ReferenceClientProxyImplementation.Configuration; +using ReferenceClientProxyImplementation.Helpers; +using ReferenceClientProxyImplementation.Patches.Implementations; + +namespace ReferenceClientProxyImplementation.Controllers; + +[Controller] +[Route("/")] +public class FrontendController(ProxyConfiguration proxyConfiguration, PatchSet patches) : Controller { + [HttpGet] + [HttpGet("/app")] + [HttpGet("/login")] + [HttpGet("/register")] + [HttpGet("/channels/@me")] + [HttpGet("/channels/{*_}")] + [HttpGet("/shop")] + [HttpGet("/app/{*_}")] + [HttpGet("/open")] + [HttpGet("/settings/{*_}")] + [HttpGet("/action/{*_}")] + [HttpGet("/library/{*_}")] + public async Task Home() { + var patchedPath = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "patched", "app.html"); + if (!System.IO.File.Exists(patchedPath)) { + var path = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "src", "app.html"); + var patchedContent = await patches.ApplyPatches("app.html", await System.IO.File.ReadAllBytesAsync(path)); + Directory.CreateDirectory(Path.GetDirectoryName(patchedPath)!); + await System.IO.File.WriteAllBytesAsync(patchedPath, patchedContent); + } + + return System.IO.File.OpenRead(patchedPath); + // return null; + // if (!proxyConfiguration.TestClient.Enabled) + // return NotFound("Test client is disabled"); + // var html = await System.IO.File.ReadAllTextAsync(proxyConfiguration.TestClient.UseLatest ? "Resources/Pages/index-updated.html" : "Resources/Pages/index.html"); + // + // //inject debug utilities + // var debugOptions = proxyConfiguration.TestClient.DebugOptions; + // if (debugOptions.DumpWebsocketTrafficToBrowserConsole) + // html = html.Replace("", + // await System.IO.File.ReadAllTextAsync("Resources/Private/Injections/WebSocketDataLog.html") + "\n"); + // if (debugOptions.DumpWebsocketTraffic) + // html = html.Replace("", + // await System.IO.File.ReadAllTextAsync("Resources/Private/Injections/WebSocketDumper.html") + "\n"); + // + // return File(Encoding.UTF8.GetBytes(html), "text/html"); + } + + [HttpGet("/developers")] + [HttpGet("/developers/{*_}")] + public async Task Developers() { + var patchedPath = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "patched", "developers.html"); + if (!System.IO.File.Exists(patchedPath)) { + var path = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "src", "developers.html"); + var patchedContent = await patches.ApplyPatches("developers.html", await System.IO.File.ReadAllBytesAsync(path)); + Directory.CreateDirectory(Path.GetDirectoryName(patchedPath)!); + await System.IO.File.WriteAllBytesAsync(patchedPath, patchedContent); + } + + return System.IO.File.OpenRead(patchedPath); + } + + [HttpGet("/popout")] + [HttpGet("/popout/{*_}")] + public async Task Popout() { + var patchedPath = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "patched", "popout.html"); + if (!System.IO.File.Exists(patchedPath)) { + var path = Path.Combine(proxyConfiguration.TestClient.RevisionPath, "src", "popout.html"); + var patchedContent = await patches.ApplyPatches("popout.html", await System.IO.File.ReadAllBytesAsync(path)); + Directory.CreateDirectory(Path.GetDirectoryName(patchedPath)!); + await System.IO.File.WriteAllBytesAsync(patchedPath, patchedContent); + } + + return System.IO.File.OpenRead(patchedPath); + } +} diff --git a/ReferenceClientProxyImplementation/Controllers/StaticController.cs b/ReferenceClientProxyImplementation/Controllers/StaticController.cs new file mode 100644 index 0000000..a2ecf2c --- /dev/null +++ b/ReferenceClientProxyImplementation/Controllers/StaticController.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Mvc; +using ReferenceClientProxyImplementation.Helpers; + +namespace ReferenceClientProxyImplementation.Controllers; + +[Controller] +[Route("/")] +public class StaticController : Controller { + [HttpGet("/resources/{*res:required}")] + public object Resource(string res) { + if (System.IO.File.Exists("./Resources/Static/" + res)) + return Resolvers.ReturnFile("./Resources/Static/" + res); + return new RedirectResult("https://discord.gg/assets/" + res); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Helpers/HtmlUtils.cs b/ReferenceClientProxyImplementation/Helpers/HtmlUtils.cs new file mode 100644 index 0000000..a8ee447 --- /dev/null +++ b/ReferenceClientProxyImplementation/Helpers/HtmlUtils.cs @@ -0,0 +1,16 @@ +using AngleSharp.Html; +using AngleSharp.Html.Parser; + +namespace ReferenceClientProxyImplementation.Helpers; + +public class HtmlUtils { + public static string CleanupHtml(string input) { + var parser = new HtmlParser(); + + var document = parser.ParseDocument(input); + + var sw = new StringWriter(); + document.ToHtml(sw, new PrettyMarkupFormatter()); + return sw.ToString(); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Helpers/Resolvers.cs b/ReferenceClientProxyImplementation/Helpers/Resolvers.cs new file mode 100644 index 0000000..813ffcb --- /dev/null +++ b/ReferenceClientProxyImplementation/Helpers/Resolvers.cs @@ -0,0 +1,80 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; + +namespace ReferenceClientProxyImplementation.Helpers; + +public static class Resolvers { + private static readonly string Navbar = File.Exists("Resources/Parts/Navbar.html") ? File.ReadAllText("Resources/Parts/Navbar.html") : "Navbar not found!"; + + public static object ReturnFile(string path) { + if (!File.Exists(path)) return new NotFoundObjectResult("File doesn't exist!"); + var ext = path.Split(".").Last(); + var contentType = ext switch { + //text types + "html" => "text/html", + "js" => "text/javascript", + "css" => "text/css", + "txt" => "text/plain", + "csv" => "text/csv", + //image types + "apng" => "image/apng", + "gif" => "image/gif", + "jpg" => "image/jpeg", + "png" => "image/png", + "svg" => "image/svg+xml", + "webp" => "image/webp", + "ico" => "image/x-icon", + _ => "application/octet-stream" + }; + switch (ext) { + case "html": + return new ContentResult { + ContentType = contentType, + Content = File.ReadAllText(path) + }; + case "js": + case "css": + case "txt": + case "csv": + case "svg": + return new ContentResult { + ContentType = contentType, + Content = File.ReadAllText(path) + }; + case "png": + case "webp": + case "jpg": + case "gif": + case "apng": + case "7z": + case "gz": + case "tar": + case "rar": + case "zip": + case "webm": + case "woff": + case "jar": + case "mp3": + case "mp4": + return new PhysicalFileResult(Path.GetFullPath(path), contentType); + default: + Console.WriteLine($"Unsupported filetype: {ext} ({path})"); + return new PhysicalFileResult(Path.GetFullPath(path), "application/octet-stream"); + } + } + + public static object ReturnFileWithVars(string path, Dictionary? customVars = null) { + if (!File.Exists(path)) return new NotFoundObjectResult(Debugger.IsAttached ? $"File {path} doesn't exist!" : "File doesn't exist!"); + var result = ReturnFile(path); + if (result.GetType() != typeof(ContentResult)) return result; + var contentResult = (ContentResult)result; + contentResult.Content = contentResult.Content?.Replace("$NAVBAR", Navbar); + if (customVars != null) + foreach (var (key, value) in customVars) + contentResult.Content = contentResult.Content?.Replace(key, value.ToString()); + + result = contentResult; + + return result; + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Constants/Constant.cs b/ReferenceClientProxyImplementation/Patches/Constants/Constant.cs new file mode 100644 index 0000000..09a00cc --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Constants/Constant.cs @@ -0,0 +1,6 @@ +namespace ReferenceClientProxyImplementation.Patches.Constants; + +public class Constant { + public required string SourceValue { get; set; } + public required string TargetValue { get; set; } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/IPatch.cs b/ReferenceClientProxyImplementation/Patches/IPatch.cs new file mode 100644 index 0000000..92cab93 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/IPatch.cs @@ -0,0 +1,8 @@ +namespace ReferenceClientProxyImplementation.Patches; + +public interface IPatch { + public int GetOrder(); + public string GetName(); + public bool Applies(string relativeName, byte[] content); + public Task Execute(string relativeName, byte[] content); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/FormatFilePatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/FormatFilePatch.cs new file mode 100644 index 0000000..649625f --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/FormatFilePatch.cs @@ -0,0 +1,91 @@ +using System.Diagnostics; +using ArcaneLibs; +using ReferenceClientProxyImplementation.Configuration; + +namespace ReferenceClientProxyImplementation.Patches.Implementations; + +public partial class FormatJsFilePatch(ProxyConfiguration config) : IPatch { + public int GetOrder() => -100; + + public string GetName() => "Format JS file"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js") || relativeName.EndsWith(".css") || relativeName.EndsWith(".html"); + + public async Task Execute(string relativeName, byte[] content) { + var cachePath = Path.Combine(config.TestClient.RevisionPath, "formatted", relativeName); + if (File.Exists(cachePath)) { + Console.WriteLine($"Using cached formatted file for {relativeName}"); + return await File.ReadAllBytesAsync(cachePath); + } + + // temporary: add some newlines + var stringContent = System.Text.Encoding.UTF8.GetString(content); + // stringContent = stringContent.Replace("function(){", "function() {\n"); + content = System.Text.Encoding.UTF8.GetBytes(stringContent); + + + Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!); + var tmpPath = $"{Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{Random.Shared.NextInt64()}_{Path.GetFileName(relativeName)}"; + await File.WriteAllBytesAsync(tmpPath, content); + var sw = Stopwatch.StartNew(); + ProcessStartInfo psi; + + // Biome doesn't support HTML and struggles with upstream emitting Sass directives + if (relativeName.EndsWith(".html") || relativeName.EndsWith(".css")) + psi = new ProcessStartInfo(config.AssetCache.PrettierPath, $"-w --print-width 240 {tmpPath}") { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + else + psi = new ProcessStartInfo(config.AssetCache.PrettierPath, $"-w --print-width 240 {tmpPath}") { + // psi = new ProcessStartInfo(config.AssetCache.BiomePath, $"format --write --line-width 240 --files-max-size=100000000 {tmpPath}") { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) { + throw new InvalidOperationException("Failed to start the formatting process."); + } + + // var stdout = await process.StandardOutput.ReadToEndAsync(); + // var stderr = await process.StandardError.ReadToEndAsync(); + // await process.WaitForExitAsync(); + + Dictionary stdoutLines = new(); + Dictionary stderrLines = new(); + + process.OutputDataReceived += (sender, args) => { + if (args.Data != null) { + stdoutLines[(ulong)sw.ElapsedMilliseconds] = args.Data; + Console.Write("O"); + } + }; + process.ErrorDataReceived += (sender, args) => { + if (args.Data != null) { + stderrLines[(ulong)sw.ElapsedMilliseconds] = args.Data; + Console.Write("E"); + } + }; + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + await process.WaitForExitAsync(); + + Console.WriteLine($"Formatted {relativeName} in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + + if (process.ExitCode != 0) { + Console.WriteLine($"Failed to format {relativeName} in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + Console.WriteLine("Standard Output:\n" + string.Join("\n", stdoutLines.OrderBy(kv => kv.Key).Select(kv => $"[{kv.Key}ms] {kv.Value}"))); + Console.WriteLine("Standard Error:\n" + string.Join("\n", stderrLines.OrderBy(kv => kv.Key).Select(kv => $"[{kv.Key}ms] {kv.Value}"))); + throw new Exception($"Failed to exec({psi.FileName} {psi.Arguments}): {string.Join("\n", stderrLines.OrderBy(kv => kv.Key).Select(kv => kv.Value))}"); + } + + var result = await File.ReadAllBytesAsync(tmpPath); + File.Move(tmpPath, cachePath); + return result; + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/FormatHtmlCssPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/FormatHtmlCssPatch.cs new file mode 100644 index 0000000..877a31a --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/FormatHtmlCssPatch.cs @@ -0,0 +1,72 @@ +using System.Diagnostics; +using ArcaneLibs; +using ReferenceClientProxyImplementation.Configuration; + +namespace ReferenceClientProxyImplementation.Patches.Implementations; + +public partial class FormatHtmlCssPatch(ProxyConfiguration config) : IPatch { + public int GetOrder() => -100; + + public string GetName() => "Format HTML/CSS file"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".css") || relativeName.EndsWith(".html"); + + public async Task Execute(string relativeName, byte[] content) { + var cachePath = Path.Combine(config.TestClient.RevisionPath, "formatted", relativeName); + if (File.Exists(cachePath)) { + Console.WriteLine($"Using cached formatted file for {relativeName}"); + return await File.ReadAllBytesAsync(cachePath); + } + + Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!); + var tmpPath = $"{Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{Random.Shared.NextInt64()}_{Path.GetFileName(relativeName)}"; + await File.WriteAllBytesAsync(tmpPath, content); + var sw = Stopwatch.StartNew(); + ProcessStartInfo psi = new ProcessStartInfo(config.AssetCache.PrettierPath, $"-w --print-width 240 {tmpPath}") { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) { + throw new InvalidOperationException("Failed to start the formatting process."); + } + + // var stdout = await process.StandardOutput.ReadToEndAsync(); + // var stderr = await process.StandardError.ReadToEndAsync(); + // await process.WaitForExitAsync(); + + Dictionary stdoutLines = new(); + Dictionary stderrLines = new(); + + while (!process.HasExited) { + while (!process.StandardOutput.EndOfStream) { + var line = await process.StandardOutput.ReadLineAsync(); + if (line == null) continue; + stdoutLines[(ulong)sw.ElapsedMilliseconds] = line; + Console.Write("O"); + } + + while (!process.StandardError.EndOfStream) { + var line = await process.StandardError.ReadLineAsync(); + if (line == null) continue; + stderrLines[(ulong)sw.ElapsedMilliseconds] = line; + Console.Write("E"); + } + } + + // Console.WriteLine($"Formatted {relativeName} in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + + if (process.ExitCode != 0) { + Console.WriteLine($"Failed to format {relativeName} in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + Console.WriteLine("Standard Output:\n" + string.Join("\n", stdoutLines.OrderBy(kv => kv.Key).Select(kv => $"[{kv.Key}ms] {kv.Value}"))); + Console.WriteLine("Standard Error:\n" + string.Join("\n", stderrLines.OrderBy(kv => kv.Key).Select(kv => $"[{kv.Key}ms] {kv.Value}"))); + throw new Exception($"Failed to format file {relativeName}: {string.Join("\n", stderrLines.OrderBy(kv => kv.Key).Select(kv => kv.Value))}"); + } + + var result = await File.ReadAllBytesAsync(tmpPath); + File.Move(tmpPath, cachePath); + return result; + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/GlobalEnvPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/GlobalEnvPatch.cs new file mode 100644 index 0000000..dc54850 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/GlobalEnvPatch.cs @@ -0,0 +1,22 @@ +using System.Text; +using System.Text.RegularExpressions; +using ReferenceClientProxyImplementation.Configuration; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.HTMLPatches; + +public class GlobalEnvPatch(ProxyConfiguration config) : IPatch { + public int GetOrder() => 1; + + public string GetName() => "Patch GLOBAL_ENV"; + public bool Applies(string relativeName, byte[] content) => relativeName is "app.html" or "developers.html" or "popout.html"; + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + foreach(var (key, value) in config.TestClient.GlobalEnv) { + stringContent = new Regex($"{key}: \".*?\"").Replace(stringContent, $"{key}: \"{value}\""); + } + + return Encoding.UTF8.GetBytes(stringContent); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/StripNoncesPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/StripNoncesPatch.cs new file mode 100644 index 0000000..6c5f312 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/StripNoncesPatch.cs @@ -0,0 +1,36 @@ +using System.Text; +using System.Text.RegularExpressions; +using ReferenceClientProxyImplementation.Configuration; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.HTMLPatches; + +public partial class StripNoncesPatch(ProxyConfiguration config) : IPatch { + public int GetOrder() => 0; + + public string GetName() => "Strip nonces/integrity from html"; + public bool Applies(string relativeName, byte[] content) => relativeName is "app.html" or "developers.html" or "popout.html"; + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + stringContent = HtmlScriptIntegrityRegex().Replace( + HtmlScriptNonceRegex().Replace( + JsElementNonceRegex().Replace( + stringContent, + "" + ), + "" + ), + "" + ); + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex("\\snonce=\"[a-zA-Z0-9+/=]+\"")] + private static partial Regex HtmlScriptNonceRegex(); + + [GeneratedRegex("\\w.nonce='[a-zA-Z0-9+/=]+';")] + private static partial Regex JsElementNonceRegex(); + + [GeneratedRegex(@"\sintegrity=""[a-zA-Z0-9+/=\-\s]+""")] + private static partial Regex HtmlScriptIntegrityRegex(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ApiProtocolPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ApiProtocolPatch.cs new file mode 100644 index 0000000..6e7f7a2 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ApiProtocolPatch.cs @@ -0,0 +1,32 @@ +using System.Text; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public class ApiProtocolPatch : IPatch { + public int GetOrder() => 0; + + public string GetName() => "API: Use GLOBAL_ENV.API_PROTOCOL instead of hardcoded https:"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + // TODO: regex + stringContent = stringContent + .Replace( + "return \"https:\" + window.GLOBAL_ENV.API_ENDPOINT + (e ? \"/v\".concat(window.GLOBAL_ENV.API_VERSION) : \"\");", + "return window.GLOBAL_ENV.API_PROTOCOL + window.GLOBAL_ENV.API_ENDPOINT + (e ? \"/v\".concat(window.GLOBAL_ENV.API_VERSION) : \"\");" + ) + .Replace( + "api_endpoint: \"\".concat(\"https:\").concat(window.GLOBAL_ENV.API_ENDPOINT)", + "api_endpoint: window.GLOBAL_ENV.API_PROTOCOL.concat(window.GLOBAL_ENV.API_ENDPOINT)" + ) + .Replace( + "f = null != d ? \"https://\".concat(d) : location.protocol + window.GLOBAL_ENV.API_ENDPOINT,", + "f = null != d ? window.GLOBAL_ENV.API_PROTOCOL.concat(d) : location.protocol + window.GLOBAL_ENV.API_ENDPOINT," + ) + ; + + return Encoding.UTF8.GetBytes(stringContent); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPatch.cs new file mode 100644 index 0000000..152a3f3 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPatch.cs @@ -0,0 +1,21 @@ +using System.Text; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public class BooleanPatch : IPatch { + public int GetOrder() => 0; + + public string GetName() => "Use real booleans in JS files"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = stringContent + .Replace("return!", "return !") + .Replace("!0", "true") + .Replace("!1", "false"); + + return Encoding.UTF8.GetBytes(stringContent); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPropagationPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPropagationPatch.cs new file mode 100644 index 0000000..fe73c8e --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/BooleanPropagationPatch.cs @@ -0,0 +1,21 @@ +// using System.Text; +// using System.Text.RegularExpressions; +// +// namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; +// +// public partial class BooleanPropagationPatch : IPatch { +// public int GetOrder() => 3; +// +// public string GetName() => "Patch pointless boolean comparisons in JS"; +// public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); +// +// public async Task Execute(string relativePath, byte[] content) { +// var stringContent = Encoding.UTF8.GetString(content); +// +// stringContent = stringContent +// .Replace(" && true", "").Replace(" || false", "").Replace("false || ", "") +// ; +// +// return Encoding.UTF8.GetBytes(stringContent); +// } +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/DisableSciencePatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/DisableSciencePatch.cs new file mode 100644 index 0000000..c44bf95 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/DisableSciencePatch.cs @@ -0,0 +1,26 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class DisableSciencePatch : IPatch { + public int GetOrder() => 0; + + public string GetName() => @"JS(web): Disable /science calls"; + public bool Applies(string relativeName, byte[] content) => relativeName.StartsWith("assets/web.") && relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + var match = HandleTrackDefinitionRegex().Match(stringContent); + stringContent = stringContent.Insert(match.Index + match.Length, @" + return (new Promise(() => { }), false); // ReferenceClientProxyImplementation: Disable /science calls + "); + + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex(@".\.handleTrack = function \(.\) \{", RegexOptions.Compiled)] + private static partial Regex HandleTrackDefinitionRegex(); + +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ExpandUnicodeEscapesPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ExpandUnicodeEscapesPatch.cs new file mode 100644 index 0000000..9eed00c --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/ExpandUnicodeEscapesPatch.cs @@ -0,0 +1,26 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class ExpandUnicodeEscapesPatch : IPatch { + public int GetOrder() => 0; + + public string GetName() => @"JS: expand \x?? to \u00??"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = XToURegex().Replace( + stringContent, + m => $"\\u00{m.Groups[1].Value}" + ); + + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex(@"\\x([0-9A-Fa-f]{2})", RegexOptions.Compiled)] + private static partial Regex XToURegex(); + +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/IsStaffPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/IsStaffPatch.cs new file mode 100644 index 0000000..b741f56 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/IsStaffPatch.cs @@ -0,0 +1,33 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class IsStaffPatch : IPatch { + public int GetOrder() => 2; + + public string GetName() => "Patch isStaff/isStaffPersonal in JS"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string relativePath, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = IsNullableStaffRegex().Replace( + stringContent, + m => $"{m.Groups[1].Value}!!{m.Groups[2].Value}" + ); + + stringContent = IsStaffRegex().Replace( + stringContent, + m => $"{m.Groups[1].Value}true" + ); + + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex(@"(\W)(\w|this|\w\.user)\.isStaff(Personal)?\(\)", RegexOptions.Compiled)] + private static partial Regex IsStaffRegex(); + + [GeneratedRegex(@"(\W)(\w|this|\w\.user)\?\.isStaff(Personal)?\(\)", RegexOptions.Compiled)] + private static partial Regex IsNullableStaffRegex(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/JsonParseMultilinePatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/JsonParseMultilinePatch.cs new file mode 100644 index 0000000..b5e7d77 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/JsonParseMultilinePatch.cs @@ -0,0 +1,79 @@ +using System.Diagnostics; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using ReferenceClientProxyImplementation.Configuration; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class JsonParseMultilinePatch(ProxyConfiguration config) : IPatch { + public int GetOrder() => 1; + + public string GetName() => "Patch null-coalescing expressions in JS"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string relativePath, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + var matches = JsonParseRegex().Matches(stringContent); + Console.WriteLine($"Found {matches.Count} JSON.parse calls in {relativePath}"); + + + + await Parallel.ForEachAsync(matches, async (match, ct) => { + string formattedJson = match.Groups[1].Value; + try { + var jsonElement = JsonSerializer.Deserialize(formattedJson.Replace("\\", "\\\\") + "waef"); + formattedJson = JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true }); + } catch (JsonException je) { + // Console.WriteLine($"STJ: Failed to parse JSON in {relativePath} at index {match.Index}: {je.Message}"); // intentinally broken + try { + formattedJson = await formatJsonWithNodejs(relativePath, match, ct); + } catch (Exception e) { + Console.WriteLine($"Node.js: Failed to parse JSON in {relativePath} at index {match.Index}: {e.Message}"); + return; + } + } + + lock (matches) stringContent = stringContent.Replace(match.Value, $"JSON.parse(`{formattedJson.Replace("\\", "\\\\")}`);"); + }); + + return Encoding.UTF8.GetBytes(stringContent); + } + + private async Task formatJsonWithNodejs(string relativePath, Match match, CancellationToken ct) { + // Extract the JSON string from the match + var id = "dcp_" + Path.GetFileName(relativePath).Replace('.', '_') + "_" + match.Index; + await File.WriteAllTextAsync($"{Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{id}.js", $"console.log(JSON.stringify(JSON.parse(`{match.Groups[1].Value.Replace("`", "\\\\\\`")}`), null, 2))"); + var sw = Stopwatch.StartNew(); + + var psi = new ProcessStartInfo(config.AssetCache.NodePath, $"{Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{id}.js") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; + + using var process = Process.Start(psi); + if (process == null) { + throw new InvalidOperationException("Failed to start the formatting process."); + } + + var stdout = await process.StandardOutput.ReadToEndAsync(); + var stderr = await process.StandardError.ReadToEndAsync(); + + await process.WaitForExitAsync(); + // Console.WriteLine($"Formatted {relativeName} in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + + if (process.ExitCode != 0) { + Console.WriteLine($"Failed to run {Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{id}.js in {sw.ElapsedMilliseconds}ms: {process.ExitCode}"); + Console.WriteLine("Standard Output: " + stdout); + Console.WriteLine("Standard Error: " + stderr); + throw new Exception($"Failed to execute {Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp"}/{id}.js: {stderr}"); + } + + var formattedJson = stdout.Trim(); + Console.WriteLine($"Parsed JSON({id}) in {sw.ElapsedMilliseconds}ms: {formattedJson.Length} bytes"); + // stringContent = stringContent.Replace(match.Value, $"JSON.parse(`{formattedJson.Replace("\\n", "\\\\n")}`);"); + await File.WriteAllTextAsync($"{config.TestClient.RevisionPath}/patched/assets/{Path.GetFileName(relativePath)}-{match.Index}.json", formattedJson); + return formattedJson; + } + + [GeneratedRegex(@"JSON\.parse\(\n\s*'(.*?)',?\s*\);", RegexOptions.Compiled | RegexOptions.Multiline)] + private static partial Regex JsonParseRegex(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/KnownConstantsPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/KnownConstantsPatch.cs new file mode 100644 index 0000000..a94e312 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/KnownConstantsPatch.cs @@ -0,0 +1,16 @@ +using System.Text; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public class KnownConstantsPatch : IPatch { + public int GetOrder() => 1; + + public string GetName() => "Use named constants"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + return Encoding.UTF8.GetBytes(stringContent); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LegacyJsPatches.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LegacyJsPatches.cs new file mode 100644 index 0000000..e7f78a0 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LegacyJsPatches.cs @@ -0,0 +1,32 @@ +// using System.Text; +// using System.Text.RegularExpressions; +// +// namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; +// +// public partial class LegacyJsPathces : IPatch { +// public int GetOrder() => 1; +// +// public string GetName() => "Patch deprecated JS constructs"; +// public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); +// +// public async Task Execute(string relativePath, byte[] content) { +// var stringContent = Encoding.UTF8.GetString(content); +// +// while(MozInputSourceRegex().IsMatch(stringContent)) { +// var match = MozInputSourceRegex().Match(stringContent); +// var replacement = match.Groups[1].Value switch { +// "0" => "", +// "1" => "mouse", +// "2" => "pen", +// "3" => "pen", +// "4" => "touch", +// _ => throw new InvalidOperationException("Unreachable") +// }; +// } +// +// return Encoding.UTF8.GetBytes(stringContent); +// } +// +// [GeneratedRegex(@"([0-6]) === (\w).mozInputSource", RegexOptions.Compiled)] +// private static partial Regex MozInputSourceRegex(); +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LogErrorContextPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LogErrorContextPatch.cs new file mode 100644 index 0000000..2005c4c --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/LogErrorContextPatch.cs @@ -0,0 +1,34 @@ +// using System.Text; +// using System.Text.RegularExpressions; +// +// namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; +// +// public partial class LogErrorContextPatch : IPatch { +// public int GetOrder() => 2; +// +// public string GetName() => "Patch assertions to log more context"; +// public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); +// +// public async Task Execute(string relativePath, byte[] content) { +// var stringContent = Encoding.UTF8.GetString(content); +// +// stringContent = NotNullAssertionRegex().Replace( +// stringContent, +// m => { +// var methodName = m.Groups[1].Value; +// var objectName = m.Groups[2].Value; +// var message = m.Groups[3].Value; +// Console.WriteLine($"Patching not-null assertion in {relativePath}: {methodName} - {message}"); +// +// return $@"{methodName}()(null != {objectName}, ""{message} - Context: "" + JSON.stringify({objectName}))"; +// } +// ); +// +// return Encoding.UTF8.GetBytes(stringContent); +// } +// +// // null assertion: u()(null != o, "PrivateChannel.renderAvatar: Invalid prop configuration - no user or channel"); +// // capture: method name, object name, message +// [GeneratedRegex(@"([a-zA-Z0-9_]+)\(\)\(\s*null != ([a-zA-Z0-9_]+),\s*""([^""]+)""\s*\)", RegexOptions.Compiled)] +// private static partial Regex NotNullAssertionRegex(); +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/NullCoalescingPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/NullCoalescingPatch.cs new file mode 100644 index 0000000..98156c8 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/NullCoalescingPatch.cs @@ -0,0 +1,32 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class NullCoalescingPatch : IPatch { + public int GetOrder() => 1; + + public string GetName() => "Patch null-coalescing expressions in JS"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string relativePath, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = NullCoalescingRegex().Replace( + stringContent, + m => $"{m.Groups[1].Value}?.{m.Groups[2].Value}" + ); + // stringContent = ParenNullCheckRegex().Replace( + // stringContent, + // m => $"{m.Groups[1].Value} == null" + // ); + + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex(@"null == ([a-zA-Z0-9_]+?) \? undefined : \1\.([a-zA-Z0-9_]+?)", RegexOptions.Compiled)] + private static partial Regex NullCoalescingRegex(); + + [GeneratedRegex(@"\(([^()]+?)\) == null", RegexOptions.Compiled)] + private static partial Regex ParenNullCheckRegex(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs new file mode 100644 index 0000000..bab0756 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs @@ -0,0 +1,56 @@ +// using System.Text; +// using System.Text.RegularExpressions; +// using ReferenceClientProxyImplementation.Services; +// +// namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; +// +// public partial class PrefetchAssetsPatch(IServiceProvider sp) : IPatch { +// public int GetOrder() => 1000000; +// +// public string GetName() => "Prefetch assets"; +// public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); +// +// private static SemaphoreSlim ss = new(2, 2); +// private static HashSet alreadyKnownAssets = new(); +// +// public async Task Execute(string relativePath, byte[] content) { +// // Can't inject service due to loop +// var stringContent = Encoding.UTF8.GetString(content); +// var matches = PrefetchAssetsRegex().Matches(stringContent); +// +// Console.WriteLine($"Found {matches.Count} prefetch assets in {relativePath}"); +// if (matches.Count == 0) { +// return content; // No matches found, return original content +// } +// +// var clientStore = sp.GetRequiredService(); +// +// var newAssets = matches +// .Select(x => x.Groups[1].Value) +// .Distinct() +// .Where(x => !clientStore.HasRawAsset(x) && alreadyKnownAssets.Add(x)); +// +// var tasks = newAssets +// .Select(async match => { +// await ss.WaitAsync(); +// Console.WriteLine($"Discovered prefetch asset in {relativePath}: {match}"); +// // var patches = sp.GetRequiredService(); +// var res = await clientStore.GetOrDownloadRawAsset(match); +// await res.DisposeAsync(); +// ss.Release(); +// Console.WriteLine($"Prefetched asset {match} in {relativePath}"); +// }).ToList(); +// +// if (tasks.Count == 0) { +// Console.WriteLine($"No new prefetch assets found in {relativePath}, returning original content."); +// return content; // No new assets to prefetch, return original content +// } +// +// await Task.WhenAny(tasks); +// +// return content; +// } +// +// [GeneratedRegex(@".\.exports = ""((?:[a-z\d/\.]*?)\.\w{2,4})""", RegexOptions.Compiled)] +// private static partial Regex PrefetchAssetsRegex(); +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/Void0Patch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/Void0Patch.cs new file mode 100644 index 0000000..9d819c7 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/Void0Patch.cs @@ -0,0 +1,27 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class Void0Patch : IPatch { + public int GetOrder() => 0; + + public string GetName() => "Use literal undefined instead of void 0"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string _, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = stringContent + .Replace("void 0", "undefined"); + stringContent = VoidFunctionRegex().Replace( + stringContent, + m => $"{m.Groups[1].Value}(" + ); + + return Encoding.UTF8.GetBytes(stringContent); + } + + [GeneratedRegex(@"\(0, ([a-zA-Z0-9_.$]+?)\)\(", RegexOptions.Compiled)] + private static partial Regex VoidFunctionRegex(); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/WhileTruePatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/WhileTruePatch.cs new file mode 100644 index 0000000..277bf8a --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/WhileTruePatch.cs @@ -0,0 +1,19 @@ +using System.Text; +using System.Text.RegularExpressions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class WhileTruePatch : IPatch { + public int GetOrder() => 1; + + public string GetName() => "Patch while(true) expressions in JS"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string relativePath, byte[] content) { + var stringContent = Encoding.UTF8.GetString(content); + + stringContent = stringContent.Replace("for (;;)", "while (true)"); + + return Encoding.UTF8.GetBytes(stringContent); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/PatchSet.cs b/ReferenceClientProxyImplementation/Patches/Implementations/PatchSet.cs new file mode 100644 index 0000000..c3dba59 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/PatchSet.cs @@ -0,0 +1,26 @@ +namespace ReferenceClientProxyImplementation.Patches.Implementations; + +public class PatchSet(IServiceProvider sp) { + public List Patches { get; } = sp.GetServices().OrderBy(x => x.GetOrder()).ToList(); + + public async Task ApplyPatches(string relativeName, byte[] content) { + var i = 0; + var patches = Patches + .Where(p => p.Applies(relativeName, content)) + .OrderBy(p => p.GetOrder()) + .ToList(); + foreach (var patch in patches) { + if (patch.Applies(relativeName, content)) { + var defaultColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.DarkBlue; + Console.Write("==> "); + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.WriteLine($"Running task {++i}/{patches.Count}: {patch.GetName()} (Type<{patch.GetType().Name}>)"); + Console.ForegroundColor = defaultColor; + content = await patch.Execute(relativeName, content); + } + } + + return content; + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Program.cs b/ReferenceClientProxyImplementation/Program.cs new file mode 100644 index 0000000..c35248b --- /dev/null +++ b/ReferenceClientProxyImplementation/Program.cs @@ -0,0 +1,107 @@ +using ArcaneLibs; +using Microsoft.AspNetCore.HttpLogging; +using Microsoft.Extensions.Hosting.Systemd; +using ReferenceClientProxyImplementation.Configuration; +using ReferenceClientProxyImplementation.Patches; +using ReferenceClientProxyImplementation.Patches.Implementations; +using ReferenceClientProxyImplementation.Services; +using ReferenceClientProxyImplementation.Tasks; + +// using Spacebar.API.Tasks.Startup; + +if (!Directory.Exists("cache_formatted")) Directory.CreateDirectory("cache_formatted"); +if (!Directory.Exists("cache")) Directory.CreateDirectory("cache"); +/*foreach (var file in Directory.GetFiles("cache").Where(x => x.EndsWith(".js"))) +{ + //JsFormatter.FormatJsFile(File.OpenRead(file), File.OpenWrite(file.Replace("cache", "cache_formatted"))); +}*/ + +/*var processes = Directory.GetFiles("cache").Where(x => x.EndsWith(".js")).Select(file => JsFormatter.SafeFormat(file, file.Replace("cache", "cache_formatted"))).ToList(); +while (processes.Any(x => !x.HasExited)) +{ + Thread.Sleep(100); +}*/ + +//Environment.Exit(0); +// Tasks.RunStartup(); + +var builder = WebApplication.CreateBuilder(args); + +builder.Host.ConfigureHostOptions(host => { + host.ServicesStartConcurrently = true; + host.ServicesStopConcurrently = true; + host.ShutdownTimeout = TimeSpan.FromSeconds(5); +}); + +// builder.Services.AddHostedService(); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddHttpLogging(o => { o.LoggingFields = HttpLoggingFields.All; }); +builder.Services.AddLogging(o => { + if (SystemdHelpers.IsSystemdService()) + o.AddSystemdConsole(); + else o.AddConsole(); +}); + +builder.Services.AddSingleton(); +// builder.Services.AddSingleton(); + +foreach (var taskType in ClassCollector.ResolveFromAllAccessibleAssemblies()) +{ + builder.Services.AddSingleton(typeof(ITask), taskType); +} +builder.Services.AddHostedService(); + +foreach (var taskType in ClassCollector.ResolveFromAllAccessibleAssemblies()) +{ + builder.Services.AddSingleton(typeof(IPatch), taskType); +} +builder.Services.AddSingleton(); + +builder.Services.AddSingleton(); + +var app = builder.Build(); + +// +if (app.Environment.IsDevelopment()) { + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpLogging(); +app.UseRouting(); +// app.UseSentryTracing(); + +// app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + +app.Use((context, next) => { + context.Response.Headers["Content-Type"] += "; charset=utf-8"; + context.Response.Headers["Access-Control-Allow-Origin"] = "*"; + return next.Invoke(); +}); +app.UseCors("*"); + +app.MapControllers(); +app.UseDeveloperExceptionPage(); + +// +app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller=FrontendController}/{action=Index}/{id?}"); }); + +Console.WriteLine("Starting web server!"); +if (args.Contains("--exit-on-modified")) { + Console.WriteLine("[WARN] --exit-on-modified enabled, exiting on source file change!"); + new FileSystemWatcher { + Path = Environment.CurrentDirectory, + Filter = "*.cs", + NotifyFilter = NotifyFilters.LastWrite, + EnableRaisingEvents = true + }.Changed += async (sender, args) => { + Console.WriteLine("Source modified. Exiting..."); + await app.StopAsync(); + Environment.Exit(0); + }; +} + +app.Run(); \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Properties/launchSettings.json b/ReferenceClientProxyImplementation/Properties/launchSettings.json new file mode 100644 index 0000000..3cac55a --- /dev/null +++ b/ReferenceClientProxyImplementation/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:60588", + "sslPort": 44301 + } + }, + "profiles": { + "Spacebar.API": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:2000;https://localhost:2001;http://localhost:2002", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "CONFIG_PATH": "../Config.json" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ReferenceClientProxyImplementation/ReferenceClientProxyImplementation.csproj b/ReferenceClientProxyImplementation/ReferenceClientProxyImplementation.csproj new file mode 100644 index 0000000..14bdb55 --- /dev/null +++ b/ReferenceClientProxyImplementation/ReferenceClientProxyImplementation.csproj @@ -0,0 +1,34 @@ + + + + net10.0 + enable + enable + false + preview + + + False + None + True + + + + + + + + + + + + + + + + + + + + + diff --git a/ReferenceClientProxyImplementation/Resources/Assets/checkLocale.js b/ReferenceClientProxyImplementation/Resources/Assets/checkLocale.js new file mode 100644 index 0000000..2f46120 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/checkLocale.js @@ -0,0 +1,47 @@ +const localStorage = window.localStorage; +// TODO: remote auth +// window.GLOBAL_ENV.REMOTE_AUTH_ENDPOINT = window.GLOBAL_ENV.GATEWAY_ENDPOINT.replace(/wss?:/, ""); +localStorage.setItem("gatewayURL", window.GLOBAL_ENV.GATEWAY_ENDPOINT); +localStorage.setItem( + "DeveloperOptionsStore", + `{"trace":false,"canary":false,"logGatewayEvents":true,"logOverlayEvents":true,"logAnalyticsEvents":true,"sourceMapsEnabled":false,"axeEnabled":false}` +); + +const supportedLocales = [ + "bg", + "cs", + "da", + "de", + "el", + "en-GB", + "es-ES", + "fi", + "fr", + "hi", + "hr", + "hu", + "it", + "ja", + "ko", + "lt", + "nl", + "no", + "pl", + "pt-BR", + "ro", + "ru", + "sv-SE", + "th", + "tr", + "uk", + "vi", + "zh-CN", + "zh-TW" +]; + +const settings = JSON.parse(localStorage.getItem("UserSettingsStore")); +if (settings && !supportedLocales.includes(settings.locale)) { + // fix client locale wrong and client not loading at all + settings.locale = "en-US"; + localStorage.setItem("UserSettingsStore", JSON.stringify(settings)); +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Resources/Assets/dff87c953f43b561d71fbcfe8a93a79a.png b/ReferenceClientProxyImplementation/Resources/Assets/dff87c953f43b561d71fbcfe8a93a79a.png new file mode 100644 index 0000000..e69de29 diff --git a/ReferenceClientProxyImplementation/Resources/Assets/endpoints.json b/ReferenceClientProxyImplementation/Resources/Assets/endpoints.json new file mode 100644 index 0000000..d5478e7 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/endpoints.json @@ -0,0 +1,115 @@ +{ + "USER_CHANNELS": "/users/@me/channels", + "USER_ACTIVITY_STATISTICS": "/users/@me/activities/statistics/applications", + "ACTIVITIES": "/activities", + "LOBBIES": "/lobbies", + "LOBBY_SEARCH": "/lobbies/search", + "NETWORKING_TOKEN": "/networking/token", + "USER_GAMES_NOTIFICATIONS": "/users/@me/settings/game-notifications", + "USER_GAMES_NOTIFICATIONS_OVERRIDES": "/users/@me/settings/game-notifications/overrides", + "UNVERIFIED_APPLICATIONS": "/unverified-applications", + "UNVERIFIED_APPLICATIONS_ICONS": "/unverified-applications/icons", + "BULK_ACK": "/read-states/ack-bulk", + "GUILDS": "/guilds", + "CHANNELS": "/channels", + "TUTORIAL_INDICATORS": "/tutorial/indicators", + "TUTORIAL_INDICATORS_SUPPRESS": "/tutorial/indicators/suppress", + "USERS": "/users", + "ME": "/users/@me", + "DELETE_ACCOUNT": "/users/@me/delete", + "DISABLE_ACCOUNT": "/users/@me/disable", + "DEVICES": "/users/@me/devices", + "SETTINGS": "/users/@me/settings", + "SETTINGS_CONSENT": "/users/@me/consent", + "PHONE": "/users/@me/phone", + "VERIFY_PHONE": "/users/@me/phone/verify", + "VERIFY_PHONE_NEW": "/phone-verifications/verify", + "RESEND_PHONE": "/phone-verifications/resend", + "CONNECTIONS": "/users/@me/connections", + "CONNECTION_SYNC_CONTACTS": "/users/@me/connections/contacts/@me/external-friend-list-entries", + "NOTES": "/users/@me/notes", + "MENTIONS": "/users/@me/mentions", + "CAPTCHA": "/users/@me/captcha/verify", + "EXPERIMENTS": "/experiments", + "LOGIN": "/auth/login", + "LOGIN_MFA": "/auth/mfa/totp", + "LOGIN_SMS": "/auth/mfa/sms", + "LOGIN_SMS_SEND": "/auth/mfa/sms/send", + "REMOTE_AUTH_INITIALIZE": "/users/@me/remote-auth", + "REMOTE_AUTH_CANCEL": "/users/@me/remote-auth/cancel", + "REMOTE_AUTH_FINISH": "/users/@me/remote-auth/finish", + "LOGOUT": "/auth/logout", + "REGISTER": "/auth/register", + "REGISTER_PHONE": "/auth/register/phone", + "TRACK": "/science", + "SSO": "/sso", + "VERIFY": "/auth/verify", + "AUTHORIZE_IP": "/auth/authorize-ip", + "VERIFY_RESEND": "/auth/verify/resend", + "FORGOT_PASSWORD": "/auth/forgot", + "RESET_PASSWORD": "/auth/reset", + "ICE": "/voice/ice", + "REPORT": "/report", + "REPORT_V2": "/reports", + "REPORT_OPTIONS": "/report/options", + "INTEGRATIONS": "/integrations", + "GATEWAY": "/gateway", + "APPLICATIONS_DETECTABLE": "/applications/detectable", + "OAUTH2_AUTHORIZE": "/oauth2/authorize", + "OAUTH2_AUTHORIZE_WEBHOOK_CHANNELS": "/oauth2/authorize/webhook-channels", + "OAUTH2_CURRENT_AUTH": "/oauth2/@me", + "OAUTH2_TOKENS": "/oauth2/tokens", + "OAUTH2_WHITELIST_ACCEPT": "/oauth2/whitelist/accept", + "MFA_TOTP_ENABLE": "/users/@me/mfa/totp/enable", + "MFA_TOTP_DISABLE": "/users/@me/mfa/totp/disable", + "MFA_SMS_ENABLE": "/users/@me/mfa/sms/enable", + "MFA_SMS_DISABLE": "/users/@me/mfa/sms/disable", + "MFA_CODES": "/users/@me/mfa/codes", + "DISABLE_EMAIL_NOTIFICATIONS": "/users/disable-email-notifications", + "GUILD_PREMIUM_SUBSCRIPTION_COOLDOWN": "/users/@me/guilds/premium/subscriptions/cooldown", + "USER_GUILD_PREMIUM_SUBSCRIPTIONS": "/users/@me/guilds/premium/subscriptions", + "USER_PREMIUM_GUILD_SUBSCRIPTION_SLOTS": "/users/@me/guilds/premium/subscription-slots", + "BILLING_STRIPE_SETUP_INTENT_SECRET": "/users/@me/billing/stripe/setup-intents", + "BILLING_PAYMENT_SOURCES": "/users/@me/billing/payment-sources", + "BILLING_PAYMENTS": "/users/@me/billing/payments", + "BILLING_BRAINTREE_POPUP_BRIDGE": "/billing/braintree/popup-bridge", + "BILLING_BRAINTREE_POPUP_BRIDGE_CALLBACK": "/billing/braintree/popup-bridge/callback", + "BILLING_SUBSCRIPTIONS": "/users/@me/billing/subscriptions", + "BILLING_APPLY_APPLE_RECEIPT": "/billing/apple/apply-receipt", + "BILLING_INVOICE_PREVIEW": "/users/@me/billing/invoices/preview", + "USER_AGREEMENTS": "/users/@me/agreements", + "HANDOFF": "/auth/handoff", + "HANDOFF_EXCHANGE": "/auth/handoff/exchange", + "LIBRARY": "/users/@me/library", + "AUTH_CONSENT_REQUIRED": "/auth/consent-required", + "USER_HARVEST": "/users/@me/harvest", + "APPLICATION_BRANCHES": "/branches", + "APPLICATIONS_PUBLIC": "/applications/public", + "APPLICATIONS_TRENDING": "/applications/trending/global", + "STORE_PUBLISHED_LISTINGS_APPLICATIONS": "/store/published-listings/applications", + "STORE_PUBLISHED_LISTINGS_SKUS": "/store/published-listings/skus", + "ENTITLEMENTS_GIFTABLE": "/users/@me/entitlements/gifts", + "PROMOTIONS": "/promotions", + "PROMOTION_ACK": "/promotions/ack", + "HYPESQUAD_ONLINE": "/hypesquad/online", + "GIFS_SEARCH": "/gifs/search", + "GIFS_TRENDING": "/gifs/trending", + "GIFS_TRENDING_GIFS": "/gifs/trending-gifs", + "GIFS_SELECT": "/gifs/select", + "GIFS_SUGGEST": "/gifs/suggest", + "GIFS_TRENDING_SEARCH": "/gifs/trending-search", + "USER_GIFT_CODE_CREATE": "/users/@me/entitlements/gift-codes", + "USER_GIFT_CODES": "/users/@me/entitlements/gift-codes", + "GUILD_DISCOVERY": "/discoverable-guilds", + "GUILD_DISCOVERY_CATEGORIES": "/discovery/categories", + "GUILD_DISCOVERY_VALID_TERM": "/discovery/valid-term", + "USER_AFFINITIES": "/users/@me/affinities/users", + "GUILD_AFFINITIES": "/users/@me/affinities/guilds", + "XBOX_GAME_PASS_PROMOTION": "/promotions/xbox-game-pass", + "XBOX_GAME_PASS_PROMOTION_REDEEM": "/promotions/xbox-game-pass/redeem", + "FUNIMATION_PROMOTION": "/promotions/funimation", + "PARTNERS_CONNECTIONS": "/partners/connections", + "PARTNERS_APPLY": "/partners/apply", + "USER_STICKER_PACKS": "/users/@me/sticker-packs", + "INTERACTIONS": "/interactions" +} diff --git a/ReferenceClientProxyImplementation/Resources/Assets/features.json b/ReferenceClientProxyImplementation/Resources/Assets/features.json new file mode 100644 index 0000000..1e1ebb1 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/features.json @@ -0,0 +1,26 @@ +[ + "ANIMATED_ICON", + "BANNER", + "COMMERCE", + "COMMUNITY", + "DISCOVERABLE", + "DISCOVERABLE_DISABLED", + "ENABLED_DISCOVERABLE_BEFORE", + "HUB", + "INVITE_SPLASH", + "MONETIZATION_ENABLED", + "MORE_EMOJI", + "MORE_STICKERS", + "NEWS", + "PARTNERED", + "PREVIEW_ENABLED", + "PRIVATE_THREADS", + "SEVEN_DAY_THREAD_ARCHIVE", + "THREE_DAY_THREAD_ARCHIVE", + "THREADS_ENABLED", + "TICKETED_EVENTS_ENABLED", + "VANITY_URL", + "VERIFIED", + "VIP_REGIONS", + "WELCOME_SCREEN_ENABLED" +] diff --git a/ReferenceClientProxyImplementation/Resources/Assets/fosscord-login.css b/ReferenceClientProxyImplementation/Resources/Assets/fosscord-login.css new file mode 100644 index 0000000..37bb111 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/fosscord-login.css @@ -0,0 +1,71 @@ +/* replace tos acceptance popup */ +#app-mount > div:nth-child(7) > div > div > div.tooltipContent-bqVLWK { + visibility: hidden; +} + +#app-mount > div:nth-child(7) > div > div > div.tooltipContent-bqVLWK::after { + visibility: visible; + display: block; + content: "You need to agree to this instance's rules to continue"; + margin-top: -32px; +} + +/* replace login header */ +#app-mount > div.app-1q1i1E > div > div > div > div > form > div > div > div.mainLoginContainer-1ddwnR > h3 { + visibility: hidden; +} + +h3.title-jXR8lp.marginBottom8-AtZOdT.base-1x0h_U.size24-RIRrxO::after { + margin-top: -32px; + content: "Welcome to Spacebar!"; + visibility: visible; + display: block; +} + +/* Logo in top left when bg removed */ +#app-mount > div.app-1q1i1E > div > a { + /* replace me: original dimensions: 130x36 */ + background: url(https://raw.githubusercontent.com/fosscord/fosscord/master/assets-rebrand/svg/Spacebar-Wordmark-Gradient.svg); + width: 130px; + height: 23px; + background-size: contain; +} + +/* replace TOS text */ + +#app-mount +> div.app-1q1i1E +> div +> div +> div +> form +> div +> div +> div.flex-1xMQg5.flex-1O1GKY.horizontal-1ae9ci.horizontal-2EEEnY.flex-1O1GKY.directionRow-3v3tfG.justifyStart-2NDFzi.alignCenter-1dQNNs.noWrap-3jynv6.marginTop20-3TxNs6 +> label +> div.label-cywgfr.labelClickable-11AuB8.labelForward-1wfipV +> * { + visibility: hidden; +} + +#app-mount +> div.app-1q1i1E +> div +> div +> div +> form +> div +> div +> div.flex-1xMQg5.flex-1O1GKY.horizontal-1ae9ci.horizontal-2EEEnY.flex-1O1GKY.directionRow-3v3tfG.justifyStart-2NDFzi.alignCenter-1dQNNs.noWrap-3jynv6.marginTop20-3TxNs6 +> label +> div.label-cywgfr.labelClickable-11AuB8.labelForward-1wfipV::after { + visibility: visible; + content: "I have read and agree with the rules set by this instance."; + display: block; + margin-top: -16px; +} + +/* shrink login box to same size as register */ +.authBoxExpanded-2jqaBe { + width: 480px !important; +} diff --git a/ReferenceClientProxyImplementation/Resources/Assets/fosscord.css b/ReferenceClientProxyImplementation/Resources/Assets/fosscord.css new file mode 100644 index 0000000..283c29f --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/fosscord.css @@ -0,0 +1,44 @@ +/* loading spinner */ +#app-mount > div.app-1q1i1E > div.container-16j22k.fixClipping-3qAKRb > div.content-1-zrf2 > video { + filter: opacity(1); + background: url("http://www.clipartbest.com/cliparts/7ca/6Rr/7ca6RrLAi.gif"); + background-size: contain; + /* width: 64px; + height: 64px; */ + padding-bottom: 64px; + background-repeat: no-repeat; +} + +/* home button icon */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div.tutorialContainer-2sGCg9 > div > div.listItemWrapper-KhRmzM > div > svg > foreignObject > div > div { + background-image: url(https://raw.githubusercontent.com/fosscord/fosscord/master/assets-rebrand/svg/Spacebar-Icon-Rounded-Subtract.svg); + background-size: contain; + border-radius: 50%; +} + +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div.tutorialContainer-2sGCg9 > div > div.listItemWrapper-KhRmzM > div > svg > foreignObject > div > div, #app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div.tutorialContainer-2sGCg9 > div > div.listItemWrapper-KhRmzM > div > svg > foreignObject > div > div:hover { + background-color: white; +} + +/* Login QR */ +#app-mount > div.app-1q1i1E > div > div > div > div > form > div > div > div.transitionGroup-aR7y1d.qrLogin-1AOZMt, +#app-mount > div.app-1q1i1E > div > div > div > div > form > div > div > div.verticalSeparator-3huAjp, + /* Remove login bg */ +#app-mount > div.app-1q1i1E > div > svg, + /* Download bar */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > div > div.notice-3bPHh-.colorDefault-22HBa0, + /* Connection problem links */ +#app-mount > div.app-1q1i1E > div.container-16j22k.fixClipping-3qAKRb > div.problems-3mgf6w.slideIn-sCvzGz > div:nth-child(2), + /* Downloads button */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div:nth-child(7) > div.listItemWrapper-KhRmzM > div > svg > foreignObject > div, +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div:nth-child(6) > div, + /* help button */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > div > div.content-98HsJk > div.chat-3bRxxu > section > div.toolbar-1t6TWx > a, + /* download button start of guild */ +#chat-messages-899316648933185083 > div > div > div:nth-child(5), + /* Thread permissions etc popups */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > div > div.content-98HsJk > div.sidebar-2K8pFh.hasNotice-1XRy4h > nav > div.container-3O_wAf, + /* home button icon */ +#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > nav > ul > div.scroller-1Bvpku.none-2Eo-qx.scrollerBase-289Jih > div.tutorialContainer-2sGCg9 > div > div.listItemWrapper-KhRmzM > div > svg > foreignObject > div > div > svg { + display: none; +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Resources/Assets/plugins/.gitkeep b/ReferenceClientProxyImplementation/Resources/Assets/plugins/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/ReferenceClientProxyImplementation/Resources/Assets/preload-plugins/autoRegister.js b/ReferenceClientProxyImplementation/Resources/Assets/preload-plugins/autoRegister.js new file mode 100644 index 0000000..15ef582 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/preload-plugins/autoRegister.js @@ -0,0 +1,62 @@ +// Auto register guest account: +const prefix = [ + "mysterious", + "adventurous", + "courageous", + "precious", + "cynical", + "flamer ", + "despicable", + "suspicious", + "gorgeous", + "impeccable", + "lovely", + "stunning", + "keyed", + "phoned", + "glorious", + "amazing", + "strange", + "arcane" +]; +const suffix = [ + "Anonymous", + "Boy", + "Lurker", + "Keyhitter", + "User", + "Enjoyer", + "Hunk", + "Coolstar", + "Wrestling", + "TylerTheCreator", + "Ad", + "Gamer", + "Games", + "Programmer" +]; + +Array.prototype.random = function () { + return this[Math.floor(Math.random() * this.length)]; +}; + +function _generateName() { + return `${prefix.random()}${suffix.random()}`; +} + +var token = JSON.parse(localStorage.getItem("token")); +if (!token && location.pathname !== "/login" && location.pathname !== "/register") { + fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/auth/register`, { + method: "POST", + headers: {"content-type": "application/json"}, + body: JSON.stringify({username: `${_generateName()}`, consent: true}) //${Date.now().toString().slice(-4)} + }) + .then((x) => x.json()) + .then((x) => { + localStorage.setItem("token", `"${x.token}"`); + if (!window.localStorage) { + // client already loaded -> need to reload to apply the newly registered user token + location.reload(); + } + }); +} diff --git a/ReferenceClientProxyImplementation/Resources/Assets/tools/rightsCalculator.js b/ReferenceClientProxyImplementation/Resources/Assets/tools/rightsCalculator.js new file mode 100644 index 0000000..c324891 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/tools/rightsCalculator.js @@ -0,0 +1,17 @@ +String.prototype.replaceAt = function (index, replacement) { + return this.substring(0, index) + replacement + this.substring(index + 1); +} + +var legacyRights = 0n; +var modernRights = '0'.repeat(document.getElementsByTagName("input").length); +var configRights = {}; + +function calculate(a) { + console.log(a) + legacyRights += a.checked ? (1n << BigInt(a.value)) : -(1n << BigInt(a.value)) + modernRights = modernRights.replaceAt(a.value, a.checked ? '1' : '0') + configRights[a.name] = a.checked + document.getElementById("legacyRights").innerText = "Legacy rights (fosscord-server-ts): " + legacyRights + document.getElementById("modernRights").innerText = "User rights: " + modernRights + document.getElementById("configRights").value = JSON.stringify(configRights, null, 4) +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Resources/Assets/user.css b/ReferenceClientProxyImplementation/Resources/Assets/user.css new file mode 100644 index 0000000..a7e5c4f --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Assets/user.css @@ -0,0 +1 @@ +/* Your custom CSS goes here, enjoy! */ \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Resources/Assets/widget/banner1.png b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner1.png new file mode 100644 index 0000000..ed9bd5c Binary files /dev/null and b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner1.png differ diff --git a/ReferenceClientProxyImplementation/Resources/Assets/widget/banner2.png b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner2.png new file mode 100644 index 0000000..90d3713 Binary files /dev/null and b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner2.png differ diff --git a/ReferenceClientProxyImplementation/Resources/Assets/widget/banner3.png b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner3.png new file mode 100644 index 0000000..2235189 Binary files /dev/null and b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner3.png differ diff --git a/ReferenceClientProxyImplementation/Resources/Assets/widget/banner4.png b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner4.png new file mode 100644 index 0000000..e6bd7b6 Binary files /dev/null and b/ReferenceClientProxyImplementation/Resources/Assets/widget/banner4.png differ diff --git a/ReferenceClientProxyImplementation/Resources/Assets/widget/shield.png b/ReferenceClientProxyImplementation/Resources/Assets/widget/shield.png new file mode 100644 index 0000000..30277db Binary files /dev/null and b/ReferenceClientProxyImplementation/Resources/Assets/widget/shield.png differ diff --git a/ReferenceClientProxyImplementation/Resources/Pages/developers.html b/ReferenceClientProxyImplementation/Resources/Pages/developers.html new file mode 100644 index 0000000..9798554 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Pages/developers.html @@ -0,0 +1,52 @@ + + + + + + + + + Discord Test Client Developer Portal + + + + +
+ + + + + + diff --git a/ReferenceClientProxyImplementation/Resources/Pages/index-template.html b/ReferenceClientProxyImplementation/Resources/Pages/index-template.html new file mode 100644 index 0000000..ef77e22 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Pages/index-template.html @@ -0,0 +1,75 @@ + + + + + + Discord Test Client + + + + + + + +
+ + + + + + + + diff --git a/ReferenceClientProxyImplementation/Resources/Pages/index.html b/ReferenceClientProxyImplementation/Resources/Pages/index.html new file mode 100644 index 0000000..0b52736 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Pages/index.html @@ -0,0 +1,130 @@ + + + + + + Discord Test Client + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDataLog.html b/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDataLog.html new file mode 100644 index 0000000..ef16e72 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDataLog.html @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDumper.html b/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDumper.html new file mode 100644 index 0000000..14dc989 --- /dev/null +++ b/ReferenceClientProxyImplementation/Resources/Private/Injections/WebSocketDumper.html @@ -0,0 +1,22 @@ + \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Services/BuildDownloadService.cs b/ReferenceClientProxyImplementation/Services/BuildDownloadService.cs new file mode 100644 index 0000000..364c6c5 --- /dev/null +++ b/ReferenceClientProxyImplementation/Services/BuildDownloadService.cs @@ -0,0 +1,76 @@ +// using System.Net; +// using AngleSharp.Html.Parser; +// +// namespace ReferenceClientProxyImplementation.Services; +// +// public class BuildDownloadService(ILogger logger) { +// private static readonly HttpClient hc = new(); +// +// public async Task DownloadBuildFromArchiveOrg(string outputDirectory, DateTime timestamp) { +// // 20150906025145 +// var paddedTimestamp = timestamp.ToString("yyyyMMddHHmmss"); +// await DownloadBuildFromUrl(outputDirectory, $"https://web.archive.org/web/{paddedTimestamp}im_/https://discordapp.com/login"); +// } +// +// public async Task DownloadBuildFromUrl(string outputDirectory, string url) { +// logger.LogInformation("Downloading build from {url} to {outDir}", url, outputDirectory); +// var response = await hc.GetAsync(url); +// if (!response.IsSuccessStatusCode) +// throw new Exception($"Failed to download build from {url}"); +// var html = await response.Content.ReadAsStringAsync(); +// File.WriteAllText(outputDirectory + "/index.html", html); +// var parser = new HtmlParser(); +// var document = parser.ParseDocument(html); +// var assets = document.QuerySelectorAll("link[rel=stylesheet], link[rel=icon], script, img"); +// foreach (var asset in assets) { +// var assetUrl = asset.GetAttribute("href") ?? asset.GetAttribute("src"); +// if (assetUrl == null) +// continue; +// if (assetUrl.StartsWith("//")) { +// logger.LogWarning("Skipping asset {assetUrl} as it is a protocol-relative URL", assetUrl); +// continue; +// } +// +// var assetStream = await GetAssetStream(assetUrl); +// var assetPath = Path.Combine(outputDirectory, assetUrl.TrimStart('/')); +// Console.WriteLine($"Downloading asset {assetUrl} to {assetPath}"); +// Directory.CreateDirectory(Path.GetDirectoryName(assetPath)); +// await using var fs = File.Create(assetPath); +// await assetStream.CopyToAsync(fs); +// } +// +// logger.LogInformation("Downloading build from {url} complete!", url); +// } +// +// public async Task GetAssetStream(string asset) { +// asset = asset.Replace("/assets/", ""); +// var urlsToTry = new Stack(new[] { +// $"https://web.archive.org/web/0id_/https://discordapp.com/assets/{asset}", +// $"https://web.archive.org/web/0id_/https://discord.com/assets/{asset}", +// $"https://discord.com/assets/{asset}" +// }); +// while (urlsToTry.TryPop(out var urlToTry)) { +// if (string.IsNullOrWhiteSpace(urlToTry)) continue; +// try { +// var response = await hc.GetAsync(urlToTry, HttpCompletionOption.ResponseHeadersRead); +// if (response.IsSuccessStatusCode) { +// Console.WriteLine($"Got success for asset {asset} from {urlToTry}"); +// return await response.Content.ReadAsStreamAsync(); +// } +// //redirect +// +// if (response.StatusCode == HttpStatusCode.Found) { +// var redirectUrl = response.Headers.Location?.ToString(); +// if (string.IsNullOrWhiteSpace(redirectUrl)) continue; +// urlsToTry.Push(redirectUrl); +// } +// else logger.LogWarning("Failed to download asset {asset} from {urlToTry}", asset, urlToTry); +// } +// catch { +// // ignored +// } +// } +// +// throw new Exception($"Failed to download asset {asset}"); +// } +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Services/ClientStoreService.cs b/ReferenceClientProxyImplementation/Services/ClientStoreService.cs new file mode 100644 index 0000000..6bd7418 --- /dev/null +++ b/ReferenceClientProxyImplementation/Services/ClientStoreService.cs @@ -0,0 +1,62 @@ +using ArcaneLibs.Extensions.Streams; +using ReferenceClientProxyImplementation.Configuration; +using ReferenceClientProxyImplementation.Patches.Implementations; + +namespace ReferenceClientProxyImplementation.Services; + +public class ClientStoreService(ProxyConfiguration config, PatchSet patches) { + private static readonly HttpClient HttpClient = new(); + + public async Task GetPatchedClientAsset(string relativePath) { + if (relativePath.StartsWith("/")) { + relativePath = relativePath[1..]; + } + + var path = Path.Combine(config.TestClient.RevisionPath, "patched", relativePath); + + if (File.Exists(path)) + return File.OpenRead(path); + + var srcAsset = (await GetOrDownloadRawAsset(relativePath)).ReadToEnd().ToArray(); + var result = await patches.ApplyPatches(relativePath, srcAsset); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + if (!result.SequenceEqual(srcAsset)) { + await File.WriteAllBytesAsync(path, result); + return File.OpenRead(path); + } + + Console.WriteLine($"No patches applied for {relativePath}, returning original asset."); + return new MemoryStream(srcAsset); + } + + public async Task GetOrDownloadRawAsset(string relativePath) { + relativePath = relativePath.TrimStart('/'); + var assetPath = Path.Combine(config.TestClient.RevisionPath, "src", relativePath); + if (File.Exists(assetPath)) { + Console.WriteLine($"Asset {relativePath} already exists at {assetPath}, returning existing file."); + return File.OpenRead(assetPath); + } + + var url = $"{config.TestClient.RevisionBaseUrl}/{relativePath}"; + var response = await HttpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); + if (!response.IsSuccessStatusCode) { + Console.WriteLine($"Failed to download asset {relativePath} from {url}, status code: {response.StatusCode}"); + throw new FileNotFoundException($"Asset not found: {relativePath}"); + } + var contentStream = await response.Content.ReadAsStreamAsync(); + Directory.CreateDirectory(Path.GetDirectoryName(assetPath)!); + await using var fileStream = File.Create(assetPath); + await contentStream.CopyToAsync(fileStream); + fileStream.Close(); + contentStream.Close(); + Console.WriteLine($"Downloaded asset {relativePath} to {assetPath}"); + + return File.OpenRead(assetPath); + } + + public bool HasRawAsset(string relativePath) { + relativePath = relativePath.TrimStart('/'); + var assetPath = Path.Combine(config.TestClient.RevisionPath, "src", relativePath); + return File.Exists(assetPath); + } +} \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Services/ModernAssetLocator.cs b/ReferenceClientProxyImplementation/Services/ModernAssetLocator.cs new file mode 100644 index 0000000..039fc74 --- /dev/null +++ b/ReferenceClientProxyImplementation/Services/ModernAssetLocator.cs @@ -0,0 +1,3 @@ +namespace ReferenceClientProxyImplementation.Services; + +public class ModernAssetLocator { } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Services/TemporaryTestJob.cs b/ReferenceClientProxyImplementation/Services/TemporaryTestJob.cs new file mode 100644 index 0000000..b8998f1 --- /dev/null +++ b/ReferenceClientProxyImplementation/Services/TemporaryTestJob.cs @@ -0,0 +1,14 @@ +// namespace ReferenceClientProxyImplementation.Services; +// +// public class TemporaryTestJob(BuildDownloadService buildDownloadService) : BackgroundService { +// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { +// Console.WriteLine("Running test job"); +// var outDir = +// "/home/Rory/git/spacebar/server-cs/DevUtils/ReferenceClientProxyImplementation/downloadCache/today/raw/"; +// if (Directory.Exists(outDir)) +// Directory.Delete(outDir, true); +// Directory.CreateDirectory(outDir); +// // await buildDownloadService.DownloadBuildFromArchiveOrg(outDir, new DateTime(2014, 1, 1)); +// await buildDownloadService.DownloadBuildFromUrl(outDir, "https://canary.discord.com/app"); +// } +// } \ No newline at end of file diff --git a/ReferenceClientProxyImplementation/Tasks/Startup/BuildClientTask.cs b/ReferenceClientProxyImplementation/Tasks/Startup/BuildClientTask.cs new file mode 100644 index 0000000..8718fc5 --- /dev/null +++ b/ReferenceClientProxyImplementation/Tasks/Startup/BuildClientTask.cs @@ -0,0 +1,39 @@ +// using ReferenceClientProxyImplementation.Configuration; +// using ReferenceClientProxyImplementation.Helpers; +// +// namespace ReferenceClientProxyImplementation.Tasks.Startup; +// +// public class BuildClientTask(ProxyConfiguration proxyConfig) : ITask { +// public int GetOrder() => 10; +// +// public string GetName() => "Build updated test client"; +// +// public async Task Execute() { +// var hc = new HttpClient(); +// if (proxyConfig.AssetCache.WipeOnStartup) { +// Directory.Delete(proxyConfig.AssetCache.DiskCachePath, true); +// Directory.CreateDirectory(proxyConfig.AssetCache.DiskCachePath); +// } +// +// // if (!proxyConfig.TestClient.Enabled || +// // !proxyConfig.TestClient.UseLatest) { +// // Console.WriteLine("[Client Updater] Test client is disabled or not set to use latest version, skipping!"); +// return; +// // } +// +// Console.WriteLine("[Client updater] Fetching client"); +// var client = HtmlUtils.CleanupHtml(await hc.GetStringAsync("https://canary.discord.com/channels/@me")); +// Console.WriteLine("[Client updater] Building client..."); +// var target = File.ReadAllText("Resources/Pages/index-template.html"); +// var lines = client.Split("\n"); +// target = target.Replace("", +// string.Join("\n", lines.Where(x => x.Contains("link rel=\"prefetch\" as=\"script\"")))); +// target = target.Replace("", +// string.Join("\n", lines.Where(x => x.Contains("