Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
store attachments (part of #14)
Browse files Browse the repository at this point in the history
fix no server keys being generated
  • Loading branch information
AEnterprise committed May 6, 2020
1 parent 0017c44 commit 759414a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config-DEFAULT.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# You can use the static key below for development, or make your own here: https://gchq.github.io/CyberChef/#recipe=Pseudo-Random_Number_Generator(32,'Byte%20array')
# NOTE: DO **NOT** use the key below in any production deployment!
#DANGER_MASTER_KEY = [99,41,244,22,238,93,36,81,71,201,10,206,57,198,233,38,221,50,65,131,199,24,207,12,181,20,47,31,151,23,104,102]
#DANGEROUS_MASTER_KEY = [99,41,244,22,238,93,36,81,71,201,10,206,57,198,233,38,221,50,65,131,199,24,207,12,181,20,47,31,151,23,104,102]

[tokens]
discord = ""
Expand Down
19 changes: 18 additions & 1 deletion src/core/context/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use aes_gcm::{
use futures::channel::oneshot;
use log::{debug, info};
use postgres_types::Type;
use rand::{thread_rng, RngCore};
use std::sync::Arc;
use twilight::http::error::Error::Response;
use twilight::http::error::ResponseError::{Client, Server};
Expand Down Expand Up @@ -142,7 +143,6 @@ impl Context {
pub async fn insert_message(&self, msg: &Message, guild_id: GuildId) -> Result<(), Error> {
// All guilds need to have a config before anything can happen thanks to encryption.
let _ = self.get_config(guild_id).await?;
let client = self.pool.get().await?;

let msg_id = msg.id.0 as i64;

Expand All @@ -163,6 +163,11 @@ impl Context {
);

database::cache::insert_message(&self.pool, ciphertext, &msg).await?;
info!("inserted");
for attachment in &msg.attachments {
info!("processing attachment");
database::cache::insert_attachment(&self.pool, msg.id.0, attachment).await?;
}

Ok(())
}
Expand Down Expand Up @@ -196,6 +201,18 @@ impl Context {
Err(FetchError::ShouldExist.into())
}
}

pub fn generate_guild_key(&self, guild_id: u64) -> Vec<u8> {
//TODO: check how crypto safe this is
let mut csprng = thread_rng();
// Each guild has its own encryption key. This allows us, in the event of a compromise of the master key,
// to simply re-encrypt the guild keys instead of millions of messages.
let mut guild_encryption_key = [0u8; 32];
csprng.fill_bytes(&mut guild_encryption_key);

let master_key = self.__get_master_key().unwrap();
encrypt_bytes(&guild_encryption_key, master_key, guild_id)
}
}

fn encrypt_bytes(plaintext: &[u8], key: &EncryptionKey, id: u64) -> Vec<u8> {
Expand Down
2 changes: 1 addition & 1 deletion src/core/context/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Context {
match self.configs.get(&guild_id) {
Some(config) => Ok(config),
None => {
let config = get_guild_config(&self.pool, guild_id.0).await?;
let config = get_guild_config(&self, guild_id.0).await?;
self.configs.insert(guild_id, config);
Ok(self.configs.get(&guild_id).unwrap())
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Context {
pub status_text: RwLock<String>,
pub bot_user: CurrentUser,
configs: DashMap<GuildId, GuildConfig>,
pool: Pool,
pub pool: Pool,
__static_master_key: Option<Vec<u8>>,
pub chunk_requests: DashMap<String, Sender<MemberChunk>>,
}
Expand Down
25 changes: 24 additions & 1 deletion src/core/guild_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
pub struct GuildConfig {
pub(crate) prefix: String,
pub prefix: String,
pub log_style: LogStyle,
pub message_logs: MessageLogs,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct MessageLogs {
pub enabled: bool,
pub ignored_users: Vec<u64>,
pub ignored_channels: Vec<u64>,
pub ignore_bots: bool,
}

#[derive(Deserialize, Serialize, Debug)]
pub enum LogStyle {
Text,
Embed,
}

impl Default for GuildConfig {
fn default() -> Self {
GuildConfig {
prefix: "!".to_string(),
log_style: LogStyle::Text,
message_logs: MessageLogs {
enabled: false,
ignored_users: vec![],
ignored_channels: vec![],
ignore_bots: true,
},
}
}
}
17 changes: 8 additions & 9 deletions src/core/handlers/modlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,16 @@ pub async fn handle_event(shard_id: u64, event: &Event, ctx: Arc<Context>) -> Re
}
}

Event::MessageCreate(msg) if !msg.author.bot => {
Event::MessageCreate(msg) => {
if let Some(guild_id) = msg.guild_id {
ctx.insert_message(&msg.0, guild_id).await?
let config = &ctx.get_config(guild_id).await?.message_logs;
if config.enabled
&& !config.ignored_users.contains(&msg.author.id.0)
&& !(config.ignore_bots && msg.author.bot)
{
ctx.insert_message(&msg.0, guild_id).await?;
}
}

// if msg.0.content.contains("giveme") {
// let fetched = ctx.fetch_user_message(ID_HERE_FOR_TESTING.into()).await?;
// ctx.http.create_message(msg.channel_id)
// .content(fetched.content)
// .await?;
// }
}

_ => (),
Expand Down
29 changes: 28 additions & 1 deletion src/database/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::Error;
use deadpool_postgres::Pool;
use postgres_types::Type;
use twilight::model::channel::Message;
use twilight::model::channel::{Attachment, Message};

pub async fn insert_message(pool: &Pool, content: Vec<u8>, msg: &Message) -> Result<(), Error> {
let client = pool.get().await?;
Expand Down Expand Up @@ -37,3 +37,30 @@ pub async fn insert_message(pool: &Pool, content: Vec<u8>, msg: &Message) -> Res
.await?;
Ok(())
}

pub async fn insert_attachment(
pool: &Pool,
message_id: u64,
attachment: &Attachment,
) -> Result<(), Error> {
let client = pool.get().await?;
let statement = client
.prepare_typed(
"INSERT INTO attachment (id, name, image, message_id)
VALUES ($1, $2, $3, $4);",
&[Type::INT8, Type::VARCHAR, Type::BOOL, Type::INT8],
)
.await?;
client
.execute(
&statement,
&[
&(attachment.id.0 as i64),
&attachment.filename,
&attachment.width.is_some(),
&(message_id as i64),
],
)
.await?;
Ok(())
}
12 changes: 7 additions & 5 deletions src/database/guild.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::core::GuildConfig;
use crate::core::{Context, GuildConfig};
use crate::utils::Error;
use deadpool_postgres::Pool;
use log::info;
use postgres_types::Type;
use rand::{thread_rng, RngCore};

pub async fn get_guild_config(pool: &Pool, guild_id: u64) -> Result<GuildConfig, Error> {
let client = pool.get().await?;
pub async fn get_guild_config(ctx: &Context, guild_id: u64) -> Result<GuildConfig, Error> {
let client = ctx.pool.get().await?;
let statement = client
.prepare_typed("SELECT config from guildconfig where id=$1", &[Type::INT8])
.await?;
Expand All @@ -17,8 +18,8 @@ pub async fn get_guild_config(pool: &Pool, guild_id: u64) -> Result<GuildConfig,
info!("No config found for {}, inserting blank one", guild_id);
let statement = client
.prepare_typed(
"INSERT INTO guildconfig (id, config) VALUES ($1, $2)",
&[Type::INT8, Type::JSON],
"INSERT INTO guildconfig (id, config, encryption_key) VALUES ($1, $2, $3)",
&[Type::INT8, Type::JSON, Type::BYTEA],
)
.await?;
client
Expand All @@ -27,6 +28,7 @@ pub async fn get_guild_config(pool: &Pool, guild_id: u64) -> Result<GuildConfig,
&[
&(guild_id as i64),
&serde_json::to_value(&GuildConfig::default()).unwrap(),
&ctx.generate_guild_key(guild_id),
],
)
.await?;
Expand Down

0 comments on commit 759414a

Please sign in to comment.