Add support codes API and portal server changes

- support_codes.rs: 6-digit code management
- main.rs: Portal routes, static file serving, AppState
- relay/mod.rs: Updated for AppState
- Cargo.toml: Added rand, tower-http fs feature

Generated with Claude Code
This commit is contained in:
2025-12-28 17:54:05 +00:00
parent 70a9fcd129
commit 611bc00d06
5 changed files with 347 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ use tracing::{error, info, warn};
use crate::proto;
use crate::session::SessionManager;
use crate::AppState;
#[derive(Debug, Deserialize)]
pub struct AgentParams {
@@ -33,11 +34,12 @@ pub struct ViewerParams {
/// WebSocket handler for agent connections
pub async fn agent_ws_handler(
ws: WebSocketUpgrade,
State(sessions): State<SessionManager>,
State(state): State<AppState>,
Query(params): Query<AgentParams>,
) -> impl IntoResponse {
let agent_id = params.agent_id;
let agent_name = params.agent_name.unwrap_or_else(|| agent_id.clone());
let sessions = state.sessions.clone();
ws.on_upgrade(move |socket| handle_agent_connection(socket, sessions, agent_id, agent_name))
}
@@ -45,10 +47,11 @@ pub async fn agent_ws_handler(
/// WebSocket handler for viewer connections
pub async fn viewer_ws_handler(
ws: WebSocketUpgrade,
State(sessions): State<SessionManager>,
State(state): State<AppState>,
Query(params): Query<ViewerParams>,
) -> impl IntoResponse {
let session_id = params.session_id;
let sessions = state.sessions.clone();
ws.on_upgrade(move |socket| handle_viewer_connection(socket, sessions, session_id))
}
@@ -78,6 +81,8 @@ async fn handle_agent_connection(
}
});
let sessions_cleanup = sessions.clone();
// Main loop: receive frames from agent and broadcast to viewers
while let Some(msg) = ws_receiver.next().await {
match msg {
@@ -113,7 +118,7 @@ async fn handle_agent_connection(
// Cleanup
input_forward.abort();
sessions.remove_session(session_id).await;
sessions_cleanup.remove_session(session_id).await;
info!("Session {} ended", session_id);
}
@@ -154,6 +159,8 @@ async fn handle_viewer_connection(
}
});
let sessions_cleanup = sessions.clone();
// Main loop: receive input from viewer and forward to agent
while let Some(msg) = ws_receiver.next().await {
match msg {
@@ -189,6 +196,6 @@ async fn handle_viewer_connection(
// Cleanup
frame_forward.abort();
sessions.leave_session(session_id).await;
sessions_cleanup.leave_session(session_id).await;
info!("Viewer left session: {}", session_id);
}