DB QOL stuff, but not breaking the migrations now (#5086)

This commit is contained in:
Saphire Lattice
2021-10-31 21:18:01 +07:00
committed by GitHub
parent 6cb6432cae
commit eb567dc0bb
8 changed files with 482 additions and 119 deletions

View File

@@ -313,6 +313,9 @@ namespace Content.Server.Database
Username = user,
Password = pass
}.ConnectionString;
Logger.DebugS("db.manager", $"Using Postgres \"{host}:{port}/{db}\"");
builder.UseNpgsql(connectionString);
SetupLogging(builder);
return builder.Options;
@@ -329,10 +332,12 @@ namespace Content.Server.Database
if (!inMemory)
{
var finalPreferencesDbPath = Path.Combine(_res.UserData.RootDir!, configPreferencesDbPath);
Logger.DebugS("db.manager", $"Using SQLite DB \"{finalPreferencesDbPath}\"");
connection = new SqliteConnection($"Data Source={finalPreferencesDbPath}");
}
else
{
Logger.DebugS("db.manager", $"Using in-memory SQLite DB");
connection = new SqliteConnection("Data Source=:memory:");
// When using an in-memory DB we have to open it manually
// so EFCore doesn't open, close and wipe it.

View File

@@ -104,7 +104,7 @@ namespace Content.Server.Database
NetUserId? userId,
ImmutableArray<byte>? hwId)
{
if (address != null && ban.Address != null && IPAddressExt.IsInSubnet(address, ban.Address))
if (address != null && ban.Address is not null && IPAddressExt.IsInSubnet(address, ban.Address.Value))
{
return true;
}
@@ -126,15 +126,9 @@ namespace Content.Server.Database
{
await using var db = await GetDbImpl();
string? addrStr = null;
if (serverBan.Address is { } addr)
{
addrStr = $"{addr.address}/{addr.cidrMask}";
}
db.SqliteDbContext.Ban.Add(new SqliteServerBan
{
Address = addrStr,
Address = serverBan.Address,
Reason = serverBan.Reason,
BanningAdmin = serverBan.BanningAdmin?.UserId,
HWId = serverBan.HWId?.ToArray(),
@@ -245,20 +239,12 @@ namespace Content.Server.Database
aUid = new NetUserId(aGuid);
}
(IPAddress, int)? addrTuple = null;
if (ban.Address != null)
{
var idx = ban.Address.IndexOf('/', StringComparison.Ordinal);
addrTuple = (IPAddress.Parse(ban.Address.AsSpan(0, idx)),
int.Parse(ban.Address.AsSpan(idx + 1), provider: CultureInfo.InvariantCulture));
}
var unban = ConvertUnban(ban.Unban);
return new ServerBanDef(
ban.Id,
uid,
addrTuple,
ban.Address,
ban.HWId == null ? null : ImmutableArray.Create(ban.HWId),
ban.BanTime,
ban.ExpirationTime,

View File

@@ -18,7 +18,7 @@ namespace Content.Server.IP
}
// First parse the address of the netmask before the prefix length.
var maskAddress = System.Net.IPAddress.Parse(subnetMask.Substring(0, slashIdx));
var maskAddress = System.Net.IPAddress.Parse(subnetMask[..slashIdx]);
if (maskAddress.AddressFamily != address.AddressFamily)
{
@@ -27,8 +27,18 @@ namespace Content.Server.IP
}
// Now find out how long the prefix is.
int maskLength = int.Parse(subnetMask.Substring(slashIdx + 1));
int maskLength = int.Parse(subnetMask[(slashIdx + 1)..]);
return address.IsInSubnet(maskAddress, maskLength);
}
public static bool IsInSubnet(this System.Net.IPAddress address, (System.Net.IPAddress maskAddress, int maskLength) tuple)
{
return address.IsInSubnet(tuple.maskAddress, tuple.maskLength);
}
public static bool IsInSubnet(this System.Net.IPAddress address, System.Net.IPAddress maskAddress, int maskLength)
{
if (maskAddress.AddressFamily == AddressFamily.InterNetwork)
{
// Convert the mask address to an unsigned integer.