mirror of
https://github.com/g3rv4/FakeRelay.git
synced 2024-11-25 00:33:09 +01:00
Initial commit
This commit is contained in:
commit
a5346d1c61
132
.editorconfig
Normal file
132
.editorconfig
Normal file
@ -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
|
12
.gitignore
vendored
Normal file
12
.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
.vscode/
|
||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
.vs/
|
||||||
|
*.csproj.user
|
||||||
|
launchSettings.json
|
||||||
|
.idea/
|
||||||
|
config.json
|
||||||
|
config-tokens.json
|
||||||
|
.DS_Store
|
||||||
|
DataDir*
|
||||||
|
.build/
|
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@ -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"]
|
9
src/Directory.Build.props
Normal file
9
src/Directory.Build.props
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')">
|
||||||
|
<Version>3.4.255</Version>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
16
src/FakeRelay.Cli/Commands/AddHostCommand.cs
Normal file
16
src/FakeRelay.Cli/Commands/AddHostCommand.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using FakeRelay.Core.Helpers;
|
||||||
|
using Spectre.Console;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
namespace FakeRelay.Cli.Commands;
|
||||||
|
|
||||||
|
public class AddHostCommand : AsyncCommand<HostSettings>
|
||||||
|
{
|
||||||
|
public override async Task<int> 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;
|
||||||
|
}
|
||||||
|
}
|
15
src/FakeRelay.Cli/Commands/DeleteHostCommand.cs
Normal file
15
src/FakeRelay.Cli/Commands/DeleteHostCommand.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using FakeRelay.Core.Helpers;
|
||||||
|
using Spectre.Console;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
namespace FakeRelay.Cli.Commands;
|
||||||
|
|
||||||
|
public class DeleteHostCommand: AsyncCommand<HostSettings>
|
||||||
|
{
|
||||||
|
public override async Task<int> ExecuteAsync(CommandContext context, HostSettings settings)
|
||||||
|
{
|
||||||
|
await ApiKeysHelper.DeleteTokenForHostAsync(settings.Host);
|
||||||
|
AnsiConsole.Markup($"[green]Key deleted for {settings.Host}[/]\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
16
src/FakeRelay.Cli/Commands/UpdateHostCommand.cs
Normal file
16
src/FakeRelay.Cli/Commands/UpdateHostCommand.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using FakeRelay.Core.Helpers;
|
||||||
|
using Spectre.Console;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
namespace FakeRelay.Cli.Commands;
|
||||||
|
|
||||||
|
public class UpdateHostCommand : AsyncCommand<HostSettings>
|
||||||
|
{
|
||||||
|
public override async Task<int> 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;
|
||||||
|
}
|
||||||
|
}
|
18
src/FakeRelay.Cli/FakeRelay.Cli.csproj
Normal file
18
src/FakeRelay.Cli/FakeRelay.Cli.csproj
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FakeRelay.Core\FakeRelay.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
9
src/FakeRelay.Cli/HostSettings.cs
Normal file
9
src/FakeRelay.Cli/HostSettings.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
namespace FakeRelay.Cli;
|
||||||
|
|
||||||
|
public class HostSettings : CommandSettings
|
||||||
|
{
|
||||||
|
[CommandArgument(0, "<HOST>")]
|
||||||
|
public string Host { get; set; }
|
||||||
|
}
|
15
src/FakeRelay.Cli/Program.cs
Normal file
15
src/FakeRelay.Cli/Program.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using FakeRelay.Cli.Commands;
|
||||||
|
using FakeRelay.Core;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
var app = new CommandApp();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.AddCommand<AddHostCommand>("add-host");
|
||||||
|
config.AddCommand<UpdateHostCommand>("update-host");
|
||||||
|
config.AddCommand<DeleteHostCommand>("delete-host");
|
||||||
|
});
|
||||||
|
|
||||||
|
Config.Init(Environment.GetEnvironmentVariable("CONFIG_PATH"));
|
||||||
|
|
||||||
|
return app.Run(args);
|
10
src/FakeRelay.Core/ActivityPubModel.cs
Normal file
10
src/FakeRelay.Core/ActivityPubModel.cs
Normal file
@ -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; }
|
||||||
|
}
|
46
src/FakeRelay.Core/Config.cs
Normal file
46
src/FakeRelay.Core/Config.cs
Normal file
@ -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<ConfigData>(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; }
|
||||||
|
}
|
||||||
|
}
|
13
src/FakeRelay.Core/FakeRelay.Core.csproj
Normal file
13
src/FakeRelay.Core/FakeRelay.Core.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Jil" Version="2.17.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
77
src/FakeRelay.Core/Helpers/ApiKeysHelper.cs
Normal file
77
src/FakeRelay.Core/Helpers/ApiKeysHelper.cs
Normal file
@ -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<ImmutableDictionary<string, string>> GetTokenToHostAsync()
|
||||||
|
{
|
||||||
|
if (!File.Exists(TokensFilePath))
|
||||||
|
{
|
||||||
|
return ImmutableDictionary<string, string>.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var content = await File.ReadAllTextAsync(TokensFilePath);
|
||||||
|
return JSON.Deserialize<Dictionary<string, string>>(content)
|
||||||
|
.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string> 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<string> 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<string> AddTokenForHostAsync(string host, ImmutableDictionary<string, string> 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;
|
||||||
|
}
|
||||||
|
}
|
24
src/FakeRelay.Core/Helpers/CryptographyHelper.cs
Normal file
24
src/FakeRelay.Core/Helpers/CryptographyHelper.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
52
src/FakeRelay.Core/Helpers/MastodonHelper.cs
Normal file
52
src/FakeRelay.Core/Helpers/MastodonHelper.cs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
|
namespace FakeRelay.Core.Helpers;
|
||||||
|
|
||||||
|
public static class MastodonHelper
|
||||||
|
{
|
||||||
|
public static Task<string> 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<string> 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\""""}}]}}";
|
||||||
|
}
|
41
src/FakeRelay.Web/Controllers/AcitivityPubController.cs
Normal file
41
src/FakeRelay.Web/Controllers/AcitivityPubController.cs
Normal file
@ -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<ActionResult> Actor([FromServices] IBackgroundTaskQueue taskQueue) =>
|
||||||
|
Content(MastodonHelper.GetActorStaticContent(), "application/activity+json; charset=utf-8");
|
||||||
|
|
||||||
|
[Route("inbox"), HttpPost]
|
||||||
|
public async Task<ActionResult> 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
51
src/FakeRelay.Web/Controllers/ApiController.cs
Normal file
51
src/FakeRelay.Web/Controllers/ApiController.cs
Normal file
@ -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<string?> GetHostFromRequest()
|
||||||
|
{
|
||||||
|
if (Request.Headers.Authorization.Count != 1)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_memoryCache.TryGetValue("tokenToHost", out ImmutableDictionary<string, string> 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<ActionResult> 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");
|
||||||
|
}
|
||||||
|
}
|
83
src/FakeRelay.Web/FakeRelay.Web.csproj
Normal file
83
src/FakeRelay.Web/FakeRelay.Web.csproj
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FakeRelay.Core\FakeRelay.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<_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" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
46
src/FakeRelay.Web/Program.cs
Normal file
46
src/FakeRelay.Web/Program.cs
Normal file
@ -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<string>("CONFIG_PATH"));
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
builder.Services.AddControllersWithViews();
|
||||||
|
|
||||||
|
builder.Services.Configure<ForwardedHeadersOptions>(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<QueuedHostedService>();
|
||||||
|
builder.Services.AddSingleton<IBackgroundTaskQueue>(_ => 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();
|
97
src/FakeRelay.Web/Services/QueuedHostedService.cs
Normal file
97
src/FakeRelay.Web/Services/QueuedHostedService.cs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
using System.Threading.Channels;
|
||||||
|
|
||||||
|
namespace FakeRelay.Web.Services;
|
||||||
|
|
||||||
|
public interface IBackgroundTaskQueue
|
||||||
|
{
|
||||||
|
ValueTask QueueBackgroundWorkItemAsync(Func<CancellationToken, Task> workItem);
|
||||||
|
|
||||||
|
ValueTask<Func<CancellationToken, Task>> DequeueAsync(
|
||||||
|
CancellationToken cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BackgroundTaskQueue : IBackgroundTaskQueue
|
||||||
|
{
|
||||||
|
private readonly Channel<Func<CancellationToken, Task>> _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<Func<CancellationToken, Task>>(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask QueueBackgroundWorkItemAsync(
|
||||||
|
Func<CancellationToken, Task> workItem)
|
||||||
|
{
|
||||||
|
if (workItem == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(workItem));
|
||||||
|
}
|
||||||
|
|
||||||
|
await _queue.Writer.WriteAsync(workItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<Func<CancellationToken, Task>> DequeueAsync(
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var workItem = await _queue.Reader.ReadAsync(cancellationToken);
|
||||||
|
|
||||||
|
return workItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class QueuedHostedService : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly ILogger<QueuedHostedService> _logger;
|
||||||
|
|
||||||
|
public QueuedHostedService(IBackgroundTaskQueue taskQueue,
|
||||||
|
ILogger<QueuedHostedService> 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);
|
||||||
|
}
|
||||||
|
}
|
8
src/FakeRelay.Web/appsettings.Development.json
Normal file
8
src/FakeRelay.Web/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
src/FakeRelay.Web/appsettings.json
Normal file
9
src/FakeRelay.Web/appsettings.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
34
src/FakeRelay.sln
Normal file
34
src/FakeRelay.sln
Normal file
@ -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
|
13
src/version.json
Normal file
13
src/version.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user