From 0c68399210546d3440bec34702e45acc9425aa5b Mon Sep 17 00:00:00 2001 From: hex Date: Tue, 29 Jul 2025 12:35:03 +0200 Subject: [PATCH 1/3] chore: clean up, rename types.rs to server.rs --- config.yaml | 2 +- src/config.rs | 21 ++++++++- src/db.rs | 5 ++- src/main.rs | 20 ++++++--- src/{types.rs => server.rs} | 4 +- src/services.rs | 4 +- src/services/api.rs | 88 +++++++++++++++++++++++++++---------- src/services/controller.rs | 5 +-- src/services/proxy.rs | 2 +- 9 files changed, 110 insertions(+), 41 deletions(-) rename src/{types.rs => server.rs} (95%) diff --git a/config.yaml b/config.yaml index b3d0032..602b3e2 100644 --- a/config.yaml +++ b/config.yaml @@ -17,4 +17,4 @@ hosts: # ignore this it doesn't function clients: - name: 'eu-central-1' # Example Client right here (the client in this case would be for example the stereo.cat backend) - secret: '$2b$12$5wH/0p702PPqVp7fCpVS4.1GA2/wAbk89w2nMjwuS8439OhjCUGbK' # password123 + hashed_secret: '$2b$12$5wH/0p702PPqVp7fCpVS4.1GA2/wAbk89w2nMjwuS8439OhjCUGbK' # password123 diff --git a/src/config.rs b/src/config.rs index 1e055a0..719f3c9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,7 +27,26 @@ pub struct Api { #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] pub struct Client { pub name: String, - pub secret: String, + pub hashed_secret: String, +} + +impl Client { + pub async fn verify(us: String, config: Config) -> bool { + // us stands for user:secret btw + let us_split: Vec<&str> = us.split(':').collect(); + + let name = us_split.first().unwrap(); + let secret = us_split.last().unwrap(); + + let client: Client = config + .clients + .into_iter() + .filter(|x| x.name.eq(name)) + .nth(0) + .unwrap(); + + return bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap(); + } } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] diff --git a/src/db.rs b/src/db.rs index b880979..86c691d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,4 +1,7 @@ -use std::{error::Error, net::{IpAddr, SocketAddr}}; +use std::{ + error::Error, + net::{IpAddr, SocketAddr}, +}; use tokio_postgres::{Client, Socket, tls::MakeTlsConnect}; diff --git a/src/main.rs b/src/main.rs index 9c9cf9e..42844c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod config; mod db; +mod server; mod services; -mod types; use std::{env, sync::Arc}; @@ -11,16 +11,20 @@ use db::BoxyDatabase; use log::{error, info}; use nanoid::nanoid; use ring::rand::SystemRandom; +use server::Server; use services::{api::ApiService, controller::ControllerService}; use tokio::{fs::File, io::AsyncReadExt, sync::Mutex}; use tokio_postgres::{NoTls, tls::NoTlsError}; -use types::Server; + +const VERSION: &str = "v0.1a"; #[tokio::main] async fn main() -> Result<(), Box> { - pretty_env_logger::formatted_builder() - .filter(None, log::LevelFilter::Info) - .init(); + if env::var("RUST_LOG").is_err() { + unsafe { env::set_var("RUST_LOG", "info") }; + } + + pretty_env_logger::init(); let args: Vec = env::args().collect(); @@ -37,6 +41,10 @@ async fn main() -> Result<(), Box> { return Ok(()); } + "version" => { + info!("Version: {}", VERSION); + return Ok(()); + } _ => {} } } @@ -64,7 +72,7 @@ async fn main() -> Result<(), Box> { let api_svc = ApiService { database: database_shared.clone(), config: config.clone(), - _address: None + _address: None, }; let svc = ControllerService { diff --git a/src/types.rs b/src/server.rs similarity index 95% rename from src/types.rs rename to src/server.rs index c96f5ae..f35f836 100644 --- a/src/types.rs +++ b/src/server.rs @@ -25,7 +25,7 @@ pub struct Server { } pub trait TcpIntercept { - fn handle(&mut self, stream: &TcpStream); + fn stream(&mut self, stream: &TcpStream); } impl Server @@ -43,7 +43,7 @@ where let (stream, _) = self.listener.accept().await.unwrap(); let mut svc_clone = self.service.clone(); - svc_clone.handle(&stream); + svc_clone.stream(&stream); let io = TokioIo::new(stream); tokio::task::spawn(async move { diff --git a/src/services.rs b/src/services.rs index e8b28e6..168dbf8 100644 --- a/src/services.rs +++ b/src/services.rs @@ -1,3 +1,3 @@ -pub mod proxy; -pub mod controller; pub mod api; +pub mod controller; +pub mod proxy; diff --git a/src/services/api.rs b/src/services/api.rs index b8a107a..a3dce19 100644 --- a/src/services/api.rs +++ b/src/services/api.rs @@ -1,16 +1,20 @@ -use std::{hash, net::{IpAddr, SocketAddr}, pin::Pin, sync::Arc}; +use std::{net::IpAddr, pin::Pin, sync::Arc}; use base64::{Engine, prelude::BASE64_STANDARD}; -use bcrypt::DEFAULT_COST; +use bcrypt::bcrypt; use http_body_util::{BodyExt, Full}; use hyper::{ - body::{Bytes, Incoming}, service::Service, Method, Request, Response, StatusCode, Uri + Method, Request, Response, StatusCode, + body::{Bytes, Incoming}, + service::Service, }; -use log::{info, trace}; +use log::{debug, info, warn}; use tokio::{net::TcpStream, sync::Mutex}; use crate::{ - config::{Client, Config}, db::{BoxyDatabase, Endpoint}, types::{GeneralBody, GeneralResponse, TcpIntercept} + config::{Client, Config}, + db::{BoxyDatabase, Endpoint}, + server::{GeneralBody, GeneralResponse, TcpIntercept}, }; #[derive(Debug, Clone)] @@ -20,7 +24,7 @@ pub struct ApiService { pub _address: Option, } -async fn default_response() -> Response>> { +async fn default_response() -> GeneralResponse { Response::builder() .status(404) .body(GeneralBody::Right(Full::from(Bytes::from( @@ -29,7 +33,7 @@ async fn default_response() -> Response Response>> { +async fn custom_resp(e: StatusCode, m: &'static str) -> GeneralResponse { Response::builder() .status(e) .body(GeneralBody::Right(Full::from(Bytes::from(m)))) @@ -37,7 +41,7 @@ async fn custom_resp(e: StatusCode, m: String) -> Response> for ApiService { let database = self.database.clone(); let config = self.config.clone(); let address = self._address.clone().unwrap(); + Box::pin(async move { match *req.method() { Method::POST => match req.uri().path() { "/register" => { - let encoded_header = req.headers().get(hyper::header::AUTHORIZATION).unwrap().to_str().unwrap(); - - let auth_string = String::from_utf8(BASE64_STANDARD.decode(&encoded_header[6..]).unwrap()).unwrap(); - - let auth_string_split: Vec<&str> = auth_string.split(':').collect(); + debug!("new api register request from {}", address); - let name = auth_string_split.first().unwrap(); - let secret = auth_string_split.get(1).unwrap(); + let encoded_header = req + .headers() + .get(hyper::header::AUTHORIZATION) + .unwrap() + .to_str() + .unwrap(); - let matched_clients: Vec<&Client> = config.clients.iter().filter(|x| x.name.eq(name)).collect(); + debug!("authorization header: {}", encoded_header); - let client = matched_clients.first().unwrap(); + let auth_string = String::from_utf8( + BASE64_STANDARD.decode(&encoded_header[6..]).unwrap(), + ) + .unwrap(); - if !bcrypt::verify(secret, client.secret.as_str()).unwrap() { - return Ok(custom_resp(StatusCode::UNAUTHORIZED, "Invalid credentials.".to_string()).await); + debug!("decoded auth string: {}", auth_string); + + if !Client::verify(auth_string.clone(), config).await { + warn!( + "Authentication for string {} from {} failed.", + auth_string, address + ); + + return Ok(custom_resp( + StatusCode::UNAUTHORIZED, + "Invalid credentials.", + ) + .await); } - let body = String::from_utf8(req.collect().await.unwrap().to_bytes().iter().cloned().collect::>()).unwrap(); + let body = String::from_utf8( + req.collect() + .await + .unwrap() + .to_bytes() + .iter() + .cloned() + .collect::>(), + ) + .unwrap(); let json = json::parse(body.as_str()).unwrap(); - info!("body: {}", body); + debug!("body: {}", body); - let mut endpoint = Endpoint::new(None, address, json["port"].as_u16().unwrap(), json["callback"].as_str().unwrap_or("/").to_string()).await; + let mut endpoint = Endpoint::new( + None, + address, + json["port"].as_u16().unwrap(), + json["callback"].as_str().unwrap_or("/").to_string(), + ) + .await; - endpoint.register(*database.lock().await, json["hostname"].as_str().unwrap().to_string()).await.unwrap(); + endpoint + .register( + *database.lock().await, + json["hostname"].as_str().unwrap().to_string(), + ) + .await + .unwrap(); - Ok(custom_resp(StatusCode::OK, "yay".to_string()).await) + Ok(custom_resp(StatusCode::OK, "").await) } _ => Ok(default_response().await), }, diff --git a/src/services/controller.rs b/src/services/controller.rs index eee4b1c..b892b20 100644 --- a/src/services/controller.rs +++ b/src/services/controller.rs @@ -12,7 +12,7 @@ use tokio::sync::Mutex; use crate::{ config::{self, Client, Config, Host}, db::{BoxyDatabase, Endpoint}, - types::{GeneralBody, GeneralResponse, TcpIntercept}, + server::{GeneralBody, GeneralResponse, TcpIntercept}, }; use super::proxy::ProxyService; @@ -23,8 +23,7 @@ pub struct ControllerService { } impl TcpIntercept for ControllerService { - fn handle(&mut self, stream: &tokio::net::TcpStream) { - } + fn stream(&mut self, _: &tokio::net::TcpStream) {} } impl Service> for ControllerService { diff --git a/src/services/proxy.rs b/src/services/proxy.rs index c0e7e7d..e4641e1 100644 --- a/src/services/proxy.rs +++ b/src/services/proxy.rs @@ -5,7 +5,7 @@ use hyper_util::rt::TokioIo; use log::error; use tokio::net::TcpStream; -use crate::types::{GeneralResponse, to_general_response}; +use crate::server::{GeneralResponse, to_general_response}; #[derive(Debug, Clone)] pub struct ProxyService { From a6b2127b0c0e46b457487f360bb8c4be36388d66 Mon Sep 17 00:00:00 2001 From: hex Date: Tue, 29 Jul 2025 12:35:23 +0200 Subject: [PATCH 2/3] chore: remove unused imports --- src/db.rs | 4 ++-- src/main.rs | 8 +++----- src/services/api.rs | 3 +-- src/services/controller.rs | 9 +++------ 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/db.rs b/src/db.rs index 86c691d..0fdccba 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,9 +1,9 @@ use std::{ error::Error, - net::{IpAddr, SocketAddr}, + net::IpAddr, }; -use tokio_postgres::{Client, Socket, tls::MakeTlsConnect}; +use tokio_postgres::Client; const ENDPOINT_TABLE: &str = "endpoints"; const HOSTS_RELATION_TABLE: &str = "hosts"; diff --git a/src/main.rs b/src/main.rs index 42844c7..8ded716 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,14 @@ mod services; use std::{env, sync::Arc}; -use bcrypt::{DEFAULT_COST, bcrypt}; +use bcrypt::DEFAULT_COST; use config::Config; use db::BoxyDatabase; use log::{error, info}; -use nanoid::nanoid; -use ring::rand::SystemRandom; use server::Server; use services::{api::ApiService, controller::ControllerService}; -use tokio::{fs::File, io::AsyncReadExt, sync::Mutex}; -use tokio_postgres::{NoTls, tls::NoTlsError}; +use tokio::sync::Mutex; +use tokio_postgres::NoTls; const VERSION: &str = "v0.1a"; diff --git a/src/services/api.rs b/src/services/api.rs index a3dce19..cab3cac 100644 --- a/src/services/api.rs +++ b/src/services/api.rs @@ -1,14 +1,13 @@ use std::{net::IpAddr, pin::Pin, sync::Arc}; use base64::{Engine, prelude::BASE64_STANDARD}; -use bcrypt::bcrypt; use http_body_util::{BodyExt, Full}; use hyper::{ Method, Request, Response, StatusCode, body::{Bytes, Incoming}, service::Service, }; -use log::{debug, info, warn}; +use log::{debug, warn}; use tokio::{net::TcpStream, sync::Mutex}; use crate::{ diff --git a/src/services/controller.rs b/src/services/controller.rs index b892b20..7665ac7 100644 --- a/src/services/controller.rs +++ b/src/services/controller.rs @@ -1,18 +1,15 @@ use std::{pin::Pin, sync::Arc}; -use http_body_util::Full; use hyper::{ - Request, Response, - body::{Bytes, Incoming}, + Request, + body::Incoming, service::Service, }; -use log::error; use tokio::sync::Mutex; use crate::{ - config::{self, Client, Config, Host}, db::{BoxyDatabase, Endpoint}, - server::{GeneralBody, GeneralResponse, TcpIntercept}, + server::{GeneralResponse, TcpIntercept}, }; use super::proxy::ProxyService; From e2d9789f9a7e2b0d827508bece5a55f35a582b50 Mon Sep 17 00:00:00 2001 From: hex Date: Tue, 29 Jul 2025 12:53:21 +0200 Subject: [PATCH 3/3] feat: extra debug logging and chore: fix unused --- Cargo.lock | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ config.yaml | 9 +------- src/config.rs | 33 ++--------------------------- src/main.rs | 15 ++++++++------ src/server.rs | 11 ++++++++-- 6 files changed, 80 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e73c9b..cff25dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_colours" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14eec43e0298190790f41679fe69ef7a829d2a2ddd78c8c00339e84710e435fe" +dependencies = [ + "rgb", +] + [[package]] name = "anyhow" version = "1.0.98" @@ -118,9 +127,11 @@ dependencies = [ name = "boxy" version = "0.1.0" dependencies = [ + "ansi_colours", "anyhow", "base64", "bcrypt", + "colour", "http-body-util", "hyper", "hyper-util", @@ -143,6 +154,12 @@ version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +[[package]] +name = "bytemuck" +version = "1.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" + [[package]] name = "byteorder" version = "1.5.0" @@ -180,6 +197,15 @@ dependencies = [ "inout", ] +[[package]] +name = "colour" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b536eebcabe54980476d120a182f7da2268fe02d22575cca99cee5fdda178280" +dependencies = [ + "winapi", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -830,6 +856,15 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rgb" +version = "0.8.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c6a884d2998352bb4daf0183589aec883f16a6da1f4dde84d8e2e9a5409a1ce" +dependencies = [ + "bytemuck", +] + [[package]] name = "ring" version = "0.17.14" @@ -1286,6 +1321,22 @@ dependencies = [ "web-sys", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -1295,6 +1346,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 0c511d3..e6bf1d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,6 @@ serde = "1.0.219" base64 = "0.22.1" string-builder = "0.2.0" json = "0.12.4" +ansi_colours = "1.2.3" +colour = "2.1.0" diff --git a/config.yaml b/config.yaml index 602b3e2..fa4c12b 100644 --- a/config.yaml +++ b/config.yaml @@ -1,7 +1,4 @@ -db: - host: '127.0.0.1' - user: 'postgres' - password: 'trust' +database: 'postgresql://postgres:trust@127.0.0.1' proxy: listen: 127.0.0.1 @@ -11,10 +8,6 @@ api: listen: 127.0.0.1 port: 8006 -hosts: # ignore this it doesn't function - - hostname: localhost:8005 - address: localhost:8000 - clients: - name: 'eu-central-1' # Example Client right here (the client in this case would be for example the stereo.cat backend) hashed_secret: '$2b$12$5wH/0p702PPqVp7fCpVS4.1GA2/wAbk89w2nMjwuS8439OhjCUGbK' # password123 diff --git a/src/config.rs b/src/config.rs index 719f3c9..a15575f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -45,45 +45,16 @@ impl Client { .nth(0) .unwrap(); - return bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap(); + bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap() } } -#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] -pub struct Host { - pub hostname: String, - pub address: String, -} - #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] pub struct Config { - pub db: Db, + pub database: String, pub proxy: Proxy, pub api: Api, pub clients: Vec, - pub hosts: Vec, -} - -impl Db { - pub async fn to_string(&self) -> String { - let mut builder = String::new(); - - builder += format!( - "host={} port={} user={} dbname={}", - self.host, - self.port.unwrap_or(5432), - self.user, - self.database.clone().unwrap_or(self.user.clone()), - ) - .as_str(); - - match &self.password { - Some(x) => builder += format!(" password={}", x).as_str(), - None => {} - } - - builder - } } impl Config { diff --git a/src/main.rs b/src/main.rs index 8ded716..8fd2d2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,12 @@ mod db; mod server; mod services; -use std::{env, sync::Arc}; +use std::{env, process::exit, sync::Arc}; use bcrypt::DEFAULT_COST; use config::Config; use db::BoxyDatabase; -use log::{error, info}; +use log::{debug, error, info}; use server::Server; use services::{api::ApiService, controller::ControllerService}; use tokio::sync::Mutex; @@ -49,20 +49,21 @@ async fn main() -> Result<(), Box> { let config = Config::get().await.unwrap(); - let db_string = config.db.to_string().await; + debug!("Database URI: {}", config.database); - info!("Database string: {}", db_string); - - let (client, conn) = tokio_postgres::connect(db_string.as_str(), NoTls) + let (client, conn) = tokio_postgres::connect(config.database.as_str(), NoTls) .await .unwrap(); tokio::spawn(async move { if let Err(e) = conn.await { error!("Error while connecting to database: {}", e); + exit(1); } }); + info!("Connected to database."); + let database = Box::new(BoxyDatabase::new(client).await.unwrap()); let database_shared = Arc::new(Mutex::new(Box::leak(database))); @@ -86,10 +87,12 @@ async fn main() -> Result<(), Box> { .unwrap(); tokio::task::spawn(async move { + info!("Starting API server..."); api_server.handle().await; }); // We don't put this on a separate thread because we'd be wasting the main thread. + info!("Starting proxy server..."); proxy_server.handle().await; Ok(()) diff --git a/src/server.rs b/src/server.rs index f35f836..0eb7c63 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use std::{any::type_name_of_val, error::Error}; use http_body_util::{Either, Full}; use hyper::{ @@ -8,7 +8,7 @@ use hyper::{ service::{HttpService, Service}, }; use hyper_util::rt::TokioIo; -use log::error; +use log::{error, info}; use tokio::net::{TcpListener, TcpStream}; pub type GeneralResponse = Response; @@ -39,6 +39,12 @@ where >::Future: Send, { pub async fn handle(&self) { + info!( + "Server started at http://{} for service: {}", + self.listener.local_addr().unwrap(), + type_name_of_val(&self.service) + ); + loop { let (stream, _) = self.listener.accept().await.unwrap(); @@ -46,6 +52,7 @@ where svc_clone.stream(&stream); let io = TokioIo::new(stream); + tokio::task::spawn(async move { if let Err(err) = http1::Builder::new() .writev(false)