add parse command

merge-requests/1/merge
Matthew 2019-10-29 23:22:41 -04:00
parent 786a14d126
commit c19092c895
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
1 changed files with 58 additions and 0 deletions

58
src/commands/parse.ts Normal file
View File

@ -0,0 +1,58 @@
import fs from 'fs-extra';
import { parseCert } from '@ghaiklor/x509';
import { Message } from 'eris';
import { Client } from '..';
import { Command, RichEmbed } from '../class';
export default class Parse extends Command {
constructor(client: Client) {
super(client);
this.name = 'parse';
this.description = 'Gets information on a user\'s x509 certificate.';
this.usage = `${this.client.config.prefix}parse [username || user ID]`;
this.permissions = { roles: ['446104438969466890'] };
}
public async run(message: Message, args: string[]) { // eslint-disable-line
try {
if (!args.length) return this.client.commands.get('help').run(message, [this.name]);
const account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0].replace(/[<@!>]/gi, '') }] });
if (!account) return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot find user.***`);
const dir = await fs.readdir(`/home/${account.username}/Validation`);
const cert = parseCert(`/home/${account.username}/Validation/${dir[0]}`);
const subjectCommonName = cert.subject.commonName ? cert.subject.commonName : 'Not Specified';
const subjectEmailAddress = cert.subject.emailAddress ? cert.subject.emailAddress : 'Not Specified';
const subjectOrganization = cert.subject.organizationName ? cert.subject.organizationName : 'Not Specified';
const subjectOrganizationalUnit = cert.subject.organizationalUnitName ? cert.subject.organizationalUnitName : 'Not Specified';
const subjectCountry = cert.subject.countryName ? cert.subject.countryName : 'Not Specified';
const issuerCommonName = cert.issuer.commonName ? cert.issuer.commonName : 'Not Specified';
const issuerEmailAddress = cert.issuer.emailAddress ? cert.issuer.emailAddress : 'Not Specified';
const issuerOrganization = cert.issuer.organizationName ? cert.issuer.organizationName : 'Not Specified';
const issuerOrganizationalUnit = cert.issuer.organizationalUnitName ? cert.issuer.organizationalUnitName : 'Not Specified';
const issuerCountry = cert.issuer.countryName ? cert.issuer.countryName : 'Not Specified';
const user = this.client.users.get(account.userID) ? this.client.users.get(account.userID) : await this.client.getRESTUser(account.userID);
const embed = new RichEmbed();
embed.setTitle('Parse x509 Certificate');
embed.setDescription(`/home/${account.username}/Validation/${dir[0]} | ${account.username} <@${user.id}>`);
embed.setThumbnail(user.avatarURL);
embed.setColor(3447003);
embed.addField('Subject', `**Common Name:** ${subjectCommonName}\n**Email Address:** ${subjectEmailAddress}\n**Organization:** ${subjectOrganization}\n**Organizational Unit:** ${subjectOrganizationalUnit}\n**Country:** ${subjectCountry}`, true);
embed.addField('Issuer', `**Common Name:** ${issuerCommonName}\n**Email Address:** ${issuerEmailAddress}\n**Organization:** ${issuerOrganization}\n**Organizational Unit:** ${issuerOrganizationalUnit}\n**Country:** ${issuerCountry}`, true);
embed.addField('Serial Number', cert.serial, true);
embed.addField('Fingerprint', cert.fingerPrint, true);
embed.addField('Signature Algorithm', cert.signatureAlgorithm, true);
embed.addField('Public Key Algorithm', cert.publicKey.algorithm, true);
embed.addField('Key Usage', cert.extensions.keyUsage, true);
embed.addField('Extended Key Usage', cert.extensions.extendedKeyUsage, true);
embed.addField('Policies', cert.extensions.certificatePolicies, true);
embed.addField('Issued On', new Date(cert.notBefore).toLocaleString('en-us'), true);
embed.addField('Expires On', new Date(cert.notAfter).toLocaleString('en-us'), true);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
// @ts-ignore
message.channel.createMessage({ embed });
} catch (error) {
await this.client.util.handleError(error, message, this);
}
}
}