Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/00-team/simurgh
Browse files Browse the repository at this point in the history
  • Loading branch information
SadraTghvi committed Oct 3, 2024
2 parents d449889 + 79e5302 commit e5e85e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DATABASE_URL="sqlite://main.db"
gmail=""
gmail_pass=""
discord_webhook=""
HEIMDALL_TOKEN="heimdall.00-team.org site token"
10 changes: 6 additions & 4 deletions src/api/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ async fn verification(
vdb.retain(|_, v| v.expires - now > 0);

let code = utils::get_random_string(Config::CODE_ABC, 5);
log::info!("send code:\n{}:{code}", body.email);

#[cfg(not(debug_assertions))]
utils::send_code(&body.email, code.as_str()).await?;

utils::send_webhook(
"Verificatin",
#[cfg(not(debug_assertions))]
utils::heimdall_message(
&format!(
"act: {:?}\nemail: ||`{}`||\ncode: `{code}`",
"action: {:?}\nemail: {}\ncode: {code}",
body.action, body.email
),
2017768,
"verificatin",
)
.await;

Expand Down
21 changes: 13 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
use std::{env, sync::OnceLock};

use lettre::{message::Mailbox, SmtpTransport};
use std::sync::OnceLock;

#[derive(Debug)]
pub struct Config {
pub mail_from: Mailbox,
pub mail_server: SmtpTransport,
pub discord_webhook: String,
pub heimdall_token: String,
}

impl Config {
pub const RECORD_DIR: &'static str = "./record/";
pub const TOKEN_ABC: &'static [u8] = b"!@#$%^&*_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+";
pub const API_KEY_ABC: &'static [u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
pub const API_KEY_ABC: &'static [u8] =
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
pub const CODE_ABC: &'static [u8] = b"0123456789";
pub const SLUG_ABC: &'static [u8] =
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
}

macro_rules! evar {
($name:literal) => {
std::env::var($name).expect(concat!($name, " was not found in env"))
};
}

pub fn config() -> &'static Config {
static STATE: OnceLock<Config> = OnceLock::new();
let mail = env::var("GMAIL").expect("no GMAIL in env");
let pass = env::var("GMAIL_PASS").expect("no GMAIL_PASS in env");
let mail = evar!("GMAIL");
let mail_server = SmtpTransport::relay("smtp.gmail.com")
.expect("smpt relay failed")
.port(465)
.credentials((&mail, &pass).into())
.credentials((&mail, &evar!("GMAIL_PASS")).into())
.build();

STATE.get_or_init(|| Config {
mail_from: mail.parse().expect("could not parse GMAIL"),
mail_server,
discord_webhook: env::var("DISCORD_WEBHOOK").unwrap(),
heimdall_token: evar!("HEIMDALL_TOKEN"),
})
}
40 changes: 10 additions & 30 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,44 +79,24 @@ pub fn remove_record(name: &str) {
let _ = std::fs::remove_file(Path::new(Config::RECORD_DIR).join(name));
}

pub async fn send_webhook(title: &str, desc: &str, color: u32) {
if cfg!(debug_assertions) {
log::info!("sending webhook:\n{title}\n-----------\n{desc}");
return;
}

pub async fn heimdall_message(text: &str, tag: &str) {
let client = awc::Client::new();
let request = client.post(&config().discord_webhook);

#[derive(Serialize, Debug)]
struct Embed {
title: String,
description: String,
color: u32,
}

#[derive(Serialize, Debug)]
struct Data {
embeds: [Embed; 1],
let request = client
.post(format!("https://heimdall.00-team.org/api/sites/messages/"))
.insert_header(("authorization", config().heimdall_token.as_str()));

#[derive(Serialize)]
struct Message {
text: String,
tag: String,
}

let _ = request
.send_json(&Data {
embeds: [Embed {
title: title.to_string(),
description: desc.to_string(),
color,
}],
})
.send_json(&Message { text: text.to_string(), tag: tag.to_string() })
.await;
}

pub async fn send_code(email: &str, code: &str) -> Result<(), AppErr> {
if cfg!(debug_assertions) {
log::info!("send code:\n{email}:{code}");
return Ok(());
}

let html = format!(
r##"<h1>Simurgh Verification</h1>
<p>your verification code: <code style='font-size: 24px'>{code}</code></p>"##
Expand Down

0 comments on commit e5e85e5

Please sign in to comment.