mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-21 22:15:40 +00:00
fix(navidrome-admin): resilient admin API calls + UI polish (#260)
* fix(navidrome-admin): force HTTP/1.1 + User-Agent + no idle pool for /auth and /api calls Replaces plain reqwest::Client::new() in all nd_* Tauri commands (navidrome_login, nd_list_users, nd_create_user, nd_update_user, nd_delete_user, nd_list_libraries, nd_set_user_libraries) with a shared nd_http_client() helper that: - sets a real User-Agent (Psysonic/<version> (Tauri)) - pins HTTP/1.1 (avoids HTTP/2 ALPN that some reverse proxies abort on) - disables the idle-connection pool so a second call doesn't reuse a TCP connection that the server or proxy has already half-closed (was producing intermittent "tls handshake eof" on external servers). Adds nd_err() to flatten the reqwest error source chain into the returned String, so the frontend surfaces the real cause (connection refused, tls handshake eof, etc.) instead of reqwest's opaque "error sending request for url (…)" wrapper. * fix(navidrome-admin): retry + graceful error + server row polish Rust (src-tauri/src/lib.rs): - nd_http_client: HTTP/1.1, TLS 1.2 only, no idle pool — browser-parity for /auth and /api so strict reverse proxies that abort reqwest's default HTTP/2+TLS-1.3 handshake mid-flight get through. - nd_retry: one retry after 500ms on connect/timeout errors only (ECONNRESET, TLS handshake EOF). Aggressive retries could push the nginx upstream probe into offline state; this is the minimal useful amount. - nd_err: flatten the reqwest error source chain so the UI surfaces the real cause instead of reqwest's opaque wrapper. User management UI: - Sequential load (users, then libraries) instead of parallel, to avoid racing two TLS connections on a single nginx upstream slot. - Friendlier failure state with a one-click Retry button that re-runs load(). Concrete Rust error is kept as muted sub-line. - User row layout: Magic-String button sits consistently next to last-seen + delete for every user, regardless of admin status or library-name length. No more mid-row jitter. Servers tab: - "Use" button no longer redirects to Home; stays on the Servers tab so the active-badge migration is visible. - Drag-and-drop reorder via grip handle (psyDnD), backed by a new setServers() action on authStore. - Active server row now has an accent-tinted background on top of the border — harder to miss. New i18n keys (userMgmtLoadFriendly, userMgmtRetry) added to all 8 locales. --------- Co-authored-by: Psychotoxical <dev@psysonic.app>
This commit is contained in:
committed by
GitHub
parent
2a496c600b
commit
21d00889aa
+122
-49
@@ -310,6 +310,79 @@ struct NdLoginResult {
|
||||
is_admin: bool,
|
||||
}
|
||||
|
||||
/// Flatten an error and its `source` chain into a single readable string so
|
||||
/// frontend toasts can show the actual transport cause (connection refused,
|
||||
/// tls handshake fail, cert expired, etc.) instead of reqwest's opaque
|
||||
/// "error sending request for url (…)" wrapper.
|
||||
fn nd_err(e: reqwest::Error) -> String {
|
||||
let mut msg = e.to_string();
|
||||
let mut src: Option<&(dyn std::error::Error + 'static)> = std::error::Error::source(&e);
|
||||
while let Some(s) = src {
|
||||
msg.push_str(" | ");
|
||||
msg.push_str(&s.to_string());
|
||||
src = s.source();
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
/// Retry a request-building closure on transient transport errors
|
||||
/// (connect/timeout — includes ECONNRESET, TLS handshake EOF, DNS flakes).
|
||||
/// Three attempts with calm backoff: 0 → 300ms → 700ms (total worst case
|
||||
/// ~1s). Retrying too aggressively (5+ attempts, short backoff) can drive
|
||||
/// an already-stressed nginx upstream-probe into "offline" mode, which
|
||||
/// turns a transient glitch into a visible outage. Status-level failures
|
||||
/// (401/403/400 with body) return immediately — we don't retry logic
|
||||
/// errors.
|
||||
async fn nd_retry<F, Fut>(mut build_and_send: F) -> Result<reqwest::Response, String>
|
||||
where
|
||||
F: FnMut() -> Fut,
|
||||
Fut: std::future::Future<Output = Result<reqwest::Response, reqwest::Error>>,
|
||||
{
|
||||
const BACKOFFS_MS: [u64; 1] = [500];
|
||||
let mut last: Option<reqwest::Error> = None;
|
||||
for attempt in 0..=BACKOFFS_MS.len() {
|
||||
if attempt > 0 {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(BACKOFFS_MS[attempt - 1])).await;
|
||||
}
|
||||
match build_and_send().await {
|
||||
Ok(resp) => return Ok(resp),
|
||||
Err(e) => {
|
||||
if !e.is_connect() && !e.is_timeout() {
|
||||
return Err(nd_err(e));
|
||||
}
|
||||
last = Some(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(nd_err(last.expect("loop ran at least once")))
|
||||
}
|
||||
|
||||
/// Build a reqwest client for Navidrome's native REST endpoints. Plain
|
||||
/// `reqwest::Client::new()` defaults to HTTP/2 over ALPN with no User-Agent,
|
||||
/// which some reverse-proxies (strict nginx rules, Cloudflare Tunnel, CDN
|
||||
/// WAFs) abort mid-TLS-handshake. Pinning HTTP/1.1 and advertising a real
|
||||
/// User-Agent makes the handshake match what browsers do for the Subsonic
|
||||
/// endpoints, so `/auth/*` + `/api/*` go through the same path as `/rest/*`.
|
||||
///
|
||||
/// `pool_max_idle_per_host(0)` disables connection pooling. Keeping stale
|
||||
/// keep-alive connections in the pool caused intermittent "tls handshake
|
||||
/// eof" errors on the second call to an admin endpoint when a server or
|
||||
/// proxy had already closed the TCP connection between calls.
|
||||
fn nd_http_client() -> reqwest::Client {
|
||||
// TLS 1.2 only: rustls + nginx with TLS-1.3 session resumption caches
|
||||
// produces intermittent ECONNRESET mid-handshake when the upstream
|
||||
// starts churning keep-alive connections. Pinning TLS 1.2 matches what
|
||||
// the WebKit-side Subsonic calls end up negotiating most of the time
|
||||
// on these setups.
|
||||
reqwest::Client::builder()
|
||||
.user_agent(format!("Psysonic/{} (Tauri)", env!("CARGO_PKG_VERSION")))
|
||||
.http1_only()
|
||||
.pool_max_idle_per_host(0)
|
||||
.max_tls_version(reqwest::tls::Version::TLS_1_2)
|
||||
.build()
|
||||
.unwrap_or_else(|_| reqwest::Client::new())
|
||||
}
|
||||
|
||||
/// Log in to Navidrome's native REST API. Returns a Bearer token and whether the user is admin.
|
||||
#[tauri::command]
|
||||
async fn navidrome_login(
|
||||
@@ -317,17 +390,17 @@ async fn navidrome_login(
|
||||
username: String,
|
||||
password: String,
|
||||
) -> Result<NdLoginResult, String> {
|
||||
let client = reqwest::Client::new();
|
||||
let resp = client
|
||||
.post(format!("{}/auth/login", server_url))
|
||||
.json(&serde_json::json!({ "username": username, "password": password }))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let body = serde_json::json!({ "username": username, "password": password });
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.post(format!("{}/auth/login", server_url))
|
||||
.json(&body)
|
||||
.send()
|
||||
}).await?;
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Navidrome login failed: HTTP {}", resp.status()));
|
||||
}
|
||||
let data: serde_json::Value = resp.json().await.map_err(|e| e.to_string())?;
|
||||
let data: serde_json::Value = resp.json().await.map_err(nd_err)?;
|
||||
let token = data["token"].as_str().ok_or("no token in response")?.to_string();
|
||||
let user_id = data["id"].as_str().unwrap_or("").to_string();
|
||||
let is_admin = data["isAdmin"].as_bool().unwrap_or(false);
|
||||
@@ -340,16 +413,16 @@ async fn nd_list_users(
|
||||
server_url: String,
|
||||
token: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let resp = reqwest::Client::new()
|
||||
.get(format!("{}/api/user", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.get(format!("{}/api/user", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
}).await?;
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("HTTP {}", resp.status()));
|
||||
}
|
||||
resp.json::<serde_json::Value>().await.map_err(|e| e.to_string())
|
||||
resp.json::<serde_json::Value>().await.map_err(nd_err)
|
||||
}
|
||||
|
||||
/// POST `/api/user` — create a user.
|
||||
@@ -370,13 +443,13 @@ async fn nd_create_user(
|
||||
"password": password,
|
||||
"isAdmin": is_admin,
|
||||
});
|
||||
let resp = reqwest::Client::new()
|
||||
.post(format!("{}/api/user", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.post(format!("{}/api/user", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
}).await?;
|
||||
let status = resp.status();
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
if !status.is_success() {
|
||||
@@ -407,13 +480,13 @@ async fn nd_update_user(
|
||||
if !password.is_empty() {
|
||||
body["password"] = serde_json::Value::String(password);
|
||||
}
|
||||
let resp = reqwest::Client::new()
|
||||
.put(format!("{}/api/user/{}", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.put(format!("{}/api/user/{}", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
}).await?;
|
||||
let status = resp.status();
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
if !status.is_success() {
|
||||
@@ -429,12 +502,12 @@ async fn nd_delete_user(
|
||||
token: String,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
let resp = reqwest::Client::new()
|
||||
.delete(format!("{}/api/user/{}", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.delete(format!("{}/api/user/{}", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
}).await?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
@@ -449,16 +522,16 @@ async fn nd_list_libraries(
|
||||
server_url: String,
|
||||
token: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let resp = reqwest::Client::new()
|
||||
.get(format!("{}/api/library", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.get(format!("{}/api/library", server_url))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.send()
|
||||
}).await?;
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("HTTP {}", resp.status()));
|
||||
}
|
||||
resp.json::<serde_json::Value>().await.map_err(|e| e.to_string())
|
||||
resp.json::<serde_json::Value>().await.map_err(nd_err)
|
||||
}
|
||||
|
||||
/// PUT `/api/user/{id}/library` — assign libraries to a non-admin user.
|
||||
@@ -471,13 +544,13 @@ async fn nd_set_user_libraries(
|
||||
library_ids: Vec<i64>,
|
||||
) -> Result<(), String> {
|
||||
let body = serde_json::json!({ "libraryIds": library_ids });
|
||||
let resp = reqwest::Client::new()
|
||||
.put(format!("{}/api/user/{}/library", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let resp = nd_retry(|| {
|
||||
nd_http_client()
|
||||
.put(format!("{}/api/user/{}/library", server_url, id))
|
||||
.header("X-ND-Authorization", format!("Bearer {}", token))
|
||||
.json(&body)
|
||||
.send()
|
||||
}).await?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
|
||||
Reference in New Issue
Block a user