Fix Punishments#ipSearch

This commit is contained in:
Annika 2021-06-08 14:08:36 -07:00
parent be2210f881
commit 9d87d9bac2
2 changed files with 29 additions and 9 deletions

View File

@ -1503,23 +1503,24 @@ export const Punishments = new class {
ipSearch(ip: string, type?: undefined): Punishment[] | undefined;
ipSearch(ip: string, type: string): Punishment | undefined;
ipSearch(ip: string, type?: string): Punishment | Punishment[] | undefined {
const allPunishments: Punishment[] = [];
let punishment = Punishments.ips.get(ip);
if (punishment) return punishment;
if (punishment) {
if (type) return punishment.find(p => p.type === type);
allPunishments.push(...punishment);
}
let dotIndex = ip.lastIndexOf('.');
for (let i = 0; i < 4 && dotIndex > 0; i++) {
ip = ip.substr(0, dotIndex);
punishment = Punishments.ips.get(ip + '.*');
if (punishment) {
if (type) {
for (const p of punishment) {
if (p.type === type) return p;
}
}
return punishment;
if (type) return punishment.find(p => p.type === type);
allPunishments.push(...punishment);
}
dotIndex = ip.lastIndexOf('.');
}
return undefined;
return allPunishments.length ? allPunishments : undefined;
}
/** Defined in Punishments.loadBanlist */
@ -1684,7 +1685,7 @@ export const Punishments = new class {
let banned: false | string = false;
const punishment = Punishments.ipSearch(ip, 'BAN');
if (punishment) {
banned = punishment.id;
banned = (Array.isArray(punishment) ? punishment[0] : punishment).id;
}
if (!banned) return false;

View File

@ -38,4 +38,23 @@ describe("Punishments", () => {
Punishments.userids.deleteOne(id, {type: 'RICKROLL', expireTime, reason, id});
assert.equal(Punishments.userids.get(id).length, 1);
});
it('should properly search for IP punishments by type', () => {
const [expireTime, reason, id] = [Date.now() + 1000, '', 'banmeplease'];
Punishments.ips.add('127.0.0.1', {type: 'BAN', expireTime, reason, id});
Punishments.ips.add('127.0.0.1', {type: 'RICKROLL', expireTime, reason, id});
Punishments.ips.add('127.0.*', {type: 'RANGEBAN', expireTime, reason, id});
const allIPPunishments = Punishments.ipSearch('127.0.0.1');
assert(Array.isArray(allIPPunishments));
assert.equal(allIPPunishments.length, 3);
const ban = Punishments.ipSearch('127.0.0.1', 'BAN');
assert(!Array.isArray(ban));
assert.equal(ban.type, 'BAN');
const rickroll = Punishments.ipSearch('127.0.0.1', 'RICKROLL');
assert(!Array.isArray(rickroll));
assert.equal(rickroll.type, 'RICKROLL');
});
});