Compare commits

..

2 Commits

Author SHA1 Message Date
defnotken
86fd24f756 clean up logging 2025-11-20 09:33:18 -06:00
defnotken
5ed802e37c move western canadians to eastern file shard 2025-11-20 09:15:15 -06:00
6 changed files with 24 additions and 35 deletions

View File

@@ -44,12 +44,14 @@ public class GeoIPService : IHostedService
if (_dbReader!.TryCity(ip, out var response))
{
string? continent = response?.Continent.Code;
string? countryIso = response?.Country.IsoCode;
if (!string.IsNullOrEmpty(continent) &&
string.Equals(continent, "NA", StringComparison.Ordinal)
&& response?.Location.Longitude != null)
{
if (response.Location.Longitude < -102)
if (response.Location.Longitude < -102 &&
!string.Equals(countryIso, "CA", StringComparison.OrdinalIgnoreCase))
{
continent = "NA-W";
}
@@ -57,6 +59,8 @@ public class GeoIPService : IHostedService
{
continent = "NA-E";
}
_logger.LogDebug("Connecting {countryIso} to {continent}", countryIso, continent);
}
return continent ?? "*";

View File

@@ -995,11 +995,11 @@ public partial class LightlessHub
return false;
}
var (isOwner, _) = await TryValidateGroupModeratorOrOwner(dto.GID).ConfigureAwait(false);
var (isOwner, _) = await TryValidateOwner(dto.GID).ConfigureAwait(false);
if (!isOwner)
{
_logger.LogCallWarning(LightlessHubLogger.Args("Unauthorized syncshell broadcast change", "User", UserUID, "GID", dto.GID));
await Clients.Caller.Client_ReceiveServerMessage(MessageSeverity.Error, "You must be the owner or moderator of the syncshell to broadcast it.");
await Clients.Caller.Client_ReceiveServerMessage(MessageSeverity.Error, "You must be the owner of the syncshell to broadcast it.");
return false;
}

View File

@@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using StackExchange.Redis;
using System.Text;
using System.Text.Json;

View File

@@ -21,7 +21,6 @@
<ItemGroup>
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="Blake3" Version="2.0.0" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -1,5 +1,4 @@
using Blake3;
using K4os.Compression.LZ4.Legacy;
using K4os.Compression.LZ4.Legacy;
using LightlessSync.API.Dto.Files;
using LightlessSync.API.Routes;
using LightlessSync.API.SignalR;
@@ -209,14 +208,11 @@ public class ServerFilesController : ControllerBase
[RequestSizeLimit(200 * 1024 * 1024)]
public async Task<IActionResult> UploadFile(string hash, CancellationToken requestAborted)
{
await using var dbContext = await _lightlessDbContext.CreateDbContextAsync();
using var dbContext = await _lightlessDbContext.CreateDbContextAsync();
_logger.LogInformation("{user}|{file}: Uploading", LightlessUser, hash);
if (hash.Length == 40)
{
hash = hash.ToUpperInvariant();
}
hash = hash.ToUpperInvariant();
var existingFile = await dbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
if (existingFile != null) return Ok();
@@ -267,14 +263,10 @@ public class ServerFilesController : ControllerBase
[RequestSizeLimit(200 * 1024 * 1024)]
public async Task<IActionResult> UploadFileMunged(string hash, CancellationToken requestAborted)
{
await using var dbContext = await _lightlessDbContext.CreateDbContextAsync();
using var dbContext = await _lightlessDbContext.CreateDbContextAsync();
_logger.LogInformation("{user}|{file}: Uploading munged", LightlessUser, hash);
if (hash.Length == 40)
{
hash = hash.ToUpperInvariant();
}
hash = hash.ToUpperInvariant();
var existingFile = await dbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
if (existingFile != null) return Ok();
@@ -327,26 +319,20 @@ public class ServerFilesController : ControllerBase
private async Task StoreData(string hash, LightlessDbContext dbContext, MemoryStream compressedFileStream)
{
var decompressedData = LZ4Wrapper.Unwrap(compressedFileStream.ToArray());
// reset streams
compressedFileStream.Seek(0, SeekOrigin.Begin);
bool valid;
// compute hash to verify
var hashString = BitConverter.ToString(SHA1.HashData(decompressedData))
.Replace("-", "", StringComparison.Ordinal).ToUpperInvariant();
if (!string.Equals(hashString, hash, StringComparison.Ordinal))
throw new InvalidOperationException($"{LightlessUser}|{hash}: Hash does not match file, computed: {hashString}, expected: {hash}");
if (hash.Length == 40)
{
var sha1Hex = Convert.ToHexString(SHA1.HashData(decompressedData));
valid = string.Equals(sha1Hex, hash, StringComparison.OrdinalIgnoreCase);
}
else
{
var blakeHash = Hasher.Hash(decompressedData);
var blakeHex = Convert.ToHexString(blakeHash.AsSpan());
valid = string.Equals(blakeHex, hash, StringComparison.OrdinalIgnoreCase);
}
if (!valid)
throw new InvalidOperationException(
$"{LightlessUser}|{hash}: Hash does not match file, computed mismatch."
);
// save file
var path = FilePathUtil.GetFilePath(_basePath, hash);
using var fileStream = new FileStream(path, FileMode.Create);
await compressedFileStream.CopyToAsync(fileStream).ConfigureAwait(false);
_logger.LogDebug("{user}|{file}: Uploaded file saved to {path}", LightlessUser, hash, path);
// update on db
await dbContext.Files.AddAsync(new FileCache()

View File

@@ -18,7 +18,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Blake3" Version="2.0.0" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>