From 8a7ff047b64abe3f22519a2a4c37f56cf03ed072 Mon Sep 17 00:00:00 2001 From: BitHeaven Date: Sat, 13 Apr 2024 12:52:15 +0500 Subject: [PATCH] Move work to notebook --- Cargo.lock | 123 +++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 1 + TODO | 4 +- src/api/v0.rs | 5 +- src/funcs.rs | 19 ++++++++ src/main.rs | 23 ++++------ 6 files changed, 146 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bfcffa5..9c5147d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ dependencies = [ "bcrypt", "chrono", "http-body-util", - "hyper", + "hyper 1.1.0", "hyper-util", "jsonwebtoken", "rand", @@ -20,6 +20,7 @@ dependencies = [ "tokio", "urlencoding", "uuid", + "webhook", ] [[package]] @@ -376,6 +377,25 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "h2" version = "0.4.1" @@ -387,7 +407,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 1.0.0", "indexmap", "slab", "tokio", @@ -407,6 +427,17 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http" version = "1.0.0" @@ -418,6 +449,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + [[package]] name = "http-body" version = "1.0.0" @@ -425,7 +467,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http", + "http 1.0.0", ] [[package]] @@ -436,8 +478,8 @@ checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "pin-project-lite", ] @@ -453,6 +495,30 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hyper" +version = "0.14.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.26", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper" version = "1.1.0" @@ -462,9 +528,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2", - "http", - "http-body", + "h2 0.4.1", + "http 1.0.0", + "http-body 1.0.0", "httparse", "httpdate", "itoa", @@ -473,6 +539,19 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.28", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "hyper-util" version = "0.1.2" @@ -482,9 +561,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http", - "http-body", - "hyper", + "http 1.0.0", + "http-body 1.0.0", + "hyper 1.1.0", "pin-project-lite", "socket2", "tokio", @@ -768,6 +847,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-src" +version = "300.2.3+3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cff92b6f71555b61bb9315f7c64da3ca43d87531622120fea0195fc761b4843" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.101" @@ -776,6 +864,7 @@ checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] @@ -1522,6 +1611,18 @@ version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" +[[package]] +name = "webhook" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d801ea0225da29d32c85b21d0cb12ed628783f5fa1fbe226e586a3ef6ca96f" +dependencies = [ + "hyper 0.14.28", + "hyper-tls", + "serde", + "serde_json", +] + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 05df1fd..d3d3768 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ skytable = "0.8.6" tokio = { version = "1.35.1", features = ["full"] } urlencoding = "2.1.3" uuid = { version = "1.6.1", features = ["v4", "v5"] } +webhook = "2.1.2" [[bin]] diff --git a/TODO b/TODO index 1c44175..64b7b3c 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,4 @@ -############################ -### MAKE USER UUID TABLE ### -############################ +Forward user back when set `back_url` Auth using tokens diff --git a/src/api/v0.rs b/src/api/v0.rs index 52491ce..f970298 100644 --- a/src/api/v0.rs +++ b/src/api/v0.rs @@ -41,10 +41,11 @@ async fn auth(req: Request, _pool: DBPool) -> Json { .or(Some(&"".to_string())) .unwrap()); match sess.as_str() { - "" => json!({"error": true, "msg": "No session in url"}), + "" => json!({"error": true, "msg": "No session in request"}), + x if x.len() > 128 => json!({"error": true, "msg": "Session len is too long"}), _ => json!({ "error": false, - "link": format!("https://auth.bitheaven.ru/authorize?session={}", sess) + "link": format!("https://auth.bitheaven.ru/authorize?v=0&session={}", sess) }) } } diff --git a/src/funcs.rs b/src/funcs.rs index ee6d154..5ee43f3 100644 --- a/src/funcs.rs +++ b/src/funcs.rs @@ -7,6 +7,7 @@ use { UNIX_EPOCH, }, }, + webhook::client::WebhookClient, urlencoding::decode as url_decode, uuid::Uuid, crate::{ @@ -53,3 +54,21 @@ pub fn time_mcs() -> u128 { .unwrap() .as_micros() } + +pub async fn discord_wh_send(text: String) { + let url: &str = "https://discord.com/api/webhooks/1228566682902204537/RXaWpZplEGzU88O8c4mD4qzXp1PDBrrp93nGvdijaY7mBXp27xc0EsThHUeU0431PQOZ"; + let client: WebhookClient = WebhookClient::new(url); + client.send(|message| message + .username("Bit.Auth") +// .avatar_url("") + .embed(|embed| embed + .title("Error") + .description(&text) +// .footer("Footer", Some(String::from(IMAGE_URL))) +// .image(IMAGE_URL) +// .thumbnail(IMAGE_URL) +// .author("bitheaven", Some(String::from(IMAGE_URL)), Some(String::from(IMAGE_URL))) +// .field("name", "value", false) + ) + ).await.unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 4ad5083..ce91b65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -181,13 +181,6 @@ async fn handle_connection(req: Request, pool: DBPool, ip: String) -> x if x.starts_with("/api/") => {}, _ => 'jwt_check: { if token == "" { break 'jwt_check; } -/* if token != "" { - parts.status = StatusCode::FOUND; - set_cookie(&mut headers, "token", ""); - set_location(&mut headers, "/login"); - parts.headers = headers; - return Ok(Response::from_parts(parts, Full::new(Bytes::new()))); - }*/ let is_live = jwt_verify(pool.clone(), &token) .await? @@ -272,9 +265,6 @@ async fn handle_connection(req: Request, pool: DBPool, ip: String) -> } } } - -// if x == "/authorize" { -// } } } @@ -359,6 +349,7 @@ async fn uri_login(req: Request, pool: DBPool, headers: &mut HeaderMap } async fn uri_authorize(req: Request, pool: DBPool, token: String) -> Result<(String, StatusCode, HeaderValue)> { + // TODO: Forward for versions. if *req.method() == Method::POST { let r = double_split(req.uri().query().or(Some("")).unwrap().to_owned(), "&", "="); @@ -368,7 +359,7 @@ async fn uri_authorize(req: Request, pool: DBPool, token: String) -> R _ => "".to_owned() }; - if session != "" { + if session != "" && session.len() <= 128 { authorize_user(pool.clone(), token, session).await; } } @@ -560,8 +551,14 @@ async fn login_user(pool: DBPool, data: HashMap) -> Res<(String, let q = con .query_parse::(&query!("SELECT * FROM bitauth.users WHERE uuid = ?", uuid.clone())) .await; - // TODO: Send to admin notify about trouble! - if q.is_err() { return Ok(ret); }; + if q.is_err() { + discord_wh_send( + format!("Пизда тут, ну тип, да, какая-то ебень с uuid, чел потерялся...\n`{}`\n**{}**", uuid, login) + ).await; + return Ok(ret); + } + + // TODO: Say to user about trouble. let q = q.unwrap(); if bcrypt::verify(pass, q.password.as_str()).unwrap() {