More struct

This commit is contained in:
BitHeaven 2024-03-16 13:34:13 +05:00
parent 73b0fb8762
commit 9473ebe7a0
4 changed files with 35 additions and 19 deletions

@ -1,5 +1,4 @@
mod type_users; mod types;
mod type_sites;
use { use {
std::{ std::{
@ -80,8 +79,8 @@ use {
Pool, Pool,
}, },
crate::{ crate::{
type_users::Users, types::users::Users,
type_sites::Sites, types::sites::Sites,
}, },
}; };
@ -159,6 +158,7 @@ const CSS3: &str = r#"<style>
footer { text-align: right; } footer { text-align: right; }
</style>"#; </style>"#;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let addr = SocketAddr::from(([0, 0, 0, 0], PORT)); let addr = SocketAddr::from(([0, 0, 0, 0], PORT));
@ -214,17 +214,22 @@ async fn handle_connection(req: Request<Incoming>, pool: DBPool, ip: String) ->
_ => "" _ => ""
}; };
println!("{}", token); match <str as AsRef<str>>::as_ref(req.uri().path()) {
x if x.starts_with("/api/") => {}
_ => {
println!("{}", token);
if token != "" && jwt_verify(pool.clone(), token) if token != "" && jwt_verify(pool.clone(), token)
.await?.claims.as_object().unwrap().len() == 0 .await?.claims.as_object().unwrap().len() == 0
{ {
println!("Invalid suka"); println!("Invalid suka");
parts.status = StatusCode::FOUND; parts.status = StatusCode::FOUND;
set_cookie(&mut headers, "token", ""); set_cookie(&mut headers, "token", "");
set_location(&mut headers, "/"); set_location(&mut headers, "/");
parts.headers = headers; parts.headers = headers;
return Ok(Response::from_parts(parts, Full::new(Bytes::new()))); return Ok(Response::from_parts(parts, Full::new(Bytes::new())));
}
}
} }
(body, parts.status, restype) = match req.uri().path().as_ref() { (body, parts.status, restype) = match req.uri().path().as_ref() {
@ -234,7 +239,7 @@ async fn handle_connection(req: Request<Incoming>, pool: DBPool, ip: String) ->
"/register" => uri_register(req, pool.clone(), &mut headers).await?, "/register" => uri_register(req, pool.clone(), &mut headers).await?,
"/recover" => uri_recover(), "/recover" => uri_recover(),
x if x.starts_with("/@") => uri_user(req, pool.clone()).await?, x if x.starts_with("/@") => uri_user(req, pool.clone()).await?,
x if x.starts_with("/api") => uri_api(req), x if x.starts_with("/api/") => uri_api(req),
_ => uri_404() _ => uri_404()
}; };
@ -337,6 +342,7 @@ fn uri_api(req: Request<Incoming>) -> (String, StatusCode, HeaderValue) {
let uri: &str = req.uri().path().as_ref(); let uri: &str = req.uri().path().as_ref();
let res: Json = match &uri[4..uri.len()] { let res: Json = match &uri[4..uri.len()] {
"/test" => json!({"error": false, "msg": "test"}), "/test" => json!({"error": false, "msg": "test"}),
x if x.starts_with("/v0/") => uri_api_v0(req),
_ => json!({"error": true, "msg": "No endpoint"}) _ => json!({"error": true, "msg": "No endpoint"})
}; };
@ -344,6 +350,14 @@ fn uri_api(req: Request<Incoming>) -> (String, StatusCode, HeaderValue) {
(res.to_string(), StatusCode::IM_A_TEAPOT, restype) (res.to_string(), StatusCode::IM_A_TEAPOT, restype)
} }
fn uri_api_v0(req: Request<Incoming>) -> Json {
let uri: &str = req.uri().path().as_ref();
match &uri[7..uri.len()] {
"/test" => json!({"error": false, "msg": "test endpoint v0"}),
_ => json!({"error": true, "msg": "No endpoint"})
}
}
async fn uri_user(req: Request<Incoming>, pool: DBPool) -> Res<(String, StatusCode, HeaderValue), SkyError> { async fn uri_user(req: Request<Incoming>, pool: DBPool) -> Res<(String, StatusCode, HeaderValue), SkyError> {
let uri: &str = req.uri().path().as_ref(); let uri: &str = req.uri().path().as_ref();
let login = &uri[2..uri.len()]; let login = &uri[2..uri.len()];
@ -542,10 +556,10 @@ fn rsa_gen() -> (Vec<u8>, Vec<u8>) {
let skey = RsaPrivateKey::new(&mut rng, bits).expect("RSA err"); let skey = RsaPrivateKey::new(&mut rng, bits).expect("RSA err");
let pkey = RsaPublicKey::from(&skey); let pkey = RsaPublicKey::from(&skey);
let skey = skey.to_pkcs1_der().unwrap().as_bytes().to_vec(); (
let pkey = pkey.to_pkcs1_der().unwrap().as_bytes().to_vec(); skey.to_pkcs1_der().unwrap().as_bytes().to_vec(),
pkey.to_pkcs1_der().unwrap().as_bytes().to_vec()
(skey, pkey) )
} }
async fn jwt_sign(pool: DBPool, data: Json) -> Result<String> { async fn jwt_sign(pool: DBPool, data: Json) -> Result<String> {

2
src/types.rs Normal file

@ -0,0 +1,2 @@
pub mod users;
pub mod sites;