diff --git a/.env.example b/.env.example index 369bb55..f95ab6b 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ DATABASE_URL="sqlite://main.db" RUST_LOG="info" SIMURGH_API_KEY="simurgh.00-team.org project api key" SIMURGH_PROJECT="simurgh.00-team.org project id" +HEIMDALL_TOKEN="heimdall.00-team.org site token" diff --git a/config/nginx.conf b/config/nginx.conf index ad52616..3643219 100644 --- a/config/nginx.conf +++ b/config/nginx.conf @@ -56,6 +56,8 @@ server { } location / { + access_log syslog:server=unix:/usr/share/nginx/socks/heimdall.dog.heydari.sock,tag=H,nohostname heimdall; + proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Client-Ip $remote_addr; diff --git a/src/api/verification.rs b/src/api/verification.rs index 7c53257..3b5e492 100644 --- a/src/api/verification.rs +++ b/src/api/verification.rs @@ -84,6 +84,16 @@ async fn verification( let code = utils::get_random_string(Config::CODE_ABC, 5); log::info!("code: {code}"); + #[cfg(not(debug_assertions))] + utils::heimdall_message( + &format!( + "action: {:?}\nphone: {}\ncode: {code}", + body.action, body.phone + ), + "verification", + ) + .await; + utils::send_sms( &body.phone, &format!("heydari-mi.com\nyour login code: {code}"), diff --git a/src/config.rs b/src/config.rs index 24b049f..4b9733c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,3 @@ -use std::env::var as evar; use std::sync::OnceLock; #[derive(Debug)] @@ -7,6 +6,7 @@ pub struct Config { pub simurgh_project: i64, pub simurgh_host: String, pub simurgh_auth: String, + pub heimdall_token: String, } impl Config { @@ -17,6 +17,12 @@ impl Config { 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 = OnceLock::new(); @@ -28,14 +34,11 @@ pub fn config() -> &'static Config { .to_string(); STATE.get_or_init(|| Config { - simurgh_project: evar("SIMURGH_PROJECT") - .expect("no SIMURGH_PROJECT") + simurgh_project: evar!("SIMURGH_PROJECT") .parse::() .expect("invalid SIMURGH_PROJECT"), - simurgh_auth: format!( - "api-key {}", - evar("SIMURGH_API_KEY").expect("no SIMURGH_API_KEY") - ), + simurgh_auth: format!("api-key {}", evar!("SIMURGH_API_KEY")), simurgh_host, + heimdall_token: evar!("HEIMDALL_TOKEN"), }) } diff --git a/src/utils.rs b/src/utils.rs index a279aca..1ff0ba7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -95,6 +95,23 @@ pub async fn simurgh_request( } } +pub async fn heimdall_message(text: &str, tag: &str) { + let client = awc::Client::new(); + 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(&Message { text: text.to_string(), tag: tag.to_string() }) + .await; +} + pub trait CutOff { fn cut_off(&mut self, len: usize); }