1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

clean up log reader/make it output more useful message if things go wrong

add unflag as a penalty
show bans/tempbans even after they've expired on penalty list
continue making alias links great again
This commit is contained in:
RaidMax
2019-04-05 13:34:03 -05:00
parent 11dc020d7c
commit e3aa62334a
15 changed files with 226 additions and 152 deletions

View File

@ -930,10 +930,8 @@ namespace SharedLibraryCore.Commands
public override async Task ExecuteAsync(GameEvent E)
{
var B = await E.Owner.Manager.GetPenaltyService().GetClientPenaltiesAsync(E.Target.ClientId);
var penalty = B.FirstOrDefault(b => b.Type > Penalty.PenaltyType.Kick &&
(b.Expires == null || b.Expires > DateTime.UtcNow));
var existingPenalties = await E.Owner.Manager.GetPenaltyService().GetActivePenaltiesAsync(E.Target.AliasLinkId, E.Target.IPAddress);
var penalty = existingPenalties.FirstOrDefault(b => b.Type > Penalty.PenaltyType.Kick);
if (penalty == null)
{
@ -944,7 +942,7 @@ namespace SharedLibraryCore.Commands
string timeRemaining = penalty.Type == Penalty.PenaltyType.TempBan ? $"({(penalty.Expires.Value - DateTime.UtcNow).TimeSpanText()} remaining)" : "";
string success = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_BANINFO_SUCCESS"];
E.Origin.Tell($"^1{E.Target.Name} ^7{string.Format(success, penalty.Punisher.Name)} {penalty.Punisher.Name} {timeRemaining}");
E.Origin.Tell($"^1{E.Target.Name} ^7{string.Format(success, "")} {penalty.Offense} {timeRemaining}");
}
}

View File

@ -34,7 +34,6 @@ namespace SharedLibraryCore.Database
AliasId = 1,
Active = true,
DateAdded = DateTime.UtcNow,
IPAddress = 0,
Name = "IW4MAdmin",
LinkId = 1
};

View File

@ -25,7 +25,7 @@ namespace SharedLibraryCore.Dtos
public string TimeRemaining => DateTime.UtcNow > Expires ? "" : $"{((Expires ?? DateTime.MaxValue).Year == DateTime.MaxValue.Year ? Utilities.GetTimePassed(TimePunished, true) : Utilities.TimeSpanText((Expires ?? DateTime.MaxValue) - DateTime.UtcNow))}";
public bool Expired => Expires.HasValue && Expires <= DateTime.UtcNow;
public DateTime? Expires { get; set; }
public override bool Sensitive => PenaltyType == PenaltyType.Flag;
public override bool Sensitive => PenaltyType == PenaltyType.Flag || PenaltyType == PenaltyType.Unflag;
public bool IsEvade { get; set; }
public string AdditionalPenaltyInformation => $"{(!string.IsNullOrEmpty(AutomatedOffense) ? $" ({AutomatedOffense})" : "")}{(IsEvade ? $" ({Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PENALTY_EVADE"]})" : "")}";
}

View File

