worked on encryption

pull/12/head
TheCodedProf 3 years ago
parent f8ef79403f
commit 75c51be81e

@ -1 +1 @@
ClicksMigratingProblems/**/* ClicksMigratingProblems/**/*

@ -152,7 +152,7 @@ const runServer = (client: NucleusClient) => {
app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) { app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) {
const code = req.params.code; const code = req.params.code;
if (code === undefined) return res.status(400).send("No code provided"); if (code === undefined) return res.status(400).send("No code provided");
const entry = await client.database.transcripts.read(code); const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
if (entry === null) return res.status(404).send("Could not find a transcript by that code"); if (entry === null) return res.status(404).send("Could not find a transcript by that code");
// Convert to a human readable format // Convert to a human readable format
const data = client.database.transcripts.toHumanReadable(entry); const data = client.database.transcripts.toHumanReadable(entry);
@ -164,7 +164,7 @@ const runServer = (client: NucleusClient) => {
app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) { app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) {
const code = req.params.code; const code = req.params.code;
if (code === undefined) return res.status(400).send("No code provided"); if (code === undefined) return res.status(400).send("No code provided");
const entry = await client.database.transcripts.read(code); const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
if (entry === null) return res.status(404).send("Could not find a transcript by that code"); if (entry === null) return res.status(404).send("Could not find a transcript by that code");
// Convert to a human readable format // Convert to a human readable format
return res.status(200).send(entry); return res.status(200).send(entry);

@ -318,7 +318,7 @@ const callback = async (interaction: CommandInteraction): Promise<unknown> => {
)).map(message => message as Message); )).map(message => message as Message);
const newOut = await client.database.transcripts.createTranscript(messageArray, interaction, interaction.member as GuildMember); const newOut = await client.database.transcripts.createTranscript(messageArray, interaction, interaction.member as GuildMember);
const code = await client.database.transcripts.create(newOut); const [code, key, iv] = await client.database.transcripts.create(newOut);
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
new EmojiEmbed() new EmojiEmbed()
@ -329,7 +329,7 @@ const callback = async (interaction: CommandInteraction): Promise<unknown> => {
], ],
components: [ components: [
new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript?code=${code}`), new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`).setDisabled(!code),
]) ])
] ]
}); });

@ -179,9 +179,10 @@ const callback = async (interaction: CommandInteraction): Promise<void> => {
continue; continue;
} }
if (confirmation.success) { if (confirmation.success) {
client.database.guilds.delete(interaction.guild!.id); await client.database.guilds.delete(interaction.guild!.id);
client.database.history.delete(interaction.guild!.id); await client.database.history.delete(interaction.guild!.id);
client.database.notes.delete(interaction.guild!.id); await client.database.notes.delete(interaction.guild!.id);
await client.database.transcripts.deleteAll(interaction.guild!.id);
nextFooter = "All data cleared"; nextFooter = "All data cleared";
continue; continue;
} else { } else {

@ -193,7 +193,7 @@ const callback = async (interaction: MessageContextMenuCommandInteraction) => {
) )
)).map(message => message as Message); )).map(message => message as Message);
const transcript = await client.database.transcripts.createTranscript(messageArray, interaction, interaction.member as GuildMember); const transcript = await client.database.transcripts.createTranscript(messageArray, interaction, interaction.member as GuildMember);
const code = await client.database.transcripts.create(transcript); const [code, key, iv] = await client.database.transcripts.create(transcript);
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
new EmojiEmbed() new EmojiEmbed()
@ -204,7 +204,7 @@ const callback = async (interaction: MessageContextMenuCommandInteraction) => {
], ],
components: [ components: [
new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript?code=${code}`), new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`).setDisabled(!code),
]) ])
] ]
}); });

@ -60,7 +60,7 @@ export default async function (interaction: CommandInteraction | MessageComponen
const newOut = await client.database.transcripts.createTranscript(messages, interaction, member); const newOut = await client.database.transcripts.createTranscript(messages, interaction, member);
const code = await client.database.transcripts.create(newOut); const [code, key, iv] = await client.database.transcripts.create(newOut);
if(!code) return await interaction.reply({ if(!code) return await interaction.reply({
embeds: [ embeds: [
new EmojiEmbed() new EmojiEmbed()
@ -86,7 +86,7 @@ export default async function (interaction: CommandInteraction | MessageComponen
], ],
components: [ components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([ new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript/${code}`), new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://testing.coded.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`),
new ButtonBuilder() new ButtonBuilder()
.setLabel("Delete") .setLabel("Delete")
.setStyle(ButtonStyle.Danger) .setStyle(ButtonStyle.Danger)

@ -21,6 +21,7 @@ await mongoClient.connect();
const database = mongoClient.db(); const database = mongoClient.db();
const collectionOptions = { authdb: "admin" }; const collectionOptions = { authdb: "admin" };
const getIV = () => crypto.randomBytes(16);
export class Guilds { export class Guilds {
guilds: Collection<GuildConfig>; guilds: Collection<GuildConfig>;
@ -203,15 +204,39 @@ export class Transcript {
do { do {
code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-"); code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
} while (await this.transcripts.findOne({ code: code })); } while (await this.transcripts.findOne({ code: code }));
const key = crypto.randomBytes(32**2).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-").substring(0, 32);
const iv = getIV().toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
for(const message of transcript.messages) {
if(message.content) {
const encCipher = crypto.createCipheriv("AES-256-CBC", key, iv);
message.content = encCipher.update(message.content, "utf8", "base64") + encCipher.final("base64");
}
}
const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions); const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions);
if(doc.acknowledged) return code; if(doc.acknowledged) return [code, key, iv];
else return null; else return [null, null, null];
} }
async read(code: string) { async read(code: string, key: string, iv: string) {
// console.log("Transcript read") // console.log("Transcript read")
return await this.transcripts.findOne({ code: code }); const doc = await this.transcripts.findOne({ code: code });
if(!doc) return null;
for(const message of doc.messages) {
if(message.content) {
const decCipher = crypto.createDecipheriv("AES-256-CBC", key, iv);
message.content = decCipher.update(message.content, "base64", "utf8") + decCipher.final("utf8");
}
}
return doc;
}
async deleteAll(guild: string) {
// console.log("Transcript delete")
const filteredDocs = await this.transcripts.find({ guild: guild }).toArray();
for (const doc of filteredDocs) {
await this.transcripts.deleteOne({ code: doc.code });
}
} }
async createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) { async createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) {

Loading…
Cancel
Save