From 56d1ce3e30ff33268b74c3dfbf8d413d42aeec56 Mon Sep 17 00:00:00 2001 From: Bsian Date: Thu, 14 Nov 2019 21:56:58 +0000 Subject: [PATCH] added command --- src/commands/parseall.ts | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/commands/parseall.ts diff --git a/src/commands/parseall.ts b/src/commands/parseall.ts new file mode 100644 index 0000000..9ff0c47 --- /dev/null +++ b/src/commands/parseall.ts @@ -0,0 +1,56 @@ +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'; + +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); + const search = await this.client.db.Account.find(); + const accounts = search.map((acc) => acc.username); + const initial = accounts.map((acc) => `${this.client.stores.emojis.loading} **${acc}** Loading...`); + embed.setDescription(initial.join('\n')); + // @ts-ignore + const msg = await message.channel.createMessage({ embed }); + + accounts.forEach(async (a) => { + try { + const certFile = readdirSync(`/home/${a}/Validation`)[0]; + const { notAfter } = parseCert(`/home/${a}/Validation/${certFile}`); + // @ts-ignore + const time = moment.preciseDiff(new Date(), notAfter); + + if (notAfter < new Date()) initial[accounts.findIndex((acc) => acc === a)] = `${this.client.stores.emojis.error} **${a}** Certificate expired ${time} ago`; + else initial[accounts.findIndex((acc) => acc === a)] = `${this.client.stores.emojis.success} **${a}** Certificate expires in ${time}`; + + embed.setDescription(initial.join('\n')); + await msg.edit({ embed }); + } catch (error) { + if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) { + initial[accounts.findIndex((acc) => acc === a)] = `${this.client.stores.emojis.error} **${a}** Unable to locate certificate`; + embed.setDescription(initial.join('\n')); + await msg.edit({ embed }); + } else throw error; + } + }); + } catch (error) { + this.client.util.handleError(error, message, this); + } + } +}