commit a5346d1c61fd4f993b12b4f7a6d7e22592d95234 Author: Gervasio Marchand Date: Sat Dec 3 14:27:50 2022 -0300 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..57d704f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,132 @@ +############################### +# Core EditorConfig Options # +############################### +root = true +# All files +[*] +indent_style = space + +# XML project files +[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}] +indent_size = 2 + +# XML config files +[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}] +indent_size = 2 + +# Code files +[*.{cs,csx,vb,vbx}] +indent_size = 4 +insert_final_newline = true +charset = utf-8-bom +############################### +# .NET Coding Conventions # +############################### +[*.{cs,vb}] +# Organize usings +dotnet_sort_system_directives_first = true +# this. preferences +dotnet_style_qualification_for_field = false:silent +dotnet_style_qualification_for_property = false:silent +dotnet_style_qualification_for_method = false:silent +dotnet_style_qualification_for_event = false:silent +# Language keywords vs BCL types preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_predefined_type_for_member_access = true:silent +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent +# Modifier preferences +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent +dotnet_style_readonly_field = true:suggestion +# Expression-level preferences +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +############################### +# Naming Conventions # +############################### +# Style Definitions +dotnet_naming_style.pascal_case_style.capitalization = pascal_case +# Use PascalCase for constant fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style +dotnet_naming_symbols.constant_fields.applicable_kinds = field +dotnet_naming_symbols.constant_fields.applicable_accessibilities = * +dotnet_naming_symbols.constant_fields.required_modifiers = const +############################### +# C# Coding Conventions # +############################### +[*.cs] +# var preferences +csharp_style_var_for_built_in_types = true:silent +csharp_style_var_when_type_is_apparent = true:silent +csharp_style_var_elsewhere = true:silent +# Expression-bodied members +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +# Pattern matching preferences +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +# Null-checking preferences +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion +# Modifier preferences +csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:suggestion +# Expression-level preferences +csharp_prefer_braces = true:silent +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_pattern_local_over_anonymous_function = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion +############################### +# C# Formatting Rules # +############################### +# New line preferences +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true +# Indentation preferences +csharp_indent_case_contents = true +csharp_indent_switch_labels = true +csharp_indent_labels = flush_left +# Space preferences +csharp_space_after_cast = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_around_binary_operators = before_and_after +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +# Wrapping preferences +csharp_preserve_single_line_statements = true +csharp_preserve_single_line_blocks = true +############################### +# VB Coding Conventions # +############################### +[*.vb] +# Modifier preferences +visual_basic_preferred_modifier_order = Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const, WithEvents, Widening, Narrowing, Custom, Async:suggestion \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d38503 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.vscode/ +bin/ +obj/ +.vs/ +*.csproj.user +launchSettings.json +.idea/ +config.json +config-tokens.json +.DS_Store +DataDir* +.build/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..81a50d8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# syntax=docker/dockerfile:1 + +FROM mcr.microsoft.com/dotnet/sdk:6.0.403-alpine3.16 AS builder +WORKDIR /src +COPY src /src/ +RUN dotnet publish -c Release /src/FakeRelay.sln -o /app + +FROM mcr.microsoft.com/dotnet/aspnet:6.0.11-alpine3.16 +VOLUME ["/data"] +ENV CONFIG_PATH=/data/config.json +COPY --from=builder /app /app +RUN ln -s /app/FakeRelay.Cli /bin/fakerelay +RUN ln -s /app/FakeRelay.Web /bin/web +ENTRYPOINT ["fakerelay"] \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props new file mode 100644 index 0000000..f036334 --- /dev/null +++ b/src/Directory.Build.props @@ -0,0 +1,9 @@ + + + + + 3.4.255 + all + + + \ No newline at end of file diff --git a/src/FakeRelay.Cli/Commands/AddHostCommand.cs b/src/FakeRelay.Cli/Commands/AddHostCommand.cs new file mode 100644 index 0000000..5dde2aa --- /dev/null +++ b/src/FakeRelay.Cli/Commands/AddHostCommand.cs @@ -0,0 +1,16 @@ +using FakeRelay.Core.Helpers; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace FakeRelay.Cli.Commands; + +public class AddHostCommand : AsyncCommand +{ + public override async Task ExecuteAsync(CommandContext context, HostSettings settings) + { + var token = await ApiKeysHelper.AddTokenForHostAsync(settings.Host); + AnsiConsole.Markup($"[green]Key generated for {settings.Host}[/]\n"); + AnsiConsole.Markup($"[red]{token}[/]\n"); + return 0; + } +} diff --git a/src/FakeRelay.Cli/Commands/DeleteHostCommand.cs b/src/FakeRelay.Cli/Commands/DeleteHostCommand.cs new file mode 100644 index 0000000..9598159 --- /dev/null +++ b/src/FakeRelay.Cli/Commands/DeleteHostCommand.cs @@ -0,0 +1,15 @@ +using FakeRelay.Core.Helpers; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace FakeRelay.Cli.Commands; + +public class DeleteHostCommand: AsyncCommand +{ + public override async Task ExecuteAsync(CommandContext context, HostSettings settings) + { + await ApiKeysHelper.DeleteTokenForHostAsync(settings.Host); + AnsiConsole.Markup($"[green]Key deleted for {settings.Host}[/]\n"); + return 0; + } +} diff --git a/src/FakeRelay.Cli/Commands/UpdateHostCommand.cs b/src/FakeRelay.Cli/Commands/UpdateHostCommand.cs new file mode 100644 index 0000000..eba8a58 --- /dev/null +++ b/src/FakeRelay.Cli/Commands/UpdateHostCommand.cs @@ -0,0 +1,16 @@ +using FakeRelay.Core.Helpers; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace FakeRelay.Cli.Commands; + +public class UpdateHostCommand : AsyncCommand +{ + public override async Task ExecuteAsync(CommandContext context, HostSettings settings) + { + var token = await ApiKeysHelper.UpdateTokenForHostAsync(settings.Host); + AnsiConsole.Markup($"[green]Key generated for {settings.Host}[/]\n"); + AnsiConsole.Markup($"[red]{token}[/]\n"); + return 0; + } +} \ No newline at end of file diff --git a/src/FakeRelay.Cli/FakeRelay.Cli.csproj b/src/FakeRelay.Cli/FakeRelay.Cli.csproj new file mode 100644 index 0000000..010e655 --- /dev/null +++ b/src/FakeRelay.Cli/FakeRelay.Cli.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/src/FakeRelay.Cli/HostSettings.cs b/src/FakeRelay.Cli/HostSettings.cs new file mode 100644 index 0000000..7543675 --- /dev/null +++ b/src/FakeRelay.Cli/HostSettings.cs @@ -0,0 +1,9 @@ +using Spectre.Console.Cli; + +namespace FakeRelay.Cli; + +public class HostSettings : CommandSettings +{ + [CommandArgument(0, "")] + public string Host { get; set; } +} \ No newline at end of file diff --git a/src/FakeRelay.Cli/Program.cs b/src/FakeRelay.Cli/Program.cs new file mode 100644 index 0000000..fd4484a --- /dev/null +++ b/src/FakeRelay.Cli/Program.cs @@ -0,0 +1,15 @@ +using FakeRelay.Cli.Commands; +using FakeRelay.Core; +using Spectre.Console.Cli; + +var app = new CommandApp(); +app.Configure(config => +{ + config.AddCommand("add-host"); + config.AddCommand("update-host"); + config.AddCommand("delete-host"); +}); + +Config.Init(Environment.GetEnvironmentVariable("CONFIG_PATH")); + +return app.Run(args); \ No newline at end of file diff --git a/src/FakeRelay.Core/ActivityPubModel.cs b/src/FakeRelay.Core/ActivityPubModel.cs new file mode 100644 index 0000000..d6c5a7e --- /dev/null +++ b/src/FakeRelay.Core/ActivityPubModel.cs @@ -0,0 +1,10 @@ +namespace FakeRelay.Core; + +public class ActivityPubModel +{ + public string Id { get; set; } + public string Actor { get; set; } + private Uri? _actorUrl; + public Uri ActorUrl => _actorUrl ??= new Uri(Actor); + public string Type { get; set; } +} \ No newline at end of file diff --git a/src/FakeRelay.Core/Config.cs b/src/FakeRelay.Core/Config.cs new file mode 100644 index 0000000..f6cfb52 --- /dev/null +++ b/src/FakeRelay.Core/Config.cs @@ -0,0 +1,46 @@ +using System.Security.Cryptography; +using Jil; + +namespace FakeRelay.Core; + +public class Config +{ + public static Config? Instance { get; private set; } + + public byte[] PrivateKey { get; } + public string PublicKey { get; } + public string Host { get; } + + public string ConfigPath { get; } + + private Config(string publicKey, byte[] privateKey, string host, string configPath) + { + PrivateKey = privateKey; + PublicKey = publicKey; + Host = host; + ConfigPath = configPath; + } + + public static void Init(string path) + { + if (Instance != null) + { + return; + } + + var data = JSON.Deserialize(File.ReadAllText(path)); + if (data.PublicKey == null || data.PrivateKey == null || data.Host == null) + { + throw new Exception("Missing config parameters"); + } + + Instance = new Config(data.PublicKey, Convert.FromBase64String(data.PrivateKey), data.Host, path); + } + + private class ConfigData + { + public string? PublicKey { get; private set; } + public string? PrivateKey { get; private set; } + public string? Host { get; private set; } + } +} \ No newline at end of file diff --git a/src/FakeRelay.Core/FakeRelay.Core.csproj b/src/FakeRelay.Core/FakeRelay.Core.csproj new file mode 100644 index 0000000..e2562fc --- /dev/null +++ b/src/FakeRelay.Core/FakeRelay.Core.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/src/FakeRelay.Core/Helpers/ApiKeysHelper.cs b/src/FakeRelay.Core/Helpers/ApiKeysHelper.cs new file mode 100644 index 0000000..4ff2365 --- /dev/null +++ b/src/FakeRelay.Core/Helpers/ApiKeysHelper.cs @@ -0,0 +1,77 @@ +using System.Collections.Immutable; +using System.Security.Cryptography; +using Jil; + +namespace FakeRelay.Core.Helpers; + +public static class ApiKeysHelper +{ + private static string? _tokensFilePath; + private static string TokensFilePath => _tokensFilePath ??= Config.Instance.ConfigPath.Replace(".json", "-tokens.json"); + + public static async Task> GetTokenToHostAsync() + { + if (!File.Exists(TokensFilePath)) + { + return ImmutableDictionary.Empty; + } + + var content = await File.ReadAllTextAsync(TokensFilePath); + return JSON.Deserialize>(content) + .ToImmutableDictionary(StringComparer.OrdinalIgnoreCase); + } + + public static async Task UpdateTokenForHostAsync(string host) + { + var dict = await GetTokenToHostAsync(); + var hostToKeys = dict.ToLookup(d => d.Value, d => d.Key, StringComparer.OrdinalIgnoreCase); + if (!hostToKeys.Contains(host)) + { + throw new ArgumentException("The host doesn't have a key", nameof(host)); + } + + foreach (var key in hostToKeys[host]) + { + dict = dict.Remove(key); + } + + return await AddTokenForHostAsync(host, dict); + } + + public static async Task AddTokenForHostAsync(string host) => + await AddTokenForHostAsync(host, await GetTokenToHostAsync()); + + public static async Task DeleteTokenForHostAsync(string host) + { + var dict = await GetTokenToHostAsync(); + var hostToKeys = dict.ToLookup(d => d.Value, d => d.Key, StringComparer.OrdinalIgnoreCase); + if (!hostToKeys.Contains(host)) + { + throw new ArgumentException("The host does not have a key", nameof(host)); + } + + foreach (var key in hostToKeys[host]) + { + dict = dict.Remove(key); + } + + var content = JSON.Serialize(dict); + await File.WriteAllTextAsync(TokensFilePath, content); + } + + private static async Task AddTokenForHostAsync(string host, ImmutableDictionary dict) + { + var hostToKeys = dict.ToLookup(d => d.Value, d => d.Key, StringComparer.OrdinalIgnoreCase); + if (hostToKeys.Contains(host)) + { + throw new ArgumentException("The host already has a key", nameof(host)); + } + + var key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(64)); + + var content = JSON.Serialize(dict.Add(key, host)); + await File.WriteAllTextAsync(TokensFilePath, content); + + return key; + } +} \ No newline at end of file diff --git a/src/FakeRelay.Core/Helpers/CryptographyHelper.cs b/src/FakeRelay.Core/Helpers/CryptographyHelper.cs new file mode 100644 index 0000000..7dcd471 --- /dev/null +++ b/src/FakeRelay.Core/Helpers/CryptographyHelper.cs @@ -0,0 +1,24 @@ +using System.Security.Cryptography; +using System.Text; + +namespace FakeRelay.Core.Helpers; + +public class CryptographyHelper +{ + public static string GetSHA256Digest(string content) + { + using var sha256 = SHA256.Create(); + var hashValue = sha256.ComputeHash(Encoding.UTF8.GetBytes(content)); + return Convert.ToBase64String(hashValue); + } + + public static string Sign(string stringToSign) + { + using var rsaProvider = new RSACryptoServiceProvider(); + using var sha256 = SHA256.Create(); + rsaProvider.ImportRSAPrivateKey(Config.Instance.PrivateKey, out _); + + var signature = rsaProvider.SignData(Encoding.UTF8.GetBytes(stringToSign), sha256); + return Convert.ToBase64String(signature); + } +} \ No newline at end of file diff --git a/src/FakeRelay.Core/Helpers/MastodonHelper.cs b/src/FakeRelay.Core/Helpers/MastodonHelper.cs new file mode 100644 index 0000000..3061422 --- /dev/null +++ b/src/FakeRelay.Core/Helpers/MastodonHelper.cs @@ -0,0 +1,52 @@ +using System.Net.Http.Headers; + +namespace FakeRelay.Core.Helpers; + +public static class MastodonHelper +{ + public static Task EnqueueStatusToFetchAsync(string targetHost, string statusUrl) => + SendMessageToInboxAsync(targetHost, $@"{{ + ""@context"": ""https://www.w3.org/ns/activitystreams"", + ""actor"": ""https://{Config.Instance.Host}/actor"", + ""id"": ""https://{Config.Instance.Host}/activities/{Guid.NewGuid()}"", + ""object"": ""{statusUrl}"", + ""to"": [ + ""https://{Config.Instance.Host}/followers"" + ], + ""type"": ""Announce"" +}}"); + + public static async Task SendMessageToInboxAsync(string targetHost, string content) + { + var client = new HttpClient(); + + var date = DateTime.UtcNow; + + var digest = CryptographyHelper.GetSHA256Digest(content); + var requestContent = new StringContent(content); + + requestContent.Headers.Add("Digest", "SHA-256=" + digest); + + var stringToSign = $"(request-target): post /inbox\ndate: {date.ToString("R")}\nhost: {targetHost}\ndigest: SHA-256={digest}\ncontent-length: {content.Length}"; + var signature = CryptographyHelper.Sign(stringToSign); + requestContent.Headers.Add("Signature", $@"keyId=""https://{Config.Instance.Host}/actor#main-key"",algorithm=""rsa-sha256"",headers=""(request-target) date host digest content-length"",signature=""{signature}"""); + + requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/activity+json"); + client.DefaultRequestHeaders.Date = date; + + var response = await client.PostAsync($"https://{targetHost}/inbox", requestContent); + return await response.Content.ReadAsStringAsync(); + } + + public static async Task ProcessInstanceFollowAsync(ActivityPubModel request) + { + var host = request.ActorUrl.Host; + await SendMessageToInboxAsync(host, $@"{{""@context"": ""https://www.w3.org/ns/activitystreams"", ""type"": ""Accept"", ""to"": [""https://{host}/actor""], ""actor"": ""https://{Config.Instance.Host}/actor"", ""object"": {{""type"": ""Follow"", ""id"": ""{request.Id}"", ""object"": ""https://{Config.Instance.Host}/actor"", ""actor"": ""{request.Actor}""}}, ""id"": ""https://{Config.Instance.Host}/activities/{Guid.NewGuid()}""}}"); + } + + public static string GetActorStaticContent() => + $@"{{""@context"": ""https://www.w3.org/ns/activitystreams"", ""endpoints"": {{""sharedInbox"": ""https://{Config.Instance.Host}/inbox""}}, ""followers"": ""https://{Config.Instance.Host}/followers"", ""following"": ""https://{Config.Instance.Host}/following"", ""inbox"": ""https://{Config.Instance.Host}/inbox"", ""name"": ""FakeRelay"", ""type"": ""Application"", ""id"": ""https://{Config.Instance.Host}/actor"", ""publicKey"": {{""id"": ""https://{Config.Instance.Host}/actor#main-key"", ""owner"": ""https://{Config.Instance.Host}/actor"", ""publicKeyPem"": ""{Config.Instance.PublicKey.Replace("\n", "\\n")}""}}, ""summary"": ""FakeRelay bot"", ""preferredUsername"": ""relay"", ""url"": ""https://{Config.Instance.Host}/actor""}}"; + + public static string GetActorWebFinger() => + $@"{{""subject"": ""acct:relay@{Config.Instance.Host}"", ""aliases"": [""https://{Config.Instance.Host}/actor""], ""links"": [{{""href"": ""https://{Config.Instance.Host}/actor"", ""rel"": ""self"", ""type"": ""application/activity+json""}}, {{""href"": ""https://{Config.Instance.Host}/actor"", ""rel"": ""self"", ""type"": ""application/ld+json; profile=\""https://www.w3.org/ns/activitystreams\""""}}]}}"; +} \ No newline at end of file diff --git a/src/FakeRelay.Web/Controllers/AcitivityPubController.cs b/src/FakeRelay.Web/Controllers/AcitivityPubController.cs new file mode 100644 index 0000000..a569193 --- /dev/null +++ b/src/FakeRelay.Web/Controllers/AcitivityPubController.cs @@ -0,0 +1,41 @@ +using FakeRelay.Core; +using FakeRelay.Core.Helpers; +using FakeRelay.Web.Services; +using Microsoft.AspNetCore.Mvc; + +namespace FakeRelay.Web.Controllers; + +public class AcitivityPubController : Controller +{ + + [Route("actor")] + public async Task Actor([FromServices] IBackgroundTaskQueue taskQueue) => + Content(MastodonHelper.GetActorStaticContent(), "application/activity+json; charset=utf-8"); + + [Route("inbox"), HttpPost] + public async Task Inbox([FromBody] ActivityPubModel model, [FromServices] IBackgroundTaskQueue taskQueue) + { + if (model.Type == "Follow") + { + await taskQueue.QueueBackgroundWorkItemAsync(_ => MastodonHelper.ProcessInstanceFollowAsync(model)); + } + + return Content("{}", "application/activity+json"); + } + + [Route(".well-known/webfinger")] + public ActionResult WebFinger(string resource) + { + if (resource == "acct:relay@" + Config.Instance.Host) + { + return Content(MastodonHelper.GetActorWebFinger(), "application/json; charset=utf-8"); + } + + return new ContentResult + { + Content = @"{""error"": ""user not found""}", + ContentType = "application/json; charset=utf-8", + StatusCode = 404 + }; + } +} \ No newline at end of file diff --git a/src/FakeRelay.Web/Controllers/ApiController.cs b/src/FakeRelay.Web/Controllers/ApiController.cs new file mode 100644 index 0000000..f4631ae --- /dev/null +++ b/src/FakeRelay.Web/Controllers/ApiController.cs @@ -0,0 +1,51 @@ +using System.Collections.Immutable; +using FakeRelay.Core.Helpers; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; + +namespace FakeRelay.Web.Controllers; + +public class ApiController : Controller +{ + private readonly IMemoryCache _memoryCache; + + public ApiController(IMemoryCache memoryCache) + { + _memoryCache = memoryCache; + } + + private async Task GetHostFromRequest() + { + if (Request.Headers.Authorization.Count != 1) + { + return null; + } + + if (!_memoryCache.TryGetValue("tokenToHost", out ImmutableDictionary tokenToHost)) + { + tokenToHost = await ApiKeysHelper.GetTokenToHostAsync(); + + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetAbsoluteExpiration(TimeSpan.FromMinutes(2)); + + _memoryCache.Set("tokenToHost", tokenToHost, cacheEntryOptions); + } + + var token = Request.Headers.Authorization[0].Replace("Bearer ", ""); + tokenToHost.TryGetValue(token, out var host); + return host; + } + + [Route("index")] + public async Task DoIndex(string statusUrl) + { + var host = await GetHostFromRequest(); + if (host == null) + { + return Unauthorized(); + } + + var response = await MastodonHelper.EnqueueStatusToFetchAsync(host, statusUrl); + return Content(response, "application/activity+json"); + } +} \ No newline at end of file diff --git a/src/FakeRelay.Web/FakeRelay.Web.csproj b/src/FakeRelay.Web/FakeRelay.Web.csproj new file mode 100644 index 0000000..e33ab3b --- /dev/null +++ b/src/FakeRelay.Web/FakeRelay.Web.csproj @@ -0,0 +1,83 @@ + + + + + + + + <_ContentIncludedByDefault Remove="Views\Home\Index.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Home\Privacy.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Shared\Error.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Shared\_Layout.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Shared\_ValidationScriptsPartial.cshtml" /> + <_ContentIncludedByDefault Remove="Views\_ViewImports.cshtml" /> + <_ContentIncludedByDefault Remove="Views\_ViewStart.cshtml" /> + <_ContentIncludedByDefault Remove="wwwroot\css\site.css" /> + <_ContentIncludedByDefault Remove="wwwroot\favicon.ico" /> + <_ContentIncludedByDefault Remove="wwwroot\js\site.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.rtl.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.rtl.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.rtl.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.rtl.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.rtl.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.rtl.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.rtl.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-reboot.rtl.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.rtl.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.rtl.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.rtl.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-utilities.rtl.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.rtl.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.rtl.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.rtl.min.css" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap.rtl.min.css.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.min.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.esm.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.esm.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.esm.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.esm.min.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\js\bootstrap.min.js.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\LICENSE" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation-unobtrusive\jquery.validate.unobtrusive.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation-unobtrusive\jquery.validate.unobtrusive.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation-unobtrusive\LICENSE.txt" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation\dist\additional-methods.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation\dist\additional-methods.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation\dist\jquery.validate.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation\dist\jquery.validate.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery-validation\LICENSE.md" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery\dist\jquery.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery\dist\jquery.min.js" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery\dist\jquery.min.map" /> + <_ContentIncludedByDefault Remove="wwwroot\lib\jquery\LICENSE.txt" /> + + + + net6.0 + enable + enable + + + diff --git a/src/FakeRelay.Web/Program.cs b/src/FakeRelay.Web/Program.cs new file mode 100644 index 0000000..332f894 --- /dev/null +++ b/src/FakeRelay.Web/Program.cs @@ -0,0 +1,46 @@ +using FakeRelay.Core; +using FakeRelay.Web.Services; +using Microsoft.AspNetCore.HttpOverrides; + +var builder = WebApplication.CreateBuilder(args); +Config.Init(builder.Configuration.GetValue("CONFIG_PATH")); + +// Add services to the container. +builder.Services.AddControllersWithViews(); + +builder.Services.Configure(options => +{ + options.ForwardedHeaders = + ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; + + // I don't want to know what the nginx ip is. Also: the container that runs this doesn't expose a port to the world + options.KnownNetworks.Clear(); + options.KnownProxies.Clear(); + + options.ForwardLimit = 1; +}); + +builder.Services.AddHostedService(); +builder.Services.AddSingleton(_ => new BackgroundTaskQueue(30)); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/Home/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); +} + +app.UseHttpsRedirection(); + +app.UseRouting(); + +app.UseAuthorization(); + +app.MapControllerRoute( + "default", + "{controller=Home}/{action=Index}/{id?}"); + +app.Run(); diff --git a/src/FakeRelay.Web/Services/QueuedHostedService.cs b/src/FakeRelay.Web/Services/QueuedHostedService.cs new file mode 100644 index 0000000..0ccbe1a --- /dev/null +++ b/src/FakeRelay.Web/Services/QueuedHostedService.cs @@ -0,0 +1,97 @@ +using System.Threading.Channels; + +namespace FakeRelay.Web.Services; + +public interface IBackgroundTaskQueue +{ + ValueTask QueueBackgroundWorkItemAsync(Func workItem); + + ValueTask> DequeueAsync( + CancellationToken cancellationToken); +} + +public class BackgroundTaskQueue : IBackgroundTaskQueue +{ + private readonly Channel> _queue; + + public BackgroundTaskQueue(int capacity) + { + // Capacity should be set based on the expected application load and + // number of concurrent threads accessing the queue. + // BoundedChannelFullMode.Wait will cause calls to WriteAsync() to return a task, + // which completes only when space became available. This leads to backpressure, + // in case too many publishers/calls start accumulating. + var options = new BoundedChannelOptions(capacity) + { + FullMode = BoundedChannelFullMode.Wait + }; + _queue = Channel.CreateBounded>(options); + } + + public async ValueTask QueueBackgroundWorkItemAsync( + Func workItem) + { + if (workItem == null) + { + throw new ArgumentNullException(nameof(workItem)); + } + + await _queue.Writer.WriteAsync(workItem); + } + + public async ValueTask> DequeueAsync( + CancellationToken cancellationToken) + { + var workItem = await _queue.Reader.ReadAsync(cancellationToken); + + return workItem; + } +} + +public class QueuedHostedService : BackgroundService +{ + private readonly ILogger _logger; + + public QueuedHostedService(IBackgroundTaskQueue taskQueue, + ILogger logger) + { + TaskQueue = taskQueue; + _logger = logger; + } + + public IBackgroundTaskQueue TaskQueue { get; } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation( + $"Queued Hosted Service is running."); + + await BackgroundProcessing(stoppingToken); + } + + private async Task BackgroundProcessing(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + var workItem = + await TaskQueue.DequeueAsync(stoppingToken); + + try + { + await workItem(stoppingToken); + } + catch (Exception ex) + { + _logger.LogError(ex, + "Error occurred executing {WorkItem}.", nameof(workItem)); + } + } + } + + public override async Task StopAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("Queued Hosted Service is stopping."); + + await base.StopAsync(stoppingToken); + } +} \ No newline at end of file diff --git a/src/FakeRelay.Web/appsettings.Development.json b/src/FakeRelay.Web/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/src/FakeRelay.Web/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/FakeRelay.Web/appsettings.json b/src/FakeRelay.Web/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/src/FakeRelay.Web/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/FakeRelay.sln b/src/FakeRelay.sln new file mode 100644 index 0000000..eafc50c --- /dev/null +++ b/src/FakeRelay.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeRelay.Web", "FakeRelay.Web\FakeRelay.Web.csproj", "{9AAB6AC5-AB58-45CF-99D0-B657E694368E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeRelay.Core", "FakeRelay.Core\FakeRelay.Core.csproj", "{EA9021D7-9E0A-4988-AC42-52204D135DCA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeRelay.Cli", "FakeRelay.Cli\FakeRelay.Cli.csproj", "{0615568F-5DBD-40E1-9925-98C84DB1EEF3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AAB6AC5-AB58-45CF-99D0-B657E694368E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AAB6AC5-AB58-45CF-99D0-B657E694368E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AAB6AC5-AB58-45CF-99D0-B657E694368E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AAB6AC5-AB58-45CF-99D0-B657E694368E}.Release|Any CPU.Build.0 = Release|Any CPU + {EA9021D7-9E0A-4988-AC42-52204D135DCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA9021D7-9E0A-4988-AC42-52204D135DCA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA9021D7-9E0A-4988-AC42-52204D135DCA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA9021D7-9E0A-4988-AC42-52204D135DCA}.Release|Any CPU.Build.0 = Release|Any CPU + {0615568F-5DBD-40E1-9925-98C84DB1EEF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0615568F-5DBD-40E1-9925-98C84DB1EEF3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0615568F-5DBD-40E1-9925-98C84DB1EEF3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0615568F-5DBD-40E1-9925-98C84DB1EEF3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/src/version.json b/src/version.json new file mode 100644 index 0000000..798813c --- /dev/null +++ b/src/version.json @@ -0,0 +1,13 @@ +{ + "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", + "version": "1.1", + "publicReleaseRefSpec": [ + "^refs/heads/main$", + "^refs/heads/v\\d+(?:\\.\\d+)?$" + ], + "cloudBuild": { + "buildNumber": { + "enabled": true + } + } + } \ No newline at end of file