cloudservices/src/commands/parseall.ts

80 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-11-14 16:56:58 -05:00
import { parseCert } from '@ghaiklor/x509';
import { Message } from 'eris';
import { readdirSync } from 'fs';
import moment from 'moment';
import { Client } from '..';
import { Command, RichEmbed } from '../class';
2020-01-04 08:47:54 -05:00
import { parseCertificate, Certificate } from '../functions';
2019-11-14 16:56:58 -05:00
export default class Parseall extends Command {
constructor(client: Client) {
super(client);
this.name = 'parseall';
this.description = 'Displays certificate validation for all accounts';
this.usage = `${this.client.config.prefix}parseall`;
this.permissions = { roles: ['446104438969466890'] };
this.aliases = ['checkcerts', 'verifyall', 'verifycerts'];
}
public async run(message: Message, args: string[]) {
try {
const embed = new RichEmbed();
embed.setTitle('Certificate Validation');
embed.setAuthor(this.client.user.username, this.client.user.avatarURL);
embed.setFooter(`Requested by ${message.member.username}#${message.member.discriminator}`, message.member.avatarURL);
2019-11-14 18:13:52 -05:00
embed.setTimestamp();
2019-11-14 16:56:58 -05:00
const search = await this.client.db.Account.find();
2020-01-04 10:05:05 -05:00
const files = search.map((acc) => {
let certfile: string;
try { certfile = readdirSync(`${acc.homepath}/Validation`)[0] } catch (error) { if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) certfile = 'not_found.crt' } // eslint-disable-line
2020-01-04 10:17:03 -05:00
this.client.signale.note('Cert directory set:');
this.client.signale.note(`${acc.homepath}/Validation/${certfile}`);
2020-01-04 10:05:05 -05:00
return `${acc.homepath}/Validation/${certfile}`;
2020-01-04 08:15:55 -05:00
});
2020-01-04 10:05:05 -05:00
2020-01-04 08:47:54 -05:00
// @ts-ignore
2020-01-04 10:05:05 -05:00
const parsed = await Promise.allSettled(files.map((c) => parseCertificate(this.client, c)));
2020-01-04 10:17:03 -05:00
this.client.signale.note('Promise settled:');
this.client.signale.note(parsed);
2020-01-04 08:15:55 -05:00
2020-01-04 10:05:05 -05:00
const final = search.map(async (a) => {
2020-01-04 09:06:10 -05:00
const result = await parsed[search.findIndex((acc) => acc === a)];
2020-01-04 10:17:03 -05:00
this.client.signale.note(result);
2020-01-04 08:47:54 -05:00
if (result.status === 'rejected') {
if (result.reason.message.includes('no such file or directory') || result.reason.message.includes('File doesn\'t exist.')) return `${this.client.stores.emojis.error} **${a.username}** Unable to locate certificate`;
2020-01-04 10:05:05 -05:00
if (result.reason.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} **${a.username}** Invalid certificate`;
2020-01-04 08:47:54 -05:00
throw result.reason;
}
const { notAfter } = result.value;
// @ts-ignore
const timeObject: {years: number, months: number, days: number, hours: number, minutes: number, seconds: number, firstDateWasLater: boolean} = moment.preciseDiff(new Date(), notAfter, true);
const precise: [number, string][] = [];
// @ts-ignore
const timeArray: number[] = Object.values(timeObject).filter((v) => typeof v === 'number');
timeArray.forEach((t) => { // eslint-disable-line
2020-01-04 08:47:54 -05:00
const index = timeArray.indexOf(t);
const measurements = ['yr', 'mo', 'd', 'h', 'm', 's'];
precise.push([t, measurements[index]]);
});
const time = precise.filter((n) => n[0]).map(((v) => v.join(''))).join(', ');
2019-11-14 16:56:58 -05:00
2020-01-04 08:47:54 -05:00
if (notAfter < new Date()) return `${this.client.stores.emojis.error} **${a.username}** Expired ${time} ago`;
return `${this.client.stores.emojis.success} **${a.username}** Expires in ${time}`;
2020-01-04 08:15:55 -05:00
});
2019-11-14 18:13:52 -05:00
if (final.join('\n').length < 2048) embed.setDescription(final.join('\n'));
else {
const split = this.client.util.splitString(final.join('\n'), 1024);
split.forEach((s) => embed.addField('\u200B', s));
}
// @ts-ignore
return await message.channel.createMessage({ embed });
2019-11-14 16:56:58 -05:00
} catch (error) {
2019-11-14 18:13:52 -05:00
return this.client.util.handleError(error, message, this);
2019-11-14 16:56:58 -05:00
}
}
}