@ -571,26 +571,28 @@ namespace SharedLibraryCore.Database.Models
Kick($"{loc["SERVER_TB_REMAIN"]} ({(profileTempBan.Expires.Value - DateTime.UtcNow).TimeSpanText()} {loc["WEBFRONT_PENALTY_TEMPLATE_REMAINING"]})", autoKickClient);
return false;
}
}
#endregion
#region CLIENT_LINKED_BAN
// we want to get any penalties that are tied to their IP or AliasLink (but not necessarily their GUID)
var activePenalties = await CurrentServer.Manager.GetPenaltyService().GetActivePenaltiesAsync(AliasLinkId, ipAddress);
#region CLIENT_LINKED_TEMPBAN
var tempBan = activePenalties.FirstOrDefault(_penalty => _penalty.Type == Penalty.PenaltyType.TempBan);
// they have an active tempban tied to their AliasLink
if (tempBan != null)
{
CurrentServer.Logger.WriteDebug($"Kicking {this} because their AliasLink is temporarily banned");
Kick($"{loc["SERVER_TB_REMAIN"]} ({(tempBan.Expires.Value - DateTime.UtcNow).TimeSpanText()} {loc["WEBFRONT_PENALTY_TEMPLATE_REMAINING"]})", autoKickClient);
CurrentServer.Logger.WriteDebug($"Tempbanning {this} because their AliasLink is temporarily banned, but they are not");
TempBan(tempBan.Offense, DateTime.UtcNow - (tempBan.Expires ?? DateTime.UtcNow), autoKickClient);
return false;
}
#endregion
#region CLIENT_LINKED_BAN
var currentBan = activePenalties.FirstOrDefault(p => p.Type == Penalty.PenaltyType.Ban);
// they have a perm ban tied to their AliasLink
// they have a perm ban tied to their AliasLink/profile
if (currentBan != null)
{
CurrentServer.Logger.WriteInfo($"Banned client {this} trying to evade...");
@ -621,7 +623,20 @@ namespace SharedLibraryCore.Database.Models
}
#endregion
else
#region CLIENT_LINKED_FLAG
if (Level != Permission.Flagged)
{
var currentFlag = activePenalties.FirstOrDefault(_penalty => _penalty.Type == Penalty.PenaltyType.Flag);
if (currentFlag != null)
{
CurrentServer.Logger.WriteDebug($"Flagging {this} because their AliasLink is flagged, but they are not");
Flag(currentFlag.Offense, autoKickClient);
}
}
#endregion
if (Level == Permission.Flagged)
{
var currentAutoFlag = activePenalties
.Where(p => p.Type == Penalty.PenaltyType.Flag && p.PunisherId == 1)
@ -629,15 +644,15 @@ namespace SharedLibraryCore.Database.Models
.FirstOrDefault();
// remove their auto flag status after a week
if (Level == Permission.Flagged &&
currentAutoFlag != null &&
if (currentAutoFlag != null &&
(DateTime.UtcNow - currentAutoFlag.When).TotalDays > 7)
{
Level = Permission.User;
CurrentServer.Logger.WriteInfo($"Unflagging {this} because the auto flag time has expired");
Unflag(Utilities.CurrentLocalization.LocalizationIndex["SERVER_AUTOFLAG_UNFLAG"], autoKickClient);
}
return true;
}
return true;
}
[NotMapped]

View File

@ -15,11 +15,7 @@ namespace SharedLibraryCore.Objects
Ban,
Unban,
Any,
}
public String GetWhenFormatted()
{
return When.ToString("MM/dd/yy HH:mm:ss"); ;
Unflag
}
}
}

View File

