2019-10-29 23:22:41 -04:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { parseCert } from '@ghaiklor/x509';
|
|
|
|
import { Message } from 'eris';
|
|
|
|
import { Client } from '..';
|
|
|
|
import { Command, RichEmbed } from '../class';
|
2020-01-01 17:21:41 -05:00
|
|
|
import { parseCertificate, Certificate } from '../functions';
|
2019-10-29 23:22:41 -04:00
|
|
|
|
|
|
|
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.***`);
|
2019-11-11 21:00:41 -05:00
|
|
|
let dir: string[];
|
|
|
|
try {
|
2019-12-23 12:08:42 -05:00
|
|
|
dir = await fs.readdir(`${account.homepath}/Validation`);
|
2019-11-11 21:00:41 -05:00
|
|
|
} catch (err) {
|
|
|
|
return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot locate Validation directory.***`);
|
|
|
|
}
|
|
|
|
if (!dir.length) return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot locate certificate.***`);
|
2020-01-01 17:21:41 -05:00
|
|
|
let cert: Certificate;
|
|
|
|
try {
|
|
|
|
cert = await parseCertificate(this.client, `${account.homepath}/Validation/${dir[0]}`);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.message.includes('panic: Certificate PEM Encode == nil')) return message.channel.createMessage(`***${this.client.stores.emojis.error} Invalid certificate.***`);
|
|
|
|
}
|
2019-12-31 22:28:57 -05:00
|
|
|
// const cert = parseCert(`${account.homepath}/Validation/${dir[0]}`);
|
|
|
|
const subjectCommonName = cert.subject.commonName || 'Not Specified';
|
|
|
|
const subjectEmailAddress = cert.subject.emailAddress || 'Not Specified';
|
|
|
|
const subjectOrganization = cert.subject.organizationName || 'Not Specified';
|
|
|
|
const subjectOrganizationalUnit = cert.subject.organizationalUnitName || 'Not Specified';
|
|
|
|
const subjectCountry = cert.subject.countryName || 'Not Specified';
|
|
|
|
const issuerCommonName = cert.issuer.commonName || 'Not Specified';
|
|
|
|
const issuerEmailAddress = cert.issuer.emailAddress || 'Not Specified';
|
|
|
|
const issuerOrganization = cert.issuer.organizationName || 'Not Specified';
|
|
|
|
const issuerOrganizationalUnit = cert.issuer.organizationalUnitName || 'Not Specified';
|
|
|
|
const issuerCountry = cert.issuer.countryName || 'Not Specified';
|
|
|
|
const user = this.client.users.get(account.userID) || await this.client.getRESTUser(account.userID);
|
2019-10-29 23:22:41 -04:00
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setTitle('Parse x509 Certificate');
|
2019-12-23 12:08:42 -05:00
|
|
|
embed.setDescription(`${account.homepath}/Validation/${dir[0]} | ${account.username} <@${user.id}>`);
|
2019-10-29 23:22:41 -04:00
|
|
|
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);
|
2019-12-31 22:28:57 -05:00
|
|
|
embed.addField('Public Key Algorithm', cert.publicKeyAlgorithm, true);
|
2019-10-29 23:22:41 -04:00
|
|
|
embed.addField('Key Usage', cert.extensions.keyUsage, true);
|
2019-12-31 22:28:57 -05:00
|
|
|
embed.addField('Extended Key Usage', cert.extensions.extendedKeyUsage.join(', '), true);
|
|
|
|
embed.addField('Policies', cert.extensions.certificatePolicies.join(', '), true);
|
2019-10-29 23:22:41 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|