Merge branch 'master' of gitlab.libraryofcode.org:engineering/cloudservices

merge-requests/4/head
Matthew 2020-01-05 14:59:48 -05:00
commit 99b905d03a
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
2 changed files with 42 additions and 35 deletions

View File

@ -3,7 +3,6 @@ import moment from 'moment';
import x509 from '@ghaiklor/x509';
import { createPaginationEmbed } from 'eris-pagination';
import { Message } from 'eris';
import { promisify } from 'util';
import { Command, RichEmbed } from '../class';
import { Client } from '..';
@ -21,7 +20,17 @@ export default class CWG_Data extends Command {
try {
if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]);
const dom = await this.client.db.Domain.find({ $or: [{ domain: args[0] }, { port: Number(args[0]) || '' }] });
if (!dom.length) return message.channel.createMessage(`***${this.client.stores.emojis.error} The domain or port you provided could not be found.***`);
if (!dom.length) {
if (!Number.isNaN(Number(args[0]))) {
try {
await this.client.util.exec(`fuser ${args[0]}/tcp`);
return message.channel.createMessage(`***${this.client.stores.emojis.error} The port you provided is being used by a system process.***`);
} catch (error) {
return message.channel.createMessage(`***${this.client.stores.emojis.error} The domain or port you provided could not be found.***`);
}
}
return message.channel.createMessage(`***${this.client.stores.emojis.error} The domain or port you provided could not be found.***`);
}
const embeds = dom.map((domain) => {
const cert = fs.readFileSync(domain.x509.cert, { encoding: 'utf8' });
const embed = new RichEmbed();

View File

@ -4,7 +4,7 @@ import { readdirSync } from 'fs';
import moment from 'moment';
import { Client } from '..';
import { Command, RichEmbed } from '../class';
import { parseCertificate } from '../functions';
import { parseCertificate, Certificate } from '../functions';
export default class Parseall extends Command {
constructor(client: Client) {
@ -19,6 +19,7 @@ export default class Parseall extends Command {
public async run(message: Message, args: string[]) {
try {
const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Loading...***`);
const embed = new RichEmbed();
embed.setTitle('Certificate Validation');
embed.setAuthor(this.client.user.username, this.client.user.avatarURL);
@ -26,21 +27,23 @@ export default class Parseall extends Command {
embed.setTimestamp();
const search = await this.client.db.Account.find();
const certificates = search.map((a) => {
let certFile: string;
try {
certFile = readdirSync(`${a.homepath}/Validation`)[0]; // eslint-disable-line
} catch (error) {
if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) certFile = 'not_found.crt';
else throw error;
}
return parseCertificate(this.client, `${a.homepath}/Validation/${certFile}`);
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
return `${acc.homepath}/Validation/${certfile}`;
});
const parsed = await Promise.all(certificates);
const final = search.map((a) => {
try {
const { notAfter } = parsed[search.findIndex((acc) => acc === a)];
// @ts-ignore
const parsed: ({ status: 'fulfilled', value: Certificate } | { status: 'rejected', reason: Error })[] = await Promise.allSettled(files.map((c) => parseCertificate(this.client, c)));
const final: string[] = await Promise.all(search.map(async (a) => {
const result = parsed[search.findIndex((acc) => acc === a)];
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`;
if (result.reason.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} **${a.username}** Invalid certificate`;
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][] = [];
@ -55,12 +58,7 @@ export default class Parseall extends Command {
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}`;
} catch (error) {
if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) return `${this.client.stores.emojis.error} **${a.username}** Unable to locate certificate`;
if (error.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} ** ${a.username}** Invalid certificate`;
throw error;
}
});
}));
if (final.join('\n').length < 2048) embed.setDescription(final.join('\n'));
else {
@ -69,7 +67,7 @@ export default class Parseall extends Command {
}
// @ts-ignore
return await message.channel.createMessage({ embed });
return await msg.edit({ content: '', embed });
} catch (error) {
return this.client.util.handleError(error, message, this);
}