refactor: reorganize backend modules and create lib.rs

This commit is contained in:
2025-12-11 16:28:28 -06:00
parent d4454d7367
commit e23c01e4fd
5 changed files with 55 additions and 54 deletions

5
backend/src/lib.rs Normal file
View File

@@ -0,0 +1,5 @@
pub mod config;
pub mod handlers;
pub mod models;
pub mod railway;
pub mod state;

View File

@@ -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)",

View File

@@ -11,6 +11,47 @@ pub struct Executable {
}
impl Executable {
pub fn search_pattern(buf: &[u8], pattern: &[u8], start_index: usize) -> Option<usize> {
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<u8> {
let mut data = self.data.clone();

View File

@@ -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<Mutex<State>> = 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);

View File

@@ -1,40 +0,0 @@
pub(crate) fn search(buf: &[u8], pattern: &[u8], start_index: usize) -> Option<usize> {
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
}