@ -17,22 +17,64 @@ namespace SharedLibraryCore.Services
{
using (var context = new DatabaseContext())
{
int? linkId = null;
int? aliasId = null;
if (entity.IPAddress != null)
{
var existingAlias = await context.Aliases
.Select(_alias => new { _alias.AliasId, _alias.LinkId, _alias.IPAddress, _alias.Name })
.FirstOrDefaultAsync(_alias => _alias.IPAddress == entity.IPAddress);
if (existingAlias != null)
{
linkId = existingAlias.LinkId;
if (existingAlias.Name == entity.Name)
{
aliasId = existingAlias.AliasId;
}
}
}
var client = new EFClient()
{
Level = Permission.User,
FirstConnection = DateTime.UtcNow,
LastConnection = DateTime.UtcNow,
NetworkId = entity.NetworkId,
AliasLink = new EFAliasLink()
NetworkId = entity.NetworkId
};
client.CurrentAlias = new Alias()
if (linkId.HasValue)
{
Name = entity.Name,
Link = client.AliasLink,
DateAdded = DateTime.UtcNow,
IPAddress = entity.IPAddress,
};
client.AliasLinkId = linkId.Value;
}
else
{
client.AliasLink = new EFAliasLink();
}
if (aliasId.HasValue)
{
client.CurrentAliasId = aliasId.Value;
}
else
{
client.CurrentAlias = new Alias()
{
Name = entity.Name,
DateAdded = DateTime.UtcNow,
IPAddress = entity.IPAddress,
Link = client.AliasLink,
};
if (client.CurrentAlias.Link == null)
{
client.CurrentAlias.LinkId = linkId.Value;
}
}
context.Clients.Add(client);
await context.SaveChangesAsync();
@ -48,13 +90,21 @@ namespace SharedLibraryCore.Services
var iqAliases = context.Aliases
.Include(a => a.Link)
// we only want alias that have the same IP address or share a link
.Where(_alias => _alias.IPAddress == ip || (_alias.LinkId == entity.AliasLinkId && _alias.Active));
.Where(_alias => _alias.IPAddress == ip || (_alias.LinkId == entity.AliasLinkId));
#if DEBUG == true
var aliasSql = iqAliases.ToSql();
#endif
var aliases = await iqAliases.ToListAsync();
// update each of the aliases where this is no IP but the name is identical
foreach (var alias in aliases.Where(_alias => (_alias.IPAddress == null || _alias.IPAddress == 0)))
{
alias.IPAddress = ip;
}
await context.SaveChangesAsync();
// see if they have a matching IP + Name but new NetworkId
var existingExactAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip);
bool hasExactAliasMatch = existingExactAlias != null;
@ -82,7 +132,7 @@ namespace SharedLibraryCore.Services
// update all previous aliases
await context.Aliases
.Where(_alias => _alias.LinkId == oldAliasLink.AliasLinkId)
.ForEachAsync(_alias => { _alias.LinkId = newAliasLink.AliasLinkId; _alias.Active = true; });
.ForEachAsync(_alias => _alias.LinkId = newAliasLink.AliasLinkId);
await context.SaveChangesAsync();
// we want to delete the now inactive alias
@ -109,62 +159,23 @@ namespace SharedLibraryCore.Services
}
// theres no exact match, but they've played before with the GUID or IP
else if (hasExistingAlias)
else
{
entity.CurrentServer.Logger.WriteDebug($"Connecting player is using a new alias {entity}");
// this happens when a temporary alias gets updated
if (entity.CurrentAlias.Name == name && entity.CurrentAlias.IPAddress == null)
var newAlias = new EFAlias()
{
entity.CurrentAlias.IPAddress = ip;
await context.SaveChangesAsync();
}
else
{
var newAlias = new EFAlias()
{
DateAdded = DateTime.UtcNow,
IPAddress = ip,
LinkId = newAliasLink.AliasLinkId,
Name = name,
Active = true,
};
entity.CurrentAlias = newAlias;
await context.SaveChangesAsync();
}
}
// no record of them playing
else
{
entity.AliasLink.Active = true;
entity.CurrentAlias.Active = true;
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Name = name;
DateAdded = DateTime.UtcNow,
IPAddress = ip,
LinkId = newAliasLink.AliasLinkId,
Name = name,
Active = true,
};
entity.CurrentAlias = newAlias;
entity.CurrentAliasId = 0;
await context.SaveChangesAsync();
}
//var linkIds = aliases.Select(a => a.LinkId);
//if (linkIds.Count() > 0 &&
// aliases.Count(_alias => _alias.Name == name && _alias.IPAddress == ip) > 0)
//{
// var highestLevel = await context.Clients
// .Where(c => linkIds.Contains(c.AliasLinkId))
// .MaxAsync(c => c.Level);
// if (entity.Level != highestLevel)
// {
// entity.CurrentServer.Logger.WriteDebug($"{entity} updating user level");
// // todo: log level changes here
// context.Update(entity);
// entity.SetLevel(highestLevel, Utilities.IW4MAdminClient(entity.CurrentServer));
// await context.SaveChangesAsync();
// }
//}
}
/// <summary>
@ -183,40 +194,51 @@ namespace SharedLibraryCore.Services
.Where(_client => _client.AliasLinkId == temporalClient.AliasLinkId)
.FirstAsync();
var oldPermission = entity.Level;
entity.Level = newPermission;
await ctx.SaveChangesAsync();
#if DEBUG == true
temporalClient.CurrentServer.Logger.WriteDebug($"Updated {temporalClient.ClientId} to {newPermission}");
#endif
// if their permission level has been changed to level that needs to be updated on all accounts
if ((entity.Level != newPermission) &&
if ((oldPermission != newPermission) &&
(newPermission == Permission.Banned ||
newPermission == Permission.Flagged ||
newPermission == Permission.User))
{
var changeSvc = new ChangeHistoryService();
// get all clients that have the same linkId
//get all clients that have the same linkId
var iqMatchingClients = ctx.Clients
.Where(_client => _client.AliasLinkId == entity.AliasLinkId)
// make sure we don't select ourselves twice
.Where(_client => _client.ClientId != temporalClient.ClientId);
var matchingClients = await iqMatchingClients.ToListAsync();
.Where(_client => _client.AliasLinkId == entity.AliasLinkId);
// make sure we don't select ourselves twice
//.Where(_client => _client.ClientId != temporalClient.ClientId);
// this updates the level for all the clients with the same LinkId
// only if their new level is flagged or banned
foreach (var client in matchingClients)
await iqMatchingClients.ForEachAsync(async (_client) =>
{
client.Level = newPermission;
_client.Level = newPermission;
// hack this saves our change to the change history log
await changeSvc.Add(new GameEvent()
{
Type = GameEvent.EventType.ChangePermission,
Extra = newPermission,
Origin = origin,
Target = client
Target = _client
}, ctx);
}
}
#if DEBUG == true
temporalClient.CurrentServer.Logger.WriteDebug($"Updated linked {_client.ClientId} to {newPermission}");
#endif
});
entity.Level = newPermission;
await ctx.SaveChangesAsync();
await ctx.SaveChangesAsync();
}
}
temporalClient.Level = newPermission;
@ -346,13 +368,6 @@ namespace SharedLibraryCore.Services
// update in database
await context.SaveChangesAsync();
// this is set so future updates don't trigger a new alias add
if (temporalClient.CurrentAlias.AliasId == 0)
{
temporalClient.CurrentAlias.AliasId = entity.CurrentAlias.AliasId;
}
return entity;
}
}

