Add /notify route, fix filename extension generation, Message u32 id, 08x format pad

This commit is contained in:
2024-12-23 19:12:59 -06:00
parent 45bd617ba7
commit 61efe6c194
3 changed files with 45 additions and 5 deletions

View File

@@ -44,7 +44,7 @@ FROM alpine:latest
WORKDIR /app
COPY --from=builder-astro /build/astro/dist/ ./public/
COPY --from=builder-demo /build/demo/target/x86_64-pc-windows-gnu/release/demo.exe ./demo-windows.exe
COPY --from=builder-demo /build/demo/target/x86_64-pc-windows-gnu/release/demo.exe ./demo.exe
COPY --from=builder-demo /build/demo/target/x86_64-unknown-linux-gnu/release/demo ./demo-linux
COPY --from=builder-server /build/server/target/release/dynamic-preauth ./dynamic-preauth

View File

@@ -201,6 +201,39 @@ pub async fn download(req: &mut Request, res: &mut Response, depot: &mut Depot)
}
}
#[handler]
pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let key = req.param::<String>("key");
match key {
Some(key) => {
// Parse key into u32
let key = key.parse::<u32>();
if key.is_err() {
res.status_code(StatusCode::BAD_REQUEST);
return;
}
let store = &mut *STORE.lock().await;
let session_id = get_session_id(req, depot).unwrap();
let session = store
.sessions
.get_mut(&session_id)
.expect("Session not found");
let message = OutgoingMessage::TokenAlert {
token: key.unwrap(),
};
session.send_message(message);
res.render("Notification sent");
}
None => {
res.status_code(StatusCode::BAD_REQUEST);
}
}
}
#[handler]
pub async fn get_session(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let store = STORE.lock().await;
@@ -271,7 +304,7 @@ async fn main() {
// Add the executables to the store
let mut store = STORE.lock().await;
store.add_executable("windows", "./demo-windows.exe");
store.add_executable("windows", "./demo.exe");
store.add_executable("linux", "./demo-linux");
drop(store); // critical: Drop the lock to avoid deadlock, otherwise the server will hang
@@ -308,6 +341,7 @@ async fn main() {
.push(Router::with_path("download/<id>").get(download))
.push(Router::with_path("session").get(get_session))
.push(Router::with_path("ws").goal(connect))
.push(Router::with_path("notify").post(notify))
.push(Router::with_path("<**path>").get(static_dir));
let service = Service::new(router).hoop(Logger::new());

View File

@@ -36,7 +36,13 @@ impl Session {
let download = SessionDownload {
token,
filename: format!("{}-{:16x}{}", exe.name, token, exe.extension),
filename: format!(
"{}-{:08x}{}{}",
exe.name,
token,
if exe.extension.len() > 0 { "." } else { "" },
exe.extension
),
last_used: chrono::Utc::now(),
download_time: chrono::Utc::now(),
};
@@ -203,14 +209,14 @@ impl Executable {
#[serde(tag = "type")]
pub enum IncomingMessage {
// A request from the client to delete a session token
DeleteSessionToken { id: u64 },
DeleteSessionToken { id: u32 },
}
#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum OutgoingMessage {
// An alert to the client that a session download has been used.
TokenAlert { token: u64 },
TokenAlert { token: u32 },
// A message describing the current session state
State { session: Session },
Executables { executables: Vec<ExecutableJson> },