Add missing files
This commit is contained in:
80
NostrRelay/Extensions.cs
Normal file
80
NostrRelay/Extensions.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
using System.Net.WebSockets;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Nostr.Client.Json;
|
||||||
|
|
||||||
|
namespace NostrRelay;
|
||||||
|
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static void MapRelay<TRelay>(this WebApplication app, string path,
|
||||||
|
RelayDocument? document = null,
|
||||||
|
string? landingPage = null)
|
||||||
|
where TRelay : INostrRelay, IDisposable
|
||||||
|
{
|
||||||
|
app.MapGet(path, async ctx =>
|
||||||
|
{
|
||||||
|
if (ctx.WebSockets.IsWebSocketRequest)
|
||||||
|
{
|
||||||
|
var logger = app.Services.GetRequiredService<ILogger<NostrRelay<TRelay>>>();
|
||||||
|
using var handler = app.Services.GetRequiredService<TRelay>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
|
||||||
|
var wsCtx = new NostrClientContext(ws, ctx.Connection.RemoteIpAddress!,
|
||||||
|
ctx.Request.Headers.UserAgent.FirstOrDefault() ?? string.Empty);
|
||||||
|
|
||||||
|
var nostrRelay = new NostrRelay<TRelay>(handler, wsCtx, ctx.RequestAborted, logger);
|
||||||
|
|
||||||
|
await nostrRelay.Read();
|
||||||
|
|
||||||
|
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, ctx.RequestAborted);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogDebug(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var isRelayDocRequest = ctx.Request.Headers.Accept.FirstOrDefault()
|
||||||
|
?.Equals("application/nostr+json", StringComparison.InvariantCultureIgnoreCase) ?? false;
|
||||||
|
|
||||||
|
if (isRelayDocRequest)
|
||||||
|
{
|
||||||
|
var doc = document ?? new RelayDocument
|
||||||
|
{
|
||||||
|
Name = typeof(TRelay).Name,
|
||||||
|
Description = "NostrServices C# Relay Framework",
|
||||||
|
Pubkey = "63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed",
|
||||||
|
SupportedNips = [1],
|
||||||
|
Software = "NostrServices",
|
||||||
|
Icon = "https://void.cat/d/CtXsF5EvqNLG3K85TgezCt.webp",
|
||||||
|
Limitation = new()
|
||||||
|
{
|
||||||
|
RestrictedWrites = false,
|
||||||
|
AuthRequired = false,
|
||||||
|
PaymentRequired = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.Response.ContentType = "application/json";
|
||||||
|
await ctx.Response.WriteAsync(JsonConvert.SerializeObject(doc, NostrSerializer.Settings));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ctx.Response.ContentType = "text/html";
|
||||||
|
await ctx.Response.WriteAsync(landingPage ?? @$"
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>NostrServices Relay</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome to {typeof(TRelay).Name}</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -5,6 +5,7 @@ using Newtonsoft.Json;
|
|||||||
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Converters;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using Nostr.Client.Client;
|
using Nostr.Client.Client;
|
||||||
|
using NostrRelay;
|
||||||
using NostrServices.Database;
|
using NostrServices.Database;
|
||||||
using NostrServices.Services;
|
using NostrServices.Services;
|
||||||
using NostrServices.Services.EventHandlers;
|
using NostrServices.Services.EventHandlers;
|
||||||
@ -106,7 +107,7 @@ public static class Program
|
|||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapMetrics();
|
app.MapMetrics();
|
||||||
app.MapNostrRelay<CacheRelay>("/");
|
app.MapRelay<CacheRelay>("/");
|
||||||
|
|
||||||
await app.RunAsync();
|
await app.RunAsync();
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ using NostrRelay;
|
|||||||
|
|
||||||
namespace NostrServices.Services;
|
namespace NostrServices.Services;
|
||||||
|
|
||||||
public class CacheRelay : INostrRelay
|
public class CacheRelay : INostrRelay, IDisposable
|
||||||
{
|
{
|
||||||
private readonly ILogger<CacheRelay> _logger;
|
private readonly ILogger<CacheRelay> _logger;
|
||||||
private readonly RedisStore _redisStore;
|
private readonly RedisStore _redisStore;
|
||||||
@ -37,4 +37,9 @@ public class CacheRelay : INostrRelay
|
|||||||
|
|
||||||
return new(false, "blocked: kind not accepted");
|
return new(false, "blocked: kind not accepted");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// TODO release managed resources here
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user