From a60713a4b043454c6e515a3c6ae67555c379fdba Mon Sep 17 00:00:00 2001 From: BitHeaven Date: Mon, 15 Jul 2024 08:30:27 +0300 Subject: [PATCH] Fix all Errors n Warns --- Cargo.lock | 36 ++++++++++----------- Cargo.toml | 2 +- src/funcs.rs | 9 +++--- src/main.rs | 31 ++---------------- src/url/authorize.rs | 19 +++++++++++ src/url/index.rs | 12 +++++++ src/url/login.rs | 75 ++------------------------------------------ src/url/nf.rs | 12 +++++++ src/url/recover.rs | 12 +++++++ src/url/register.rs | 22 +++++++++++++ src/url/user.rs | 17 ++++++++++ 11 files changed, 119 insertions(+), 128 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c5147d..cf7e7b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,9 +55,9 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.77" +version = "0.1.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", @@ -99,12 +99,11 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "bb8" -version = "0.8.1" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98b4b0f25f18bcdc3ac72bdb486ed0acf7e185221fd4dc985bc15db5800b0ba2" +checksum = "b10cf871f3ff2ce56432fddc2615ac7acc3aa22ca321f8fea800846fbb32f188" dependencies = [ "async-trait", - "futures-channel", "futures-util", "parking_lot", "tokio", @@ -340,12 +339,10 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ - "futures-channel", "futures-core", "futures-task", "pin-project-lite", "pin-utils", - "slab", ] [[package]] @@ -616,9 +613,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" @@ -715,11 +712,10 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" dependencies = [ - "lazy_static", "libc", "log", "openssl", @@ -1226,9 +1222,9 @@ dependencies = [ [[package]] name = "sky-derive" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a5d926a1f01fa0f21cf9ba651777b78e0a8a236be400d94cbb03396f5e22287" +checksum = "653a697e7fcd1ecf5f9af1d3277d6d48290f29f076cb0190011eb09cff2238ea" dependencies = [ "proc-macro2", "quote", @@ -1237,9 +1233,9 @@ dependencies = [ [[package]] name = "skytable" -version = "0.8.6" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03e8d2a2164bdf8d5612caf70c31baf3ae905bc7bc4292dd631cfbe67eb097a9" +checksum = "6ad7801f368596253646e42e1a11360431d115ab576fffa26e5da08e6ddb9d8d" dependencies = [ "async-trait", "bb8", @@ -1390,9 +1386,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.35.1" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", @@ -1409,9 +1405,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index d3d3768..567191d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ jsonwebtoken = "9.2.0" rand = "0.8.5" rsa = "0.9.6" serde_json = "1.0.111" -skytable = "0.8.6" +skytable = "0.8.10" tokio = { version = "1.35.1", features = ["full"] } urlencoding = "2.1.3" uuid = { version = "1.6.1", features = ["v4", "v5"] } diff --git a/src/funcs.rs b/src/funcs.rs index 43e33f6..df77e09 100644 --- a/src/funcs.rs +++ b/src/funcs.rs @@ -1,6 +1,5 @@ use { std::{ - sync::Arc, collections::HashMap, any::type_name, time::{ @@ -14,6 +13,7 @@ use { chrono::DateTime, serde_json::{ Value as Json, + Map as JsonMap, json, }, rsa::{ @@ -37,10 +37,8 @@ use { }, skytable::{ query, - pool::ConnectionMgrTcp, error::Error as SkyError, }, - bb8::Pool, http_body_util::BodyExt, hyper::{ Request, @@ -59,6 +57,7 @@ use { html::*, Users, Sites, + APIV0_LIFETIME, }, }; @@ -379,7 +378,7 @@ pub async fn get_user(pool: DBPool, login: String) -> Res { Ok(q?) } -fn get_cookies(headers: HeaderMap) -> HashMap { +pub fn get_cookies(headers: HeaderMap) -> HashMap { let header = headers.get(hyper::header::COOKIE); let cookies = match header.is_none() { false => header.unwrap().to_str().unwrap(), @@ -389,7 +388,7 @@ fn get_cookies(headers: HeaderMap) -> HashMap { double_split(cookies.to_owned(), ";", "=") } -async fn authorize_user(pool: DBPool, token: String, session: String) { +pub async fn authorize_user(pool: DBPool, token: String, session: String) { let mut con = pool.get().await.unwrap(); let data: JsonMap = jwt_verify(pool.clone(), &token) diff --git a/src/main.rs b/src/main.rs index f68834f..20e0499 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,18 +10,13 @@ use { collections::HashMap, process::exit, }, - chrono::{ - DateTime, - }, http_body_util::{ Full, - BodyExt, }, hyper::{ StatusCode, Request, Response, - Method, HeaderMap, header::HeaderValue, body::{ @@ -37,28 +32,8 @@ use { tokio::{ net::TcpListener, }, - rsa::{ - pkcs1::{ - EncodeRsaPublicKey, - EncodeRsaPrivateKey, - }, - RsaPrivateKey, - RsaPublicKey, - }, - jsonwebtoken as jwt, - jwt::{ - Header, - Algorithm, - TokenData, - Validation, - EncodingKey, - DecodingKey, - encode as jwt_encode, - decode as jwt_decode, - }, serde_json::{ Value as Json, - Map as JsonMap, json, }, skytable::{ @@ -75,7 +50,6 @@ use { }, urlencoding::{ encode as url_encode, - decode as url_decode, }, crate::{ types::{ @@ -84,7 +58,6 @@ use { }, html::*, funcs::*, - url, }, }; @@ -97,10 +70,10 @@ type FullBytes = Result>>; const PORT: u16 = 8083; const DB_POOL: u32 = 32; -const DB_ADDR: &str = "192.168.1.49"; +const DB_ADDR: &str = "127.0.0.1"; const DB_PORT: u16 = 2003; const DB_USER: &str = "root"; -const DB_PASS: &str = "rootpass12345678"; +const DB_PASS: &str = "5a14775a-a490-4212345678"; const TOKEN_LIFETIME: u32 = 300; const REFRESH_LIFETIME: u32 = 2_678_400; diff --git a/src/url/authorize.rs b/src/url/authorize.rs index 02ff5b0..809ae6c 100644 --- a/src/url/authorize.rs +++ b/src/url/authorize.rs @@ -1,3 +1,22 @@ +use { + hyper::{ + Request, + body::Incoming, + Method, + StatusCode, + header::HeaderValue, + }, + crate::{ + AUTHORIZE_HTML, + DBPool, + Result, + build_html, + double_split, + authorize_user, + }, +}; + + pub async fn authorize(req: Request, pool: DBPool, token: String) -> Result<(String, StatusCode, HeaderValue)> { // TODO: Forward for versions. if *req.method() == Method::POST { diff --git a/src/url/index.rs b/src/url/index.rs index f496c97..f58a315 100644 --- a/src/url/index.rs +++ b/src/url/index.rs @@ -1,3 +1,15 @@ +use { + hyper::{ + StatusCode, + header::HeaderValue, + }, + crate::{ + INDEX_HTML, + build_html, + }, +}; + + pub fn index() -> (String, StatusCode, HeaderValue) { let restype: HeaderValue = "text/html".parse().unwrap(); (build_html(INDEX_HTML), StatusCode::OK, restype) diff --git a/src/url/login.rs b/src/url/login.rs index a8e827b..4df48f0 100644 --- a/src/url/login.rs +++ b/src/url/login.rs @@ -1,96 +1,25 @@ use { - std::{ - sync::Arc, - net::SocketAddr, - collections::HashMap, - process::exit, - }, - chrono::{ - DateTime, - }, - http_body_util::{ - Full, - BodyExt, - }, hyper::{ StatusCode, Request, - Response, Method, HeaderMap, header::HeaderValue, body::{ - Bytes, Incoming, }, - server::conn::http1 as Server, - service::service_fn, - }, - hyper_util::{ - rt::TokioIo, - }, - tokio::{ - net::TcpListener, - }, - rsa::{ - pkcs1::{ - EncodeRsaPublicKey, - EncodeRsaPrivateKey, - }, - RsaPrivateKey, - RsaPublicKey, - }, - jsonwebtoken as jwt, - jwt::{ - Header, - Algorithm, - TokenData, - Validation, - EncodingKey, - DecodingKey, - encode as jwt_encode, - decode as jwt_decode, - }, - serde_json::{ - Value as Json, - Map as JsonMap, - json, - }, - skytable::{ - query, - Config, - pool::{ - self, - ConnectionMgrTcp - }, - error::Error as SkyError, - }, - bb8::{ - Pool, }, urlencoding::{ - encode as url_encode, decode as url_decode, }, crate::{ - types::{ - users::Users, - sites::Sites, - }, + Result, + DBPool, html::*, funcs::*, - url::{ - api, - login, - }, }, }; -type Res = std::result::Result; -type Result = std::result::Result>; -type DBPool = Arc>; -type FullBytes = Result>>; - pub async fn login(req: Request, pool: DBPool, headers: &mut HeaderMap) -> Result<(String, StatusCode, HeaderValue)> { let mut body = build_html(LOGIN_HTML); diff --git a/src/url/nf.rs b/src/url/nf.rs index f76d923..256fa84 100644 --- a/src/url/nf.rs +++ b/src/url/nf.rs @@ -1,3 +1,15 @@ +use { + hyper::{ + StatusCode, + header::HeaderValue, + }, + crate::{ + NF_HTML, + build_html, + }, +}; + + pub fn nf() -> (String, StatusCode, HeaderValue) { let restype: HeaderValue = "text/html".parse().unwrap(); (build_html(NF_HTML), StatusCode::NOT_FOUND, restype) diff --git a/src/url/recover.rs b/src/url/recover.rs index 46a2be4..e196f5f 100644 --- a/src/url/recover.rs +++ b/src/url/recover.rs @@ -1,3 +1,15 @@ +use { + hyper::{ + StatusCode, + header::HeaderValue, + }, + crate::{ + build_html, + RECOVER_HTML, + }, +}; + + pub fn recover() -> (String, StatusCode, HeaderValue) { let restype: HeaderValue = "text/html".parse().unwrap(); (build_html(RECOVER_HTML), StatusCode::OK, restype) diff --git a/src/url/register.rs b/src/url/register.rs index af144b2..2650754 100644 --- a/src/url/register.rs +++ b/src/url/register.rs @@ -1,3 +1,25 @@ +use { + hyper::{ + body::Incoming, + HeaderMap, + Method, + Request, + header::HeaderValue, + StatusCode, + }, + crate::{ + Result, + DBPool, + REG_HTML, + build_html, + set_location, + create_user, + double_split, + get_body_from_request, + }, +}; + + pub async fn register(req: Request, pool: DBPool, headers: &mut HeaderMap) -> Result<(String, StatusCode, HeaderValue)> { let mut body = "".to_owned(); let mut status = StatusCode::OK; diff --git a/src/url/user.rs b/src/url/user.rs index 6b20207..d201f6c 100644 --- a/src/url/user.rs +++ b/src/url/user.rs @@ -1,3 +1,20 @@ +use { + hyper::{ + Request, + StatusCode, + body::Incoming, + header::HeaderValue, + }, + crate::{ + Res, + DBPool, + SkyError, + get_user, + build_html, + }, +}; + + pub async fn user(req: Request, pool: DBPool) -> Res<(String, StatusCode, HeaderValue), SkyError> { let uri: &str = req.uri().path().as_ref(); let login = &uri[2..uri.len()];