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' ;
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 ( ) ;
2019-12-23 12:08:42 -05:00
const accounts = search . map ( ( acc ) = > acc . homepath ) ;
2019-11-14 18:13:52 -05:00
const final : string [ ] = [ ] ;
2019-11-14 16:56:58 -05:00
accounts . forEach ( async ( a ) = > {
try {
2019-12-23 12:08:42 -05:00
const certFile = readdirSync ( ` ${ a } /Validation ` ) [ 0 ] ;
const { notAfter } = parseCert ( ` ${ a } /Validation/ ${ certFile } ` ) ;
2019-11-14 16:56:58 -05:00
// @ts-ignore
const time = moment . preciseDiff ( new Date ( ) , notAfter ) ;
2019-11-14 18:13:52 -05:00
if ( notAfter < new Date ( ) ) final . push ( ` ${ this . client . stores . emojis . error } ** ${ a } ** Certificate expired ${ time } ago ` ) ;
else final . push ( ` ${ this . client . stores . emojis . success } ** ${ a } ** Certificate expires in ${ time } ` ) ;
2019-11-14 16:56:58 -05:00
} catch ( error ) {
2019-11-14 18:13:52 -05:00
if ( error . message . includes ( 'no such file or directory' ) || error . message . includes ( 'File doesn\'t exist.' ) ) final . push ( ` ${ this . client . stores . emojis . error } ** ${ a } ** Unable to locate certificate ` ) ;
else throw error ;
2019-11-14 16:56:58 -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
}
}
}