View File

@ -8,7 +8,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using static SharedLibraryCore.Database.Models.EFClient;
namespace SharedLibraryCore.Services
{
@ -35,6 +34,35 @@ namespace SharedLibraryCore.Services
newEntity.Offender.ReceivedPenalties?.Add(penalty);
context.Penalties.Add(penalty);
await context.SaveChangesAsync();
// certain penalties we want to save across all profiles
if (penalty.Type.ShouldPenaltyApplyToAllProfiles())
{
var iqLinkedProfiles = context.Clients
.Where(_client => _client.AliasLinkId == newEntity.Link.AliasLinkId)
// prevent adding the penalty twice to the same profile
.Where(_client => _client.ClientId != penalty.OffenderId);
await iqLinkedProfiles.ForEachAsync(_client =>
{
var linkedPenalty = new EFPenalty()
{
OffenderId = _client.ClientId,
PunisherId = newEntity.Punisher.ClientId,
LinkId = newEntity.Link.AliasLinkId,
Type = newEntity.Type,
Expires = newEntity.Expires,
Offense = newEntity.Offense,
When = DateTime.UtcNow,
AutomatedOffense = newEntity.AutomatedOffense,
IsEvadedOffense = newEntity.IsEvadedOffense
};
context.Penalties.Add(linkedPenalty);
});
await context.SaveChangesAsync();
}
}
return newEntity;
@ -70,26 +98,12 @@ namespace SharedLibraryCore.Services
throw new NotImplementedException();
}
public async Task<IList<EFPenalty>> GetClientPenaltiesAsync(int clientId)
{
using (var context = new DatabaseContext(true))
{
return await context.Penalties
.Where(p => p.OffenderId == clientId)
.Where(p => p.Active)
.Include(p => p.Offender.CurrentAlias)
.Include(p => p.Punisher.CurrentAlias)
.ToListAsync();
}
}
public async Task<IList<PenaltyInfo>> GetRecentPenalties(int count, int offset, Penalty.PenaltyType showOnly = Penalty.PenaltyType.Any)
{
using (var context = new DatabaseContext(true))
{
var iqPenalties = context.Penalties
.Where(p => showOnly == Penalty.PenaltyType.Any ? p.Type != Penalty.PenaltyType.Any : p.Type == showOnly)
.Where(p => p.Active)
.OrderByDescending(p => p.When)
.Skip(offset)
.Take(count)
@ -129,7 +143,7 @@ namespace SharedLibraryCore.Services
using (var ctx = new DatabaseContext(true))
{
var iqPenalties = ctx.Penalties.AsNoTracking()
.Where(_penalty => _penalty.Active)
//.Where(_penalty => _penalty.Active)
.Where(_penalty => _penalty.OffenderId == clientId || _penalty.PunisherId == clientId)
.Where(_penalty => _penalty.When < startAt)
.OrderByDescending(_penalty => _penalty.When)
@ -159,7 +173,7 @@ namespace SharedLibraryCore.Services
}
}
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int? ip = null)
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int? ip = null, bool includePunisherName = false)
{
var now = DateTime.UtcNow;
@ -210,7 +224,7 @@ namespace SharedLibraryCore.Services
await penalties.ForEachAsync(p =>
{
p.Active = false;
});
await context.SaveChangesAsync();

View File

@ -14,6 +14,7 @@ using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static SharedLibraryCore.Objects.Penalty;
using static SharedLibraryCore.Server;
namespace SharedLibraryCore
@ -478,6 +479,15 @@ namespace SharedLibraryCore
return "unknown";
}
public static bool ShouldPenaltyApplyToAllProfiles(this PenaltyType penaltyType)
{
return penaltyType == PenaltyType.Ban ||
penaltyType == PenaltyType.Unban ||
penaltyType == PenaltyType.Flag ||
penaltyType == PenaltyType.Unflag ||
penaltyType == PenaltyType.TempBan;
}
/// <summary>
/// Helper extension that determines if a user is a privileged client
/// </summary>