Trying to fix boardcast to upstream

This commit is contained in:
Creeper Lv
2026-06-01 18:58:36 +10:00
parent 9decde570f
commit 2920a67a61
3 changed files with 46 additions and 26 deletions
+10 -6
View File
@@ -18,7 +18,7 @@ public static class AuthEndpoints
{ {
var group = routes.MapGroup("/api/auth"); var group = routes.MapGroup("/api/auth");
group.MapPost("/register", async (RegisterRequest req, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient) => group.MapPost("/register", async (RegisterRequest req, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
if (string.IsNullOrWhiteSpace(req.Username) || string.IsNullOrWhiteSpace(req.Password) || string.IsNullOrWhiteSpace(req.EncryptedKey)) if (string.IsNullOrWhiteSpace(req.Username) || string.IsNullOrWhiteSpace(req.Password) || string.IsNullOrWhiteSpace(req.EncryptedKey))
{ {
@@ -45,9 +45,11 @@ public static class AuthEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, rsaKeyManager, configuration, httpClient); var scopeRsa = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var scopeConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, scopeRsa, scopeConfig, httpClient);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -58,7 +60,7 @@ public static class AuthEndpoints
return Results.Ok(new { message = "Registration successful." }); return Results.Ok(new { message = "Registration successful." });
}); });
group.MapPost("/change-password", async (ChangePasswordRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient) => group.MapPost("/change-password", async (ChangePasswordRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -88,9 +90,11 @@ public static class AuthEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, rsaKeyManager, configuration, httpClient); var scopeRsa = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var scopeConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, scopeRsa, scopeConfig, httpClient);
} }
catch (Exception ex) catch (Exception ex)
{ {
+12 -12
View File
@@ -131,7 +131,7 @@ public static class NodeEndpoints
}); });
// POST create new node (folder/note) // POST create new node (folder/note)
group.MapPost("/", async (CreateNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapPost("/", async (CreateNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -188,7 +188,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
@@ -206,7 +206,7 @@ public static class NodeEndpoints
}); });
// PUT update node (rename/edit content) // PUT update node (rename/edit content)
group.MapPut("/{uuid}", async (string uuid, UpdateNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapPut("/{uuid}", async (string uuid, UpdateNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -266,7 +266,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
@@ -282,7 +282,7 @@ public static class NodeEndpoints
}); });
// DELETE a node (soft delete) // DELETE a node (soft delete)
group.MapDelete("/{uuid}", async (string uuid, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapDelete("/{uuid}", async (string uuid, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -306,7 +306,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
@@ -338,7 +338,7 @@ public static class NodeEndpoints
}); });
// PUT update ACL entries // PUT update ACL entries
group.MapPut("/{uuid}/acl", async (string uuid, List<AclEntryRequest> aclReqs, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapPut("/{uuid}/acl", async (string uuid, List<AclEntryRequest> aclReqs, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -435,7 +435,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
@@ -451,7 +451,7 @@ public static class NodeEndpoints
}); });
// POST Share a Node (generates k0 encrypted by k1 and saves in PendingShare) // POST Share a Node (generates k0 encrypted by k1 and saves in PendingShare)
group.MapPost("/share", async (ShareNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapPost("/share", async (ShareNodeRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -522,7 +522,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
@@ -538,7 +538,7 @@ public static class NodeEndpoints
}); });
// POST Accept a Share (decrypts E_k1(k0) using k1 and re-encrypts using recipient's masterKey) // POST Accept a Share (decrypts E_k1(k0) using k1 and re-encrypts using recipient's masterKey)
group.MapPost("/accept-share", async (AcceptShareRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient) => group.MapPost("/accept-share", async (AcceptShareRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
var username = AuthHelper.GetAuthenticatedUser(context, certManager); var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized(); if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
@@ -609,7 +609,7 @@ public static class NodeEndpoints
{ {
try try
{ {
using var scope = context.RequestServices.CreateScope(); using var scope = serviceProvider.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>(); var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>(); var rsaKeyManager = scope.ServiceProvider.GetRequiredService<RsaKeyManager>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
+24 -8
View File
@@ -211,7 +211,7 @@ public static class SyncEndpoints
}); });
// 5. Receive broadcast of changes (broadcasts single node updates) // 5. Receive broadcast of changes (broadcasts single node updates)
routes.MapPost("/api/sync/broadcast", async (SyncPushItem item, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient) => routes.MapPost("/api/sync/broadcast", async (SyncPushItem item, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
if (!AuthHelper.IsServerTokenValid(context, peerCache)) if (!AuthHelper.IsServerTokenValid(context, peerCache))
{ {
@@ -273,13 +273,21 @@ public static class SyncEndpoints
{ {
await db.SaveChangesAsync(); await db.SaveChangesAsync();
// Relay broadcast to all peers (upstream + downstream) with loop prevention!
if (!string.IsNullOrEmpty(broadcastId)) if (!string.IsNullOrEmpty(broadcastId))
{ {
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
var configuration = context.RequestServices.GetRequiredService<IConfiguration>(); try
await BroadcastNodeChangeAsync(item.Node.Uuid, db, peerCache, rsaKeyManager, configuration, httpClient, broadcastId); {
using var scope = serviceProvider.CreateScope();
var backgroundDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var backgroundConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await BroadcastNodeChangeAsync(item.Node.Uuid, backgroundDb, peerCache, rsaKeyManager, backgroundConfig, httpClient, broadcastId);
}
catch (Exception ex)
{
Console.WriteLine($"Error during background BroadcastNodeChangeAsync: {ex.Message}");
}
}); });
} }
} }
@@ -288,7 +296,7 @@ public static class SyncEndpoints
}); });
// Receive broadcast of user registration or password updates // Receive broadcast of user registration or password updates
routes.MapPost("/api/sync/broadcast-user", async (UserBroadcastPayload payload, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient) => routes.MapPost("/api/sync/broadcast-user", async (UserBroadcastPayload payload, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient, IServiceProvider serviceProvider) =>
{ {
if (!AuthHelper.IsServerTokenValid(context, peerCache)) if (!AuthHelper.IsServerTokenValid(context, peerCache))
{ {
@@ -326,13 +334,21 @@ public static class SyncEndpoints
{ {
await db.SaveChangesAsync(); await db.SaveChangesAsync();
// Relay the user broadcast to other peers
if (!string.IsNullOrEmpty(broadcastId)) if (!string.IsNullOrEmpty(broadcastId))
{ {
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
var configuration = context.RequestServices.GetRequiredService<IConfiguration>(); try
await BroadcastUserChangeAsync(payload.User.Username, db, peerCache, rsaKeyManager, configuration, httpClient, broadcastId); {
using var scope = serviceProvider.CreateScope();
var backgroundDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var backgroundConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await BroadcastUserChangeAsync(payload.User.Username, backgroundDb, peerCache, rsaKeyManager, backgroundConfig, httpClient, broadcastId);
}
catch (Exception ex)
{
Console.WriteLine($"Error during background BroadcastUserChangeAsync: {ex.Message}");
}
}); });
} }
} }