diff --git a/backend/src/lib.rs b/backend/src/lib.rs new file mode 100644 index 0000000..fdabce7 --- /dev/null +++ b/backend/src/lib.rs @@ -0,0 +1,5 @@ +pub mod config; +pub mod handlers; +pub mod models; +pub mod railway; +pub mod state; diff --git a/backend/src/main.rs b/backend/src/main.rs index 10c700a..f8570fb 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,20 +1,16 @@ +use dynamic_preauth::config::Config; +use dynamic_preauth::handlers::{ + connect, download, get_build_logs, get_session, notify, session_middleware, +}; +use dynamic_preauth::railway; +use dynamic_preauth::state::STORE; + use salvo::cors::Cors; use salvo::http::Method; use salvo::logging::Logger; use salvo::prelude::{CatchPanic, Listener, Router, Server, Service, StaticDir, TcpListener}; use tracing_subscriber::EnvFilter; -use crate::config::Config; -use crate::handlers::{connect, download, get_build_logs, get_session, notify, session_middleware}; -use crate::state::STORE; - -mod config; -mod handlers; -mod models; -mod railway; -mod state; -mod utility; - #[tokio::main] async fn main() { // Load environment variables from .env file (development only) @@ -47,7 +43,7 @@ async fn main() { // Try to fetch actual build logs using Railway API if config.railway.has_token() { - match crate::railway::fetch_build_logs().await { + match railway::fetch_build_logs().await { Ok(build_logs) => { tracing::info!( "Successfully fetched build logs ({} bytes)", diff --git a/backend/src/models/executable.rs b/backend/src/models/executable.rs index 9fadab7..63a379f 100644 --- a/backend/src/models/executable.rs +++ b/backend/src/models/executable.rs @@ -11,6 +11,47 @@ pub struct Executable { } impl Executable { + pub fn search_pattern(buf: &[u8], pattern: &[u8], start_index: usize) -> Option { + let mut i = start_index; + + // If the buffer is empty, the pattern is too long + if pattern.len() > buf.len() { + return None; + } + + // If the pattern is empty + if pattern.is_empty() { + return None; + } + + // If the starting index is too high + if start_index >= buf.len() { + return None; + } + + while i < buf.len() { + for j in 0..pattern.len() { + // If the pattern is too long to fit in the buffer anymore + if i + j >= buf.len() { + return None; + } + + // If the pattern stops matching + if buf[i + j] != pattern[j] { + break; + } + + // If the pattern is found + if j == pattern.len() - 1 { + return Some(i); + } + } + + i += 1; + } + None + } + pub fn with_key(&self, new_key: &[u8]) -> Vec { let mut data = self.data.clone(); diff --git a/backend/src/state.rs b/backend/src/state.rs index 6f51d7d..83f2f58 100644 --- a/backend/src/state.rs +++ b/backend/src/state.rs @@ -6,7 +6,6 @@ use salvo::{http::cookie::Cookie, Response}; use tokio::sync::Mutex; use crate::models::{BuildLogs, Executable, ExecutableJson, Session}; -use crate::utility::search; pub static STORE: LazyLock> = LazyLock::new(|| Mutex::new(State::new())); @@ -32,7 +31,7 @@ impl State { let data = std::fs::read(exe_path).expect("Unable to read file"); let pattern = "a".repeat(1024); - let key_start = search(&data, pattern.as_bytes(), 0).unwrap(); + let key_start = Executable::search_pattern(&data, pattern.as_bytes(), 0).unwrap(); let key_end = key_start + pattern.len(); let path = path::Path::new(&exe_path); diff --git a/backend/src/utility.rs b/backend/src/utility.rs deleted file mode 100644 index cf06f23..0000000 --- a/backend/src/utility.rs +++ /dev/null @@ -1,40 +0,0 @@ -pub(crate) fn search(buf: &[u8], pattern: &[u8], start_index: usize) -> Option { - let mut i = start_index; - - // If the buffer is empty, the pattern is too long - if pattern.len() > buf.len() { - return None; - } - - // If the pattern is empty - if pattern.is_empty() { - return None; - } - - // If the starting index is too high - if start_index >= buf.len() { - return None; - } - - while i < buf.len() { - for j in 0..pattern.len() { - // If the pattern is too long to fit in the buffer anymore - if i + j >= buf.len() { - return None; - } - - // If the pattern stops matching - if buf[i + j] != pattern[j] { - break; - } - - // If the pattern is found - if j == pattern.len() - 1 { - return Some(i); - } - } - - i += 1; - } - None -}