fix(ssh): recover first-connect host-key prompts on web

This commit is contained in:
hhzorg authored and GitHub committed 2026-08-03 16:59:34 +08:00
1 parent db791b5aed
commit 7fbee89144
5 files changed
+132 -3

No files matched your search

+1
View File
@@ -312,6 +312,7 @@ async fn main() {
.route("/system/fonts", get(routes::jdbc::list_system_fonts))
.route("/ssh/config-hosts", get(routes::ssh_config::list_ssh_config_hosts))
.route("/ssh/prompts", get(routes::ssh_prompt::stream_ssh_prompts))
.route("/ssh/prompts/pending", get(routes::ssh_prompt::list_pending_ssh_prompts))
.route("/ssh/prompts/resolve", post(routes::ssh_prompt::resolve_ssh_prompt))
// Tunnel profiles
.route("/tunnel-profiles/list", get(routes::tunnel_profiles::load_tunnel_profiles))
+8 -1
View File
@@ -1,7 +1,7 @@
use axum::extract::State;
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::Json;
use dbx_core::db::ssh_prompt::SshPromptAnswer;
use dbx_core::db::ssh_prompt::{SshPromptAnswer, SshPromptRequest};
use futures::Stream;
use serde::Deserialize;
use std::convert::Infallible;
@@ -54,6 +54,13 @@ pub async fn resolve_ssh_prompt(
Ok(Json(()))
}
/// Returns all currently-pending host-key prompts. The frontend polls this as a
/// fallback to the SSE stream so a prompt that fired before the EventSource was
/// open is still recovered (see the SSH host-key first-connect regression).
pub async fn list_pending_ssh_prompts(State(state): State<Arc<WebState>>) -> Json<Vec<SshPromptRequest>> {
Json(state.ssh_prompts.pending_requests())
}
fn ssh_prompt_event(event: SshPromptEvent) -> Event {
Event::default().data(serde_json::to_string(&event).expect("SSH prompt events must serialize"))
}
+28
View File
@@ -47,6 +47,14 @@ impl SshPromptHub {
(replay, receiver)
}
/// Snapshot of all currently-pending prompt requests. The frontend uses this
/// as a fallback alongside the SSE stream: an SSE `Prompt` event that was
/// lost (e.g. the EventSource was not yet open when the prompt fired) is
/// recovered by polling this endpoint. Duplicates are deduped by the caller.
pub fn pending_requests(&self) -> Vec<SshPromptRequest> {
self.pending.lock().unwrap().iter().map(|prompt| prompt.request.clone()).collect()
}
pub fn resolve(&self, id: &str, answer: SshPromptAnswer) -> Result<(), String> {
let pending = {
let mut pending = self.pending.lock().unwrap();
@@ -159,4 +167,24 @@ mod tests {
hub.resolve("prompt-2", SshPromptAnswer::Reject).unwrap();
assert!(matches!(receiver.await.unwrap(), SshPromptAnswer::Reject));
}
#[tokio::test]
async fn pending_requests_snapshots_unresolved_prompts_and_drops_resolved() {
let hub = SshPromptHub::new();
let (responder_a, _ra) = tokio::sync::oneshot::channel();
let (responder_b, _rb) = tokio::sync::oneshot::channel();
hub.register(SshPromptEnvelope { request: request("prompt-a"), responder: responder_a });
hub.register(SshPromptEnvelope { request: request("prompt-b"), responder: responder_b });
let pending = hub.pending_requests();
assert_eq!(pending.len(), 2);
assert_eq!(pending[0].id, "prompt-a");
assert_eq!(pending[1].id, "prompt-b");
// A resolved prompt is removed from the pending snapshot.
hub.resolve("prompt-a", SshPromptAnswer::Reject).unwrap();
let pending = hub.pending_requests();
assert_eq!(pending.len(), 1);
assert_eq!(pending[0].id, "prompt-b");
}
}