mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: paste views; configuration validation; hmacsha256 method; enhance paste clean up serivce; paste view clean up service;
This commit is contained in:
@@ -29,14 +29,9 @@ namespace SharpbinV3.Server.Services
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var expiredPastes = db.Pastes.Where(p => p.ExpiresAt != 0 && p.ExpiresAt <= now);
|
||||
if (await expiredPastes.AnyAsync(stoppingToken))
|
||||
{
|
||||
var deletedCount = await expiredPastes.CountAsync(stoppingToken);
|
||||
db.Pastes.RemoveRange(expiredPastes);
|
||||
await db.SaveChangesAsync(stoppingToken);
|
||||
var deletedCount = await expiredPastes.ExecuteDeleteAsync(stoppingToken);
|
||||
if (deletedCount > 0)
|
||||
logger.LogInformation("Deleted {Count} expired pastes", deletedCount);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.Extensions;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text;
|
||||
|
||||
@@ -79,6 +80,37 @@ namespace SharpbinV3.Server.Services
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
public async Task<(Paste? paste, bool alreadyExists)> RecordView(Paste paste, HttpContext context)
|
||||
{
|
||||
if (paste == null) return (null, false);
|
||||
if (context == null) return (null, false);
|
||||
if (_pasteSettings.View_HMAC_Secret == null) return (null, false);
|
||||
|
||||
var viewerIp = context.GetRequestIP();
|
||||
var viewerUserAgent = context.Request.Headers.UserAgent.ToString();
|
||||
JwtUser? viewerUser = context.GetJwtUser();
|
||||
|
||||
string viewerHash;
|
||||
if (viewerUser != null)
|
||||
viewerHash = Utilities.ComputeHmacSha256(_pasteSettings.View_HMAC_Secret, viewerUser.UUID.ToString());
|
||||
else
|
||||
viewerHash = Utilities.ComputeHmacSha256(_pasteSettings.View_HMAC_Secret, viewerIp + viewerUserAgent);
|
||||
|
||||
if (await _db.PasteViews.AnyAsync(pv => pv.PastePID == paste.PID && pv.ViewerHash == viewerHash))
|
||||
return (null, true);
|
||||
|
||||
_db.PasteViews.Add(new PasteView
|
||||
{
|
||||
PastePID = paste.PID,
|
||||
ViewerHash = viewerHash,
|
||||
ViewedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
});
|
||||
|
||||
paste.Views += 1;
|
||||
_db.Pastes.Update(paste);
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return (result > 0 ? paste : null, result > 0);
|
||||
}
|
||||
public async Task<Paste?> Get(string id)
|
||||
{
|
||||
var paste = await _db.Pastes.Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Server.Data;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class PasteViewCleanUpService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<PasteViewCleanUpService> logger
|
||||
) : BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan Period = TimeSpan.FromHours(1);
|
||||
private static readonly long RetentionMs =
|
||||
(long)TimeSpan.FromDays(7).TotalMilliseconds;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(Period);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested &&
|
||||
await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
await Cleanup(stoppingToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "PasteView cleanup failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Cleanup(CancellationToken token)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
var cutoff =
|
||||
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - RetentionMs;
|
||||
|
||||
var deleted = await db.PasteViews
|
||||
.Where(v => v.ViewedAt < cutoff)
|
||||
.ExecuteDeleteAsync(token);
|
||||
|
||||
if (deleted > 0)
|
||||
logger.LogInformation("Deleted {Count} stale paste views", deleted);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,18 +6,11 @@ using SharpbinV3.Server.Data;
|
||||
|
||||
namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
{
|
||||
public sealed class TotpVerificationProvider : IVerificationProvider
|
||||
public sealed class TotpVerificationProvider(AppDbContext db, IDataProtectionProvider dataProtectionProvider, IMemoryCache cache) : IVerificationProvider
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly IDataProtector _protector;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public TotpVerificationProvider(AppDbContext db, IDataProtectionProvider dataProtectionProvider, IMemoryCache cache)
|
||||
{
|
||||
_db = db;
|
||||
_protector = dataProtectionProvider.CreateProtector("TotpSecret-v1");
|
||||
_cache = cache;
|
||||
}
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly IDataProtector _protector = dataProtectionProvider.CreateProtector("TotpSecret-v1");
|
||||
private readonly IMemoryCache _cache = cache;
|
||||
|
||||
public int Priority => 0;
|
||||
public bool IsConfigured => false;
|
||||
@@ -62,12 +55,10 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
return VerifyCode(secret, code, out _, digits, period, window);
|
||||
}
|
||||
|
||||
private bool VerifyCode(byte[] secret, string code, out long matchedStep, int digits = 6, int period = 30, int window = 1)
|
||||
private static bool VerifyCode(byte[] secret, string code, out long matchedStep, int digits = 6, int period = 30, int window = 1)
|
||||
{
|
||||
var totp = new Totp(secret, step: period, mode: OtpHashMode.Sha1, totpSize: digits);
|
||||
return totp.VerifyTotp(code, out matchedStep, new VerificationWindow(previous: window, future: window));
|
||||
}
|
||||
|
||||
private string ComputeCode(byte[] key, long timestep, int digits) => new Totp(key, totpSize: digits).ComputeTotp(DateTime.UtcNow);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user