diff --git a/DESIGN_SYSTEM.md b/DESIGN_SYSTEM.md index d29d73b9..71b59f30 100644 --- a/DESIGN_SYSTEM.md +++ b/DESIGN_SYSTEM.md @@ -347,12 +347,18 @@ secondary line, if any, is `text-ui-2xs text-muted-foreground` in plain words (`Postgres · annapurna`), never a boxed chip; trailing count/timestamp `font-mono text-ui-2xs tabular-nums text-muted-foreground`. -**Sidebar search row** (top of a sidebar): frameless - no field border - with -a `Search size-3.5` icon at `left-2`, placeholder "Search", `text-ui-sm`, a -`bg-sidebar-accent/60` fill only while focused, and a `size-5` clear (×) -button at the right while it has text. Quiet `size-7` icon actions sit on the -same row or the row above. In code: `no-field-frame` on a bare input, or -`border-transparent! bg-transparent!` on the `Input` wrapper. +**Search fields** (sidebar, dialogs, pages, pickers): one component, +`SearchField.svelte`. An outlined box: `h-8 rounded-lg`, a 1px +`border-border/80` outline over a faint `bg-muted/30` fill, no shadow, a +`Search size-3.5` icon at `left-2.5`, and the `/` keycap at the right that +focuses it (a `size-5` clear (×) button replaces the keycap while it has text). +Focus moves the outline to `border-ring/70`. Do not hand-roll a search input; +pass `rank={null}` for a field `/` should not focus. +Where a field keeps its own keyboard handling or toolbar cluster, put +`search-box no-field-frame` on the input (icon positioned over it), or +`search-shell` on a wrapper row that holds icon, input and buttons. Both draw +the same outline. Filter rows at the top of a menu or popover list +(`data-slot="menu-search"`) stay frameless: the menu is already the box. **Prominent pick lists** (provider DB lists, account lists): ``` diff --git a/src-tauri/src/cloud_backup.rs b/src-tauri/src/cloud_backup.rs index b61437ef..344af799 100644 --- a/src-tauri/src/cloud_backup.rs +++ b/src-tauri/src/cloud_backup.rs @@ -251,6 +251,8 @@ async fn dump_and_upload( }; let result = async { + // Through the saved connection's SSH tunnel, held until the dump is done. + let (config, _tunnel) = crate::db::connection::tunnel_any(config).await?; let conn = open(config).await?; crate::db::backup::reset_cancel(); let rep = crate::db::backup::Reporter::silent(); diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 4783573e..019f20ec 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -417,7 +417,7 @@ use crate::db::{ SqlResult, SqliteConfig, TableInfo, TableRowCount, TableRows, TriggerInfo, SequenceInfo, ColumnStructureRow, TableColumnStructure, IncomingForeignKey, InsertRowResult, TunnelState, apply_table_changes, ApplyChangesResult, RowUpdate, - explain_pg, explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, + explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, }; use crate::db::connection::{require_conn, ClickhouseConfig, DuckdbConfig, MssqlConfig, MysqlConfig, OracleConfig, RedisConfig}; use crate::db::ActiveConnection; @@ -522,8 +522,8 @@ pub async fn test_clickhouse(config: ClickhouseConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_clickhouse_db(state: Db<'_>, config: ClickhouseConfig) -> Result<(), String> { - connect_clickhouse(state, config).await +pub async fn connect_clickhouse_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: ClickhouseConfig) -> Result<(), String> { + connect_clickhouse(state, tunnel_state, config).await } // ── Redis ───────────────────────────────────────────────────────────────────── @@ -534,8 +534,8 @@ pub async fn test_redis(config: RedisConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_redis_db(state: Db<'_>, config: RedisConfig) -> Result<(), String> { - connect_redis(state, config).await +pub async fn connect_redis_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: RedisConfig) -> Result<(), String> { + connect_redis(state, tunnel_state, config).await } /// Non-blocking keyspace iteration for the active Redis connection. Returns one @@ -675,12 +675,13 @@ pub async fn dynamodb_request( #[tauri::command] pub async fn test_couchdb(config: crate::db::couchdb::CouchConfig) -> Result<(), String> { + let (config, _tunnel) = crate::db::connection::resolve_couch_ssh(config).await?; crate::db::couchdb::ping(&config).await } #[tauri::command] -pub async fn connect_couchdb_db(state: Db<'_>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { - crate::db::connection::connect_couchdb(state, config).await +pub async fn connect_couchdb_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { + crate::db::connection::connect_couchdb(state, tunnel_state, config).await } /// One CouchDB HTTP call (path allowlisted) on the active connection. @@ -703,12 +704,13 @@ pub async fn couchdb_request( /// Connection test; answers which engine and version the server is. #[tauri::command] pub async fn test_elastic(config: crate::db::elastic::EsConfig) -> Result { + let (config, _tunnel) = crate::db::connection::resolve_elastic_ssh(config).await?; crate::db::elastic::ping(&config).await } #[tauri::command] -pub async fn connect_elastic_db(state: Db<'_>, config: crate::db::elastic::EsConfig) -> Result<(), String> { - crate::db::connection::connect_elastic(state, config).await +pub async fn connect_elastic_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::elastic::EsConfig) -> Result<(), String> { + crate::db::connection::connect_elastic(state, tunnel_state, config).await } /// One REST call (path allowlisted) on the active connection. @@ -746,12 +748,13 @@ pub async fn elastic_import( #[tauri::command] pub async fn test_mongo(config: crate::db::mongo::MongoConfig) -> Result<(), String> { + let (config, _tunnel) = crate::db::connection::resolve_mongo_ssh(config).await?; crate::db::mongo::connect(&config).await.map(|_| ()) } #[tauri::command] -pub async fn connect_mongo_db(state: Db<'_>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { - crate::db::connection::connect_mongo(state, config).await +pub async fn connect_mongo_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { + crate::db::connection::connect_mongo(state, tunnel_state, config).await } fn require_mongo(state: &Db<'_>) -> Result { @@ -942,12 +945,13 @@ pub async fn firestore_request( #[tauri::command] pub async fn test_cassandra(config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { + let (config, _tunnel) = crate::db::connection::resolve_cassandra_ssh(config).await?; crate::db::cassandra::connect(&config).await.map(|_| ()) } #[tauri::command] -pub async fn connect_cassandra_db(state: Db<'_>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { - crate::db::connection::connect_cassandra(state, config).await +pub async fn connect_cassandra_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { + crate::db::connection::connect_cassandra(state, tunnel_state, config).await } fn require_cassandra(state: &Db<'_>) -> Result { @@ -1018,8 +1022,8 @@ pub async fn test_mssql(config: MssqlConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_mssql_db(state: Db<'_>, config: MssqlConfig) -> Result<(), String> { - connect_mssql(state, config).await +pub async fn connect_mssql_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: MssqlConfig) -> Result<(), String> { + connect_mssql(state, tunnel_state, config).await } // ── Oracle ────────────────────────────────────────────────────────────────────── @@ -1030,8 +1034,8 @@ pub async fn test_oracle(config: OracleConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_oracle_db(state: Db<'_>, config: OracleConfig) -> Result<(), String> { - connect_oracle(state, config).await +pub async fn connect_oracle_db(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: OracleConfig) -> Result<(), String> { + connect_oracle(state, tunnel_state, config).await } /// The Oracle container the active connection is in (FREEPDB1, CDB$ROOT). @@ -1144,9 +1148,18 @@ pub async fn pg_explain_sql( state: Db<'_>, sql: String, analyze: Option, + // Names the run for `cancel_query`, like a console run's queryId. + query_id: Option, ) -> Result { match require_conn(&state)? { - ActiveConnection::Postgres(pool) => explain_pg(&pool, &sql, analyze.unwrap_or(true)).await, + ActiveConnection::Postgres(pool) => { + let (tx, rx) = tokio::sync::oneshot::channel::<()>(); + let key = query_id.unwrap_or_else(|| "default".to_string()); + crate::db::connection::register_cancel(&state, &key, tx); + let r = crate::db::explain::explain_pg_stoppable(&pool, &sql, analyze.unwrap_or(true), Some(rx)).await; + crate::db::connection::unregister_cancel(&state, &key); + r + } ActiveConnection::Mysql(pool) => explain_mysql(&pool, &sql).await, ActiveConnection::Sqlite(pool) => explain_sqlite(&pool, &sql).await, ActiveConnection::Duckdb(h) => { diff --git a/src-tauri/src/db/admin.rs b/src-tauri/src/db/admin.rs index 5dc88b74..0bf6a029 100644 --- a/src-tauri/src/db/admin.rs +++ b/src-tauri/src/db/admin.rs @@ -877,7 +877,7 @@ mod admin_live { #[ignore] async fn clickhouse_copies_and_drops() { let port = std::env::var("STROKE_CH_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(58123); - let cfg = ClickhouseConfig { + let cfg = ClickhouseConfig { ssh: None, via: None, name: "test".into(), host: "127.0.0.1".into(), port, @@ -928,7 +928,7 @@ mod admin_live { #[tokio::test] #[ignore] async fn mssql_copies_and_drops() { - let cfg = super::super::connection::MssqlConfig { + let cfg = super::super::connection::MssqlConfig { ssh: None, via: None, name: "test".into(), host: "127.0.0.1".into(), port: 51433, diff --git a/src-tauri/src/db/cassandra.rs b/src-tauri/src/db/cassandra.rs index 089718eb..08c2c65d 100644 --- a/src-tauri/src/db/cassandra.rs +++ b/src-tauri/src/db/cassandra.rs @@ -31,6 +31,10 @@ pub struct CassandraConfig { /// Default keyspace for unqualified table names in the console. #[serde(default)] pub keyspace: Option, + /// Reach the server through an SSH tunnel (connection::tunnel_to and friends). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + } pub type CassandraHandle = Arc; @@ -535,7 +539,7 @@ mod tests { #[test] fn contact_points_default_port() { - let cfg = CassandraConfig { name: "t".into(), hosts: "a, b:9142 ,,10.0.0.1".into(), user: None, password: None, keyspace: None }; + let cfg = CassandraConfig { ssh: None, name: "t".into(), hosts: "a, b:9142 ,,10.0.0.1".into(), user: None, password: None, keyspace: None }; assert_eq!(contact_points(&cfg), ["a:9042", "b:9142", "10.0.0.1:9042"]); } @@ -553,7 +557,7 @@ mod tests { #[tokio::test] #[ignore] async fn cassandra_live() { - let cfg = CassandraConfig { name: "t".into(), hosts: "127.0.0.1".into(), user: None, password: None, keyspace: None }; + let cfg = CassandraConfig { ssh: None, name: "t".into(), hosts: "127.0.0.1".into(), user: None, password: None, keyspace: None }; let s = connect(&cfg).await.unwrap(); let tables = list_tables(&s).await.unwrap(); assert!(tables.iter().any(|t| t.starts_with("shop.")), "{tables:?}"); diff --git a/src-tauri/src/db/clickhouse.rs b/src-tauri/src/db/clickhouse.rs index 537581ab..52256e52 100644 --- a/src-tauri/src/db/clickhouse.rs +++ b/src-tauri/src/db/clickhouse.rs @@ -12,6 +12,33 @@ use std::time::Instant; static CH_CLIENT: OnceLock = OnceLock::new(); +/// The HTTP client for a config. Through an SSH tunnel it resolves the +/// server's name to the tunnel's local end: the URL (and so TLS) keeps the real +/// name, and only the TCP connection goes to the tunnel. One per tunnel, kept. +fn client_for(config: &ClickhouseConfig) -> reqwest::Client { + static VIA: std::sync::LazyLock>> = + std::sync::LazyLock::new(Default::default); + let Some(via) = config.via else { return client().clone() }; + let key = (config.host.clone(), via); + let mut map = VIA.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(c) = map.get(&key) { + return c.clone(); + } + let c = builder().resolve(&config.host, via).build().unwrap_or_else(|_| client().clone()); + map.insert(key, c.clone()); + c +} + +fn builder() -> reqwest::ClientBuilder { + reqwest::Client::builder() + .tcp_keepalive(std::time::Duration::from_secs(60)) + .pool_max_idle_per_host(10) + .pool_idle_timeout(std::time::Duration::from_secs(90)) + // Bounded, but generously: this carries queries, not just metadata. + .connect_timeout(std::time::Duration::from_secs(10)) + .timeout(std::time::Duration::from_secs(120)) +} + fn client() -> &'static reqwest::Client { CH_CLIENT.get_or_init(|| { reqwest::Client::builder() @@ -95,11 +122,209 @@ pub async fn query_readonly(config: &ClickhouseConfig, sql: &str) -> Result, +) -> Result { + let id = fresh_query_id(); + let run = query_in_full(config, &config.database, sql, false, Some(&id)); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = async { let _ = cancel.await; } => { + // Users may always kill their own queries. SYNC waits for it to end. + let kill = format!("KILL QUERY WHERE query_id = '{id}' SYNC"); + let _ = tokio::time::timeout(std::time::Duration::from_secs(10), query_in_full(config, &config.database, &kill, false, None)).await; + Err(super::query::QUERY_CANCELLED.into()) + } + } +} + +fn fresh_query_id() -> String { + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + format!( + "stroke-{:x}-{:x}-{:x}", + std::process::id(), + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(0), + SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + ) +} + +/// Whether `stream_into` takes this statement (it returns rows). +pub(crate) fn reads(sql: &str) -> bool { + is_read_query(sql.trim().trim_end_matches(';')) +} + +/// A read, streamed into `sink` row by row as the server sends it. +/// +/// `FORMAT JSONCompact` is one JSON document, so a 2 GB result had to arrive +/// whole (and be parsed whole) before the grid saw a row. The EachRow format +/// sends one JSON array per line, after a line of names and one of types, so +/// rows reach the grid (or the result store) while the server is still +/// producing them and memory stays flat. Stop kills the query by id. +pub async fn stream_into( + config: &ClickhouseConfig, + sql: &str, + sink: &mut super::query::RowSink, + stop: &super::stop::Stop, +) -> Result { + let id = fresh_query_id(); + let run = stream_rows(config, sql, sink, &id); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = stop.wait() => { + drop(run); + let kill = format!("KILL QUERY WHERE query_id = '{id}' SYNC"); + let _ = tokio::time::timeout(std::time::Duration::from_secs(10), query_in_full(config, &config.database, &kill, false, None)).await; + Err(super::query::QUERY_CANCELLED.into()) + } + } +} + +async fn stream_rows( + config: &ClickhouseConfig, + sql: &str, + sink: &mut super::query::RowSink, + query_id: &str, +) -> Result { + use futures::StreamExt; + let trimmed = sql.trim().trim_end_matches(';'); + let url = format!( + "{}/?database={}&query_id={}", + config.base_url(), + urlencoding::encode(&config.database), + urlencoding::encode(query_id) + ); + let t0 = Instant::now(); + let res = client_for(config) + .post(&url) + .header("X-ClickHouse-User", &config.user) + .header("X-ClickHouse-Key", &config.password) + .header("Content-Type", "text/plain; charset=utf-8") + .body(format!("{trimmed}\nFORMAT JSONCompactEachRowWithNamesAndTypes")) + .send() + .await + .map_err(|e| format!("ClickHouse request failed: {e}"))?; + if !res.status().is_success() { + let msg = res.text().await.unwrap_or_default(); + return Err(format!("ClickHouse error: {}", msg.trim().chars().take(500).collect::())); + } + + let mut lines = EachRowLines::default(); + let mut body = res.bytes_stream(); + while let Some(chunk) = body.next().await { + let chunk = chunk.map_err(|e| format!("ClickHouse response was cut off: {e}"))?; + for line in lines.feed(&chunk) { + lines.take(&line, sink)?; + } + } + if let Some(rest) = lines.rest() { + lines.take(&rest, sink)?; + } + if lines.rows > 0 { + sink.finish()?; + } + Ok(SqlResult { + columns: lines.columns(), + rows: vec![], + row_count: Some(lines.rows as i64), + message: None, + query_ms: t0.elapsed().as_millis() as u64, + sql: sql.to_string(), + }) +} + +/// The JSONCompactEachRowWithNamesAndTypes body, a line at a time: names, +/// types, then one JSON array per row. A line that is not JSON is the +/// server's own error text, which it writes into the body when a query fails +/// after the first rows went out. +#[derive(Default)] +struct EachRowLines { + pending: Vec, + names: Option>, + types: Option>, + rows: usize, +} + +impl EachRowLines { + fn feed(&mut self, chunk: &[u8]) -> Vec> { + self.pending.extend_from_slice(chunk); + let mut out = Vec::new(); + let mut start = 0; + while let Some(i) = self.pending[start..].iter().position(|b| *b == b'\n') { + out.push(self.pending[start..start + i].to_vec()); + start += i + 1; + } + self.pending.drain(..start); + out + } + + fn rest(&mut self) -> Option> { + let rest = std::mem::take(&mut self.pending); + (!rest.iter().all(u8::is_ascii_whitespace)).then_some(rest) + } + + fn columns(&self) -> Vec { + let names = self.names.clone().unwrap_or_default(); + let types = self.types.clone().unwrap_or_default(); + names + .into_iter() + .enumerate() + .map(|(i, n)| { + let ty = types.get(i).cloned().unwrap_or_default(); + let mut c = ColumnInfo::new(n, ch_type_label(&ty)); + c.nullable = ty.starts_with("Nullable("); + c + }) + .collect() + } + + fn take(&mut self, line: &[u8], sink: &mut super::query::RowSink) -> Result<(), String> { + if line.iter().all(u8::is_ascii_whitespace) { + return Ok(()); + } + let parsed: Result, _> = serde_json::from_slice(line); + let Ok(values) = parsed else { + let text = String::from_utf8_lossy(line); + return Err(format!("ClickHouse error: {}", text.trim().chars().take(500).collect::())); + }; + let strings = || values.iter().map(|v| v.as_str().unwrap_or_default().to_string()).collect::>(); + if self.names.is_none() { + self.names = Some(strings()); + return Ok(()); + } + if self.types.is_none() { + self.types = Some(strings()); + return Ok(()); + } + if self.rows == 0 { + sink.start(self.columns())?; + } + self.rows += 1; + sink.push(values.into_iter().map(|v| super::sql_util::cap_json_value("text", v)).collect()) + } +} + async fn query_in_with( config: &ClickhouseConfig, database: &str, sql: &str, readonly: bool, +) -> Result { + query_in_full(config, database, sql, readonly, None).await +} + +async fn query_in_full( + config: &ClickhouseConfig, + database: &str, + sql: &str, + readonly: bool, + query_id: Option<&str>, ) -> Result { let trimmed = sql.trim().trim_end_matches(';'); let read = is_read_query(trimmed); @@ -110,13 +335,14 @@ async fn query_in_with( }; let url = format!( - "{}/?database={}{}", + "{}/?database={}{}{}", config.base_url(), urlencoding::encode(database), - if readonly { "&readonly=1" } else { "" } + if readonly { "&readonly=1" } else { "" }, + query_id.map(|id| format!("&query_id={}", urlencoding::encode(id))).unwrap_or_default() ); let t0 = Instant::now(); - let res = client() + let res = client_for(config) .post(&url) .header("X-ClickHouse-User", &config.user) .header("X-ClickHouse-Key", &config.password) @@ -559,3 +785,28 @@ mod tree_tests { assert_eq!(idx[2].index_type, "bloom_filter"); } } + +#[cfg(test)] +mod via_tests { + // As redis::via_tests: an unresolvable name, reached through `via`, and + // the server sees that name in the Host header (and so in TLS SNI). + // STROKE_TEST_FAKE_HTTP=127.0.0.1:18123 cargo test --lib clickhouse::via_tests + #[tokio::test] + async fn dials_the_tunnel_and_keeps_the_name() { + let Ok(addr) = std::env::var("STROKE_TEST_FAKE_HTTP") else { return }; + let via: std::net::SocketAddr = addr.parse().unwrap(); + let cfg = crate::db::connection::ClickhouseConfig { + name: "t".into(), + host: "ch.stroke.test".into(), + port: 8443, + database: "default".into(), + user: "default".into(), + password: String::new(), + secure: false, + ssh: None, + via: Some(via), + }; + let r = super::query(&cfg, "SELECT 1").await.expect("query through the tunnel address"); + assert_eq!(r.rows[0][0], serde_json::json!(format!("ch.stroke.test:{}", via.port()))); + } +} diff --git a/src-tauri/src/db/connection.rs b/src-tauri/src/db/connection.rs index 2ce51837..445b35db 100644 --- a/src-tauri/src/db/connection.rs +++ b/src-tauri/src/db/connection.rs @@ -34,6 +34,11 @@ pub struct PgConfig { pub ssl_root_cert: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub ssh: Option, + /// Where to dial instead of `host`, through an SSH tunnel (with `port` + /// set to the tunnel's). `host` stays the server's real name so TLS checks + /// the certificate against it. Set by the tunnel resolver, never saved. + #[serde(skip)] + pub via: Option, /// Session time zone applied on every pooled connection (IANA name, e.g. /// "America/New_York"). `None`/"SYSTEM" leaves the server default in place. #[serde(default, skip_serializing_if = "Option::is_none")] @@ -115,6 +120,11 @@ pub struct MysqlConfig { pub ssl_root_cert: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub ssh: Option, + /// Where to dial instead of `host`, through an SSH tunnel (with `port` + /// set to the tunnel's). `host` stays the server's real name so TLS checks + /// the certificate against it. Set by the tunnel resolver, never saved. + #[serde(skip)] + pub via: Option, /// Session time zone applied on connect (`SET time_zone`). `None`/"SYSTEM" /// leaves the server default in place. #[serde(default, skip_serializing_if = "Option::is_none")] @@ -187,13 +197,28 @@ pub struct ClickhouseConfig { /// Use HTTPS (port 8443) instead of plain HTTP (port 8123). #[serde(default)] pub secure: bool, + /// Reach the server through an SSH tunnel (see `tunnel_to`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + /// Where to dial instead of `host:port`: the tunnel's local end. `host` + /// stays the server's real name, so TLS checks the certificate against + /// it rather than against 127.0.0.1. Set by `tunnel_via`, never saved. + #[serde(skip)] + pub via: Option, } impl ClickhouseConfig { /// Base HTTP(S) URL for the ClickHouse HTTP interface, no trailing slash. pub fn base_url(&self) -> String { let scheme = if self.secure { "https" } else { "http" }; - format!("{scheme}://{}:{}", self.host, self.port) + match self.via { + // Through a tunnel: the real name (TLS checks it, and the HTTP + // client resolves it to the tunnel), at the tunnel's port. An IP + // address has no name to keep, so it is replaced outright. + Some(via) if self.host.parse::().is_ok() => format!("{scheme}://{via}"), + Some(via) => format!("{scheme}://{}:{}", self.host, via.port()), + None => format!("{scheme}://{}:{}", self.host, self.port), + } } } @@ -239,6 +264,14 @@ pub struct RedisConfig { /// Use TLS (rediss://) instead of plain TCP (redis://). #[serde(default)] pub tls: bool, + /// Reach the server through an SSH tunnel (see `tunnel_to`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + /// Where to dial instead of `host:port`: the tunnel's local end. `host` + /// stays the server's real name, so TLS checks the certificate against + /// it rather than against 127.0.0.1. Set by `tunnel_via`, never saved. + #[serde(skip)] + pub via: Option, } // ── DuckDB ──────────────────────────────────────────────────────────────────── @@ -273,6 +306,14 @@ pub struct MssqlConfig { /// Skip TLS certificate validation (self-signed dev servers). #[serde(default)] pub trust_cert: bool, + /// Reach the server through an SSH tunnel (see `tunnel_to`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + /// Where to dial instead of `host:port`: the tunnel's local end. `host` + /// stays the server's real name, so TLS checks the certificate against + /// it rather than against 127.0.0.1. Set by `tunnel_via`, never saved. + #[serde(skip)] + pub via: Option, } /// tiberius `Client` is async and not clonable; share it behind an async mutex. @@ -306,6 +347,9 @@ pub struct OracleConfig { /// finds by itself. #[serde(default)] pub client_lib_dir: String, + /// Reach the server through an SSH tunnel (see `tunnel_to`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, } fn default_oracle_port() -> u16 { @@ -544,6 +588,8 @@ pub fn update_d1_token(token: &str) { } fn set_conn(state: &Db<'_>, conn: Option) -> Result<(), String> { + // Open transactions belong to the connection being replaced. + super::tx::connection_changed(); *state.conn.lock().map_err(|e| e.to_string())? = conn; // A fresh pool: the page names its key once it knows it (set_active_conn_key). *state.active_key.lock().map_err(|e| e.to_string())? = None; @@ -551,6 +597,10 @@ fn set_conn(state: &Db<'_>, conn: Option) -> Result<(), String } async fn close_existing(state: &Db<'_>) { + super::tx::connection_changed(); + // The tunnel under the closing connection goes with it. Connects that + // tunnel set their new one after this. + super::ssh_tunnel::drop_active(); let old = state.conn.lock().ok().and_then(|mut g| g.take()); let Some(conn) = old else { return }; close_in_background(conn); @@ -1267,6 +1317,10 @@ pub(crate) async fn open_pg(config: &PgConfig) -> Result { .connection_url() .parse() .map_err(|e| format!("Connection failed: {e}"))?; + let opts = match config.via { + Some(via) => opts.hostaddr(&via.ip().to_string()), + None => opts, + }; // The hostname is deliberately kept rather than swapped for the probed IP: // the driver needs it for TLS SNI (managed Postgres behind a proxy routes on // it) and the OS resolver caches the lookup anyway. @@ -1334,7 +1388,8 @@ connect_pg_pool( } }; - connect_racing_probe(&config.host, config.port, Some(&tcp_ok), connect).await + let (probe_host, probe_port) = probe_target(&config.host, config.port, config.via); + connect_racing_probe(&probe_host, probe_port, Some(&tcp_ok), connect).await } pub async fn test_connection(config: PgConfig) -> Result<(), String> { @@ -1353,7 +1408,6 @@ pub async fn connect( tunnel_state: State<'_, TunnelState>, config: PgConfig, ) -> Result<(), String> { - tunnel_state.clear(); let (effective, tunnel) = resolve_pg_ssh(config).await?; let pool = open_pg(&effective).await?; close_existing(&state).await; @@ -1365,6 +1419,154 @@ pub async fn connect( Ok(()) } +/// Open `ssh`, when set, to `host:port`, and say where to dial instead: the +/// tunnel's local end. The tunnel must outlive every connection made through it. +pub(crate) async fn tunnel_to( + ssh: &Option, + host: &str, + port: u16, +) -> Result<(String, u16, Option), String> { + let Some(ssh) = ssh else { return Ok((host.to_string(), port, None)) }; + let tunnel = SshTunnel::establish(ssh, host, port).await?; + Ok(("127.0.0.1".into(), tunnel.local_port, Some(tunnel))) +} + +/// Open `ssh`, when set, and return the address its local end listens on, for +/// a driver that dials one address while TLS checks another name. +pub(crate) async fn tunnel_via( + ssh: &Option, + host: &str, + port: u16, +) -> Result<(Option, Option), String> { + let Some(ssh) = ssh else { return Ok((None, None)) }; + let tunnel = SshTunnel::establish(ssh, host, port).await?; + Ok((Some(std::net::SocketAddr::from(([127, 0, 0, 1], tunnel.local_port))), Some(tunnel))) +} + +/// The address a probe should race: the tunnel's local end when there is one. +pub(crate) fn probe_target(host: &str, port: u16, via: Option) -> (String, u16) { + match via { + Some(a) => (a.ip().to_string(), a.port()), + None => (host.to_string(), port), + } +} + +pub(crate) async fn resolve_clickhouse_ssh(mut c: ClickhouseConfig) -> Result<(ClickhouseConfig, Option), String> { + let (via, t) = tunnel_via(&c.ssh, &c.host, c.port).await?; + c.via = via; + Ok((c, t)) +} + +pub(crate) async fn resolve_redis_ssh(mut c: RedisConfig) -> Result<(RedisConfig, Option), String> { + let (via, t) = tunnel_via(&c.ssh, &c.host, c.port).await?; + c.via = via; + Ok((c, t)) +} + +pub(crate) async fn resolve_mssql_ssh(mut c: MssqlConfig) -> Result<(MssqlConfig, Option), String> { + let (via, t) = tunnel_via(&c.ssh, &c.host, c.port).await?; + c.via = via; + Ok((c, t)) +} + +pub(crate) async fn resolve_oracle_ssh(mut c: OracleConfig) -> Result<(OracleConfig, Option), String> { + if c.ssh.is_some() && !c.connect_string.trim().is_empty() { + return Err("An SSH tunnel needs the host and port fields, not a connect string: the tunnel has to know where to forward to.".into()); + } + let (host, port, t) = tunnel_to(&c.ssh, &c.host, c.port).await?; + (c.host, c.port) = (host, port); + Ok((c, t)) +} + +pub(crate) async fn resolve_mongo_ssh(mut c: crate::db::mongo::MongoConfig) -> Result<(crate::db::mongo::MongoConfig, Option), String> { + let Some(ssh) = c.ssh.clone() else { return Ok((c, None)) }; + // Refuse what the tunnel can't carry before opening it. + crate::db::ssh_tunnel::mongo_uri_via(&c.uri, 0)?; + let (host, port) = crate::db::ssh_tunnel::mongo_first_host(&c.uri).ok_or("No host in the connection string")?; + let tunnel = SshTunnel::establish(&ssh, &host, port).await?; + c.uri = crate::db::ssh_tunnel::mongo_uri_via(&c.uri, tunnel.local_port)?; + Ok((c, Some(tunnel))) +} + +pub(crate) async fn resolve_cassandra_ssh(mut c: crate::db::cassandra::CassandraConfig) -> Result<(crate::db::cassandra::CassandraConfig, Option), String> { + let Some(ssh) = c.ssh.clone() else { return Ok((c, None)) }; + // One contact point is enough: the driver reaches every node through it. + let first = c.hosts.split(',').map(str::trim).find(|h| !h.is_empty()).ok_or("Enter at least one host")?.to_string(); + let (host, port) = match first.rsplit_once(':') { + Some((h, p)) if p.parse::().is_ok() => (h.trim_matches(['[', ']']).to_string(), p.parse::().unwrap_or(9042)), + _ => (first.clone(), 9042), + }; + let tunnel = SshTunnel::establish(&ssh, &host, port).await?; + c.hosts = format!("127.0.0.1:{}", tunnel.local_port); + Ok((c, Some(tunnel))) +} + +pub(crate) async fn resolve_couch_ssh(mut c: crate::db::couchdb::CouchConfig) -> Result<(crate::db::couchdb::CouchConfig, Option), String> { + let Some(ssh) = c.ssh.clone() else { return Ok((c, None)) }; + let (host, port) = crate::db::ssh_tunnel::url_target(&c.url)?; + let (via, t) = tunnel_via(&Some(ssh), &host, port).await?; + c.via = via; + Ok((c, t)) +} + +pub(crate) async fn resolve_elastic_ssh(mut c: crate::db::elastic::EsConfig) -> Result<(crate::db::elastic::EsConfig, Option), String> { + let Some(ssh) = c.ssh.clone() else { return Ok((c, None)) }; + let (host, port) = crate::db::ssh_tunnel::url_target(&c.url)?; + let (via, t) = tunnel_via(&Some(ssh), &host, port).await?; + c.via = via; + Ok((c, t)) +} + +/// Open the SSH tunnel a saved connection asks for, whatever its engine, and +/// return the config pointed through it. Hold the tunnel for as long as +/// anything opened from the config is in use: dropping it closes ssh. +/// +/// For the paths that open a connection of their own beside the open one +/// (Data Diff, schema compare, scheduled backups). They dialed the server +/// directly, so a database only reachable through a bastion failed there +/// while it worked everywhere else. +pub(crate) async fn tunnel_any(config: AnyConnectionConfig) -> Result<(AnyConnectionConfig, Option), String> { + Ok(match config { + AnyConnectionConfig::Postgres(c) => { + let (c, t) = resolve_pg_ssh(c).await?; + (AnyConnectionConfig::Postgres(c), t) + } + AnyConnectionConfig::Mysql(c) => { + let (c, t) = resolve_mysql_ssh(c).await?; + (AnyConnectionConfig::Mysql(c), t) + } + AnyConnectionConfig::Mssql(c) => { + let (c, t) = resolve_mssql_ssh(c).await?; + (AnyConnectionConfig::Mssql(c), t) + } + AnyConnectionConfig::Oracle(c) => { + let (c, t) = resolve_oracle_ssh(c).await?; + (AnyConnectionConfig::Oracle(c), t) + } + AnyConnectionConfig::Clickhouse(c) => { + let (c, t) = resolve_clickhouse_ssh(c).await?; + (AnyConnectionConfig::Clickhouse(c), t) + } + AnyConnectionConfig::Redis(c) => { + let (c, t) = resolve_redis_ssh(c).await?; + (AnyConnectionConfig::Redis(c), t) + } + AnyConnectionConfig::Mongodb(c) => { + let (c, t) = resolve_mongo_ssh(c).await?; + (AnyConnectionConfig::Mongodb(c), t) + } + AnyConnectionConfig::Couchdb(c) => { + let (c, t) = resolve_couch_ssh(c).await?; + (AnyConnectionConfig::Couchdb(c), t) + } + AnyConnectionConfig::Elasticsearch(c) => { + let (c, t) = resolve_elastic_ssh(c).await?; + (AnyConnectionConfig::Elasticsearch(c), t) + } + other => (other, None), + }) +} + /// Establish an SSH tunnel if `config.ssh` is set, return a direct config pointing /// at the local forwarded port. The tunnel's lifetime must outlive the connection. pub(crate) async fn resolve_pg_ssh(config: PgConfig) -> Result<(PgConfig, Option), String> { @@ -1372,7 +1574,9 @@ pub(crate) async fn resolve_pg_ssh(config: PgConfig) -> Result<(PgConfig, Option let tunnel = SshTunnel::establish(ssh_cfg, &config.host, config.port).await?; let local_port = tunnel.local_port; let mut direct = config.clone(); - direct.host = "127.0.0.1".to_string(); + // Dial the tunnel, keep the name: TLS (verify-full, VERIFY_IDENTITY) + // checks the certificate against the real host, not 127.0.0.1. + direct.via = Some(std::net::SocketAddr::from(([127, 0, 0, 1], local_port))); direct.port = local_port; direct.ssh = None; Ok((direct, Some(tunnel))) @@ -1440,6 +1644,10 @@ pub(crate) async fn open_mysql(config: &MysqlConfig) -> Result opts.hostaddr(&via.ip().to_string()), + None => opts, + }; let opts = opts.log_slow_statements(LevelFilter::Debug, Duration::from_secs(5)); // Optional session time zone (Settings → Database), applied per connection @@ -1507,9 +1715,10 @@ pub(crate) async fn open_mysql(config: &MysqlConfig) -> Result(pool) }; + let (probe_host, probe_port) = probe_target(&config.host, config.port, config.via); connect_racing_probe( - &config.host, - config.port, + &probe_host, + probe_port, None, async { connect.await.map_err(|e| format!("Connection failed: {e}")) }, ) @@ -1532,7 +1741,6 @@ pub async fn connect_mysql( tunnel_state: State<'_, TunnelState>, config: MysqlConfig, ) -> Result<(), String> { - tunnel_state.clear(); let (effective, tunnel) = resolve_mysql_ssh(config).await?; let pool = open_mysql(&effective).await?; close_existing(&state).await; @@ -1546,7 +1754,9 @@ pub(crate) async fn resolve_mysql_ssh(config: MysqlConfig) -> Result<(MysqlConfi let tunnel = SshTunnel::establish(ssh_cfg, &config.host, config.port).await?; let local_port = tunnel.local_port; let mut direct = config.clone(); - direct.host = "127.0.0.1".to_string(); + // Dial the tunnel, keep the name: TLS (verify-full, VERIFY_IDENTITY) + // checks the certificate against the real host, not 127.0.0.1. + direct.via = Some(std::net::SocketAddr::from(([127, 0, 0, 1], local_port))); direct.port = local_port; direct.ssh = None; Ok((direct, Some(tunnel))) @@ -1585,7 +1795,13 @@ pub async fn connect_libsql(state: Db<'_>, config: LibSqlConfig) -> Result<(), S // ── ClickHouse connect / test ───────────────────────────────────────────────── pub async fn test_clickhouse_connection(config: ClickhouseConfig) -> Result<(), String> { - let (host, port) = (config.host.clone(), config.port); + let (config, _tunnel) = resolve_clickhouse_ssh(config).await?; + probe_clickhouse(config).await +} + +/// `config` already points where to dial (through its tunnel, if any). +async fn probe_clickhouse(config: ClickhouseConfig) -> Result<(), String> { + let (host, port) = probe_target(&config.host, config.port, config.via); connect_racing_probe(&host, port, None, async move { crate::db::clickhouse::query(&config, "SELECT 1").await?; Ok(()) @@ -1593,11 +1809,14 @@ pub async fn test_clickhouse_connection(config: ClickhouseConfig) -> Result<(), .await } -pub async fn connect_clickhouse(state: Db<'_>, config: ClickhouseConfig) -> Result<(), String> { +pub async fn connect_clickhouse(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: ClickhouseConfig) -> Result<(), String> { + let (config, tunnel) = resolve_clickhouse_ssh(config).await?; // Validate credentials/reachability before storing. - test_clickhouse_connection(config.clone()).await?; + probe_clickhouse(config.clone()).await?; close_existing(&state).await; - set_conn(&state, Some(ActiveConnection::Clickhouse(config))) + set_conn(&state, Some(ActiveConnection::Clickhouse(config)))?; + tunnel_state.set(tunnel); + Ok(()) } // ── PostHog connect / test ──────────────────────────────────────────────────── @@ -1621,14 +1840,22 @@ pub async fn connect_dynamodb(state: Db<'_>, config: crate::db::dynamodb::Dynamo // ── Document engines connect ────────────────────────────────────────────────── -pub async fn connect_cassandra(state: Db<'_>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { +pub async fn connect_cassandra(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { + let (config, tunnel) = resolve_cassandra_ssh(config).await?; let session = crate::db::cassandra::connect(&config).await?; - set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Cassandra(session)))) + close_existing(&state).await; + set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Cassandra(session))))?; + tunnel_state.set(tunnel); + Ok(()) } -pub async fn connect_mongo(state: Db<'_>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { +pub async fn connect_mongo(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { + let (config, tunnel) = resolve_mongo_ssh(config).await?; let handle = crate::db::mongo::connect(&config).await?; - set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Mongo(handle)))) + close_existing(&state).await; + set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Mongo(handle))))?; + tunnel_state.set(tunnel); + Ok(()) } pub async fn connect_firestore(state: Db<'_>, config: crate::db::firestore::FirestoreConfig) -> Result<(), String> { @@ -1636,28 +1863,45 @@ pub async fn connect_firestore(state: Db<'_>, config: crate::db::firestore::Fire set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Firestore(config)))) } -pub async fn connect_couchdb(state: Db<'_>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { +pub async fn connect_couchdb(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { + let (config, tunnel) = resolve_couch_ssh(config).await?; crate::db::couchdb::ping(&config).await?; - set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Couch(config)))) + close_existing(&state).await; + set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Couch(config))))?; + tunnel_state.set(tunnel); + Ok(()) } -pub async fn connect_elastic(state: Db<'_>, config: crate::db::elastic::EsConfig) -> Result<(), String> { +pub async fn connect_elastic(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: crate::db::elastic::EsConfig) -> Result<(), String> { + let (config, tunnel) = resolve_elastic_ssh(config).await?; let config = crate::db::elastic::connect(config).await?; - set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(config)))) + close_existing(&state).await; + set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(config))))?; + tunnel_state.set(tunnel); + Ok(()) } // ── Redis connect / test ────────────────────────────────────────────────────── pub async fn test_redis_connection(config: RedisConfig) -> Result<(), String> { - let (host, port) = (config.host.clone(), config.port); - connect_racing_probe(&host, port, None, crate::db::redis::ping(&config)).await + let (config, _tunnel) = resolve_redis_ssh(config).await?; + probe_redis(&config).await } -pub async fn connect_redis(state: Db<'_>, config: RedisConfig) -> Result<(), String> { +/// `config` already points where to dial (through its tunnel, if any). +async fn probe_redis(config: &RedisConfig) -> Result<(), String> { + let (host, port) = probe_target(&config.host, config.port, config.via); + connect_racing_probe(&host, port, None, crate::db::redis::ping(config)).await +} + +pub async fn connect_redis(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: RedisConfig) -> Result<(), String> { + let (config, tunnel) = resolve_redis_ssh(config).await?; // Validate credentials/reachability before storing. - test_redis_connection(config.clone()).await?; + probe_redis(&config).await?; close_existing(&state).await; - set_conn(&state, Some(ActiveConnection::Redis(config))) + set_conn(&state, Some(ActiveConnection::Redis(config)))?; + tunnel_state.set(tunnel); + Ok(()) } // ── DuckDB connect / test ────────────────────────────────────────────────────── @@ -1699,20 +1943,27 @@ pub async fn connect_duckdb(state: Db<'_>, config: DuckdbConfig) -> Result<(), S // ── MS SQL Server connect / test ──────────────────────────────────────────────── pub(crate) async fn open_mssql(config: &MssqlConfig) -> Result { - let client = - connect_racing_probe(&config.host, config.port, None, crate::db::mssql::connect(config)).await?; - Ok(Arc::new(tokio::sync::Mutex::new(client))) + let (host, port) = probe_target(&config.host, config.port, config.via); + let client = connect_racing_probe(&host, port, None, crate::db::mssql::connect(config)).await?; + let handle = Arc::new(tokio::sync::Mutex::new(client)); + // So Stop can find this session (see mssql::cancel). + crate::db::mssql::remember(&handle, config).await; + Ok(handle) } pub async fn test_mssql_connection(config: MssqlConfig) -> Result<(), String> { + let (config, _tunnel) = resolve_mssql_ssh(config).await?; let handle = open_mssql(&config).await?; crate::db::mssql::ping(&handle).await } -pub async fn connect_mssql(state: Db<'_>, config: MssqlConfig) -> Result<(), String> { +pub async fn connect_mssql(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: MssqlConfig) -> Result<(), String> { + let (config, tunnel) = resolve_mssql_ssh(config).await?; let handle = open_mssql(&config).await?; close_existing(&state).await; - set_conn(&state, Some(ActiveConnection::Mssql(handle))) + set_conn(&state, Some(ActiveConnection::Mssql(handle)))?; + tunnel_state.set(tunnel); + Ok(()) } // ── Oracle connect / test ─────────────────────────────────────────────────────── @@ -1729,14 +1980,18 @@ pub(crate) async fn open_oracle(config: &OracleConfig) -> Result Result<(), String> { + let (config, _tunnel) = resolve_oracle_ssh(config).await?; let handle = open_oracle(&config).await?; crate::db::oracle::ping(&handle).await } -pub async fn connect_oracle(state: Db<'_>, config: OracleConfig) -> Result<(), String> { +pub async fn connect_oracle(state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: OracleConfig) -> Result<(), String> { + let (config, tunnel) = resolve_oracle_ssh(config).await?; let handle = open_oracle(&config).await?; close_existing(&state).await; - set_conn(&state, Some(ActiveConnection::Oracle(handle))) + set_conn(&state, Some(ActiveConnection::Oracle(handle)))?; + tunnel_state.set(tunnel); + Ok(()) } // ── Disconnect ──────────────────────────────────────────────────────────────── @@ -1765,6 +2020,7 @@ mod tests { fn pg(ssl: bool, mode: Option<&str>, ca: Option<&str>) -> PgConfig { PgConfig { + via: None, name: "t".into(), host: "db.example.com".into(), port: 5432, @@ -1779,6 +2035,48 @@ mod tests { } } + // Live: TLS through an SSH tunnel verifies the server's real name. The + // scratch server's certificate names db.stroke.test, which does not even + // resolve: only dialing the tunnel while TLS checks that name can work. + // Before, the config was rewritten to 127.0.0.1 and verify-full failed. + // STROKE_TEST_SSH=user@127.0.0.1:2222 STROKE_TEST_SSH_KEY=… STROKE_TEST_KNOWN_HOSTS=… + // STROKE_TEST_TLS_PG=127.0.0.1:5544 STROKE_TEST_TLS_CA=/path/ca.crt + #[tokio::test] + async fn live_tls_through_a_tunnel_verifies_the_real_name() { + let (Ok(target), Ok(key), Ok(tls_pg), Ok(ca)) = ( + std::env::var("STROKE_TEST_SSH"), + std::env::var("STROKE_TEST_SSH_KEY"), + std::env::var("STROKE_TEST_TLS_PG"), + std::env::var("STROKE_TEST_TLS_CA"), + ) else { + return; + }; + let (user, hostport) = target.split_once('@').unwrap(); + let (sh, sp) = hostport.split_once(':').unwrap(); + let (_, pg_port) = tls_pg.split_once(':').unwrap(); + let mut c = pg(true, Some("verify-full"), Some(&ca)); + c.host = "db.stroke.test".into(); + c.port = pg_port.parse().unwrap(); + c.user = "postgres".into(); + c.password = String::new(); + c.database = "postgres".into(); + // The tunnel forwards to the scratch server; its far end resolves + // nothing, it just connects to 127.0.0.1. + let ssh = SshConfig { host: sh.into(), port: sp.parse().unwrap(), username: user.into(), private_key_path: key, keepalive: 30 }; + let tunnel = SshTunnel::establish(&ssh, "127.0.0.1", c.port).await.unwrap(); + c.via = Some(std::net::SocketAddr::from(([127, 0, 0, 1], tunnel.local_port))); + c.port = tunnel.local_port; + let pool = open_pg(&c).await.expect("verify-full against db.stroke.test through the tunnel"); + let ssl: bool = sqlx::query_scalar("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()").fetch_one(&pool).await.unwrap(); + assert!(ssl); + // The old way, dialing and verifying 127.0.0.1, is refused. + let mut old = c.clone(); + old.via = None; + old.host = "127.0.0.1".into(); + let err = open_pg(&old).await.err().expect("127.0.0.1 is not the certificate's name"); + assert!(err.to_lowercase().contains("certificate") || err.to_lowercase().contains("name"), "{err}"); + } + /// A password ending in `%` and a database name with `%`/`#` must reach /// sqlx intact: it percent-decodes the whole URL, so anything left raw was /// cut or misread. @@ -1828,6 +2126,7 @@ mod tests { fn my(ssl: bool, mode: Option<&str>, ca: Option<&str>) -> MysqlConfig { MysqlConfig { + via: None, name: "t".into(), host: "db.example.com".into(), port: 3306, diff --git a/src-tauri/src/db/couchdb.rs b/src-tauri/src/db/couchdb.rs index bf43e8d2..35ad5fdb 100644 --- a/src-tauri/src/db/couchdb.rs +++ b/src-tauri/src/db/couchdb.rs @@ -22,6 +22,13 @@ pub struct CouchConfig { pub user: Option, #[serde(default)] pub password: Option, + /// Reach the server through an SSH tunnel (connection::tunnel_to and friends). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + /// The tunnel's local end, when there is one: the URL keeps the server's + /// real name (TLS checks it) and the client resolves that name to here. + #[serde(skip)] + pub via: Option, } fn client() -> &'static reqwest::Client { @@ -38,7 +45,9 @@ fn client() -> &'static reqwest::Client { fn base(cfg: &CouchConfig) -> Result { let raw = cfg.url.trim().trim_end_matches('/'); let raw = if raw.contains("://") { raw.to_string() } else { format!("http://{raw}") }; - reqwest::Url::parse(&format!("{raw}/")).map_err(|e| format!("Invalid CouchDB URL \"{raw}\": {e}")) + reqwest::Url::parse(&format!("{raw}/")) + .map(|u| super::ssh_tunnel::url_via(u, cfg.via)) + .map_err(|e| format!("Invalid CouchDB URL \"{raw}\": {e}")) } /// Requests the app may make. Server administration (`/_config`, `/_node`, @@ -77,7 +86,10 @@ pub async fn request(cfg: &CouchConfig, method: &str, path: &str, body: Option<& .join(path.trim_start_matches('/')) .map_err(|e| format!("Invalid path \"{path}\": {e}"))?; let m = reqwest::Method::from_bytes(method.as_bytes()).map_err(|e| e.to_string())?; - let mut req = client().request(m, url.clone()); + let host = url.host_str().unwrap_or_default().to_string(); + let mut req = super::ssh_tunnel::http_client(&host, cfg.via, false) + .unwrap_or_else(|| client().clone()) + .request(m, url.clone()); if let Some(u) = cfg.user.as_deref().filter(|u| !u.is_empty()) { req = req.basic_auth(u, cfg.password.as_deref()); } @@ -396,7 +408,7 @@ mod tests { #[tokio::test] #[ignore] async fn couch_live() { - let cfg = CouchConfig { name: "t".into(), url: "http://localhost:5984".into(), user: Some("admin".into()), password: Some("admin".into()) }; + let cfg = CouchConfig { ssh: None, via: None, name: "t".into(), url: "http://localhost:5984".into(), user: Some("admin".into()), password: Some("admin".into()) }; ping(&cfg).await.unwrap(); let dbs = list_databases(&cfg).await.unwrap(); assert!(dbs.contains(&"products".to_string()), "{dbs:?}"); diff --git a/src-tauri/src/db/duckdb.rs b/src-tauri/src/db/duckdb.rs index 0f49440a..425efc8e 100644 --- a/src-tauri/src/db/duckdb.rs +++ b/src-tauri/src/db/duckdb.rs @@ -148,6 +148,24 @@ fn collect_rows( sql: &str, params: &[DuckValue], ) -> Result<(Vec, Vec>), String> { + let mut columns = Vec::new(); + let mut out: Vec> = Vec::new(); + for_each_row(conn, sql, params, |c| columns = c, |r| { + out.push(r); + true + })?; + Ok((columns, out)) +} + +/// Run `sql` and hand each row to `on_row` as it is read, after the columns to +/// `on_columns`. `on_row` returns false to stop reading. +fn for_each_row( + conn: &::duckdb::Connection, + sql: &str, + params: &[DuckValue], + on_columns: impl FnOnce(Vec), + mut on_row: impl FnMut(Vec) -> bool, +) -> Result<(), String> { let mut stmt = conn.prepare(sql).map_err(|e| format!("DuckDB error: {e}"))?; // column_names()/column_count() panic before the statement is executed, so we @@ -161,18 +179,41 @@ fn collect_rows( None => (Vec::new(), 0), }; - let mut out: Vec> = Vec::new(); + on_columns(col_names.into_iter().map(|n| ColumnInfo::new(n, "")).collect()); while let Some(row) = rows.next().map_err(|e| format!("DuckDB row error: {e}"))? { let mut r = Vec::with_capacity(col_count); for i in 0..col_count { let vref = row.get_ref(i).map_err(|e| format!("DuckDB cell error: {e}"))?; r.push(valueref_to_json(vref)); } - out.push(r); + if !on_row(r) { + break; + } } + Ok(()) +} - let columns = col_names.into_iter().map(|n| ColumnInfo::new(n, "")).collect(); - Ok((columns, out)) +/// A second connection to the same database, for a transaction: DuckDB's +/// handle is one shared connection, and every other query would otherwise +/// have run inside the transaction. +pub async fn open_tx_conn(handle: &DuckdbHandle) -> Result { + run_blocking(handle.clone(), |conn| { + conn.try_clone() + .map(|c| std::sync::Arc::new(std::sync::Mutex::new(c))) + .map_err(|e| format!("DuckDB could not open a second connection: {e}")) + }) + .await +} + +/// One statement for its side effect (BEGIN, COMMIT, ROLLBACK). +pub async fn batch(handle: &DuckdbHandle, sql: &str) -> Result<(), String> { + let sql = sql.to_string(); + run_blocking(handle.clone(), move |conn| conn.execute_batch(&sql).map_err(|e| format!("DuckDB error: {e}"))).await +} + +/// Whether `sql` reads (and so has rows to stream). +pub(crate) fn reads(sql: &str) -> bool { + is_read_query(sql) } fn is_read_query(sql: &str) -> bool { @@ -214,6 +255,187 @@ pub async fn execute_sql_readonly(handle: &DuckdbHandle, sql: &str) -> Result, +) -> Result { + #[derive(Default)] + struct Slot { + stopped: bool, + interrupt: Option>, + } + let slot = std::sync::Arc::new(std::sync::Mutex::new(Slot::default())); + let s = slot.clone(); + let arm = super::stop::Arm::new(cancel, move || { + let mut g = s.lock().unwrap_or_else(|e| e.into_inner()); + g.stopped = true; + if let Some(h) = &g.interrupt { + h.interrupt(); + } + }); + let h = handle.clone(); + let sql = sql.trim().to_string(); + let t0 = Instant::now(); + let read = is_read_query(&sql); + let result = run_blocking(h, move |conn| { + { + let mut g = slot.lock().unwrap_or_else(|e| e.into_inner()); + if g.stopped { + return Err(super::query::QUERY_CANCELLED.into()); + } + g.interrupt = Some(conn.interrupt_handle()); + } + let out = run_on(conn, &sql, read, t0); + slot.lock().unwrap_or_else(|e| e.into_inner()).interrupt = None; + out + }) + .await; + let stopped = arm.disarm(); + match result { + Err(_) if stopped => Err(super::query::QUERY_CANCELLED.into()), + other => other, + } +} + +fn run_on(conn: &::duckdb::Connection, sql: &str, read: bool, t0: Instant) -> Result { + if read { + let mut columns = Vec::new(); + let mut rows: Vec> = Vec::new(); + let mut budget = super::query::CollectBudget::default(); + let mut capped = false; + for_each_row(conn, sql, &[], |c| columns = c, |r| { + let fits = budget.take(&r); + rows.push(r); + capped = !fits; + fits + })?; + let row_count = Some(rows.len() as i64); + let message = capped.then(|| super::query::budget_message(rows.len())); + Ok(SqlResult { columns, rows, row_count, message, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }) + } else { + let affected = conn.execute(sql, []).map_err(|e| format!("DuckDB error: {e}"))?; + Ok(SqlResult { + columns: vec![], + rows: vec![], + row_count: Some(affected as i64), + message: Some(format!("{affected} row(s) affected")), + query_ms: t0.elapsed().as_millis() as u64, + sql: sql.to_string(), + }) + } +} + +/// One row-returning statement streamed into the console's sink as DuckDB +/// produces it. The driver is synchronous: the blocking thread reads rows and +/// sends them in batches over a small bounded channel, so it never runs far +/// ahead of the window, and a sink that fails (the window went away) stops it. +pub async fn stream_into( + handle: &DuckdbHandle, + sql: &str, + sink: &mut super::query::RowSink, + cancel: tokio::sync::oneshot::Receiver<()>, +) -> Result { + enum Msg { + Columns(Vec), + Rows(Vec>), + } + const BATCH: usize = 1_000; + let slot = std::sync::Arc::new(std::sync::Mutex::new((false, None::>))); + let s = slot.clone(); + let arm = super::stop::Arm::new(cancel, move || { + let mut g = s.lock().unwrap_or_else(|e| e.into_inner()); + g.0 = true; + if let Some(h) = &g.1 { + h.interrupt(); + } + }); + let (tx, mut rx) = tokio::sync::mpsc::channel::(8); + let h = handle.clone(); + let sql_owned = sql.trim().to_string(); + let reader = run_blocking(h, move |conn| { + { + let mut g = slot.lock().unwrap_or_else(|e| e.into_inner()); + if g.0 { + return Err(super::query::QUERY_CANCELLED.into()); + } + g.1 = Some(conn.interrupt_handle()); + } + let mut batch = Vec::with_capacity(BATCH); + let out = for_each_row( + conn, + &sql_owned, + &[], + |c| { + let _ = tx.blocking_send(Msg::Columns(c)); + }, + |r| { + batch.push(r); + if batch.len() < BATCH { + return true; + } + tx.blocking_send(Msg::Rows(std::mem::replace(&mut batch, Vec::with_capacity(BATCH)))).is_ok() + }, + ); + if out.is_ok() && !batch.is_empty() { + let _ = tx.blocking_send(Msg::Rows(batch)); + } + slot.lock().unwrap_or_else(|e| e.into_inner()).1 = None; + out + }); + tokio::pin!(reader); + let t0 = Instant::now(); + let mut columns = Vec::new(); + let mut total = 0usize; + let mut sink_err: Option = None; + let mut reader_out: Option> = None; + loop { + tokio::select! { + msg = rx.recv() => match msg { + Some(Msg::Columns(c)) => columns = c, + Some(Msg::Rows(rows)) => { + if sink_err.is_some() { + continue; + } + for r in rows { + if total == 0 { + if let Err(e) = sink.start(columns.clone()) { sink_err = Some(e); break; } + } + if let Err(e) = sink.push(r) { sink_err = Some(e); break; } + total += 1; + } + if sink_err.is_some() { + // Stop reading: the reader's next send fails. + rx.close(); + } + } + None => break, + }, + out = &mut reader, if reader_out.is_none() => reader_out = Some(out), + } + } + let out = match reader_out { + Some(o) => o, + None => reader.await, + }; + let stopped = arm.disarm(); + if let Some(e) = sink_err { + return Err(e); + } + match out { + Err(_) if stopped => return Err(super::query::QUERY_CANCELLED.into()), + Err(e) => return Err(e), + Ok(()) => {} + } + if total > 0 { + sink.finish()?; + } + Ok(SqlResult { columns, rows: vec![], row_count: Some(total as i64), message: None, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }) +} + pub async fn execute_sql(handle: &DuckdbHandle, sql: &str) -> Result { let h = handle.clone(); let sql = sql.trim().to_string(); @@ -700,6 +922,25 @@ fn json_to_i64(v: &Value) -> Option { #[cfg(test)] mod tests { + + /// A transaction lives on its own connection: the shared one (grid, + /// sidebar) never sees uncommitted rows, and ROLLBACK throws them away. + #[tokio::test] + async fn tx_conn_isolates_and_rolls_back() { + let shared: DuckdbHandle = std::sync::Arc::new(std::sync::Mutex::new(duckdb::Connection::open_in_memory().unwrap())); + batch(&shared, "CREATE TABLE t (id INTEGER)").await.unwrap(); + let tx = open_tx_conn(&shared).await.unwrap(); + batch(&tx, "BEGIN TRANSACTION").await.unwrap(); + batch(&tx, "INSERT INTO t VALUES (1)").await.unwrap(); + let inside = execute_sql(&tx, "SELECT count(*) AS n FROM t").await.unwrap(); + assert_eq!(inside.rows[0][0], serde_json::json!(1)); + let outside = execute_sql(&shared, "SELECT count(*) AS n FROM t").await.unwrap(); + assert_eq!(outside.rows[0][0], serde_json::json!(0)); + batch(&tx, "ROLLBACK").await.unwrap(); + let after = execute_sql(&shared, "SELECT count(*) AS n FROM t").await.unwrap(); + assert_eq!(after.rows[0][0], serde_json::json!(0)); + } + use super::*; use std::sync::{Arc, Mutex}; diff --git a/src-tauri/src/db/elastic.rs b/src-tauri/src/db/elastic.rs index 66003e7b..47c5296c 100644 --- a/src-tauri/src/db/elastic.rs +++ b/src-tauri/src/db/elastic.rs @@ -38,6 +38,13 @@ pub struct EsConfig { /// Accept a self-signed certificate (a local OpenSearch's demo TLS). #[serde(default)] pub insecure: bool, + /// Reach the server through an SSH tunnel (connection::tunnel_to and friends). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + /// The tunnel's local end, when there is one: the URL keeps the server's + /// real name (TLS checks it) and the client resolves that name to here. + #[serde(skip)] + pub via: Option, } impl EsConfig { @@ -66,7 +73,9 @@ fn client(insecure: bool) -> &'static reqwest::Client { fn base(cfg: &EsConfig) -> Result { let raw = cfg.url.trim().trim_end_matches('/'); let raw = if raw.contains("://") { raw.to_string() } else { format!("http://{raw}") }; - reqwest::Url::parse(&format!("{raw}/")).map_err(|e| format!("Invalid {} URL \"{raw}\": {e}", cfg.label())) + reqwest::Url::parse(&format!("{raw}/")) + .map(|u| super::ssh_tunnel::url_via(u, cfg.via)) + .map_err(|e| format!("Invalid {} URL \"{raw}\": {e}", cfg.label())) } /// Endpoints (`_name` path segments) that only read. The console may call @@ -226,7 +235,10 @@ enum Body<'a> { async fn send(cfg: &EsConfig, method: &str, path: &str, body: Option>) -> Result { let url = base(cfg)?.join(path.trim_start_matches('/')).map_err(|e| format!("Invalid path \"{path}\": {e}"))?; let m = reqwest::Method::from_bytes(method.as_bytes()).map_err(|e| e.to_string())?; - let mut req = client(cfg.insecure).request(m, url); + let host = url.host_str().unwrap_or_default().to_string(); + let mut req = super::ssh_tunnel::http_client(&host, cfg.via, cfg.insecure) + .unwrap_or_else(|| client(cfg.insecure).clone()) + .request(m, url); if let Some(k) = cfg.api_key.as_deref().map(str::trim).filter(|k| !k.is_empty()) { req = req.header("Authorization", format!("ApiKey {k}")); } else if let Some(u) = cfg.user.as_deref().filter(|u| !u.is_empty()) { @@ -856,7 +868,7 @@ mod tests { #[ignore] async fn es_live() { let url = std::env::var("ES_URL").unwrap_or_else(|_| "http://localhost:9200".into()); - let cfg = EsConfig { name: "t".into(), url, user: None, password: None, api_key: None, flavor: None, insecure: true }; + let cfg = EsConfig { ssh: None, via: None, name: "t".into(), url, user: None, password: None, api_key: None, flavor: None, insecure: true }; let cfg = connect(cfg).await.unwrap(); request(&cfg, "PUT", "stroke_it/_doc/1?refresh=wait_for", Some(&json!({"level": "error", "ms": 12}))).await.unwrap(); request(&cfg, "PUT", "stroke_it/_doc/2?refresh=wait_for", Some(&json!({"level": "info", "ms": 3}))).await.unwrap(); diff --git a/src-tauri/src/db/explain.rs b/src-tauri/src/db/explain.rs index fccd69ce..15e52cb2 100644 --- a/src-tauri/src/db/explain.rs +++ b/src-tauri/src/db/explain.rs @@ -112,7 +112,17 @@ fn strip_sql(sql: &str) -> &str { /// what gives actual rows and times), so it runs inside a transaction that is /// always rolled back: explaining a DELETE never deletes anything. Without it /// only the planner's estimates come back and nothing runs. -pub async fn explain_pg(pool: &sqlx::PgPool, sql: &str, analyze: bool) -> Result { +/// A Postgres plan that Stop can end. ANALYZE runs the statement for real, so a +/// plan of a 20-minute query took 20 minutes with nothing to press: it now runs +/// in the console's own transaction shape (the statement timeout, and a tag +/// Stop finds the backend by), and Stop cancels it on the server. +pub async fn explain_pg_stoppable( + pool: &sqlx::PgPool, + sql: &str, + analyze: bool, + cancel_rx: Option>, +) -> Result { + use std::sync::atomic::{AtomicBool, Ordering}; let stripped = strip_sql(sql); let q = if analyze { format!("EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT JSON) {stripped}") @@ -121,14 +131,20 @@ pub async fn explain_pg(pool: &sqlx::PgPool, sql: &str, analyze: bool) -> Result }; // PG returns [{Plan: {...}, "Planning Time": x, "Execution Time": y}] + let cancelled = std::sync::Arc::new(AtomicBool::new(false)); let fetched = async { - let mut tx = pool.begin().await.map_err(|e| e.to_string())?; + let tag = super::query::run_tag(); + let (mut tx, target) = super::query::begin_console_tx(pool, &tag).await?; + super::query::arm_pg_cancel(pool, cancel_rx, cancelled.clone(), target); let out = sqlx::query_as::<_, (serde_json::Value,)>(&q).fetch_one(&mut *tx).await; // Roll back whatever ANALYZE did, success or not. let _ = tx.rollback().await; out.map_err(|e| e.to_string()) } .await; + if cancelled.load(Ordering::Relaxed) { + return Err(super::query::QUERY_CANCELLED.into()); + } let arr = match fetched { Ok(row) => row.0, // CockroachDB speaks the PG wire protocol but rejects PG's EXPLAIN option @@ -397,3 +413,31 @@ pub fn explain_from_sqlite_plan(res: &SqlResult, driver: &str) -> ExplainResult driver: driver.into(), } } + +#[cfg(test)] +mod stop_tests { + // STROKE_TEST_PG_URL=postgres://postgres@127.0.0.1:5433/postgres cargo test --lib explain::stop_tests + #[tokio::test] + async fn explain_analyze_stops_on_the_server() { + let Ok(url) = std::env::var("STROKE_TEST_PG_URL") else { return }; + let pool = sqlx::postgres::PgPoolOptions::new().max_connections(3).connect(&url).await.unwrap(); + let (tx, rx) = tokio::sync::oneshot::channel(); + tokio::spawn(async move { + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + let _ = tx.send(()); + }); + let t = std::time::Instant::now(); + let r = super::explain_pg_stoppable(&pool, "SELECT pg_sleep(30)", true, Some(rx)).await; + assert_eq!(r.unwrap_err(), crate::db::query::QUERY_CANCELLED); + assert!(t.elapsed() < std::time::Duration::from_secs(5), "took {:?}", t.elapsed()); + // Nothing of it is still running on the server. + let left: i64 = sqlx::query_scalar("SELECT count(*) FROM pg_stat_activity WHERE query LIKE 'EXPLAIN (ANALYZE%pg_sleep(30)%' AND state = 'active'") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(left, 0); + // And without Stop it still returns a plan. + let ok = super::explain_pg_stoppable(&pool, "SELECT 1", true, None).await.unwrap(); + assert!(ok.plan.get("Node Type").is_some()); + } +} diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs index b3a800fc..194b7313 100644 --- a/src-tauri/src/db/mod.rs +++ b/src-tauri/src/db/mod.rs @@ -3,6 +3,7 @@ pub mod backup; pub mod migrations; pub mod clickhouse; pub mod parked; +pub mod stop; pub mod posthog; pub mod connection; pub mod d1; @@ -45,7 +46,7 @@ pub use connection::{ test_clickhouse_connection, test_connection, test_d1_connection, test_duckdb_connection, test_libsql_connection, test_mssql_connection, test_mysql_connection, test_oracle_connection, test_redis_connection, test_sqlite_connection, ActiveConnection, AnyConnectionConfig, ConnectionConfig, D1Config, DbState, LibSqlConfig, SqliteConfig, }; -pub use explain::{explain_pg, explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, ExplainResult}; +pub use explain::{explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, ExplainResult}; pub use insights::{ instance_activity, instance_config, instance_replication, instance_set_config, instance_state, instance_version, ConfigSetting, InstanceActivity, InstanceReplication, InstanceState, diff --git a/src-tauri/src/db/mongo.rs b/src-tauri/src/db/mongo.rs index c6588f0f..a27fc656 100644 --- a/src-tauri/src/db/mongo.rs +++ b/src-tauri/src/db/mongo.rs @@ -32,6 +32,10 @@ pub struct MongoConfig { /// Database for `db.` in the console; the URI's, else the first one found. #[serde(default)] pub database: Option, + /// Reach the server through an SSH tunnel (connection::tunnel_to and friends). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, + } pub struct MongoConn { @@ -1740,7 +1744,7 @@ mod tests { #[tokio::test] #[ignore] async fn mongo_live() { - let m = connect(&MongoConfig { name: "t".into(), uri: "mongodb://localhost:27017".into(), database: Some("shop".into()) }).await.unwrap(); + let m = connect(&MongoConfig { ssh: None, name: "t".into(), uri: "mongodb://localhost:27017".into(), database: Some("shop".into()) }).await.unwrap(); let colls = list_collections(&m).await.unwrap(); assert!(colls.contains(&"shop.users".to_string()), "{colls:?}"); let info = describe(&m, "shop.orders").await.unwrap(); diff --git a/src-tauri/src/db/mssql.rs b/src-tauri/src/db/mssql.rs index 0dfc9e5e..4c0293cf 100644 --- a/src-tauri/src/db/mssql.rs +++ b/src-tauri/src/db/mssql.rs @@ -10,6 +10,88 @@ use tokio_util::compat::{Compat, TokioAsyncWriteCompatExt}; pub type MssqlClient = Client>; +// ── Stop ───────────────────────────────────────────────────────────────────── +// +// tiberius has no way to send SQL Server's attention signal, and dropping a +// query's future mid-stream leaves the TDS reply half read on the socket: the +// next query on that client would read the rest of the old one. So Stop does +// two things: `KILL`s the session from a second connection, which ends the +// statement on the server, and then `reset` swaps in a fresh client. +// +// The handle does not carry its config, so it is kept here, keyed by the +// handle's address, with the session id (`@@SPID`) to kill. + +static SESSIONS: std::sync::LazyLock>> = + std::sync::LazyLock::new(Default::default); + +fn key(handle: &MssqlHandle) -> usize { + std::sync::Arc::as_ptr(handle) as *const () as usize +} + +async fn spid(client: &mut MssqlClient) -> Option { + let row = client.simple_query("SELECT CAST(@@SPID AS smallint)").await.ok()?.into_row().await.ok()??; + row.get::(0) +} + +/// Remember how to reach a handle's session, so Stop can kill it. +pub async fn remember(handle: &MssqlHandle, cfg: &MssqlConfig) { + let id = spid(&mut *handle.lock().await).await; + if let Some(id) = id { + SESSIONS.lock().unwrap_or_else(|e| e.into_inner()).insert(key(handle), (cfg.clone(), id)); + } +} + +/// A new client on the same server and login, for a transaction: on the +/// shared client every other query would have run inside it. +pub async fn open_tx_client(handle: &MssqlHandle) -> Result { + let cfg = SESSIONS.lock().unwrap_or_else(|e| e.into_inner()).get(&key(handle)).map(|(c, _)| c.clone()); + let cfg = cfg.ok_or("This SQL Server connection can't open a second session. Reconnect and try again.")?; + let client = connect(&cfg).await?; + let h = std::sync::Arc::new(tokio::sync::Mutex::new(client)); + remember(&h, &cfg).await; + Ok(h) +} + +/// One batch on the handle, for BEGIN / COMMIT / ROLLBACK. +pub async fn batch(handle: &MssqlHandle, sql: &str) -> Result<(), String> { + let mut client = handle.lock().await; + client.simple_query(sql).await.map_err(|e| format!("MS SQL error: {e}"))?.into_results().await.map_err(|e| format!("MS SQL error: {e}"))?; + Ok(()) +} + +/// End the handle's running statement on the server. Best effort: KILL needs +/// ALTER ANY CONNECTION, and without it `reset` closing the socket is what +/// stops the work. +pub async fn cancel(handle: &MssqlHandle) { + let Some((cfg, id)) = SESSIONS.lock().unwrap_or_else(|e| e.into_inner()).get(&key(handle)).cloned() else { return }; + let kill = async { + let mut other = connect(&cfg).await?; + other.simple_query(format!("KILL {id}")).await.map_err(|e| e.to_string())?.into_results().await.map_err(|e| e.to_string())?; + Ok::<(), String>(()) + }; + match tokio::time::timeout(std::time::Duration::from_secs(10), kill).await { + Ok(Ok(())) => {} + Ok(Err(e)) => log::info!("mssql stop: KILL {id} refused ({e}); closing the connection instead"), + Err(_) => log::info!("mssql stop: KILL {id} timed out; closing the connection instead"), + } +} + +/// Replace the handle's client after a Stop: the old socket is closed (which +/// also makes the server abandon anything still running) and the next query +/// gets a clean protocol stream. +pub async fn reset(handle: &MssqlHandle) -> Result<(), String> { + let Some((cfg, _)) = SESSIONS.lock().unwrap_or_else(|e| e.into_inner()).get(&key(handle)).cloned() else { + return Err("The connection was stopped and can't be reopened here. Reconnect to keep working.".into()); + }; + let mut fresh = connect(&cfg).await?; + let id = spid(&mut fresh).await; + *handle.lock().await = fresh; + if let Some(id) = id { + SESSIONS.lock().unwrap_or_else(|e| e.into_inner()).insert(key(handle), (cfg, id)); + } + Ok(()) +} + // ── Connect ────────────────────────────────────────────────────────────────── /// Open a tiberius client, following one Azure-style routing redirect if needed. @@ -18,7 +100,10 @@ pub type MssqlClient = Client>; /// bounds the whole attempt and reports which phase stalled. pub async fn connect(cfg: &MssqlConfig) -> Result { let config = build_config(cfg); - let addr = config.get_addr(); + // Through an SSH tunnel the socket goes to the tunnel's local end while + // the config keeps the server's name, which TLS checks the certificate + // against. + let addr = cfg.via.map(|a| a.to_string()).unwrap_or_else(|| config.get_addr()); let tcp = TcpStream::connect(&addr) .await .map_err(|e| format!("Can't reach {addr}: {e}"))?; @@ -262,6 +347,57 @@ pub async fn execute_sql(handle: &MssqlHandle, sql: &str) -> Result Result { + use futures::TryStreamExt; + let t0 = Instant::now(); + let mut client = handle.lock().await; + let mut stream = client.simple_query(sql).await.map_err(|e| format!("MS SQL error: {e}"))?; + let mut columns: Vec = Vec::new(); + let mut total = 0usize; + let mut sink_err: Option = None; + while let Some(item) = stream.try_next().await.map_err(|e| format!("MS SQL error: {e}"))? { + match item { + tiberius::QueryItem::Metadata(m) if m.result_index() == 0 => { + columns = m + .columns() + .iter() + .map(|c| ColumnInfo::new(c.name().to_string(), format!("{:?}", c.column_type()).to_lowercase())) + .collect(); + } + tiberius::QueryItem::Row(r) if r.result_index() == 0 && sink_err.is_none() => { + if total == 0 { + if let Err(e) = sink.start(columns.clone()) { + sink_err = Some(e); + continue; + } + } + match sink.push((0..r.columns().len()).map(|i| cell_to_json(&r, i)).collect()) { + Ok(()) => total += 1, + Err(e) => sink_err = Some(e), + } + } + _ => {} + } + } + drop(stream); + if let Some(e) = sink_err { + return Err(e); + } + if total > 0 { + sink.finish()?; + } + Ok(SqlResult { columns, rows: vec![], row_count: Some(total as i64), message: None, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }) +} + +/// Whether `sql` reads (and so has rows to stream). +pub(crate) fn reads(sql: &str) -> bool { + is_read_query(sql) +} + /// Run a read query and return the first result set's rows (helper for introspection). async fn fetch_rows(handle: &MssqlHandle, sql: &str) -> Result, String> { let mut client = handle.lock().await; @@ -849,7 +985,7 @@ mod tests { use crate::db::connection::open_mssql; fn test_config() -> MssqlConfig { - MssqlConfig { + MssqlConfig { ssh: None, via: None, name: "test".into(), host: "127.0.0.1".into(), port: 1433, @@ -921,3 +1057,33 @@ mod tests { execute_sql(&h, "DROP TABLE dbo.t_users").await.unwrap(); } } + +#[cfg(test)] +mod via_tests { + // The socket goes to `via` while the config keeps the server's name. With + // a name that does not resolve and something listening at `via` that is + // not SQL Server, the connect must get as far as the handshake: failing on + // a lookup or "Can't reach" would mean the name was dialed. + // STROKE_TEST_FAKE_RESP=127.0.0.1:16390 cargo test --lib mssql::via_tests + #[tokio::test] + async fn dials_the_tunnel_and_keeps_the_name() { + let Ok(addr) = std::env::var("STROKE_TEST_FAKE_RESP") else { return }; + let cfg = super::MssqlConfig { + name: "t".into(), + host: "mssql.stroke.test".into(), + port: 1433, + database: "master".into(), + user: "sa".into(), + password: "x".into(), + encrypt: false, + trust_cert: false, + ssh: None, + via: Some(addr.parse().unwrap()), + }; + let err = tokio::time::timeout(std::time::Duration::from_secs(5), super::connect(&cfg)) + .await + .map(|r| r.err().unwrap_or_default()) + .unwrap_or_else(|_| "handshake stalled (reached the listener)".into()); + assert!(!err.contains("Can't reach") && !err.to_lowercase().contains("lookup"), "{err}"); + } +} diff --git a/src-tauri/src/db/mysql.rs b/src-tauri/src/db/mysql.rs index ad26cc14..d1b44983 100644 --- a/src-tauri/src/db/mysql.rs +++ b/src-tauri/src/db/mysql.rs @@ -661,20 +661,7 @@ pub async fn execute_sql( .await .map_err(|e| format!("Failed to acquire connection: {e}"))?; if let Some(rx) = cancel_rx { - if let Ok(conn_id) = sqlx::query_scalar::<_, u64>("SELECT CONNECTION_ID()") - .fetch_one(&mut *conn) - .await - { - let cancel_pool = pool.clone(); - tokio::spawn(async move { - if rx.await.is_ok() { - // conn_id is a numeric id from the server; safe to inline. - let _ = sqlx::query(&format!("KILL QUERY {conn_id}")) - .execute(&cancel_pool) - .await; - } - }); - } + arm_kill(pool, &mut conn, rx).await; } // A script (`CREATE DATABASE x; USE x; CREATE TABLE …`) cannot go through @@ -704,6 +691,73 @@ pub async fn execute_sql( result } +/// Arm Stop for a run on `conn`: `KILL QUERY` its connection id from another +/// pooled connection when `rx` fires. The run's end drops the sender, which +/// disarms it. +async fn arm_kill(pool: &MySqlPool, conn: &mut sqlx::pool::PoolConnection, rx: tokio::sync::oneshot::Receiver<()>) { + if let Ok(conn_id) = sqlx::query_scalar::<_, u64>("SELECT CONNECTION_ID()").fetch_one(&mut **conn).await { + let cancel_pool = pool.clone(); + tokio::spawn(async move { + if rx.await.is_ok() { + // conn_id is a numeric id from the server; safe to inline. + let _ = sqlx::query(&format!("KILL QUERY {conn_id}")).execute(&cancel_pool).await; + } + }); + } +} + +/// One row-returning statement, streamed into the console's sink as it +/// arrives instead of collected: a 2 GB result no longer needs 2 GB here, a +/// JSON reply as large, and the window's heap after that. Rows go to the +/// result store when the sink has one. Stop is KILL QUERY, as in `execute_sql`. +pub async fn stream_into( + pool: &MySqlPool, + sql: &str, + sink: &mut super::query::RowSink, + cancel_rx: tokio::sync::oneshot::Receiver<()>, +) -> Result { + let started = Instant::now(); + let mut conn = pool.acquire().await.map_err(|e| format!("Failed to acquire connection: {e}"))?; + arm_kill(pool, &mut conn, cancel_rx).await; + let mut columns: Vec = Vec::new(); + let mut total = 0usize; + let mut text_protocol = false; + loop { + let mut stream = if text_protocol { sqlx::Executor::fetch(&mut *conn, sql) } else { sqlx::query(sql).fetch(&mut *conn) }; + let outcome: Result<(), sqlx::Error> = async { + while let Some(row) = stream.try_next().await? { + if total == 0 { + columns = row.columns().iter().map(|c| ColumnInfo::new(c.name(), c.type_info().name().to_lowercase())).collect(); + sink.start(columns.clone()).map_err(sqlx::Error::Protocol)?; + } + sink.push((0..row.len()).map(|i| cell_to_json(&row, i)).collect()).map_err(sqlx::Error::Protocol)?; + total += 1; + } + Ok(()) + } + .await; + drop(stream); + match outcome { + // Some statements can't be prepared: run them again as text, once, + // if nothing has streamed yet. + Err(e) if total == 0 && !text_protocol && unsupported_in_prepared(&e) => text_protocol = true, + Err(e) => return Err(format!("Query failed: {e}")), + Ok(()) => break, + } + } + if total > 0 { + sink.finish()?; + } + Ok(SqlResult { + columns, + rows: vec![], + row_count: Some(total as i64), + message: None, + query_ms: started.elapsed().as_millis() as u64, + sql: sql.to_string(), + }) +} + /// Put a connection that ran `USE` back on the pool's own database. Left as it /// was, the connection went back to the pool still pointed at the other /// database, and whichever later call happened to draw it ran there: a @@ -804,11 +858,7 @@ async fn run_one_on(conn: &mut sqlx::MySqlConnection, sql: &str) -> Result Result<(Vec, Vec>, bool), sqlx::Error> { let mut columns: Vec = Vec::new(); let mut data: Vec> = Vec::new(); + let mut budget = super::query::CollectBudget::default(); while let Some(row) = stream.try_next().await? { if data.is_empty() { columns = row @@ -857,8 +908,10 @@ async fn collect_result_rows( .map(|c| ColumnInfo::new(c.name(), c.type_info().name().to_lowercase())) .collect(); } - data.push((0..row.len()).map(|i| cell_to_json(&row, i)).collect()); - if data.len() >= EXECUTE_SQL_MAX_ROWS { + let cells: Vec = (0..row.len()).map(|i| cell_to_json(&row, i)).collect(); + let fits = budget.take(&cells); + data.push(cells); + if !fits || data.len() >= EXECUTE_SQL_MAX_ROWS { return Ok((columns, data, true)); } } @@ -1296,6 +1349,7 @@ mod ai_tool_live { let spec = std::env::var("STROKE_AI_MYSQL").ok()?; let p: Vec<&str> = spec.splitn(5, ':').collect(); Some(MysqlConfig { + via: None, name: "ai".into(), host: p[0].into(), port: p[1].parse().unwrap(), diff --git a/src-tauri/src/db/oracle.rs b/src-tauri/src/db/oracle.rs index c9c3872f..ee04d246 100644 --- a/src-tauri/src/db/oracle.rs +++ b/src-tauri/src/db/oracle.rs @@ -28,6 +28,8 @@ use std::time::Instant; pub struct OracleSession { conn: Connection, lock: Mutex<()>, + /// How this session was opened, so a transaction can open its own. + cfg: OracleConfig, /// What the grid needs to know about a table besides its rows, by /// (schema, table). The ALL_* dictionary views behind it are the slow part /// of a page fetch (seconds for a user who sees every schema), and none of @@ -349,7 +351,7 @@ pub async fn connect(cfg: &OracleConfig) -> Result { .connect() .map_err(|e| explain_connect_error(&e.to_string()))?; conn.execute(SESSION_SETUP, &[]).map_err(|e| format!("Oracle session setup failed: {}", clean_err(&e.to_string())))?; - Ok(std::sync::Arc::new(OracleSession { conn, lock: Mutex::new(()), meta: Mutex::new(HashMap::new()) })) + Ok(std::sync::Arc::new(OracleSession { conn, lock: Mutex::new(()), meta: Mutex::new(HashMap::new()), cfg })) }) .await .map_err(|e| format!("Oracle connect task failed: {e}"))? @@ -944,6 +946,45 @@ fn run_commit(conn: &Connection, sql: &str, binds: &[Bind]) -> Result Result { + connect(&handle.cfg).await +} + +/// Statements inside the open transaction: nothing commits until tx_commit. +pub async fn execute_in_tx(handle: &OracleHandle, sql: &str) -> Result { + let stmts = split_statements(sql); + if stmts.is_empty() { + return Err("Query is empty".into()); + } + let original = sql.trim().to_string(); + run_blocking(handle, move |conn| { + let mut last = None; + for s in &stmts { + last = Some(run_one(conn, s, &[])?); + } + let mut out = last.expect("non-empty"); + out.sql = original; + Ok(out) + }) + .await +} + +pub async fn commit(handle: &OracleHandle) -> Result<(), String> { + run_blocking(handle, |conn| conn.commit().map_err(ora_err)).await +} + +pub async fn rollback(handle: &OracleHandle) -> Result<(), String> { + run_blocking(handle, |conn| conn.rollback().map_err(ora_err)).await +} + pub async fn execute_sql(handle: &OracleHandle, sql: &str) -> Result { let stmts = split_statements(sql); if stmts.is_empty() { @@ -1811,7 +1852,7 @@ mod live_tests { let url = std::env::var("STROKE_ORACLE_TEST").ok()?; let (creds, cs) = url.split_once('@')?; let (user, password) = creds.split_once('/')?; - Some(OracleConfig { + Some(OracleConfig { ssh: None, name: "live".into(), host: String::new(), port: 1521, @@ -1863,7 +1904,7 @@ mod tests { use super::*; fn cfg() -> OracleConfig { - OracleConfig { + OracleConfig { ssh: None, name: "t".into(), host: "db.example.com".into(), port: 1521, @@ -2050,7 +2091,7 @@ mod tests { let url = std::env::var("STROKE_ORACLE_TEST_URL").ok()?; let (creds, cs) = url.split_once('@')?; let (user, password) = creds.split_once('/')?; - Some(OracleConfig { + Some(OracleConfig { ssh: None, name: "live".into(), host: String::new(), port: 1521, diff --git a/src-tauri/src/db/parked.rs b/src-tauri/src/db/parked.rs index 6c84f0ff..1e996655 100644 --- a/src-tauri/src/db/parked.rs +++ b/src-tauri/src/db/parked.rs @@ -11,16 +11,20 @@ const MAX_PARKED: usize = 8; /// Set the open connection aside under `key`. Nothing open: nothing to do. #[tauri::command] pub fn park_connection(state: Db<'_>, key: String) -> Result<(), String> { + super::tx::connection_changed(); let Some(conn) = state.conn.lock().map_err(|e| e.to_string())?.take() else { return Ok(()); }; *state.active_key.lock().map_err(|e| e.to_string())? = None; + // Its SSH tunnel, if any, goes aside with it. + super::ssh_tunnel::park_active(&key); let mut parked = state.parked.lock().map_err(|e| e.to_string())?; parked.retain(|(k, _)| k != &key); parked.push((key, conn)); while parked.len() > MAX_PARKED { - let (_, old) = parked.remove(0); + let (old_key, old) = parked.remove(0); close_in_background(old); + super::ssh_tunnel::drop_parked(&[old_key]); } Ok(()) } @@ -34,6 +38,9 @@ pub fn resume_connection(state: Db<'_>, key: String) -> Result { parked.iter().position(|(k, _)| k == &key).map(|i| parked.remove(i).1) }; let Some(conn) = found else { return Ok(false) }; + super::tx::connection_changed(); + // Its tunnel comes back with it (and whatever was in front closes). + super::ssh_tunnel::resume_parked(&key); let previous = state.conn.lock().map_err(|e| e.to_string())?.replace(conn); *state.active_key.lock().map_err(|e| e.to_string())? = Some(key); if let Some(prev) = previous { @@ -59,9 +66,11 @@ pub fn close_parked( (None, None) => true, }); *parked = kept; + let keys: Vec = gone.iter().map(|(k, _)| k.clone()).collect(); for (_, c) in gone { close_in_background(c); } + super::ssh_tunnel::drop_parked(&keys); Ok(()) } diff --git a/src-tauri/src/db/query.rs b/src-tauri/src/db/query.rs index 3edeb457..fd399066 100644 --- a/src-tauri/src/db/query.rs +++ b/src-tauri/src/db/query.rs @@ -3532,6 +3532,7 @@ pub async fn execute_sql( return Err("Query is empty".into()); } let conn = require_conn(&state)?; + let tunnel = super::ssh_tunnel::RunWatch::start(); let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>(); // Keyed by the caller's id so several editor tabs can run at once and each // one's Cancel reaches its own query. @@ -3547,45 +3548,13 @@ pub async fn execute_sql( // when the receiver fires. ActiveConnection::Postgres(pool) => execute_sql_pg(&pool, &sql_str, Some(cancel_rx)).await, ActiveConnection::Mysql(pool) => super::mysql::execute_sql(&pool, &sql_str, Some(cancel_rx)).await, - // Oracle interrupts the running call (OCIBreak); it then fails with - // ORA-01013, reported as a cancel. - ActiveConnection::Oracle(h) => { - let run = super::oracle::execute_sql(&h, &sql_str); - tokio::pin!(run); - tokio::select! { - r = &mut run => r, - _ = async { let _ = cancel_rx.await; } => { - super::oracle::cancel(&h).await; - let _ = run.await; - Err(QUERY_CANCELLED.to_string()) - } - } - } - // Engines without a server-side cancel primitive: abandon the future when - // cancelled so the UI unblocks (the statement then finishes or times out - // server-side). This preserves the previous behavior for these engines. - other => tokio::select! { - r = async move { - match other { - ActiveConnection::Sqlite(pool) => super::sqlite::execute_sql(&pool, &sql_str).await, - ActiveConnection::D1(cfg) => super::d1::query(&cfg, &sql_str, vec![]).await, - ActiveConnection::LibSql(cfg) => super::libsql::query(&cfg, &sql_str, vec![]).await, - ActiveConnection::Clickhouse(cfg) => super::clickhouse::query(&cfg, &sql_str).await, - ActiveConnection::Posthog(cfg) => super::posthog::query(&cfg, &sql_str).await, - ActiveConnection::Redis(cfg) => super::redis::query(&cfg, &sql_str).await, - ActiveConnection::Dynamodb(cfg) => super::dynamodb::query(&cfg, &sql_str).await, - ActiveConnection::Doc(d) => super::docdb::query(&d, &sql_str).await, - ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(&h, &sql_str).await, - ActiveConnection::Mssql(h) => super::mssql::execute_sql(&h, &sql_str).await, - ActiveConnection::Postgres(_) | ActiveConnection::Mysql(_) | ActiveConnection::Oracle(_) => unreachable!(), - } - } => r, - _ = async { let _ = cancel_rx.await; } => Err(QUERY_CANCELLED.to_string()), - }, + // Everything else stops through its own driver (see run_one_cancellable). + other => run_one_cancellable(&other, &sql_str, &super::stop::Stop::from_rx(cancel_rx)).await, }; super::connection::unregister_cancel(&state, &cancel_key); - if let Ok(r) = &mut result { - r.sql = sql_out; + match &mut result { + Ok(r) => r.sql = sql_out, + Err(e) => *e = tunnel.explain(std::mem::take(e), !is_row_returning_sql(&sql_out)), } result } @@ -3597,6 +3566,8 @@ pub async fn execute_sql_on_conn( config: super::connection::AnyConnectionConfig, sql: &str, ) -> Result { + // Its own connection, through the saved connection's SSH tunnel if any. + let (config, _tunnel) = super::connection::tunnel_any(config).await?; use super::connection::{open_mysql, open_pg, open_sqlite, AnyConnectionConfig}; let sql = sql.trim(); if sql.is_empty() { @@ -3654,6 +3625,36 @@ pub async fn execute_sql_on_conn( /// only as a last-resort circuit breaker whose "add a LIMIT" message tells the /// user what happened. const EXECUTE_SQL_MAX_ROWS: usize = 1_000_000_000; + +/// How much a result collected in memory may hold, as JSON. The SQL console +/// streams to disk and has no limit (RowSink); this guards every caller that +/// still gets its rows in one reply (the MCP server, the AI chat, exports of +/// a single statement), where a 2 GB result meant several GB in the backend, +/// a JSON string as large again, and the window's heap after that. A row cap +/// of a billion guarded nothing. +pub(crate) const COLLECT_BYTE_BUDGET: usize = 512 << 20; + +/// Counts a collected result against COLLECT_BYTE_BUDGET. +#[derive(Default)] +pub(crate) struct CollectBudget { + bytes: usize, +} + +impl CollectBudget { + /// Add a row; false once the result is over budget (stop reading). + pub(crate) fn take(&mut self, row: &[Value]) -> bool { + self.bytes += row.iter().map(approx_json_len).sum::() + row.len() + 1; + self.bytes < COLLECT_BYTE_BUDGET + } +} + +/// What a result cut short by the budget says, in place of its rows' end. +pub(crate) fn budget_message(rows: usize) -> String { + format!( + "Stopped after {rows} rows: the result passed {} MB, more than is safe to hold in memory here. The SQL console streams results of any size; elsewhere, add a LIMIT.", + COLLECT_BYTE_BUDGET >> 20 + ) +} /// Statement timeout for ad-hoc queries (milliseconds). Generous enough for /// heavier scans (e.g. tables with large TOASTed JSON columns) to finish. pub(crate) const EXECUTE_SQL_TIMEOUT_MS: i64 = 60_000; @@ -3662,8 +3663,14 @@ pub(crate) const EXECUTE_SQL_TIMEOUT_MS: i64 = 60_000; /// as stopped rather than failed. pub(crate) const QUERY_CANCELLED: &str = "Query cancelled"; +/// What a stopped run reports on an engine with no way to stop a statement +/// (HTTP engines, document stores). Starts like QUERY_CANCELLED so the window +/// shows it as stopped, then says the part that matters. +pub(crate) const QUERY_ABANDONED: &str = + "Query cancelled here, but this database has no way to stop a running statement, so the server may still finish it."; + /// Where Stop finds a run's backend. -enum CancelTarget { +pub(crate) enum CancelTarget { /// The run's own `application_name`, set in its BEGIN batch. Tag(String), /// A server that refused the tag: the backend pid, asked for up front. @@ -3672,7 +3679,7 @@ enum CancelTarget { /// An `application_name` no other run has, in this app or in another one on /// the same database. Well under Postgres's 63-byte limit. -fn run_tag() -> String { +pub(crate) fn run_tag() -> String { static SEQ: AtomicU64 = AtomicU64::new(0); let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -3687,7 +3694,7 @@ fn run_tag() -> String { /// five round trips before the user's SQL was even sent. Measured at a 100ms /// round trip (`console_run_live`): `SELECT 1` took 8 round trips, now 4; a /// repeat took 5, now 3. A retry passes the same tag, so Stop still reaches it. -async fn begin_console_tx( +pub(crate) async fn begin_console_tx( pool: &sqlx::PgPool, tag: &str, ) -> Result<(sqlx::Transaction<'static, Postgres>, CancelTarget), String> { @@ -3728,7 +3735,7 @@ async fn begin_console_tx( /// also exact: the tag ends with the transaction, so a late Stop matches nothing /// instead of whatever that connection runs next. If the run finishes first the /// sender is dropped, `rx.await` errors and the watcher exits. -fn arm_pg_cancel( +pub(crate) fn arm_pg_cancel( pool: &sqlx::PgPool, rx: Option>, cancelled: Arc, @@ -3839,7 +3846,7 @@ impl RowSink { } /// A result starts (again, after a retry): its columns go with the first message. - fn start(&mut self, columns: Vec) -> Result<(), String> { + pub(crate) fn start(&mut self, columns: Vec) -> Result<(), String> { if let Some(spool) = self.spool.as_mut() { spool.start(columns.clone())?; } @@ -3854,7 +3861,7 @@ impl RowSink { Ok(()) } - fn push(&mut self, row: Vec) -> Result<(), String> { + pub(crate) fn push(&mut self, row: Vec) -> Result<(), String> { match self.spool.as_mut() { Some(spool) => { self.bytes += spool.push(&row)?; @@ -3915,7 +3922,7 @@ impl RowSink { } /// Roughly how many bytes a cell takes as JSON: enough to size a chunk. -fn approx_json_len(v: &Value) -> usize { +pub(crate) fn approx_json_len(v: &Value) -> usize { match v { Value::Null => 4, Value::Bool(_) => 5, @@ -4482,6 +4489,16 @@ pub async fn execute_sql_multi( state: Db<'_>, sql: String, query_id: Option, +) -> Result, String> { + let tunnel = super::ssh_tunnel::RunWatch::start(); + let writes = split_sql_statements(&sql).iter().any(|s| !s.trim().is_empty() && !is_row_returning_sql(s)); + execute_sql_multi_run(state, sql, query_id).await.map_err(|e| tunnel.explain(e, writes)) +} + +async fn execute_sql_multi_run( + state: Db<'_>, + sql: String, + query_id: Option, ) -> Result, String> { let sql_str = sql.trim().to_string(); if sql_str.is_empty() { @@ -4505,36 +4522,30 @@ pub async fn execute_sql_multi( super::connection::unregister_cancel(&state, &cancel_key); return result; } - // Oracle's blocking call outlives the dropped future; interrupt it too. - let oracle_h = match &conn { - ActiveConnection::Oracle(h) => Some(h.clone()), - _ => None, - }; - let result = tokio::select! { - r = async move { - // Other engines: execute sequentially, one result set per statement. - // Cancellation happens at the outer select! (the future is dropped), - // so per-statement executors get no cancel receiver - same as the - // other diff/multi callers. - let multi = stmts.len() > 1; - let mut results: Vec = Vec::with_capacity(stmts.len()); - for (idx, stmt) in stmts.iter().enumerate() { - let r = run_one_other(&conn, stmt).await; - match r { - Ok(res) => results.push(res), - Err(e) if multi => return Err(format!("Statement {} failed: {e}", idx + 1)), - Err(e) => return Err(e), - } + // Other engines: execute sequentially, one result set per statement, each + // stoppable on the server (run_one_cancellable). + let stop = super::stop::Stop::from_rx(cancel_rx); + let multi = stmts.len() > 1; + let mut results: Vec = Vec::with_capacity(stmts.len()); + let mut result = Ok(()); + for (idx, stmt) in stmts.iter().enumerate() { + match run_one_cancellable(&conn, stmt, &stop).await { + Ok(res) => results.push(res), + Err(e) if e.starts_with(QUERY_CANCELLED) => { + result = Err(e); + break; } - Ok(results) - } => r, - _ = async { let _ = cancel_rx.await; } => { - if let Some(h) = &oracle_h { - super::oracle::cancel(h).await; + Err(e) if multi => { + result = Err(format!("Statement {} failed: {e}", idx + 1)); + break; + } + Err(e) => { + result = Err(e); + break; } - Err(QUERY_CANCELLED.to_string()) } - }; + } + let result = result.map(|()| results); super::connection::unregister_cancel(&state, &cancel_key); result } @@ -4558,6 +4569,113 @@ async fn run_one_other(conn: &ActiveConnection, stmt: &str) -> Result Result { + if stop.fired() { + return Err(QUERY_CANCELLED.into()); + } + match conn { + ActiveConnection::Postgres(_) => unreachable!("Postgres runs through its own path"), + // KILL QUERY on the statement's connection id. + ActiveConnection::Mysql(pool) => super::mysql::execute_sql(pool, stmt, Some(stop.receiver())).await, + // sqlite3_interrupt / DuckDB's InterruptHandle on the running connection. + ActiveConnection::Sqlite(pool) => super::sqlite::execute_sql_cancellable(pool, stmt, stop.receiver()).await, + ActiveConnection::Duckdb(h) => super::duckdb::execute_sql_cancellable(h, stmt, stop.receiver()).await, + // KILL QUERY WHERE query_id. + ActiveConnection::Clickhouse(cfg) => super::clickhouse::query_cancellable(cfg, stmt, stop.receiver()).await, + // OCIBreak; the call then fails with ORA-01013. + ActiveConnection::Oracle(h) => { + let run = super::oracle::execute_sql(h, stmt); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = stop.wait() => { + super::oracle::cancel(h).await; + let _ = run.await; + Err(QUERY_CANCELLED.to_string()) + } + } + } + // KILL the session from a second connection, then a fresh client: + // the dropped reply would otherwise be read by the next query. + ActiveConnection::Mssql(h) => { + let run = super::mssql::execute_sql(h, stmt); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = stop.wait() => { + super::mssql::cancel(h).await; + // The killed batch fails at once; give it a moment, then + // let go of the client either way. + let finished = tokio::time::timeout(std::time::Duration::from_secs(5), &mut run).await; + if let Ok(Ok(r)) = finished { + // It completed before the KILL landed: that is the result. + return Ok(r); + } + drop(run); + match super::mssql::reset(h).await { + Ok(()) => Err(QUERY_CANCELLED.to_string()), + Err(e) => Err(format!("{QUERY_CANCELLED}. Reopening the connection afterwards failed: {e}")), + } + } + } + } + // No server-side stop exists: stop waiting, and say the server may not. + other => tokio::select! { + r = run_one_other(other, stmt) => r, + _ = stop.wait() => Err(QUERY_ABANDONED.into()), + }, + } +} + +/// Stream one row-returning statement into `sink`, on the engines whose +/// drivers read rows as they arrive. None: this engine or statement doesn't +/// stream, so the caller runs it the ordinary way. +async fn stream_one( + conn: &ActiveConnection, + stmt: &str, + sink: &mut RowSink, + stop: &super::stop::Stop, +) -> Option> { + let reads = is_row_returning_sql(stmt); + Some(match conn { + ActiveConnection::Mysql(pool) if reads => super::mysql::stream_into(pool, stmt, sink, stop.receiver()).await, + ActiveConnection::Sqlite(pool) if reads || super::sql_util::statement_head(stmt) == "pragma" => { + super::sqlite::stream_into(pool, stmt, sink, stop.receiver()).await + } + ActiveConnection::Duckdb(h) if super::duckdb::reads(stmt) => super::duckdb::stream_into(h, stmt, sink, stop.receiver()).await, + ActiveConnection::Mssql(h) if super::mssql::reads(stmt) => { + let run = super::mssql::stream_into(h, stmt, sink); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = stop.wait() => { + super::mssql::cancel(h).await; + let finished = tokio::time::timeout(std::time::Duration::from_secs(5), &mut run).await; + if let Ok(Ok(r)) = finished { + return Some(Ok(r)); + } + drop(run); + Err(match super::mssql::reset(h).await { + Ok(()) => QUERY_CANCELLED.to_string(), + Err(e) => format!("{QUERY_CANCELLED}. Reopening the connection afterwards failed: {e}"), + }) + } + } + } + ActiveConnection::Clickhouse(cfg) if super::clickhouse::reads(stmt) => super::clickhouse::stream_into(cfg, stmt, sink, stop).await, + _ => return None, + }) +} + /// One statement of a console run: its result, or why it failed. #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] @@ -4609,14 +4727,23 @@ pub async fn execute_sql_script( return Err("Query is empty".into()); } log::info!("console run: {} statement(s)", stmts.len()); + let tunnel = super::ssh_tunnel::RunWatch::start(); let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>(); let cancel_key = query_id.unwrap_or_else(|| "default".to_string()); super::connection::register_cancel(&state, &cancel_key, cancel_tx); - let outcomes = match &conn { + let mut outcomes = match &conn { ActiveConnection::Postgres(pool) => run_script_pg(pool, &stmts, cancel_rx, sink).await, - other => run_script_other(other, &stmts, cancel_rx).await, + other => run_script_other(other, &stmts, cancel_rx, sink).await, }; super::connection::unregister_cancel(&state, &cancel_key); + // A script that failed because its tunnel went away: say whether it got + // as far as the server. Any write in the script makes the result unknown. + let writes = stmts.iter().any(|s| !is_row_returning_sql(s)); + for o in &mut outcomes { + if let Some(e) = o.error.take() { + o.error = Some(tunnel.explain(e, writes)); + } + } Ok(outcomes) } @@ -4676,30 +4803,37 @@ async fn run_script_other( conn: &ActiveConnection, stmts: &[String], cancel_rx: tokio::sync::oneshot::Receiver<()>, + sink: Option<&mut RowSink>, ) -> Vec { - // A Mutex for Send; never held across an await. - let out = std::sync::Mutex::new(Vec::with_capacity(stmts.len())); - let run = async { - for stmt in stmts { - let t = std::time::Instant::now(); - let r = run_one_other(conn, stmt).await; + let stop = super::stop::Stop::from_rx(cancel_rx); + // One row-returning statement on an engine with a cursor streams into the + // sink (and the result store), as Postgres does, instead of arriving whole. + if let (Some(sink), [stmt]) = (sink, stmts) { + let t = std::time::Instant::now(); + if let Some(r) = stream_one(conn, stmt, sink, &stop).await { let ms = t.elapsed().as_millis() as u64; - out.lock().unwrap().push(match r { + return vec![match r { Ok(res) => StatementOutcome::ok(res), Err(e) => StatementOutcome::failed(stmt, ms, e.into()), - }); + }]; } - }; - tokio::select! { - _ = run => {} - _ = async { let _ = cancel_rx.await; } => { - if let ActiveConnection::Oracle(h) = conn { - super::oracle::cancel(h).await; + } + let mut out = Vec::with_capacity(stmts.len()); + for stmt in stmts { + let t = std::time::Instant::now(); + let r = run_one_cancellable(conn, stmt, &stop).await; + let ms = t.elapsed().as_millis() as u64; + match r { + Ok(res) => out.push(StatementOutcome::ok(res)), + // Stopped: this statement says so, and nothing after it runs. + Err(e) if stop.fired() && e.starts_with(QUERY_CANCELLED) => { + out.push(StatementOutcome::failed(stmt, ms, e.into())); + break; } - out.lock().unwrap().push(StatementOutcome::failed("", 0, QUERY_CANCELLED.into())); + Err(e) => out.push(StatementOutcome::failed(stmt, ms, e.into())), } } - out.into_inner().unwrap_or_default() + out } // ── D1 / LibSQL helpers (SQLite-over-HTTP) ──────────────────────────────────── @@ -5794,7 +5928,7 @@ mod nosql_console_live { #[ignore] async fn nosql_console_live() { // MongoDB - let m = crate::db::mongo::connect(&crate::db::mongo::MongoConfig { name: "t".into(), uri: "mongodb://localhost:27017".into(), database: Some("shop".into()) }).await.unwrap(); + let m = crate::db::mongo::connect(&crate::db::mongo::MongoConfig { ssh: None, name: "t".into(), uri: "mongodb://localhost:27017".into(), database: Some("shop".into()) }).await.unwrap(); let mc = ActiveConnection::Doc(DocConn::Mongo(m)); let r = run(&mc, "db.users.find({}).limit(3)").await; assert_eq!(r.rows.len(), 3); @@ -5812,13 +5946,13 @@ mod nosql_console_live { assert_eq!(r.rows.len(), 3); // Cassandra (CQL) - let s = crate::db::cassandra::connect(&crate::db::cassandra::CassandraConfig { name: "t".into(), hosts: "127.0.0.1".into(), user: None, password: None, keyspace: Some("shop".into()) }).await.unwrap(); + let s = crate::db::cassandra::connect(&crate::db::cassandra::CassandraConfig { ssh: None, name: "t".into(), hosts: "127.0.0.1".into(), user: None, password: None, keyspace: Some("shop".into()) }).await.unwrap(); let cc = ActiveConnection::Doc(DocConn::Cassandra(s)); assert!(!run(&cc, "SELECT * FROM users LIMIT 5").await.rows.is_empty(), "unqualified name uses the keyspace"); assert!(!run(&cc, "SELECT * FROM shop.orders_by_user LIMIT 5").await.columns.is_empty()); // CouchDB (Mango shell syntax) - let couch = ActiveConnection::Doc(DocConn::Couch(crate::db::couchdb::CouchConfig { name: "t".into(), url: "http://localhost:5984".into(), user: Some("admin".into()), password: Some("admin".into()) })); + let couch = ActiveConnection::Doc(DocConn::Couch(crate::db::couchdb::CouchConfig { ssh: None, via: None, name: "t".into(), url: "http://localhost:5984".into(), user: Some("admin".into()), password: Some("admin".into()) })); assert_eq!(run(&couch, r#"products.find({"price": {"$gt": 50}})"#).await.rows.len(), 2); // Firestore (emulator) @@ -5834,7 +5968,7 @@ mod nosql_console_live { assert!(run(&cc, r#"SELECT * FROM "shop"."users" LIMIT 2"#).await.rows.len() <= 2); // Redis - let rc = ActiveConnection::Redis(crate::db::connection::RedisConfig { name: "t".into(), host: "127.0.0.1".into(), port: 6391, password: None, db: 0, tls: false }); + let rc = ActiveConnection::Redis(crate::db::connection::RedisConfig { ssh: None, via: None, name: "t".into(), host: "127.0.0.1".into(), port: 6391, password: None, db: 0, tls: false }); assert!(!run(&rc, "DBSIZE").await.rows.is_empty()); } } @@ -6019,3 +6153,185 @@ pub async fn create_search_index( } Ok(created) } + +/// Big results on engines other than Postgres stream to the result store +/// instead of arriving whole. +#[cfg(test)] +mod stream_tests { + use super::*; + + fn rss_mb() -> u64 { + std::fs::read_to_string("/proc/self/status") + .ok() + .and_then(|s| s.lines().find(|l| l.starts_with("VmRSS")).map(|l| l.split_whitespace().nth(1).unwrap_or("0").parse::().unwrap_or(0) / 1024)) + .unwrap_or(0) + } + + fn sink_into(store: &super::super::result_store::ResultStore, id: &str) -> RowSink { + let channel = tauri::ipc::Channel::::new(|_| Ok(())); + RowSink::with_spool(channel, Some(store.create(id).unwrap())) + } + + const N: usize = 2_000_000; + + async fn check(store: &super::super::result_store::ResultStore, id: &str, r: SqlResult, before: u64, limit_mb: u64) { + assert!(r.rows.is_empty(), "rows went to the store, not the reply"); + assert_eq!(r.row_count, Some(N as i64)); + let stored = store.get(id).unwrap(); + assert_eq!(stored.len(), N); + assert!(stored.is_done()); + let deep: Vec = serde_json::from_str(&stored.read_window(N - 3, 3).unwrap()).unwrap(); + assert_eq!(deep[2][0], serde_json::json!(N as i64)); + let grew = rss_mb().saturating_sub(before); + assert!(grew < limit_mb, "{id}: memory grew {grew} MB while streaming"); + } + + #[tokio::test] + async fn sqlite_streams_a_large_result_to_the_store() { + let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await.unwrap(); + let store = super::super::result_store::ResultStore::default(); + let mut sink = sink_into(&store, "sqlite-big"); + let before = rss_mb(); + let sql = format!("WITH RECURSIVE g(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM g WHERE x < {N}) SELECT x AS id, hex(randomblob(32)) AS v FROM g"); + let (_tx, rx) = tokio::sync::oneshot::channel(); + let r = super::super::sqlite::stream_into(&pool, &sql, &mut sink, rx).await.unwrap(); + // Collected, these rows are ~150 MB of JSON plus the values behind it. + check(&store, "sqlite-big", r, before, 120).await; + } + + /// A fake ClickHouse HTTP endpoint answering in + /// JSONCompactEachRowWithNamesAndTypes, written in uneven chunks so lines + /// split across reads. `tail` is appended after the rows. + async fn fake_clickhouse(rows: usize, tail: &'static str) -> u16 { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = l.local_addr().unwrap().port(); + tokio::spawn(async move { + let (mut s, _) = l.accept().await.unwrap(); + let mut buf = vec![0u8; 65536]; + let _ = s.read(&mut buf).await; + s.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n").await.unwrap(); + let mut out = String::from("[\"id\",\"v\"]\n[\"UInt64\",\"Nullable(String)\"]\n"); + for i in 1..=rows { + out.push_str(&format!("[{i},\"{:064x}\"]\n", i)); + if out.len() > 7_919 { + let cut = out.len() / 2 + 3; + s.write_all(&out.as_bytes()[..cut]).await.unwrap(); + out = out[cut..].to_string(); + } + } + out.push_str(tail); + s.write_all(out.as_bytes()).await.unwrap(); + }); + port + } + + fn fake_clickhouse_cfg(port: u16) -> super::super::connection::ClickhouseConfig { + super::super::connection::ClickhouseConfig { + name: "t".into(), host: "127.0.0.1".into(), port, database: "default".into(), + user: "default".into(), password: String::new(), secure: false, ssh: None, via: None, + } + } + + #[tokio::test] + async fn clickhouse_streams_a_large_result_to_the_store() { + let port = fake_clickhouse(N, "").await; + let store = super::super::result_store::ResultStore::default(); + let mut sink = sink_into(&store, "ch-big"); + let before = rss_mb(); + let (_tx, rx) = tokio::sync::oneshot::channel(); + let stop = super::super::stop::Stop::from_rx(rx); + let r = super::super::clickhouse::stream_into(&fake_clickhouse_cfg(port), "SELECT id, v FROM t", &mut sink, &stop).await.unwrap(); + assert_eq!(r.columns.len(), 2); + assert_eq!(r.columns[1].data_type, "String"); + assert!(r.columns[1].nullable); + // Collected as one JSONCompact document this was the body text plus + // the parsed tree: several hundred MB. + check(&store, "ch-big", r, before, 120).await; + } + + #[tokio::test] + async fn clickhouse_error_after_the_first_rows_is_reported() { + let port = fake_clickhouse(500, "Code: 241. DB::Exception: Memory limit exceeded\n").await; + let store = super::super::result_store::ResultStore::default(); + let mut sink = sink_into(&store, "ch-err"); + let (_tx, rx) = tokio::sync::oneshot::channel(); + let stop = super::super::stop::Stop::from_rx(rx); + let e = super::super::clickhouse::stream_into(&fake_clickhouse_cfg(port), "SELECT 1", &mut sink, &stop).await.unwrap_err(); + assert!(e.contains("Memory limit exceeded"), "{e}"); + } + + #[tokio::test] + async fn duckdb_streams_a_large_result_to_the_store() { + let cfg = super::super::connection::DuckdbConfig { name: "t".into(), file_path: ":memory:".into() }; + let h = super::super::connection::open_duckdb(&cfg).await.unwrap(); + let store = super::super::result_store::ResultStore::default(); + let mut sink = sink_into(&store, "duck-big"); + let before = rss_mb(); + let sql = format!("SELECT range + 1 AS id, md5(range::varchar) || md5((range * 7)::varchar) AS v FROM range({N})"); + let (_tx, rx) = tokio::sync::oneshot::channel(); + let r = super::super::duckdb::stream_into(&h, &sql, &mut sink, rx).await.unwrap(); + // duckdb-rs reads rows only from a materialized result (its streaming + // cursor is Arrow-only, and panics on a mid-stream error), so DuckDB + // holds the result natively: measured 214 MB here. This side holds one + // 1,000-row batch. Collected, the same result grew memory 931 MB + // (duckdb_collected_for_comparison), before the window's copy. + check(&store, "duck-big", r, before, 350).await; + } + + // For the record: what the same DuckDB result costs collected. + #[tokio::test] + #[ignore] + async fn duckdb_collected_for_comparison() { + let cfg = super::super::connection::DuckdbConfig { name: "t".into(), file_path: ":memory:".into() }; + let h = super::super::connection::open_duckdb(&cfg).await.unwrap(); + let before = rss_mb(); + let sql = format!("SELECT range + 1 AS id, md5(range::varchar) || md5((range * 7)::varchar) AS v FROM range({N})"); + let r = super::super::duckdb::execute_sql(&h, &sql).await.unwrap(); + let json = serde_json::to_string(&r).unwrap(); + println!("collected: rows {} json {} MB, rss grew {} MB", r.rows.len(), json.len() >> 20, rss_mb().saturating_sub(before)); + } + + #[test] + fn budget_stops_a_collected_result() { + let mut b = CollectBudget::default(); + let row = vec![Value::String("x".repeat(1 << 20))]; + let taken = (0..1000).take_while(|_| b.take(&row)).count(); + assert!((500..=512).contains(&taken), "{taken}"); + assert!(budget_message(taken).contains("SQL console streams")); + } +} + +#[cfg(test)] +mod on_conn_tunnel_tests { + // Live: Data Diff's own connection goes through the saved connection's SSH + // tunnel. The throwaway sshd logs each login, so one more login is the + // proof it went through; the ssh is gone again afterwards. + // STROKE_TEST_SSH=… STROKE_TEST_SSH_KEY=… STROKE_TEST_KNOWN_HOSTS=… + // STROKE_TEST_PG_PORT=5433 STROKE_TEST_SSHD_LOG=/path/sshd.log + #[tokio::test] + async fn data_diff_goes_through_the_tunnel() { + let (Ok(target), Ok(key), Ok(pg_port), Ok(log)) = ( + std::env::var("STROKE_TEST_SSH"), + std::env::var("STROKE_TEST_SSH_KEY"), + std::env::var("STROKE_TEST_PG_PORT"), + std::env::var("STROKE_TEST_SSHD_LOG"), + ) else { + return; + }; + let logins = || std::fs::read_to_string(&log).unwrap_or_default().matches("Accepted publickey").count(); + let (user, hostport) = target.split_once('@').unwrap(); + let (host, port) = hostport.split_once(':').unwrap(); + let cfg = serde_json::json!({ + "type": "postgres", "name": "t", "host": "127.0.0.1", "port": pg_port.parse::().unwrap(), + "database": "postgres", "user": "postgres", "password": "", "ssl": false, + "ssh": { "host": host, "port": port.parse::().unwrap(), "username": user, "privateKeyPath": key, "keepalive": 30 } + }); + let config: crate::db::connection::AnyConnectionConfig = serde_json::from_value(cfg).unwrap(); + let before = logins(); + let r = super::execute_sql_on_conn(config, "SELECT 7 AS n").await.unwrap(); + assert_eq!(r.rows[0][0], serde_json::json!(7)); + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + assert_eq!(logins(), before + 1, "the query went through one SSH login"); + } +} diff --git a/src-tauri/src/db/redis.rs b/src-tauri/src/db/redis.rs index 73339a0e..b3db5215 100644 --- a/src-tauri/src/db/redis.rs +++ b/src-tauri/src/db/redis.rs @@ -30,6 +30,22 @@ static POOL: std::sync::LazyLock( + &'a self, + _host: &'b str, + _port: u16, + ) -> ::redis::RedisFuture<'a, Box + Send + 'a>> { + let addr = self.0; + Box::pin(async move { Ok(Box::new(std::iter::once(addr)) as Box + Send>) }) + } +} + fn url_for(cfg: &RedisConfig) -> String { let scheme = if cfg.tls { "rediss" } else { "redis" }; let auth = match cfg.password.as_deref().map(str::trim).filter(|p| !p.is_empty()) { @@ -39,13 +55,21 @@ fn url_for(cfg: &RedisConfig) -> String { format!("{scheme}://{auth}{}:{}/{}", cfg.host, cfg.port, cfg.db) } -async fn dial(url: &str) -> Result<::redis::aio::MultiplexedConnection, String> { - let client = ::redis::Client::open(url).map_err(|e| format!("Redis connection failed: {e}"))?; +fn connection_config(via: Option) -> ::redis::AsyncConnectionConfig { // Bounded: with no timeout an unreachable host (or a TLS port spoken to in // plain TCP) left the connect awaiting until the OS gave up. let config = ::redis::AsyncConnectionConfig::new() .set_connection_timeout(Some(std::time::Duration::from_secs(10))) .set_response_timeout(Some(std::time::Duration::from_secs(30))); + match via { + Some(addr) => config.set_dns_resolver(ViaTunnel(addr)), + None => config, + } +} + +async fn dial(url: &str, via: Option) -> Result<::redis::aio::MultiplexedConnection, String> { + let client = ::redis::Client::open(url).map_err(|e| format!("Redis connection failed: {e}"))?; + let config = connection_config(via); client .get_multiplexed_async_connection_with_config(&config) .await @@ -55,7 +79,11 @@ async fn dial(url: &str) -> Result<::redis::aio::MultiplexedConnection, String> /// A multiplexed connection for this config: the cached one when it is still /// alive, otherwise a new one (which then becomes the cached one). async fn open(cfg: &RedisConfig) -> Result<::redis::aio::MultiplexedConnection, String> { - let url = url_for(cfg); + // Keyed by the tunnel too: the same server reached two ways is two connections. + let url = match cfg.via { + Some(via) => format!("{}#via={via}", url_for(cfg)), + None => url_for(cfg), + }; let mut pool = POOL.lock().await; if let Some(entry) = pool.get_mut(&url) { let fresh = entry.last_used.elapsed() < IDLE_CHECK; @@ -73,7 +101,7 @@ async fn open(cfg: &RedisConfig) -> Result<::redis::aio::MultiplexedConnection, // Dial without holding the lock for the whole handshake would let two // commands race to open two connections; holding it is the simpler bound, // and it only ever waits on the first connect for a given host. - let conn = dial(&url).await?; + let conn = dial(&url_for(cfg), cfg.via).await?; pool.insert(url, Cached { conn: conn.clone(), last_used: Instant::now() }); Ok(conn) } @@ -81,7 +109,11 @@ async fn open(cfg: &RedisConfig) -> Result<::redis::aio::MultiplexedConnection, /// Drop the cached connection for a config (disconnect, or a failed PING in /// `ping`), so the next command dials fresh. pub async fn forget(cfg: &RedisConfig) { - POOL.lock().await.remove(&url_for(cfg)); + let mut pool = POOL.lock().await; + pool.remove(&url_for(cfg)); + if let Some(via) = cfg.via { + pool.remove(&format!("{}#via={via}", url_for(cfg))); + } } /// Connectivity/credential check - issues a `PING`. @@ -784,6 +816,9 @@ where if channels.is_empty() && patterns.is_empty() { return Err("Name a channel or a pattern to subscribe to".into()); } + if let Some(via) = cfg.via { + return subscribe_via(cfg, via, id, channels, patterns, emit).await; + } let client = ::redis::Client::open(url_for(cfg)).map_err(|e| format!("Redis connection failed: {e}"))?; let mut ps = client.get_async_pubsub().await.map_err(|e| format!("Could not open a pub/sub connection: {e}"))?; for c in &channels { @@ -810,6 +845,55 @@ where Ok(()) } +/// `subscribe` through an SSH tunnel. The dedicated pub/sub connection in +/// redis-rs always uses the system resolver, so it would dial the server's +/// name directly instead of the tunnel. A RESP3 connection takes the tunnel's +/// resolver and delivers messages as push frames (Redis 6+). +async fn subscribe_via(cfg: &RedisConfig, via: std::net::SocketAddr, id: String, channels: Vec, patterns: Vec, emit: F) -> Result<(), String> +where + F: Fn(PubSubMessage) + Send + 'static, +{ + let url = format!("{}?protocol=resp3", url_for(cfg)); + let client = ::redis::Client::open(url).map_err(|e| format!("Redis connection failed: {e}"))?; + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<::redis::PushInfo>(); + let config = connection_config(Some(via)).set_push_sender(tx); + let mut conn = client + .get_multiplexed_async_connection_with_config(&config) + .await + .map_err(|e| format!("Could not open a pub/sub connection: {e}"))?; + for c in &channels { + conn.subscribe(c).await.map_err(|e| format!("SUBSCRIBE {c} failed (subscriptions through an SSH tunnel need Redis 6 or later): {e}"))?; + } + for p in &patterns { + conn.psubscribe(p).await.map_err(|e| format!("PSUBSCRIBE {p} failed (subscriptions through an SSH tunnel need Redis 6 or later): {e}"))?; + } + unsubscribe(&id); + let task = tokio::spawn(async move { + // Held for the subscription's life: dropping it closes the connection. + let _conn = conn; + while let Some(push) = rx.recv().await { + let text = |v: Option<&::redis::Value>| v.map(value_to_string).unwrap_or_default(); + let payload = |v: Option<&::redis::Value>| match v { + Some(::redis::Value::BulkString(b)) => bytes_to_display(b), + other => text(other), + }; + let (channel, pattern, body) = match push.kind { + ::redis::PushKind::Message => (text(push.data.first()), None, payload(push.data.get(1))), + ::redis::PushKind::PMessage => (text(push.data.get(1)), Some(text(push.data.first())), payload(push.data.get(2))), + _ => continue, + }; + emit(PubSubMessage { + channel, + pattern, + payload: body, + at: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0), + }); + } + }); + SUBS.lock().map_err(|_| "Subscription registry poisoned")?.insert(id, task.abort_handle()); + Ok(()) +} + pub fn unsubscribe(id: &str) { if let Ok(mut m) = SUBS.lock() { if let Some(h) = m.remove(id) { @@ -951,7 +1035,7 @@ mod redis_live { use super::*; fn cfg() -> RedisConfig { - RedisConfig { + RedisConfig { ssh: None, via: None, name: "it".into(), host: "127.0.0.1".into(), port: std::env::var("STROKE_REDIS_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(6391), @@ -1039,3 +1123,26 @@ mod redis_live { apply(&c, vec![op(&["FLUSHDB"])]).await.unwrap(); } } + +#[cfg(test)] +mod via_tests { + // Through a tunnel the URL keeps the server's real name (TLS checks it) + // and only the TCP connection goes to the tunnel. Proven with a name that + // does not resolve: any lookup of it would fail the connect. + // STROKE_TEST_FAKE_RESP=127.0.0.1:16390 cargo test --lib redis::via_tests + #[tokio::test] + async fn dials_the_tunnel_and_keeps_the_name() { + let Ok(addr) = std::env::var("STROKE_TEST_FAKE_RESP") else { return }; + let cfg = crate::db::connection::RedisConfig { + name: "t".into(), + host: "redis.stroke.test".into(), + port: 6379, + password: None, + db: 0, + tls: false, + ssh: None, + via: Some(addr.parse().unwrap()), + }; + super::ping(&cfg).await.expect("PING through the tunnel address"); + } +} diff --git a/src-tauri/src/db/result_store.rs b/src-tauri/src/db/result_store.rs index 6aca5b9b..8ed0330f 100644 --- a/src-tauri/src/db/result_store.rs +++ b/src-tauri/src/db/result_store.rs @@ -28,6 +28,36 @@ use std::sync::{Arc, Mutex, RwLock}; /// only bounds what a window that forgot to drop could leave behind on disk. const MAX_RESULTS: usize = 16; +/// A run won't start spooling with less free than this in the temp folder. +const MIN_FREE: u64 = 1 << 30; + +/// Free bytes on the volume holding `dir`, where the platform says. +#[cfg(unix)] +fn free_bytes(dir: &std::path::Path) -> Option { + use std::os::unix::ffi::OsStrExt; + let c = std::ffi::CString::new(dir.as_os_str().as_bytes()).ok()?; + let mut st: libc::statvfs = unsafe { std::mem::zeroed() }; + // SAFETY: a valid C string and a zeroed out-struct, as statvfs expects. + if unsafe { libc::statvfs(c.as_ptr(), &mut st) } != 0 { + return None; + } + Some(st.f_bavail as u64 * st.f_frsize as u64) +} + +#[cfg(not(unix))] +fn free_bytes(_dir: &std::path::Path) -> Option { + None +} + +/// A write that failed because the disk is full, said so. +fn write_error(e: std::io::Error, rows: usize) -> String { + if e.kind() == std::io::ErrorKind::StorageFull { + format!("The disk filled up while the result streamed to it, after {rows} rows. Free some space and run it again, or add a LIMIT.") + } else { + format!("Could not write the result file: {e}") + } +} + /// Every stored result, by the run id the console sent with its query. pub struct ResultStore { results: Mutex)>>, @@ -60,6 +90,15 @@ fn remove_other_spools(dir: &std::path::Path) { impl ResultStore { /// A new, empty result for run `id`, replacing any earlier one with that id. pub fn create(&self, id: &str) -> Result { + if let Some(free) = free_bytes(&self.dir) { + if free < MIN_FREE { + return Err(format!( + "Only {} MB free in {}, where results stream to disk. Free some space, or turn off Settings → Database → Stream results to hold results in memory instead.", + free >> 20, + self.dir.display() + )); + } + } let safe: String = id.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c } else { '_' }).collect(); let path = self.dir.join(format!("{}-{safe}.jsonl", std::process::id())); let write = File::create(&path).map_err(|e| format!("Could not create the result file: {e}"))?; @@ -372,7 +411,8 @@ impl SpoolWriter { self.scratch.clear(); serde_json::to_writer(&mut self.scratch, row).map_err(|e| e.to_string())?; self.scratch.push(b'\n'); - self.file.write_all(&self.scratch).map_err(|e| format!("Could not write the result file: {e}"))?; + let rows = self.result.offsets.read().unwrap().len() + self.pending.len(); + self.file.write_all(&self.scratch).map_err(|e| write_error(e, rows))?; self.pending.push(self.pos); self.pos += self.scratch.len() as u64; Ok(self.scratch.len()) @@ -380,7 +420,8 @@ impl SpoolWriter { /// Make every row written so far readable. Returns how many rows are. pub fn flush(&mut self) -> Result { - self.file.flush().map_err(|e| format!("Could not write the result file: {e}"))?; + let rows = self.result.offsets.read().unwrap().len() + self.pending.len(); + self.file.flush().map_err(|e| write_error(e, rows))?; let mut offsets = self.result.offsets.write().unwrap(); offsets.append(&mut self.pending); self.result.end.store(self.pos, AtomicOrdering::Release); @@ -915,3 +956,13 @@ mod tests { assert_eq!(window(&r, 0, 4), rows); } } + +#[cfg(all(test, unix))] +mod disk_tests { + #[test] + fn reads_free_space_and_names_a_full_disk() { + assert!(super::free_bytes(&std::env::temp_dir()).is_some_and(|b| b > 0)); + let full = std::io::Error::from(std::io::ErrorKind::StorageFull); + assert!(super::write_error(full, 1234).contains("disk filled up") ); + } +} diff --git a/src-tauri/src/db/schema.rs b/src-tauri/src/db/schema.rs index 3495ccc2..e155c9d5 100644 --- a/src-tauri/src/db/schema.rs +++ b/src-tauri/src/db/schema.rs @@ -2706,6 +2706,8 @@ pub async fn get_table_ddl( /// List schemas for an arbitrary saved connection without changing global state. pub async fn list_schemas_on_conn(config: AnyConnectionConfig) -> Result, String> { + // Its own connection, through the saved connection's SSH tunnel if any. + let (config, _tunnel) = super::connection::tunnel_any(config).await?; use super::connection::{open_mysql, open_pg}; match config { AnyConnectionConfig::Postgres(c) => { @@ -2734,6 +2736,8 @@ pub async fn list_tables_on_conn( config: AnyConnectionConfig, schema: String, ) -> Result, String> { + // Its own connection, through the saved connection's SSH tunnel if any. + let (config, _tunnel) = super::connection::tunnel_any(config).await?; use super::connection::{open_mysql, open_pg, open_sqlite}; let to_names = |ts: Vec| ts.into_iter().map(|t| t.name).collect::>(); match config { @@ -2786,6 +2790,8 @@ pub async fn get_table_ddl_on_conn( schema: String, table: String, ) -> Result { + // Its own connection, through the saved connection's SSH tunnel if any. + let (config, _tunnel) = super::connection::tunnel_any(config).await?; use super::connection::{open_mysql, open_pg, open_sqlite}; if let AnyConnectionConfig::Oracle(c) = &config { let h = super::connection::open_oracle(c).await?; diff --git a/src-tauri/src/db/sqlite.rs b/src-tauri/src/db/sqlite.rs index d2fee48e..22feaa49 100644 --- a/src-tauri/src/db/sqlite.rs +++ b/src-tauri/src/db/sqlite.rs @@ -107,19 +107,110 @@ pub async fn fetch_foreign_keys(pool: &SqlitePool, table: &str) -> Result Result { + let mut conn = pool.acquire().await.map_err(|e| format!("{e}"))?; + run_sql(&mut conn, sql).await +} + +extern "C" { + /// SQLite's own cancel: the statement running on `db` stops at its next + /// step with SQLITE_INTERRUPT. Safe to call from another thread. Linked + /// from the SQLite that sqlx already builds in. + fn sqlite3_interrupt(db: *mut std::ffi::c_void); +} + +/// A connection's `sqlite3*`, sent to the Stop watcher's thread. +struct RawDb(*mut std::ffi::c_void); +// SAFETY: only ever passed to sqlite3_interrupt, which SQLite documents as +// safe to call from any thread while the connection is open; `Arm` keeps the +// call inside the window where this run holds the connection. +unsafe impl Send for RawDb {} + +/// `execute_sql`, stoppable. Stop interrupts the statement inside SQLite, so +/// it really ends: before, the future was dropped while the worker thread kept +/// running it, holding the write lock for whatever came next. +pub async fn execute_sql_cancellable( + pool: &SqlitePool, + sql: &str, + cancel: tokio::sync::oneshot::Receiver<()>, +) -> Result { + let mut conn = pool.acquire().await.map_err(|e| format!("{e}"))?; + let raw = RawDb( + conn.lock_handle().await.map_err(|e| format!("{e}"))?.as_raw_handle().as_ptr() as *mut std::ffi::c_void, + ); + let arm = super::stop::Arm::new(cancel, move || { + let raw = raw; + // SAFETY: see RawDb. The connection is checked out by this run until + // `disarm` below, and Arm never calls this after disarm. + unsafe { sqlite3_interrupt(raw.0) } + }); + let result = run_sql(&mut conn, sql).await; + let stopped = arm.disarm(); + match result { + Err(_) if stopped => Err(super::query::QUERY_CANCELLED.into()), + other => other, + } +} + +/// One row-returning statement streamed into the console's sink (and so into +/// the result store) as it arrives, stoppable like `execute_sql_cancellable`. +pub async fn stream_into( + pool: &SqlitePool, + sql: &str, + sink: &mut super::query::RowSink, + cancel: tokio::sync::oneshot::Receiver<()>, +) -> Result { + let t0 = Instant::now(); + let mut conn = pool.acquire().await.map_err(|e| format!("{e}"))?; + let raw = RawDb( + conn.lock_handle().await.map_err(|e| format!("{e}"))?.as_raw_handle().as_ptr() as *mut std::ffi::c_void, + ); + let arm = super::stop::Arm::new(cancel, move || { + let raw = raw; + // SAFETY: see RawDb and execute_sql_cancellable. + unsafe { sqlite3_interrupt(raw.0) } + }); + let mut columns: Vec = Vec::new(); + let mut total = 0usize; + let streamed: Result<(), String> = async { + let mut stream = sqlx::query(sql).fetch(&mut *conn); + while let Some(row) = stream.try_next().await.map_err(|e| format!("{e}"))? { + if total == 0 { + columns = row.columns().iter().map(|c| ColumnInfo::new(c.name(), c.type_info().name().to_lowercase())).collect(); + sink.start(columns.clone())?; + } + sink.push((0..columns.len()).map(|i| cell_to_json(&row, i)).collect())?; + total += 1; + } + Ok(()) + } + .await; + let stopped = arm.disarm(); + match streamed { + Err(_) if stopped => return Err(super::query::QUERY_CANCELLED.into()), + Err(e) => return Err(e), + Ok(()) => {} + } + if total > 0 { + sink.finish()?; + } + Ok(SqlResult { columns, rows: vec![], row_count: Some(total as i64), message: None, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }) +} + +async fn run_sql(conn: &mut sqlx::SqliteConnection, sql: &str) -> Result { let t0 = Instant::now(); let sql = sql.trim(); let head = super::sql_util::statement_head(sql); if matches!(head.as_str(), "select" | "with" | "pragma" | "explain" | "values") { - let mut stream = sqlx::query(sql).fetch(pool); + let mut stream = sqlx::query(sql).fetch(&mut *conn); // Convert each row to JSON as it streams in and drop the driver row // immediately - retaining the full Vec alongside the JSON // rows would double peak memory on a large result. let mut columns: Vec = Vec::new(); let mut data: Vec> = Vec::new(); let mut capped = false; + let mut budget = super::query::CollectBudget::default(); loop { match stream.try_next().await { @@ -131,8 +222,10 @@ pub async fn execute_sql(pool: &SqlitePool, sql: &str) -> Result= EXECUTE_SQL_MAX_ROWS { + let cells: Vec = (0..columns.len()).map(|i| cell_to_json(&row, i)).collect(); + let fits = budget.take(&cells); + data.push(cells); + if !fits || data.len() >= EXECUTE_SQL_MAX_ROWS { capped = true; break; } @@ -151,19 +244,13 @@ pub async fn execute_sql(pool: &SqlitePool, sql: &str) -> Result u16 { 22 } +// ── Loss tracking ───────────────────────────────────────────────────────────── +// +// A query that fails because its tunnel went away has two very different +// meanings. If the tunnel was already down when it started, nothing reached the +// server. If the tunnel dropped while it ran, nobody knows: a write may have +// committed before the reply was lost. The query paths read these counters +// around a run (`RunWatch`) and say which one happened. + +/// Bumped every time a tunnel's ssh process exits on its own. +static LOSSES: AtomicU64 = AtomicU64::new(0); +/// Tunnels currently down and reconnecting. +static DOWN: AtomicUsize = AtomicUsize::new(0); +/// For the `tunnel-status` events the window listens to. +static APP: OnceLock = OnceLock::new(); + +pub fn init(app: tauri::AppHandle) { + let _ = APP.set(app); +} + +/// Taken before a query runs; asked about its error afterwards. +#[derive(Clone, Copy)] +pub struct RunWatch { + losses: u64, + down: bool, +} + +impl RunWatch { + pub fn start() -> Self { + Self { losses: LOSSES.load(Ordering::SeqCst), down: DOWN.load(Ordering::SeqCst) > 0 } + } + + /// Reword a failed run's error when a tunnel explains it. `writes` is + /// whether the SQL could have changed anything. + pub fn explain(self, err: String, writes: bool) -> String { + if err == super::query::QUERY_CANCELLED { + return err; + } + if self.down { + return format!("The SSH tunnel is down and reconnecting, so this did not reach the server. Run it again once the tunnel is back. ({err})"); + } + if LOSSES.load(Ordering::SeqCst) != self.losses { + return if writes { + format!("Result unknown: the SSH tunnel dropped while this ran, so it may or may not have been applied. Check the data before running it again. ({err})") + } else { + format!("The SSH tunnel dropped while this ran. Nothing was changed; run it again once the tunnel is back. ({err})") + }; + } + err + } +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +struct TunnelEvent { + /// `user@host:port` of the SSH server. + via: String, + /// "down" (lost, reconnecting), "retrying" (a reconnect failed), "up", + /// or "closed" (the connection was left while it was down). + state: &'static str, + message: Option, +} + +fn announce(via: &str, state: &'static str, message: Option) { + if let Some(app) = APP.get() { + let _ = app.emit("tunnel-status", TunnelEvent { via: via.to_string(), state, message }); + } +} + // ── SshTunnel ───────────────────────────────────────────────────────────────── -/// A live SSH local-port-forward tunnel backed by a system `ssh -N -L` process. -/// Dropping the struct kills the SSH process automatically. +/// A live SSH local-port-forward tunnel backed by a system `ssh -N -L` process, +/// watched by a supervisor that rebuilds it on the same local port when the +/// process exits. Dropping the struct stops the supervisor, which kills ssh. pub struct SshTunnel { pub local_port: u16, - child: Child, + supervisor: tauri::async_runtime::JoinHandle<()>, + /// This tunnel is down and its supervisor is reconnecting. + down: Arc, + via: String, } impl Drop for SshTunnel { fn drop(&mut self) { - let _ = self.child.start_kill(); + // The child is spawned with kill_on_drop, and the supervisor owns it. + self.supervisor.abort(); + // Closed while down (the connection was left mid-reconnect): it no + // longer counts as down, and the window's "reconnecting" toast goes. + // Left counted, every later failed query read as a tunnel failure. + if self.down.swap(false, Ordering::SeqCst) { + DOWN.fetch_sub(1, Ordering::SeqCst); + announce(&self.via, "closed", None); + } } } @@ -56,104 +141,569 @@ fn find_free_port() -> Result { Ok(port) } +/// The last lines ssh wrote to stderr: the only place it says *why*. +type StderrTail = Arc>>; + +fn args_for(ssh: &SshConfig, forward: &str) -> Vec { + let mut args: Vec = vec![ + "-N".into(), + // Kill immediately if port-forward binding fails + "-o".into(), "ExitOnForwardFailure=yes".into(), + // Never prompt: there is no terminal to answer, so a passphrase or + // password prompt would hang until the deadline and then say nothing. + "-o".into(), "BatchMode=yes".into(), + // Accept new hosts but reject changed fingerprints + "-o".into(), "StrictHostKeyChecking=accept-new".into(), + "-o".into(), "ServerAliveCountMax=3".into(), + "-o".into(), "ConnectTimeout=10".into(), + "-p".into(), ssh.port.to_string(), + "-L".into(), forward.to_string(), + ]; + // Keep-alive so long-idle tunnels don't stall the pool. Configurable + // because the useful value is a property of the network in between, not + // of the database: 30s is right behind a NAT, pointless on a LAN, and + // too slow for a load balancer that reaps at 20. + if ssh.keepalive > 0 { + args.push("-o".into()); + args.push(format!("ServerAliveInterval={}", ssh.keepalive)); + } + if !ssh.private_key_path.trim().is_empty() { + args.push("-i".into()); + args.push(ssh.private_key_path.clone()); + } + // Tests run against a throwaway sshd and must not write to ~/.ssh. + #[cfg(test)] + if let Ok(file) = std::env::var("STROKE_TEST_KNOWN_HOSTS") { + args.push("-o".into()); + args.push(format!("UserKnownHostsFile={file}")); + } + args.push(format!("{}@{}", ssh.username, ssh.host)); + args +} + +/// Turn what ssh printed into a sentence someone can act on. +fn explain_ssh_failure(ssh: &SshConfig, tail: &[String], fallback: &str) -> String { + let all = tail.join("\n"); + let has = |needle: &str| all.contains(needle); + let who = format!("{}@{}:{}", ssh.username, ssh.host, ssh.port); + let reason = if has("REMOTE HOST IDENTIFICATION HAS CHANGED") || has("Host key verification failed") { + format!("The host key for {} has changed since it was last seen. If the server was rebuilt, remove its old entry from ~/.ssh/known_hosts; otherwise do not connect.", ssh.host) + } else if has("Permission denied") { + if ssh.private_key_path.trim().is_empty() { + format!("{who} rejected the login. No key file is set, so ssh used your agent: check the key is loaded (ssh-add -l) and allowed on the server.") + } else { + format!("{who} rejected the key {}. Check it is the right key and that it has no passphrase, or load it into your SSH agent.", ssh.private_key_path) + } + } else if has("Could not resolve hostname") { + format!("Could not find the SSH host {}. Check the name.", ssh.host) + } else if has("Connection refused") { + format!("{} refused the SSH connection on port {}. Check the port and that sshd is running.", ssh.host, ssh.port) + } else if has("timed out") { + format!("No answer from {} on port {} within 10 seconds. Check the host is reachable and the port is not blocked.", ssh.host, ssh.port) + } else if has("Address already in use") || has("cannot listen to port") { + "The local end of the tunnel could not bind its port. Another program may have taken it.".to_string() + } else if has("Identity file") && has("not accessible") { + format!("The key file {} could not be read. Check the path.", ssh.private_key_path) + } else { + return match tail.iter().rev().find(|l| !l.trim().is_empty()) { + Some(line) => format!("SSH tunnel to {who} failed: {}", line.trim()), + None => fallback.to_string(), + }; + }; + reason +} + +/// Spawn ssh forwarding `local_port` and wait until the port answers, ssh +/// exits (fail with its own reason), or the deadline passes. +async fn spawn_ready(ssh: &SshConfig, local_port: u16, db_host: &str, db_port: u16) -> Result<(Child, StderrTail), String> { + let forward = format!("{local_port}:{db_host}:{db_port}"); + let mut cmd = Command::new("ssh"); + cmd.args(args_for(ssh, &forward)) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .kill_on_drop(true); + // quiet: the tunnel is a background process the user never interacts with, + // so on Windows it must not get a console window of its own. + let mut child = crate::proc::quiet(&mut cmd) + .spawn() + .map_err(|e| format!("Failed to launch SSH: {e}. Ensure the 'ssh' binary is on your PATH."))?; + + let tail: StderrTail = Arc::new(Mutex::new(VecDeque::new())); + if let Some(stderr) = child.stderr.take() { + let tail = tail.clone(); + tauri::async_runtime::spawn(async move { + let mut lines = BufReader::new(stderr).lines(); + while let Ok(Some(line)) = lines.next_line().await { + let mut t = tail.lock().unwrap_or_else(|e| e.into_inner()); + if t.len() == 20 { + t.pop_front(); + } + t.push_back(line); + } + }); + } + + // Poll until the forwarded port accepts connections (up to 10 s). + let deadline = tokio::time::Instant::now() + Duration::from_secs(10); + loop { + if let Ok(Some(_)) = child.try_wait() { + // Give the reader a moment to collect the last lines. + tokio::time::sleep(Duration::from_millis(100)).await; + let lines: Vec = tail.lock().unwrap_or_else(|e| e.into_inner()).iter().cloned().collect(); + return Err(explain_ssh_failure(ssh, &lines, &format!("SSH exited before the tunnel to {}@{} was up.", ssh.username, ssh.host))); + } + if tokio::net::TcpStream::connect(format!("127.0.0.1:{local_port}")).await.is_ok() { + return Ok((child, tail)); + } + if tokio::time::Instant::now() >= deadline { + let lines: Vec = tail.lock().unwrap_or_else(|e| e.into_inner()).iter().cloned().collect(); + return Err(explain_ssh_failure( + ssh, + &lines, + &format!( + "SSH tunnel to {}@{} timed out - check credentials, host reachability, and that port {} is not blocked", + ssh.username, ssh.host, ssh.port + ), + )); + } + tokio::time::sleep(Duration::from_millis(250)).await; + } +} + +/// Backoff between reconnect attempts: 1, 2, 4, 8, 16, then every 30 s. +fn backoff(attempt: u32) -> Duration { + Duration::from_secs((1u64 << attempt.min(5)).min(30)) +} + impl SshTunnel { - /// Spawn `ssh -N -L {local_port}:{db_host}:{db_port} …` and block until - /// the forwarded port is reachable (or the 10 s deadline is hit). + /// Spawn `ssh -N -L {local_port}:{db_host}:{db_port} …`, block until the + /// forwarded port is reachable, then supervise it for as long as the + /// tunnel lives. pub async fn establish(ssh: &SshConfig, db_host: &str, db_port: u16) -> Result { let local_port = find_free_port()?; - let forward = format!("{local_port}:{db_host}:{db_port}"); - let dest = format!("{}@{}", ssh.username, ssh.host); - - let mut args: Vec = vec![ - "-N".into(), - // Kill immediately if port-forward binding fails - "-o".into(), "ExitOnForwardFailure=yes".into(), - // Accept new hosts but reject changed fingerprints - "-o".into(), "StrictHostKeyChecking=accept-new".into(), - "-o".into(), "ServerAliveCountMax=3".into(), - "-o".into(), "ConnectTimeout=10".into(), - "-p".into(), ssh.port.to_string(), - "-L".into(), forward, - ]; - - // Keep-alive so long-idle tunnels don't stall the pool. Configurable - // because the useful value is a property of the network in between, not - // of the database: 30s is right behind a NAT, pointless on a LAN, and - // too slow for a load balancer that reaps at 20. - if ssh.keepalive > 0 { - args.push("-o".into()); - args.push(format!("ServerAliveInterval={}", ssh.keepalive)); - } + let (child, tail) = spawn_ready(ssh, local_port, db_host, db_port).await?; + let down = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let supervisor = tauri::async_runtime::spawn(supervise(ssh.clone(), local_port, db_host.to_string(), db_port, child, tail, down.clone())); + Ok(SshTunnel { local_port, supervisor, down, via: format!("{}@{}:{}", ssh.username, ssh.host, ssh.port) }) + } +} - if !ssh.private_key_path.trim().is_empty() { - args.push("-i".into()); - args.push(ssh.private_key_path.clone()); +/// Wait on ssh; when it exits on its own, say so and bring it back on the same +/// local port, so the pool's address stays valid and its keepalive replaces the +/// dead connections without anyone reconnecting by hand. +async fn supervise( + ssh: SshConfig, + local_port: u16, + db_host: String, + db_port: u16, + mut child: Child, + mut tail: StderrTail, + down: Arc, +) { + let via = format!("{}@{}:{}", ssh.username, ssh.host, ssh.port); + loop { + let _ = child.wait().await; + LOSSES.fetch_add(1, Ordering::SeqCst); + if !down.swap(true, Ordering::SeqCst) { + DOWN.fetch_add(1, Ordering::SeqCst); } + let lines: Vec = tail.lock().unwrap_or_else(|e| e.into_inner()).iter().cloned().collect(); + let why = explain_ssh_failure(&ssh, &lines, "The SSH connection closed."); + log::info!("ssh tunnel {via} lost: {why}"); + announce(&via, "down", Some(why)); - args.push(dest); - - let mut cmd = Command::new("ssh"); - cmd.args(&args) - .stdin(std::process::Stdio::null()) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::null()); - // quiet: the tunnel is a background process the user never interacts with - // (all three streams are already closed), so on Windows it must not get a - // console window of its own. - let child = crate::proc::quiet(&mut cmd) - .spawn() - .map_err(|e| { - format!( - "Failed to launch SSH: {e}. \ - Ensure the 'ssh' binary is on your PATH." - ) - })?; - - // Poll until the forwarded port accepts connections (up to 10 s). - let deadline = tokio::time::Instant::now() + Duration::from_secs(10); + let mut attempt = 0; loop { - if tokio::net::TcpStream::connect(format!("127.0.0.1:{local_port}")) - .await - .is_ok() - { - break; - } - if tokio::time::Instant::now() >= deadline { - return Err(format!( - "SSH tunnel to {}@{} timed out - \ - check credentials, host reachability, and that port {} is not blocked", - ssh.username, ssh.host, ssh.port - )); + tokio::time::sleep(backoff(attempt)).await; + match spawn_ready(&ssh, local_port, &db_host, db_port).await { + Ok((c, t)) => { + child = c; + tail = t; + break; + } + Err(e) => { + attempt += 1; + announce(&via, "retrying", Some(e)); + } } - tokio::time::sleep(Duration::from_millis(250)).await; } + if down.swap(false, Ordering::SeqCst) { + DOWN.fetch_sub(1, Ordering::SeqCst); + } + log::info!("ssh tunnel {via} back on 127.0.0.1:{local_port}"); + announce(&via, "up", None); + } +} - Ok(SshTunnel { local_port, child }) +// ── HTTP engines through a tunnel ───────────────────────────────────────────── + +/// A base URL pointed through `via`: the port becomes the tunnel's, and the +/// host stays the server's name (TLS checks it; `http_client` resolves it to +/// the tunnel). An IP address has no name to keep, so it becomes 127.0.0.1. +pub fn url_via(mut url: reqwest::Url, via: Option) -> reqwest::Url { + let Some(via) = via else { return url }; + let is_ip = url.host_str().map(|h| h.trim_matches(['[', ']'])).and_then(|h| h.parse::().ok()).is_some(); + if is_ip { + let _ = url.set_host(Some("127.0.0.1")); + } + let _ = url.set_port(Some(via.port())); + url +} + +/// The HTTP client for a server reached through `via`: its name resolves to +/// the tunnel. One per (name, tunnel), kept. None without a tunnel, so the +/// caller keeps its shared client. +pub fn http_client(host: &str, via: Option, insecure: bool) -> Option { + static CLIENTS: std::sync::LazyLock>> = + std::sync::LazyLock::new(Default::default); + let via = via?; + let key = (host.to_string(), via, insecure); + let mut map = CLIENTS.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(c) = map.get(&key) { + return Some(c.clone()); + } + let c = reqwest::Client::builder() + .timeout(Duration::from_secs(60)) + .connect_timeout(Duration::from_secs(10)) + .danger_accept_invalid_certs(insecure) + .resolve(host, via) + .build() + .ok()?; + map.insert(key, c.clone()); + Some(c) +} + +/// Host and port of an HTTP(S) URL, the port defaulting by scheme. +pub fn url_target(url: &str) -> Result<(String, u16), String> { + let raw = url.trim().trim_end_matches('/'); + let raw = if raw.contains("://") { raw.to_string() } else { format!("http://{raw}") }; + let u = reqwest::Url::parse(&raw).map_err(|e| format!("Invalid URL \"{raw}\": {e}"))?; + let host = u.host_str().ok_or_else(|| format!("No host in \"{raw}\""))?.trim_matches(['[', ']']).to_string(); + let port = u.port_or_known_default().unwrap_or(80); + Ok((host, port)) +} + +/// A `mongodb://` URI pointed at `local`: the first host replaced by the +/// tunnel and `directConnection=true`, since a replica set would otherwise +/// hand back member names this machine can't reach. Refuses what a tunnel +/// can't carry: `mongodb+srv://` (several hosts found by DNS) and TLS (the +/// driver can't dial one address while verifying another name). +pub fn mongo_uri_via(uri: &str, local: u16) -> Result { + let uri = uri.trim(); + if uri.starts_with("mongodb+srv://") { + return Err("An SSH tunnel needs a mongodb:// connection string with a host and port; mongodb+srv:// finds its hosts through DNS. Use the standard connection string from your provider.".into()); + } + let rest = uri.strip_prefix("mongodb://").ok_or("The connection string starts with mongodb://")?; + let (auth, after_auth) = match rest.rfind('@') { + Some(i) if rest[..i].find('/').is_none() => (&rest[..=i], &rest[i + 1..]), + _ => ("", rest), + }; + let end = after_auth.find(['/', '?']).unwrap_or(after_auth.len()); + let tail = &after_auth[end..]; + let query = tail.split_once('?').map(|(_, q)| q).unwrap_or(""); + let tls = query.split('&').any(|kv| { + let (k, v) = kv.split_once('=').unwrap_or((kv, "")); + (k.eq_ignore_ascii_case("tls") || k.eq_ignore_ascii_case("ssl")) && v.eq_ignore_ascii_case("true") + }); + if tls { + return Err("MongoDB with TLS can't go through an SSH tunnel here: the driver would check the certificate against 127.0.0.1. Turn TLS off for a server only reachable through the tunnel, or connect without SSH.".into()); + } + let path = if tail.is_empty() { "/".to_string() } else if tail.starts_with('?') { format!("/{tail}") } else { tail.to_string() }; + let sep = if path.contains('?') { "&" } else { "?" }; + let direct = if query.to_ascii_lowercase().contains("directconnection=") { String::new() } else { format!("{sep}directConnection=true") }; + Ok(format!("mongodb://{auth}127.0.0.1:{local}{path}{direct}")) +} + +/// The first host of a `mongodb://` URI, port defaulting to 27017. +pub fn mongo_first_host(uri: &str) -> Option<(String, u16)> { + let rest = uri.trim().strip_prefix("mongodb://")?; + let after_auth = match rest.rfind('@') { + Some(i) if rest[..i].find('/').is_none() => &rest[i + 1..], + _ => rest, + }; + let hosts = &after_auth[..after_auth.find(['/', '?']).unwrap_or(after_auth.len())]; + let first = hosts.split(',').next()?.trim(); + if let Some(v6) = first.strip_prefix('[') { + let (h, rest) = v6.split_once(']')?; + let port = rest.strip_prefix(':').and_then(|p| p.parse().ok()).unwrap_or(27017); + return Some((h.to_string(), port)); + } + match first.rsplit_once(':') { + Some((h, p)) => Some((h.to_string(), p.parse().ok()?)), + None => Some((first.to_string(), 27017)), } } // ── TunnelState ─────────────────────────────────────────────────────────────── -/// Tauri-managed state holding the currently active SSH tunnel (if any). -/// Set on connect, cleared on disconnect; Drop kills the ssh process. -pub struct TunnelState { - inner: std::sync::Mutex>, +/// The tunnel under the open connection, and the tunnels under connections +/// Labs has set aside (by the same key as the parked pool). A tunnel moves with +/// its connection: before, there was one slot, so connecting anything through +/// SSH killed the tunnel a parked connection still depended on, and switching +/// to an engine without SSH left the old tunnel's ssh running. +#[derive(Default)] +struct Tunnels { + active: Option, + parked: std::collections::HashMap, +} + +static TUNNELS: std::sync::LazyLock> = std::sync::LazyLock::new(Default::default); + +fn tunnels() -> std::sync::MutexGuard<'static, Tunnels> { + TUNNELS.lock().unwrap_or_else(|e| e.into_inner()) +} + +/// The open connection is being set aside under `key`: so is its tunnel. +pub fn park_active(key: &str) { + let mut t = tunnels(); + match t.active.take() { + Some(tunnel) => { + t.parked.insert(key.to_string(), tunnel); + } + None => { + t.parked.remove(key); + } + } } +/// The connection under `key` is open again: its tunnel is the active one. +pub fn resume_parked(key: &str) { + let mut t = tunnels(); + let tunnel = t.parked.remove(key); + t.active = tunnel; +} + +/// Parked connections were closed: close their tunnels. +pub fn drop_parked(keys: &[String]) { + let mut t = tunnels(); + for k in keys { + t.parked.remove(k); + } +} + +/// The open connection is closing: close its tunnel. +pub fn drop_active() { + tunnels().active = None; +} + +/// Tauri-managed handle on the active tunnel (kept so commands can take it as +/// state). Set on connect, cleared on disconnect; dropping a tunnel stops its +/// supervisor and ssh. +pub struct TunnelState; + impl TunnelState { pub fn new() -> Self { - Self { - inner: std::sync::Mutex::new(None), - } + Self } pub fn set(&self, tunnel: Option) { - if let Ok(mut g) = self.inner.lock() { - *g = tunnel; - } + tunnels().active = tunnel; } pub fn clear(&self) { self.set(None); } } + +#[cfg(test)] +mod tests { + use super::*; + + fn cfg(key: &str) -> SshConfig { + SshConfig { host: "bastion".into(), port: 22, username: "me".into(), private_key_path: key.into(), keepalive: 30 } + } + + #[test] + fn names_the_real_reason() { + let key = explain_ssh_failure(&cfg("~/.ssh/id"), &["me@bastion: Permission denied (publickey).".into()], "x"); + assert!(key.contains("rejected the key ~/.ssh/id"), "{key}"); + let agent = explain_ssh_failure(&cfg(""), &["Permission denied (publickey).".into()], "x"); + assert!(agent.contains("ssh-add -l"), "{agent}"); + let changed = explain_ssh_failure(&cfg(""), &["@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @".into()], "x"); + assert!(changed.contains("known_hosts"), "{changed}"); + let other = explain_ssh_failure(&cfg(""), &["".to_string(), "kex_exchange_identification: read: Connection reset".into(), "".into()], "x"); + assert!(other.ends_with("kex_exchange_identification: read: Connection reset"), "{other}"); + assert_eq!(explain_ssh_failure(&cfg(""), &[], "fallback"), "fallback"); + } + + // Live: a tunnel whose ssh dies comes back on the same local port, and a + // bad key says so. Needs an sshd that forwards to a Postgres: + // STROKE_TEST_SSH=user@127.0.0.1:2222 STROKE_TEST_SSH_KEY=/path/key + // STROKE_TEST_SSH_WRONG_KEY=/path/other STROKE_TEST_KNOWN_HOSTS=/tmp/kh + // STROKE_TEST_PG_PORT=5433 cargo test --lib ssh_tunnel::tests::live_ -- --nocapture + #[tokio::test] + async fn live_tunnel_comes_back_after_ssh_dies() { + let (Ok(target), Ok(key), Ok(pg_port)) = ( + std::env::var("STROKE_TEST_SSH"), + std::env::var("STROKE_TEST_SSH_KEY"), + std::env::var("STROKE_TEST_PG_PORT"), + ) else { + return; + }; + let (user, hostport) = target.split_once('@').unwrap(); + let (host, port) = hostport.split_once(':').unwrap(); + let ssh = SshConfig { host: host.into(), port: port.parse().unwrap(), username: user.into(), private_key_path: key, keepalive: 5 }; + let pg_port: u16 = pg_port.parse().unwrap(); + + let tunnel = SshTunnel::establish(&ssh, "127.0.0.1", pg_port).await.unwrap(); + let url = format!("postgres://postgres@127.0.0.1:{}/postgres", tunnel.local_port); + let pool = sqlx::postgres::PgPoolOptions::new().max_connections(2).connect(&url).await.unwrap(); + let one: i32 = sqlx::query_scalar("SELECT 1").fetch_one(&pool).await.unwrap(); + assert_eq!(one, 1); + + // A dropped network: ssh exits on its own. + let watch = RunWatch::start(); + let killed = std::process::Command::new("pkill") + .args(["-f", &format!("{}:127.0.0.1:{pg_port} ", tunnel.local_port)]) + .status() + .unwrap(); + assert!(killed.success(), "found the tunnel's ssh to kill"); + tokio::time::sleep(Duration::from_millis(300)).await; + assert!(watch.explain("x".into(), true).starts_with("Result unknown"), "the loss is counted"); + assert!(RunWatch::start().down, "the tunnel reports down while it reconnects"); + + // Back on the same port within the first backoff (1 s) plus a connect. + let deadline = tokio::time::Instant::now() + Duration::from_secs(15); + loop { + if DOWN.load(Ordering::SeqCst) == 0 { + break; + } + assert!(tokio::time::Instant::now() < deadline, "tunnel did not come back"); + tokio::time::sleep(Duration::from_millis(200)).await; + } + // The pool's address never changed: a fresh connection just works. + let pool2 = sqlx::postgres::PgPoolOptions::new().max_connections(1).connect(&url).await.unwrap(); + let two: i32 = sqlx::query_scalar("SELECT 2").fetch_one(&pool2).await.unwrap(); + assert_eq!(two, 2); + drop(tunnel); + } + + // Live: Labs parks a tunnelled connection and opens another through SSH. + // The parked one's tunnel must survive, come back on resume, and the one + // in front at that point must close. + #[tokio::test] + async fn live_parked_tunnels_survive_and_come_back() { + let (Ok(target), Ok(key), Ok(pg_port)) = ( + std::env::var("STROKE_TEST_SSH"), + std::env::var("STROKE_TEST_SSH_KEY"), + std::env::var("STROKE_TEST_PG_PORT"), + ) else { + return; + }; + let (user, hostport) = target.split_once('@').unwrap(); + let (host, port) = hostport.split_once(':').unwrap(); + let ssh = SshConfig { host: host.into(), port: port.parse().unwrap(), username: user.into(), private_key_path: key, keepalive: 30 }; + let pg_port: u16 = pg_port.parse().unwrap(); + let up = |p: u16| async move { tokio::net::TcpStream::connect(("127.0.0.1", p)).await.is_ok() }; + + let a = SshTunnel::establish(&ssh, "127.0.0.1", pg_port).await.unwrap(); + let a_port = a.local_port; + let state = TunnelState::new(); + state.set(Some(a)); + park_active("conn-a|db"); + + // Connecting B: what connect does (close_existing, then set). + drop_active(); + let b = SshTunnel::establish(&ssh, "127.0.0.1", pg_port).await.unwrap(); + let b_port = b.local_port; + state.set(Some(b)); + assert!(up(a_port).await, "the parked connection's tunnel must stay up"); + + // Back to A: B is set aside by the page first in real use, but a + // resume alone replaces whatever was in front. + resume_parked("conn-a|db"); + tokio::time::sleep(Duration::from_millis(300)).await; + assert!(up(a_port).await, "A's tunnel is in front again"); + assert!(!up(b_port).await, "B's tunnel closed with it"); + + drop_active(); + tokio::time::sleep(Duration::from_millis(300)).await; + assert!(!up(a_port).await); + } + + // Live: leaving a connection while its tunnel is down must not leave the + // tunnel counted as down (later failures would all read as tunnel loss). + #[tokio::test] + async fn live_dropping_a_down_tunnel_settles_it() { + let (Ok(target), Ok(key), Ok(pg_port), Ok(pidfile)) = ( + std::env::var("STROKE_TEST_SSH"), + std::env::var("STROKE_TEST_SSH_KEY"), + std::env::var("STROKE_TEST_PG_PORT"), + std::env::var("STROKE_TEST_SSHD_PID"), + ) else { + return; + }; + let (user, hostport) = target.split_once('@').unwrap(); + let (host, port) = hostport.split_once(':').unwrap(); + let ssh = SshConfig { host: host.into(), port: port.parse().unwrap(), username: user.into(), private_key_path: key, keepalive: 30 }; + let t = SshTunnel::establish(&ssh, "127.0.0.1", pg_port.parse().unwrap()).await.unwrap(); + // Take the server away and kill the tunnel: it stays down, retrying. + let sshd = std::fs::read_to_string(&pidfile).unwrap().trim().to_string(); + std::process::Command::new("kill").arg(&sshd).status().unwrap(); + std::process::Command::new("pkill").args(["-f", &format!("{}:127.0.0.1:{pg_port} ", t.local_port)]).status().unwrap(); + tokio::time::sleep(Duration::from_millis(500)).await; + assert!(t.down.load(Ordering::SeqCst)); + let before = DOWN.load(Ordering::SeqCst); + drop(t); + assert_eq!(DOWN.load(Ordering::SeqCst), before - 1); + } + + #[tokio::test] + async fn live_wrong_key_says_rejected() { + let (Ok(target), Ok(key)) = (std::env::var("STROKE_TEST_SSH"), std::env::var("STROKE_TEST_SSH_WRONG_KEY")) else { + return; + }; + let (user, hostport) = target.split_once('@').unwrap(); + let (host, port) = hostport.split_once(':').unwrap(); + let ssh = SshConfig { host: host.into(), port: port.parse().unwrap(), username: user.into(), private_key_path: key.clone(), keepalive: 30 }; + let err = SshTunnel::establish(&ssh, "127.0.0.1", 5433).await.err().expect("a wrong key must fail"); + assert!(err.contains(&format!("rejected the key {key}")), "{err}"); + } + + #[test] + fn points_a_mongo_uri_at_the_tunnel() { + assert_eq!( + mongo_uri_via("mongodb://u:p%40ss@db1:27018,db2:27019/shop?replicaSet=rs0", 40001).unwrap(), + "mongodb://u:p%40ss@127.0.0.1:40001/shop?replicaSet=rs0&directConnection=true" + ); + assert_eq!(mongo_uri_via("mongodb://db1", 40001).unwrap(), "mongodb://127.0.0.1:40001/?directConnection=true"); + assert_eq!(mongo_uri_via("mongodb://db1/?directConnection=false", 7).unwrap(), "mongodb://127.0.0.1:7/?directConnection=false"); + assert!(mongo_uri_via("mongodb+srv://c.example.net/shop", 7).unwrap_err().contains("mongodb+srv")); + assert!(mongo_uri_via("mongodb://db1/?tls=true", 7).unwrap_err().contains("TLS")); + assert_eq!(mongo_first_host("mongodb://u:p@db1:27018,db2/shop"), Some(("db1".into(), 27018))); + assert_eq!(mongo_first_host("mongodb://db1"), Some(("db1".into(), 27017))); + assert_eq!(mongo_first_host("mongodb://[::1]:27020/x"), Some(("::1".into(), 27020))); + } + + #[test] + fn points_an_http_url_at_the_tunnel() { + assert_eq!(url_target("https://es.example.com").unwrap(), ("es.example.com".into(), 443)); + assert_eq!(url_target("couch.local:5984").unwrap(), ("couch.local".into(), 5984)); + let via: std::net::SocketAddr = "127.0.0.1:41000".parse().unwrap(); + let named = url_via(reqwest::Url::parse("https://es.example.com:9200/").unwrap(), Some(via)); + assert_eq!(named.as_str(), "https://es.example.com:41000/", "the name stays for TLS"); + let ip = url_via(reqwest::Url::parse("http://10.0.0.5:5984/").unwrap(), Some(via)); + assert_eq!(ip.as_str(), "http://127.0.0.1:41000/"); + let direct = url_via(reqwest::Url::parse("http://10.0.0.5:5984/").unwrap(), None); + assert_eq!(direct.as_str(), "http://10.0.0.5:5984/"); + } + + #[test] + fn backs_off_to_thirty_seconds() { + let s: Vec = (0..8).map(|a| backoff(a).as_secs()).collect(); + assert_eq!(s, vec![1, 2, 4, 8, 16, 30, 30, 30]); + } + + #[test] + fn says_whether_a_failed_run_reached_the_server() { + let before = RunWatch { losses: LOSSES.load(Ordering::SeqCst), down: false }; + assert_eq!(before.explain("boom".into(), true), "boom"); + LOSSES.fetch_add(1, Ordering::SeqCst); + assert!(before.explain("boom".into(), true).starts_with("Result unknown")); + assert!(before.explain("boom".into(), false).contains("Nothing was changed")); + let down = RunWatch { losses: 0, down: true }; + assert!(down.explain("refused".into(), true).contains("did not reach the server")); + assert_eq!(down.explain(crate::db::query::QUERY_CANCELLED.into(), true), crate::db::query::QUERY_CANCELLED); + } +} diff --git a/src-tauri/src/db/stop.rs b/src-tauri/src/db/stop.rs new file mode 100644 index 00000000..712ad03e --- /dev/null +++ b/src-tauri/src/db/stop.rs @@ -0,0 +1,211 @@ +//! Stop for the console, shared by every engine that is not Postgres. +//! +//! The window sends one Stop per run (a oneshot), but a script runs several +//! statements and each engine wants the signal in its own shape: a receiver, +//! a future to race, or a callback on another thread. `Stop` turns the one +//! signal into as many of those as a run needs. +//! +//! `Arm` is the callback shape, for drivers that interrupt a running call from +//! another thread (SQLite's `sqlite3_interrupt`, DuckDB's `InterruptHandle`). +//! The interrupt goes to whatever the connection is running *now*, so it must +//! never fire after the run is over and the connection has moved on to the next +//! caller: `disarm` and the watcher take the same lock, and the watcher checks +//! `done` under it. + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use tokio::sync::{oneshot, Notify}; + +#[derive(Clone)] +pub struct Stop { + fired: Arc, + notify: Arc, +} + +impl Stop { + /// Listen on the run's Stop. If the run ends first, the sender is dropped + /// and the listener exits. + pub fn from_rx(rx: oneshot::Receiver<()>) -> Self { + let stop = Self { fired: Arc::new(AtomicBool::new(false)), notify: Arc::new(Notify::new()) }; + let s = stop.clone(); + tauri::async_runtime::spawn(async move { + if rx.await.is_ok() { + s.fired.store(true, Ordering::SeqCst); + s.notify.notify_waiters(); + } + }); + stop + } + + pub fn fired(&self) -> bool { + self.fired.load(Ordering::SeqCst) + } + + /// Resolves once Stop is pressed (at once if it already was). + pub async fn wait(&self) { + loop { + let notified = self.notify.notified(); + tokio::pin!(notified); + notified.as_mut().enable(); + if self.fired() { + return; + } + notified.await; + } + } + + /// A receiver for one statement. Its forwarder exits when the statement + /// drops the receiver, so an unpressed Stop leaves nothing behind. + pub fn receiver(&self) -> oneshot::Receiver<()> { + let (mut tx, rx) = oneshot::channel(); + let s = self.clone(); + tauri::async_runtime::spawn(async move { + tokio::select! { + _ = s.wait() => { let _ = tx.send(()); } + _ = tx.closed() => {} + } + }); + rx + } +} + +#[derive(Default)] +struct Flags { + done: bool, + fired: bool, +} + +/// An interrupt armed for one run on one connection. +pub struct Arm { + flags: Arc>, +} + +impl Arm { + pub fn new(rx: oneshot::Receiver<()>, interrupt: F) -> Self + where + F: FnOnce() + Send + 'static, + { + let flags = Arc::new(Mutex::new(Flags::default())); + let f = flags.clone(); + tauri::async_runtime::spawn(async move { + if rx.await.is_err() { + return; + } + let mut g = f.lock().unwrap_or_else(|e| e.into_inner()); + if !g.done { + g.fired = true; + interrupt(); + } + }); + Self { flags } + } + + /// The run is over: no interrupt can reach the connection from here on. + /// True when Stop interrupted it. + pub fn disarm(self) -> bool { + let mut g = self.flags.lock().unwrap_or_else(|e| e.into_inner()); + g.done = true; + g.fired + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::Duration; + + #[tokio::test] + async fn one_stop_reaches_every_statement() { + let (tx, rx) = oneshot::channel(); + let stop = Stop::from_rx(rx); + let a = stop.receiver(); + let b = stop.receiver(); + tx.send(()).unwrap(); + tokio::time::timeout(Duration::from_secs(1), stop.wait()).await.unwrap(); + assert!(a.await.is_ok() && b.await.is_ok()); + // A receiver taken after Stop still fires. + assert!(stop.receiver().await.is_ok()); + } + + #[tokio::test] + async fn an_arm_never_fires_after_disarm() { + let (tx, rx) = oneshot::channel(); + let hit = Arc::new(AtomicBool::new(false)); + let h = hit.clone(); + let arm = Arm::new(rx, move || h.store(true, Ordering::SeqCst)); + assert!(!arm.disarm()); + let _ = tx.send(()); + tokio::time::sleep(Duration::from_millis(50)).await; + assert!(!hit.load(Ordering::SeqCst), "a late Stop must not interrupt the connection's next user"); + } + + #[tokio::test] + async fn an_arm_fires_while_armed() { + let (tx, rx) = oneshot::channel(); + let hit = Arc::new(AtomicBool::new(false)); + let h = hit.clone(); + let arm = Arm::new(rx, move || h.store(true, Ordering::SeqCst)); + tx.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(50)).await; + assert!(hit.load(Ordering::SeqCst)); + assert!(arm.disarm()); + } +} + +/// Stop has to end the statement, not just the wait for it. +#[cfg(test)] +mod engine_tests { + use std::time::{Duration, Instant}; + use tokio::sync::oneshot; + + /// Runs forever unless interrupted. + const SQLITE_FOREVER: &str = + "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT count(*) FROM c WHERE x < 0"; + + #[tokio::test] + async fn sqlite_stop_interrupts_the_statement() { + let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await.unwrap(); + let (tx, rx) = oneshot::channel(); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(300)).await; + let _ = tx.send(()); + }); + let t = Instant::now(); + let r = crate::db::sqlite::execute_sql_cancellable(&pool, SQLITE_FOREVER, rx).await; + assert_eq!(r.unwrap_err(), crate::db::query::QUERY_CANCELLED); + assert!(t.elapsed() < Duration::from_secs(3), "took {:?}", t.elapsed()); + // The one connection is free and clean: the statement really ended. + let r = crate::db::sqlite::execute_sql(&pool, "SELECT 1").await.unwrap(); + assert_eq!(r.row_count, Some(1)); + } + + #[tokio::test] + async fn sqlite_finishes_when_nobody_stops_it() { + let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await.unwrap(); + let (_tx, rx) = oneshot::channel::<()>(); + let r = crate::db::sqlite::execute_sql_cancellable(&pool, "SELECT 42", rx).await.unwrap(); + assert_eq!(r.rows[0][0], serde_json::json!(42)); + } + + #[tokio::test] + async fn duckdb_stop_interrupts_the_statement() { + let cfg = crate::db::connection::DuckdbConfig { name: "t".into(), file_path: ":memory:".into() }; + let h = crate::db::connection::open_duckdb(&cfg).await.unwrap(); + let (tx, rx) = oneshot::channel(); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(300)).await; + let _ = tx.send(()); + }); + let t = Instant::now(); + let r = crate::db::duckdb::execute_sql_cancellable( + &h, + "SELECT count(*) FROM range(100000000) a, range(100000000) b WHERE a.range + b.range < 0", + rx, + ) + .await; + assert_eq!(r.unwrap_err(), crate::db::query::QUERY_CANCELLED); + assert!(t.elapsed() < Duration::from_secs(5), "took {:?}", t.elapsed()); + let r = crate::db::duckdb::execute_sql(&h, "SELECT 1").await.unwrap(); + assert_eq!(r.row_count, Some(1)); + } +} diff --git a/src-tauri/src/db/terminal.rs b/src-tauri/src/db/terminal.rs index 105d4c27..f903ffdd 100644 --- a/src-tauri/src/db/terminal.rs +++ b/src-tauri/src/db/terminal.rs @@ -480,6 +480,10 @@ async fn pg_launch(config: PgConfig) -> Result { ("PGDATABASE", c.database.clone()), ("PGAPPNAME", "Stroke".to_string()), ]; + // Through a tunnel: dial it, and keep PGHOST for TLS verification. + if let Some(via) = c.via { + env.push(("PGHOSTADDR", via.ip().to_string())); + } if !c.password.is_empty() { env.push(("PGPASSWORD", c.password.clone())); } @@ -522,7 +526,9 @@ async fn pg_launch(config: PgConfig) -> Result { async fn mysql_launch(config: MysqlConfig, mariadb: bool) -> Result { let (c, tunnel) = resolve_mysql_ssh(config).await?; let mut args = vec![ - format!("--host={}", c.host), + // The mysql client has no separate dial address: through a tunnel it + // connects to the tunnel by IP. + format!("--host={}", c.via.map(|v| v.ip().to_string()).unwrap_or_else(|| c.host.clone())), format!("--port={}", c.port), format!("--user={}", c.user), "--default-character-set=utf8mb4".to_string(), @@ -1355,6 +1361,7 @@ mod tests { #[test] fn pg_password_and_ssl_travel_in_env_not_argv() { let config = PgConfig { + via: None, name: "t".into(), host: "db.example.com".into(), port: 5433, @@ -1401,6 +1408,7 @@ mod tests { #[test] fn mariadb_gets_switches_instead_of_ssl_mode() { let config = |ssl_mode: &str| MysqlConfig { + via: None, name: "t".into(), host: "h".into(), port: 3306, diff --git a/src-tauri/src/db/tx.rs b/src-tauri/src/db/tx.rs index b03fab1d..2deced08 100644 --- a/src-tauri/src/db/tx.rs +++ b/src-tauri/src/db/tx.rs @@ -7,20 +7,29 @@ the user can run a few statements, look at what they did, and then decide whether to keep them. A transaction holds a real connection (and, on most engines, locks) for as long -as it stays open, so a session that is left behind is closed automatically after -`IDLE_TIMEOUT` - see `sweep_idle`. +as it stays open, so a session that is left behind is rolled back after +`IDLE_TIMEOUT` by a timer (`sweep_idle`), and every session is rolled back when +the active connection changes (`connection_changed`). + +A session ends through `release` and nowhere else. Its `BEGIN` was a plain +statement, so sqlx does not know the connection is in a transaction: dropping +the `PoolConnection` hands it back to the pool still inside it (sqlx only pings +on release), and the next grid edit or console run that picks it up joins the +abandoned transaction. Its writes are never committed and the locks stay held. +`release` rolls back first and closes the socket if the rollback fails. */ use crate::db::connection::Db; use std::collections::HashMap; -use std::sync::Mutex; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{LazyLock, Mutex, OnceLock}; use std::time::{Duration, Instant}; use futures::TryStreamExt; use serde::Serialize; use serde_json::Value; use sqlx::{Column, Row, TypeInfo}; -use tauri::State; +use tauri::Emitter; use super::connection::{require_conn, ActiveConnection}; use super::query::{ @@ -33,25 +42,70 @@ use super::query::{ /// idle this long is rolled back and released on the next transaction command. const IDLE_TIMEOUT: Duration = Duration::from_secs(30 * 60); +/// How often the timer looks for sessions past `IDLE_TIMEOUT`. +pub const SWEEP_EVERY: Duration = Duration::from_secs(60); + +/// Postgres's own backstop, a little past ours: if this process dies or hangs +/// with a transaction open, the server ends the session itself. +const SERVER_IDLE_TIMEOUT: &str = "35min"; + +/// A ROLLBACK that takes longer than this is talking to a dead socket. +const RELEASE_TIMEOUT: Duration = Duration::from_secs(5); + /// The checked-out connection, per engine. enum TxConn { Postgres(sqlx::pool::PoolConnection), Mysql(sqlx::pool::PoolConnection), Sqlite(sqlx::pool::PoolConnection), + // These engines hold one shared connection rather than a pool, so a + // transaction opens a connection of its own beside it (see each module's + // open_tx_*): on the shared one, every grid page and sidebar query would + // have run inside the transaction. + Duckdb(super::connection::DuckdbHandle), + Mssql(super::connection::MssqlHandle), + Oracle(super::connection::OracleHandle), } impl TxConn { + /// Take the connection out of the pool for good. Dropping the raw + /// connection closes its socket, which makes the server roll back. + fn close_hard(self) { + match self { + TxConn::Postgres(c) => drop(c.detach()), + TxConn::Mysql(c) => drop(c.detach()), + TxConn::Sqlite(c) => drop(c.detach()), + // Their own connection: dropping it closes it, and the server + // rolls back what it held. + TxConn::Duckdb(h) => drop(h), + TxConn::Mssql(h) => drop(h), + TxConn::Oracle(h) => drop(h), + } + } + fn engine(&self) -> &'static str { match self { TxConn::Postgres(_) => "postgres", TxConn::Mysql(_) => "mysql", TxConn::Sqlite(_) => "sqlite", + TxConn::Duckdb(_) => "duckdb", + TxConn::Mssql(_) => "mssql", + TxConn::Oracle(_) => "oracle", } } } +/// Where to ask who this transaction is holding up: a second pooled +/// connection and this session's server-side id. +enum Blocker { + Postgres(sqlx::PgPool, i32), + Mysql(sqlx::MySqlPool, u64), +} + struct TxSession { conn: TxConn, + /// The connection epoch at BEGIN (see `connection_changed`). + epoch: u64, + blocker: Option, /// Statements run since BEGIN, for the status line. statements: usize, /// Rows reported changed by those statements. @@ -60,9 +114,101 @@ struct TxSession { last_used: Instant, } +/// Open sessions by id. Module state rather than Tauri state so a connection +/// switch (`connection_changed`, called from `db::connection`) can reach them. +static SESSIONS: LazyLock>> = LazyLock::new(Default::default); + +/// Bumped every time the active connection is replaced, parked or closed. A +/// session from an older epoch belongs to a database the window has left. +static EPOCH: AtomicU64 = AtomicU64::new(0); + +/// For the `tx-ended` event the window listens to. +static APP: OnceLock = OnceLock::new(); + +pub fn init(app: tauri::AppHandle) { + let _ = APP.set(app); +} + +/// Kept so `lib.rs` can still `.manage()` it; the sessions live in `SESSIONS`. #[derive(Default)] -pub struct TxState { - sessions: Mutex>, +pub struct TxState; + +/// Why a session ended without the user pressing Commit or Rollback. +#[derive(Debug, Clone, Copy, Serialize, PartialEq)] +#[serde(rename_all = "camelCase")] +pub enum EndReason { + Idle, + ConnectionChanged, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +struct TxEnded { + session_id: String, + reason: EndReason, + /// False when the rollback failed and the socket was closed instead (the + /// server rolls back on its own when it sees the socket go). + rolled_back: bool, +} + +/// Roll the session back and give its connection back to the pool, or close +/// it if the rollback does not come back clean. True when it rolled back. +async fn release(mut session: TxSession) -> bool { + let ok = matches!( + tokio::time::timeout(RELEASE_TIMEOUT, run_statement(&mut session.conn, "ROLLBACK")).await, + Ok(Ok(_)) + ); + if !ok { + log::info!("tx: rollback failed, closing the {} connection", session.conn.engine()); + session.conn.close_hard(); + } + ok +} + +/// End sessions taken out of the map, tell the window, and report how many. +async fn end_sessions(ended: Vec<(String, TxSession)>, reason: EndReason) -> usize { + let n = ended.len(); + for (id, session) in ended { + let rolled_back = release(session).await; + if let Some(app) = APP.get() { + let _ = app.emit("tx-ended", TxEnded { session_id: id, reason, rolled_back }); + } + } + n +} + +/// The active connection is about to be replaced, parked or closed. Every open +/// session belongs to the old one: roll them all back. Without this, a tab's +/// session kept a live connection to the database just left, and its next +/// statement ran there while the window showed the new one. +pub fn connection_changed() { + EPOCH.fetch_add(1, Ordering::SeqCst); + let ended: Vec<(String, TxSession)> = SESSIONS.lock().unwrap().drain().collect(); + if ended.is_empty() { + return; + } + tauri::async_runtime::spawn(end_sessions(ended, EndReason::ConnectionChanged)); +} + +/// Roll back sessions idle past `IDLE_TIMEOUT`. Runs on a timer (lib.rs) as well +/// as on every begin and status call. +pub async fn sweep_idle() -> usize { + let ended: Vec<(String, TxSession)> = { + let mut sessions = SESSIONS.lock().unwrap(); + let stale: Vec = sessions + .iter() + .filter(|(_, s)| s.last_used.elapsed() >= IDLE_TIMEOUT) + .map(|(id, _)| id.clone()) + .collect(); + stale.into_iter().filter_map(|id| sessions.remove(&id).map(|s| (id, s))).collect() + }; + end_sessions(ended, EndReason::Idle).await +} + +/// Ids of the open sessions, for the quit guard. +#[tauri::command] +pub fn tx_open_sessions() -> Vec { + SESSIONS.lock().unwrap().keys().cloned().collect() } #[derive(Debug, Clone, Serialize)] @@ -74,11 +220,14 @@ pub struct TxStatus { /// Milliseconds since BEGIN, so the UI can warn about a long-held lock. pub open_ms: u64, pub engine: Option, + /// Other sessions waiting on a lock this transaction holds. None when the + /// engine can't say (SQLite) or the server would not tell us. + pub blocking: Option, } impl TxStatus { fn closed() -> Self { - Self { open: false, statements: 0, rows_affected: 0, open_ms: 0, engine: None } + Self { open: false, statements: 0, rows_affected: 0, open_ms: 0, engine: None, blocking: None } } fn of(session: &TxSession) -> Self { @@ -88,6 +237,7 @@ impl TxStatus { rows_affected: session.rows_affected, open_ms: session.started.elapsed().as_millis() as u64, engine: Some(session.conn.engine().to_string()), + blocking: None, } } } @@ -99,40 +249,92 @@ impl TxStatus { // uncontended across awaits, and makes a second concurrent command for the same // session fail fast instead of interleaving statements on one connection. -/// Remove sessions idle past the timeout. Their connections are dropped, which -/// returns them to the pool; the server rolls back the abandoned transaction. -fn sweep_idle(state: &State<'_, TxState>) { - let mut sessions = state.sessions.lock().unwrap(); - sessions.retain(|_, s| s.last_used.elapsed() < IDLE_TIMEOUT); +fn take(id: &str) -> Option { + SESSIONS.lock().unwrap().remove(id) } -fn take(state: &State<'_, TxState>, id: &str) -> Option { - state.sessions.lock().unwrap().remove(id) +/// Put a session back after a command. If the connection changed while the +/// command ran, `connection_changed` has already drained the map, so the +/// session comes back to a world that has moved on: end it instead. +async fn put(id: &str, session: TxSession) -> Result<(), String> { + if session.epoch != EPOCH.load(Ordering::SeqCst) { + let id = id.to_string(); + end_sessions(vec![(id, session)], EndReason::ConnectionChanged).await; + return Err(LEFT_CONNECTION.into()); + } + SESSIONS.lock().unwrap().insert(id.to_string(), session); + Ok(()) } -fn put(state: &State<'_, TxState>, id: &str, session: TxSession) { - state.sessions.lock().unwrap().insert(id.to_string(), session); -} +const LEFT_CONNECTION: &str = + "This transaction was on a connection you have since left, so it was rolled back. Nothing it did was saved."; fn no_session(id: &str) -> String { format!("No open transaction for {id}. Start one before running statements in it.") } +/// This session's server-side id, so `tx_status` can ask who it is blocking. +async fn blocker_for(conn: &mut TxConn, db: &ActiveConnection) -> Option { + match (conn, db) { + (TxConn::Postgres(c), ActiveConnection::Postgres(pool)) => { + let pid = sqlx::query_scalar::<_, i32>("SELECT pg_backend_pid()").fetch_one(&mut **c).await.ok()?; + Some(Blocker::Postgres(pool.clone(), pid)) + } + (TxConn::Mysql(c), ActiveConnection::Mysql(pool)) => { + let id = sqlx::query_scalar::<_, u64>("SELECT CONNECTION_ID()").fetch_one(&mut **c).await.ok()?; + Some(Blocker::Mysql(pool.clone(), id)) + } + _ => None, + } +} + +/// Sessions waiting on a lock this one holds, asked on a second connection. +/// None when the server won't say (MySQL needs performance_schema access). +async fn blocking_count(blocker: &Blocker) -> Option { + let ask = async { + match blocker { + // Everyone held up by it, not only those waiting on it directly: + // a second UPDATE of the same row waits on the first waiter, which + // waits on this transaction (measured: pg_blocking_pids names only + // the next link of the chain). + Blocker::Postgres(pool, pid) => sqlx::query_scalar::<_, i64>( + "WITH RECURSIVE held(pid) AS ( \ + SELECT pid FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)) \ + UNION \ + SELECT a.pid FROM pg_stat_activity a JOIN held h ON h.pid = ANY(pg_blocking_pids(a.pid)) \ + ) SELECT count(*)::bigint FROM held", + ) + .bind(pid) + .fetch_one(pool) + .await + .ok(), + Blocker::Mysql(pool, id) => sqlx::query_scalar::<_, i64>( + "SELECT COUNT(*) FROM performance_schema.data_lock_waits w \ + JOIN performance_schema.threads t ON t.THREAD_ID = w.BLOCKING_THREAD_ID \ + WHERE t.PROCESSLIST_ID = ?", + ) + .bind(id) + .fetch_one(pool) + .await + .ok(), + } + }; + tokio::time::timeout(Duration::from_secs(2), ask).await.ok().flatten() +} + // ── Commands ────────────────────────────────────────────────────────────────── /// Open a transaction and hold its connection until commit or rollback. #[tauri::command] -pub async fn tx_begin( - db: Db<'_>, - tx: State<'_, TxState>, - session_id: String, -) -> Result { - sweep_idle(&tx); - if tx.sessions.lock().unwrap().contains_key(&session_id) { +pub async fn tx_begin(db: Db<'_>, session_id: String) -> Result { + sweep_idle().await; + if SESSIONS.lock().unwrap().contains_key(&session_id) { return Err("A transaction is already open here. Commit or roll it back first.".into()); } + let epoch = EPOCH.load(Ordering::SeqCst); + let active = require_conn(&db)?; - let mut conn = match require_conn(&db)? { + let mut conn = match &active { ActiveConnection::Postgres(pool) => TxConn::Postgres( pool.acquire().await.map_err(|e| format!("Could not open a transaction: {e}"))?, ), @@ -142,9 +344,9 @@ pub async fn tx_begin( ActiveConnection::Sqlite(pool) => TxConn::Sqlite( pool.acquire().await.map_err(|e| format!("Could not open a transaction: {e}"))?, ), - ActiveConnection::Duckdb(_) | ActiveConnection::Mssql(_) | ActiveConnection::Oracle(_) => { - return Err("Explicit transactions are not supported for this engine yet".into()) - } + ActiveConnection::Duckdb(h) => TxConn::Duckdb(super::duckdb::open_tx_conn(h).await.map_err(|e| format!("Could not open a transaction: {e}"))?), + ActiveConnection::Mssql(h) => TxConn::Mssql(super::mssql::open_tx_client(h).await.map_err(|e| format!("Could not open a transaction: {e}"))?), + ActiveConnection::Oracle(h) => TxConn::Oracle(super::oracle::open_tx_session(h).await.map_err(|e| format!("Could not open a transaction: {e}"))?), ActiveConnection::D1(_) | ActiveConnection::LibSql(_) => { return Err("Explicit transactions need a persistent connection, which HTTP-based engines do not have".into()) } @@ -157,37 +359,39 @@ pub async fn tx_begin( }; run_statement(&mut conn, "BEGIN").await.map_err(|e| format!("Could not begin: {e}"))?; - // A statement that hangs inside a transaction holds its locks for as long as - // it runs, so cap it the same way the autocommit path does. if let TxConn::Postgres(_) = conn { + // A statement that hangs inside a transaction holds its locks for as + // long as it runs, so cap it the same way the autocommit path does. + let _ = run_statement(&mut conn, &format!("SET LOCAL statement_timeout = {EXECUTE_SQL_TIMEOUT_MS}")).await; + // The server's own backstop for a session this process never comes + // back to. Postgres 9.6+; older servers refuse it, which is harmless. let _ = run_statement( &mut conn, - &format!("SET LOCAL statement_timeout = {EXECUTE_SQL_TIMEOUT_MS}"), + &format!("SET LOCAL idle_in_transaction_session_timeout = '{SERVER_IDLE_TIMEOUT}'"), ) .await; } + let blocker = blocker_for(&mut conn, &active).await; let now = Instant::now(); let session = TxSession { conn, + epoch, + blocker, statements: 0, rows_affected: 0, started: now, last_used: now, }; let status = TxStatus::of(&session); - put(&tx, &session_id, session); + put(&session_id, session).await?; Ok(status) } /// Run SQL inside the open transaction. Nothing is visible to anyone else until /// the transaction commits. #[tauri::command] -pub async fn tx_execute( - tx: State<'_, TxState>, - session_id: String, - sql: String, -) -> Result { +pub async fn tx_execute(session_id: String, sql: String) -> Result { let trimmed = sql.trim().to_string(); if trimmed.is_empty() { return Err("Query is empty".into()); @@ -204,7 +408,11 @@ pub async fn tx_execute( } } - let mut session = take(&tx, &session_id).ok_or_else(|| no_session(&session_id))?; + let mut session = take(&session_id).ok_or_else(|| no_session(&session_id))?; + if session.epoch != EPOCH.load(Ordering::SeqCst) { + end_sessions(vec![(session_id, session)], EndReason::ConnectionChanged).await; + return Err(LEFT_CONNECTION.into()); + } let started = Instant::now(); let result = execute_in(&mut session.conn, &trimmed).await; @@ -221,7 +429,7 @@ pub async fn tx_execute( session.statements += 1; } } - put(&tx, &session_id, session); + put(&session_id, session).await?; result.map(|mut r| { r.query_ms = started.elapsed().as_millis() as u64; @@ -232,40 +440,60 @@ pub async fn tx_execute( /// Commit the open transaction and give the connection back. #[tauri::command] -pub async fn tx_commit(tx: State<'_, TxState>, session_id: String) -> Result { - let mut session = take(&tx, &session_id).ok_or_else(|| no_session(&session_id))?; +pub async fn tx_commit(session_id: String) -> Result { + let mut session = take(&session_id).ok_or_else(|| no_session(&session_id))?; + if session.epoch != EPOCH.load(Ordering::SeqCst) { + end_sessions(vec![(session_id, session)], EndReason::ConnectionChanged).await; + return Err(LEFT_CONNECTION.into()); + } match run_statement(&mut session.conn, "COMMIT").await { - // The connection is dropped with `session`, returning it to the pool. + // Clean: the connection is out of its transaction, back to the pool. Ok(_) => Ok(TxStatus::closed()), Err(e) => { // The commit failed, so the transaction is neither applied nor - // cleanly closed. Drop the connection rather than hand back a - // session whose state nobody can describe. - Err(format!("Could not commit: {e}")) + // cleanly closed. Roll back what is left, or close the socket. + release(session).await; + Err(format!("Could not commit: {e}. Nothing was saved.")) } } } /// Roll the open transaction back and give the connection back. #[tauri::command] -pub async fn tx_rollback(tx: State<'_, TxState>, session_id: String) -> Result { - let mut session = take(&tx, &session_id).ok_or_else(|| no_session(&session_id))?; - run_statement(&mut session.conn, "ROLLBACK") - .await - .map_err(|e| format!("Could not roll back: {e}"))?; - Ok(TxStatus::closed()) +pub async fn tx_rollback(session_id: String) -> Result { + let session = take(&session_id).ok_or_else(|| no_session(&session_id))?; + // Whatever happens the session is over: a failed ROLLBACK closes the + // socket, and the server rolls back when it sees it go. + if release(session).await { + Ok(TxStatus::closed()) + } else { + Err("The rollback did not complete, so the connection was closed. The server rolls back a closed connection's transaction, so nothing was saved.".into()) + } } /// What the UI needs to render the transaction indicator. #[tauri::command] -pub async fn tx_status(tx: State<'_, TxState>, session_id: String) -> Result { - sweep_idle(&tx); - let sessions = tx.sessions.lock().unwrap(); - Ok(sessions.get(&session_id).map_or_else(TxStatus::closed, TxStatus::of)) +pub async fn tx_status(session_id: String) -> Result { + sweep_idle().await; + let (mut status, blocker) = { + let sessions = SESSIONS.lock().unwrap(); + match sessions.get(&session_id) { + None => return Ok(TxStatus::closed()), + Some(s) => ( + TxStatus::of(s), + s.blocker.as_ref().map(|b| match b { + Blocker::Postgres(p, pid) => Blocker::Postgres(p.clone(), *pid), + Blocker::Mysql(p, id) => Blocker::Mysql(p.clone(), *id), + }), + ), + } + }; + if let Some(b) = blocker { + status.blocking = blocking_count(&b).await; + } + Ok(status) } -// ── Execution ───────────────────────────────────────────────────────────────── - /// The first word of a statement, lowercased, skipping leading comments. fn leading_keyword(sql: &str) -> Option { let mut rest = sql.trim_start(); @@ -291,10 +519,45 @@ async fn run_statement(conn: &mut TxConn, sql: &str) -> Result { TxConn::Postgres(c) => sqlx::query(sql).execute(&mut **c).await.map(|r| r.rows_affected()), TxConn::Mysql(c) => sqlx::query(sql).execute(&mut **c).await.map(|r| r.rows_affected()), TxConn::Sqlite(c) => sqlx::query(sql).execute(&mut **c).await.map(|r| r.rows_affected()), + // BEGIN / COMMIT / ROLLBACK in each engine's own words. + TxConn::Duckdb(h) => { + let stmt = if sql == "BEGIN" { "BEGIN TRANSACTION" } else { sql }; + return super::duckdb::batch(h, stmt).await.map(|_| 0); + } + TxConn::Mssql(h) => { + let stmt = match sql { + "BEGIN" => "BEGIN TRANSACTION", + "COMMIT" => "COMMIT TRANSACTION", + // A failed statement can have ended the transaction already. + "ROLLBACK" => "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", + other => other, + }; + return super::mssql::batch(h, stmt).await.map(|_| 0); + } + TxConn::Oracle(h) => { + return match sql { + // Oracle starts a transaction with its first statement. + "BEGIN" => Ok(0), + "COMMIT" => super::oracle::commit(h).await.map(|_| 0), + "ROLLBACK" => super::oracle::rollback(h).await.map(|_| 0), + other => super::oracle::execute_in_tx(h, other).await.map(|r| r.row_count.unwrap_or(0).max(0) as u64), + }; + } } .map_err(|e| e.to_string()) } +/// A statement on one of the single-connection engines, inside its open +/// transaction. None for the pooled engines, which take the sqlx path. +async fn execute_on_handle(conn: &TxConn, stmt: &str) -> Option> { + Some(match conn { + TxConn::Duckdb(h) => super::duckdb::execute_sql(h, stmt).await, + TxConn::Mssql(h) => super::mssql::execute_sql(h, stmt).await, + TxConn::Oracle(h) => super::oracle::execute_in_tx(h, stmt).await, + _ => return None, + }) +} + /// Run the user's SQL, returning rows when the last statement produces them. /// /// Mirrors the autocommit executor: a script is split so a multi-statement @@ -308,6 +571,23 @@ async fn execute_in(conn: &mut TxConn, sql: &str) -> Result { let last = stmts.len() - 1; let mut affected: u64 = 0; + if matches!(conn, TxConn::Duckdb(_) | TxConn::Mssql(_) | TxConn::Oracle(_)) { + let mut out = None; + for stmt in &stmts { + let r = execute_on_handle(conn, stmt).await.expect("single-connection engine").map_err(|e| format!("Query failed: {e}"))?; + if r.columns.is_empty() { + affected += r.row_count.unwrap_or(0).max(0) as u64; + } + out = Some(r); + } + let mut r = out.expect("non-empty"); + if r.columns.is_empty() { + r.row_count = Some(affected as i64); + r.message = Some(format!("{affected} row{} affected (not committed yet)", if affected == 1 { "" } else { "s" })); + } + return Ok(r); + } + for (i, stmt) in stmts.iter().enumerate() { if i == last && is_row_returning_sql(stmt) { return fetch_rows(conn, stmt).await; @@ -386,6 +666,9 @@ async fn fetch_rows(conn: &mut TxConn, stmt: &str) -> Result ); } } + TxConn::Duckdb(_) | TxConn::Mssql(_) | TxConn::Oracle(_) => { + return execute_on_handle(conn, stmt).await.unwrap_or_else(|| Err("unsupported".into())); + } } let count = rows.len() as i64; @@ -403,6 +686,86 @@ async fn fetch_rows(conn: &mut TxConn, stmt: &str) -> Result mod tests { use super::leading_keyword; + // Live: a released session must give back a connection that is out of its + // transaction. Before `release`, the idle sweep dropped the PoolConnection, + // sqlx returned it to the pool still inside the BEGIN, and the next user of + // the pool ran in the abandoned transaction. + // STROKE_TEST_PG_URL=postgres://postgres@127.0.0.1:5433/postgres \ + // cargo test --lib tx::tests::release_ -- --nocapture + #[tokio::test] + async fn release_returns_a_clean_connection() { + use super::*; + let Ok(url) = std::env::var("STROKE_TEST_PG_URL") else { return }; + // One connection, so the next acquire gets the one we released. + let pool = sqlx::postgres::PgPoolOptions::new().max_connections(1).connect(&url).await.unwrap(); + sqlx::query("CREATE TABLE IF NOT EXISTS stroke_tx_release (v int)").execute(&pool).await.unwrap(); + sqlx::query("TRUNCATE stroke_tx_release").execute(&pool).await.unwrap(); + + let session = |conn| TxSession { + conn, + epoch: 0, + blocker: None, + statements: 0, + rows_affected: 0, + started: Instant::now(), + last_used: Instant::now(), + }; + let open = |pool: &sqlx::PgPool| { + let pool = pool.clone(); + async move { + let mut conn = TxConn::Postgres(pool.acquire().await.unwrap()); + run_statement(&mut conn, "BEGIN").await.unwrap(); + run_statement(&mut conn, "INSERT INTO stroke_tx_release VALUES (1)").await.unwrap(); + conn + } + }; + let in_tx = |pool: &sqlx::PgPool| { + let pool = pool.clone(); + async move { + // Assigned only inside a transaction that wrote something. + sqlx::query_scalar::<_, Option>("SELECT txid_current_if_assigned()") + .fetch_one(&pool) + .await + .unwrap() + .is_some() + } + }; + + // What the sweep used to do: drop it. + drop(session(open(&pool).await)); + tokio::time::sleep(Duration::from_millis(200)).await; + assert!(in_tx(&pool).await, "a dropped session leaks its transaction into the pool"); + // Clean up after the demonstration. + sqlx::query("ROLLBACK").execute(&pool).await.unwrap(); + + // What every exit does now. + assert!(release(session(open(&pool).await)).await); + tokio::time::sleep(Duration::from_millis(200)).await; + assert!(!in_tx(&pool).await, "a released session must not leave a transaction open"); + let rows: i64 = sqlx::query_scalar("SELECT count(*) FROM stroke_tx_release").fetch_one(&pool).await.unwrap(); + assert_eq!(rows, 0, "the uncommitted insert must be gone"); + sqlx::query("DROP TABLE stroke_tx_release").execute(&pool).await.unwrap(); + } + + // Live: switching connections ends every open session. + #[tokio::test] + async fn connection_change_ends_sessions() { + use super::*; + let Ok(url) = std::env::var("STROKE_TEST_PG_URL") else { return }; + let pool = sqlx::postgres::PgPoolOptions::new().max_connections(2).connect(&url).await.unwrap(); + let mut conn = TxConn::Postgres(pool.acquire().await.unwrap()); + run_statement(&mut conn, "BEGIN").await.unwrap(); + let epoch = EPOCH.load(Ordering::SeqCst); + let now = Instant::now(); + let s = TxSession { conn, epoch, blocker: None, statements: 0, rows_affected: 0, started: now, last_used: now }; + put("test-switch", s).await.unwrap(); + assert!(tx_open_sessions().contains(&"test-switch".to_string())); + connection_changed(); + assert!(!tx_open_sessions().contains(&"test-switch".to_string())); + let err = tx_execute("test-switch".into(), "SELECT 1".into()).await.unwrap_err(); + assert!(err.contains("No open transaction"), "{err}"); + } + #[test] fn finds_the_first_keyword() { assert_eq!(leading_keyword("SELECT 1").as_deref(), Some("select")); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 102635e7..f7184e53 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -498,6 +498,18 @@ pub fn run() { .manage(launch::LaunchState::default()) .setup(move |app| { display_backend::watch(app.handle().clone()); + // Explicit transactions: roll back the ones left idle, on a timer, + // so a forgotten tab does not hold its locks until someone looks. + db::tx::init(app.handle().clone()); + // SSH tunnels report when they drop and when they are back. + db::ssh_tunnel::init(app.handle().clone()); + tauri::async_runtime::spawn(async { + let mut tick = tokio::time::interval(db::tx::SWEEP_EVERY); + loop { + tick.tick().await; + db::tx::sweep_idle().await; + } + }); // Local servers marked "Start with Stroke". local_servers::autostart(app.handle().clone()); // Linux packages renamed the binary from `app` to `stroke` (2.5.6), and @@ -904,6 +916,7 @@ pub fn run() { db::tx::tx_commit, db::tx::tx_rollback, db::tx::tx_status, + db::tx::tx_open_sessions, db::terminal::terminal_client, db::terminal::terminal_open, db::terminal::terminal_install_client, @@ -1007,11 +1020,19 @@ fn install_open_links(app: &tauri::AppHandle) { let links = app.deep_link(); // The installers register the scheme; a dev build or an AppImage has no // installer, so it registers itself (per-user, overwritten on each run). + // Off the main thread: on Linux it runs `update-desktop-database` and + // `xdg-mime`, and setup waited for both before the window could open. + // Measured with realistic 250ms tools: first screen at 1.52s instead of + // 1.07s. Nothing at startup needs the registration to have finished. #[cfg(any(target_os = "linux", all(debug_assertions, windows)))] { - if let Err(e) = links.register_all() { - log::warn!("Could not register stroke:// links: {e}"); - } + let handle = app.clone(); + std::thread::spawn(move || { + use tauri_plugin_deep_link::DeepLinkExt; + if let Err(e) = handle.deep_link().register_all() { + log::warn!("Could not register stroke:// links: {e}"); + } + }); } let cwd = std::env::current_dir().unwrap_or_default(); let first = links diff --git a/src-tauri/vendor/sqlx-mysql/src/connection/establish.rs b/src-tauri/vendor/sqlx-mysql/src/connection/establish.rs index 85a9d84f..e41f1ef1 100644 --- a/src-tauri/vendor/sqlx-mysql/src/connection/establish.rs +++ b/src-tauri/vendor/sqlx-mysql/src/connection/establish.rs @@ -18,7 +18,10 @@ impl MySqlConnection { let handshake = match &options.socket { Some(path) => crate::net::connect_uds(path, do_handshake).await?, - None => crate::net::connect_tcp(&options.host, options.port, do_handshake).await?, + None => { + let dial = options.hostaddr.as_deref().unwrap_or(&options.host); + crate::net::connect_tcp(dial, options.port, do_handshake).await? + } }; let stream = handshake?; diff --git a/src-tauri/vendor/sqlx-mysql/src/options/mod.rs b/src-tauri/vendor/sqlx-mysql/src/options/mod.rs index 87732cb4..4b37970e 100644 --- a/src-tauri/vendor/sqlx-mysql/src/options/mod.rs +++ b/src-tauri/vendor/sqlx-mysql/src/options/mod.rs @@ -64,6 +64,9 @@ pub struct MySqlConnectOptions { pub(crate) host: String, pub(crate) port: u16, pub(crate) socket: Option, + /// Stroke: where to open the TCP connection when it isn't `host`. `host` + /// still names the server for TLS. Used for SSH tunnels. + pub(crate) hostaddr: Option, pub(crate) username: String, pub(crate) password: Option, pub(crate) database: Option, @@ -95,6 +98,7 @@ impl MySqlConnectOptions { port: 3306, host: String::from("localhost"), socket: None, + hostaddr: None, username: String::from("root"), password: None, database: None, @@ -123,6 +127,13 @@ impl MySqlConnectOptions { self } + /// Stroke: open the TCP connection to `addr` instead of `host`. `host` is + /// still the name TLS verifies. + pub fn hostaddr(mut self, addr: &str) -> Self { + self.hostaddr = Some(addr.to_owned()); + self + } + /// Sets the port to connect to at the server host. /// /// The default port for MySQL is `3306`. diff --git a/src-tauri/vendor/sqlx-postgres/src/connection/stream.rs b/src-tauri/vendor/sqlx-postgres/src/connection/stream.rs index e8a1aedc..8380ac14 100644 --- a/src-tauri/vendor/sqlx-postgres/src/connection/stream.rs +++ b/src-tauri/vendor/sqlx-postgres/src/connection/stream.rs @@ -44,7 +44,10 @@ impl PgStream { pub(super) async fn connect(options: &PgConnectOptions) -> Result { let socket_result = match options.fetch_socket() { Some(ref path) => net::connect_uds(path, MaybeUpgradeTls(options)).await?, - None => net::connect_tcp(&options.host, options.port, MaybeUpgradeTls(options)).await?, + None => { + let dial = options.hostaddr.as_deref().unwrap_or(&options.host); + net::connect_tcp(dial, options.port, MaybeUpgradeTls(options)).await? + } }; let socket = socket_result?; diff --git a/src-tauri/vendor/sqlx-postgres/src/options/mod.rs b/src-tauri/vendor/sqlx-postgres/src/options/mod.rs index 723721a9..59d7b204 100644 --- a/src-tauri/vendor/sqlx-postgres/src/options/mod.rs +++ b/src-tauri/vendor/sqlx-postgres/src/options/mod.rs @@ -18,6 +18,9 @@ pub struct PgConnectOptions { pub(crate) host: String, pub(crate) port: u16, pub(crate) socket: Option, + /// Stroke: where to open the TCP connection when it isn't `host` (libpq's + /// `hostaddr`). `host` still names the server for TLS. Used for SSH tunnels. + pub(crate) hostaddr: Option, pub(crate) username: String, pub(crate) password: Option, pub(crate) database: Option, @@ -72,6 +75,7 @@ impl PgConnectOptions { port, host, socket: None, + hostaddr: None, username, password: var("PGPASSWORD").ok(), database, @@ -127,6 +131,13 @@ impl PgConnectOptions { self } + /// Stroke: open the TCP connection to `addr` instead of `host`, like + /// libpq's `hostaddr`. `host` is still the name TLS verifies. + pub fn hostaddr(mut self, addr: &str) -> Self { + self.hostaddr = Some(addr.to_owned()); + self + } + /// Sets the port to connect to at the server host. /// /// The default port for PostgreSQL is `5432`. diff --git a/src/App.svelte b/src/App.svelte index f9178eef..e671c55e 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -2,8 +2,9 @@ import { toastPosition } from '$lib/stores/toast-position.js' import { onMount } from 'svelte' import { Toaster } from '$lib/components/ui/sonner/index.js' + import ZoomIndicator from '$lib/components/ZoomIndicator.svelte' import StudioShell from './lib/components/StudioShell.svelte' - import DashboardWindow from './lib/components/DashboardWindow.svelte' + // A window opened for one dashboard (open_dashboard_window) shows only that. const params = typeof location !== 'undefined' ? new URLSearchParams(location.search) : new URLSearchParams() @@ -88,6 +89,7 @@ + {#if dashboardView} - + {#await import('./lib/components/DashboardWindow.svelte') then { default: DashboardWindow }}{/await} {:else if localServersFrom} {#await import('./lib/components/LocalServersWindow.svelte') then { default: LocalServersWindow }} diff --git a/src/app.css b/src/app.css index b7b25386..1c37b802 100644 --- a/src/app.css +++ b/src/app.css @@ -97,7 +97,13 @@ `.dark` block below (toggled for every dark theme via classList) swaps in stronger black + a dim rim, since a light drop-shadow is nearly invisible on a dark surface and depth there comes from the rim highlight instead. */ - --elevate-1: 0 1px 2px oklch(0 0 0 / 0.05), 0 1px 1px oklch(0 0 0 / 0.04); + /* Level 1 is an outline, not a shadow: things that sit in the page (a + selected segment, a raised chip, a card) get a crisp 1px hairline. Small + soft shadows rendered as a grey smudge on Linux and Windows, and against + a dark surface they did not show at all. Only what floats (menus, + popovers, dialogs: levels 2 and 3) keeps a real shadow. Tailwind's + `shadow-xs` and `shadow-sm` resolve to this too (@theme below). */ + --elevate-1: 0 0 0 1px oklch(0 0 0 / 0.09); --elevate-2: 0 2px 5px -1px oklch(0 0 0 / 0.07), 0 6px 16px -3px oklch(0 0 0 / 0.09); --elevate-3: 0 10px 22px -6px oklch(0 0 0 / 0.11), 0 22px 52px -12px oklch(0 0 0 / 0.16); /* Top rim highlight - a 1px inner catch-light that gives raised surfaces a @@ -136,7 +142,7 @@ --warning-foreground: oklch(0.20 0.04 78); --info: oklch(0.71 0.13 245); --info-foreground: oklch(0.16 0.03 245); - --elevate-1: 0 1px 2px oklch(0 0 0 / 0.4), 0 1px 3px oklch(0 0 0 / 0.3); + --elevate-1: 0 0 0 1px oklch(1 0 0 / 0.09); --elevate-2: 0 2px 5px -1px oklch(0 0 0 / 0.45), 0 8px 20px -4px oklch(0 0 0 / 0.5); --elevate-3: 0 12px 28px -8px oklch(0 0 0 / 0.55), 0 28px 60px -16px oklch(0 0 0 / 0.62); --rim: inset 0 1px 0 0 oklch(1 0 0 / 0.055); @@ -424,6 +430,9 @@ html[data-os="linux"] { --window-radius: 8px; } @theme inline { + /* In-page "shadows" are hairline outlines (see --elevate-1). */ + --shadow-xs: var(--elevate-1); + --shadow-sm: var(--elevate-1); --radius-sm: calc(var(--radius) - 2px); --radius-md: var(--radius); --radius-lg: calc(var(--radius) + 2px); @@ -492,6 +501,18 @@ html[data-os="linux"] { --window-radius: 8px; } spellcheck: false; } + /* The window's chrome (title bar and its menus, tab strip, toolbars, + status bar) always reads in a proportional face, even when the app font + is all-monospace: the data keeps the chosen font, the frame around it + stays a desktop app's. In the base layer, so a `font-mono` utility inside + (a shortcut cap, a path) still wins. */ + [data-studio-region="titlebar"], + [data-studio-region="statusbar"], + [data-studio-chrome], + .chrome-font { + font-family: var(--font-chrome, var(--font-sans)); + } + html { font-size: var(--app-font-size); font-family: var(--font-sans); @@ -1329,6 +1350,51 @@ html[data-motion="reduced"] .animate-spin { animation-iteration-count: infinite !important; } +/* ── Search fields ───────────────────────────────────────────────────────── + One look for every search box (DESIGN_SYSTEM.md, "Search fields"): an + outlined box over a faint fill, no shadow. A 1px outline reads as a field + on every platform; the fill alone vanished on light themes and drop shadows + rendered muddy on Linux and Windows. SearchField.svelte uses it, and so do + the hand-built filters that keep their own keyboard handling (the sidebar's). + In the components layer, so a size utility on one field still wins. */ +@layer components { + .search-box { + height: 2rem; + border-radius: var(--radius-lg); + border: 1px solid color-mix(in oklab, var(--border) 80%, transparent); + background-color: color-mix(in oklab, var(--muted) 30%, transparent); + color: var(--foreground); + outline: none; + transition: border-color 150ms ease-out, background-color 150ms ease-out, box-shadow 150ms ease-out; + } + .search-box::placeholder { color: var(--muted-foreground); } + .search-box:hover:not(:disabled) { border-color: color-mix(in oklab, var(--foreground) 20%, transparent); } + .search-box:focus { + border-color: color-mix(in oklab, var(--ring) 70%, transparent); + background-color: color-mix(in oklab, var(--muted) 45%, transparent); + } + .search-box:focus-visible { box-shadow: 0 0 0 2px color-mix(in oklab, var(--ring) 15%, transparent); } + .search-box:disabled { cursor: not-allowed; opacity: 0.4; } + .search-box::-webkit-search-cancel-button { display: none; } + /* The same box for a search built as a wrapper (icon, input, buttons in one + row): the wrapper carries the outline and lights up while the input has focus. */ + .search-shell { + border-radius: var(--radius-lg); + border: 1px solid color-mix(in oklab, var(--border) 80%, transparent); + background-color: color-mix(in oklab, var(--muted) 30%, transparent); + color: var(--foreground); + transition: border-color 150ms ease-out, background-color 150ms ease-out, box-shadow 150ms ease-out; + } + .search-shell:hover { border-color: color-mix(in oklab, var(--foreground) 20%, transparent); } + .search-shell:focus-within { + border-color: color-mix(in oklab, var(--ring) 70%, transparent); + background-color: color-mix(in oklab, var(--muted) 45%, transparent); + } + .search-shell:has(:focus-visible) { box-shadow: 0 0 0 2px color-mix(in oklab, var(--ring) 15%, transparent); } + .search-shell input { background: transparent; color: inherit; } + .search-shell input::placeholder { color: var(--muted-foreground); } +} + /* ── Hover-revealed row actions stay reachable by keyboard ─────────────────── Row actions across the app (remove-from-recent, copy-field, JSON row tools, policy row menus) fade in on `group-hover`. A keyboard user tabs into them diff --git a/src/lib/api.js b/src/lib/api.js index 7bc335f4..fa8dfc95 100644 --- a/src/lib/api.js +++ b/src/lib/api.js @@ -191,13 +191,16 @@ export async function liveStop() { // ── PostgreSQL ──────────────────────────────────────────────────────────────── -/** @param {{ host: string, port?: number, username: string, privateKeyPath?: string }} ssh */ +/** @param {{ host: string, port?: number, username: string, privateKeyPath?: string, keepalive?: number }} ssh */ function normalizeSshConfig(ssh) { return { host: String(ssh.host || '').trim(), port: Math.min(65535, Math.max(1, Number(ssh.port) || 22)), username: String(ssh.username || '').trim(), privateKeyPath: String(ssh.privateKeyPath || '').trim(), + // Seconds; 0 turns it off. Dropped here before, so the form's value never + // reached ssh and every tunnel ran at the default 30. + keepalive: Math.max(0, Math.min(3600, Number(ssh.keepalive ?? 30) || 0)), } } @@ -418,7 +421,7 @@ export async function connectPosthog(config) { /** @param {{ name: string, host: string, port: number|string, database: string, user: string, password: string, secure?: boolean }} config */ function normalizeClickhouse(config) { - return { + return withSsh({ name: String(config.name || 'ClickHouse'), host: String(config.host || '127.0.0.1'), port: Math.min(65535, Math.max(1, Number(config.port) || 8123)), @@ -426,7 +429,7 @@ function normalizeClickhouse(config) { user: String(config.user || 'default'), password: String(config.password || ''), secure: Boolean(config.secure), - } + }, config) } /** @param {{ name: string, host: string, port: number|string, database: string, user: string, password: string, secure?: boolean }} config */ @@ -443,7 +446,7 @@ export async function connectClickhouse(config) { /** @param {{ name: string, host: string, port: number|string, password?: string, db?: number|string, tls?: boolean }} config */ function normalizeRedis(config) { - return { + return withSsh({ type: 'redis', name: String(config.name || 'Redis'), host: String(config.host || '127.0.0.1'), @@ -451,7 +454,7 @@ function normalizeRedis(config) { password: config.password || null, db: Number(config.db) || 0, tls: !!config.tls, - } + }, config) } /** @param {{ name: string, host: string, port: number|string, password?: string, db?: number|string, tls?: boolean }} config */ @@ -609,13 +612,13 @@ export async function dynamoRequest(op, body = {}) { /** @param {CouchConnConfig} config */ function normalizeCouch(config) { - return { + return withSsh({ type: 'couchdb', name: String(config.name || 'CouchDB'), url: String(config.url || 'http://localhost:5984').trim(), user: (config.user && String(config.user).trim()) || null, password: config.password || null, - } + }, config) } /** @param {CouchConnConfig} config */ @@ -646,7 +649,7 @@ export async function couchRequest(method, path, body) { /** @param {EsConnConfig} config */ function normalizeEs(config) { const flavor = config.type === 'opensearch' ? 'opensearch' : 'elasticsearch' - return { + return withSsh({ type: flavor, flavor, name: String(config.name || (flavor === 'opensearch' ? 'OpenSearch' : 'Elasticsearch')), @@ -655,7 +658,7 @@ function normalizeEs(config) { password: config.password || null, apiKey: (config.apiKey && String(config.apiKey).trim()) || null, insecure: !!config.insecure, - } + }, config) } /** @@ -702,12 +705,12 @@ export async function elasticImport(index, path, opts = {}) { /** @param {MongoConnConfig} config */ function normalizeMongo(config) { - return { + return withSsh({ type: 'mongodb', name: String(config.name || 'MongoDB'), uri: String(config.uri || 'mongodb://localhost:27017').trim(), database: (config.database && String(config.database).trim()) || null, - } + }, config) } /** @param {MongoConnConfig} config */ @@ -907,14 +910,14 @@ export async function firestoreRequest(method, path, body) { function normalizeCassandra(config) { /** @param {string | undefined} s */ const opt = (s) => (s && String(s).trim()) || null - return { + return withSsh({ type: 'cassandra', name: String(config.name || 'Cassandra'), hosts: String(config.hosts || '127.0.0.1').trim(), user: opt(config.user), password: config.password || null, keyspace: opt(config.keyspace), - } + }, config) } /** @param {CassandraConnConfig} config */ @@ -985,7 +988,7 @@ export async function connectDuckdb(config) { /** @param {{ name: string, host: string, port: number|string, database: string, user: string, password: string, encrypt?: boolean, trustCert?: boolean }} config */ function normalizeMssql(config) { - return { + return withSsh({ name: String(config.name || 'SQL Server'), host: String(config.host || '127.0.0.1'), port: Math.min(65535, Math.max(1, Number(config.port) || 1433)), @@ -994,7 +997,7 @@ function normalizeMssql(config) { password: String(config.password || ''), encrypt: Boolean(config.encrypt), trustCert: Boolean(config.trustCert), - } + }, config) } /** @param {{ name: string, host: string, port: number|string, database: string, user: string, password: string, encrypt?: boolean, trustCert?: boolean }} config */ @@ -1016,7 +1019,7 @@ export async function connectMssql(config) { /** @param {OracleConnConfig} config */ function normalizeOracle(config) { - return { + return withSsh({ name: String(config.name || 'Oracle'), host: String(config.host || '127.0.0.1').trim(), port: Math.min(65535, Math.max(1, Number(config.port) || 1521)), @@ -1026,7 +1029,7 @@ function normalizeOracle(config) { user: String(config.user ?? '').trim(), password: String(config.password ?? ''), clientLibDir: String(config.clientLibDir ?? '').trim(), - } + }, config) } /** @param {OracleConnConfig} config */ @@ -1690,10 +1693,11 @@ export async function instanceSetConfig(name, value) { /** * The query plan. `analyze` (Postgres) runs the statement for actual rows and * times, inside a transaction that is rolled back; off, only estimates. - * @param {string} sql @param {{ analyze?: boolean }} [opts] + * `queryId` lets `cancelQuery` stop an ANALYZE on the server. + * @param {string} sql @param {{ analyze?: boolean, queryId?: string }} [opts] */ export async function explainSql(sql, opts = {}) { - return inv('pg_explain_sql', { sql, analyze: opts.analyze ?? true }) + return inv('pg_explain_sql', { sql, analyze: opts.analyze ?? true, queryId: opts.queryId ?? null }) } /** @@ -1966,6 +1970,7 @@ export async function backupCancel() { * @property {number} rowsAffected * @property {number} openMs how long the transaction has been held * @property {string|null} engine + * @property {number|null} [blocking] sessions waiting on a lock it holds (null: the engine can't say) */ /** @@ -2012,6 +2017,11 @@ export async function txStatus(sessionId) { return inv('tx_status', { sessionId }) } +/** Ids of every open transaction session, for the quit guard. @returns {Promise} */ +export async function txOpenSessions() { + return inv('tx_open_sessions', {}) +} + /** * A read for a console write's undo copy (sql-undo.js), outside any * transaction. Not logged as a query of the user's, and stoppable through the diff --git a/src/lib/app-reveal.js b/src/lib/app-reveal.js index ba807040..3f460e0c 100644 --- a/src/lib/app-reveal.js +++ b/src/lib/app-reveal.js @@ -47,6 +47,36 @@ export function revealApp() { // rather than a page fading in. document.documentElement.dataset.revealed = '' void showWindow() + recordBootTiming() +} + +/** + * Where the last launch spent its time before the first screen, in ms from + * navigation start: the page parsed (`html`), every statically imported + * module evaluated (`main`), the app mounted (`mount`), the first screen was + * ready (`reveal`). Kept in + * localStorage as `stroke:last-boot`, so a slow start can be read back + * instead of guessed at. Marks only; nothing is sent anywhere. + */ +function recordBootTiming() { + try { + const at = (/** @type {string} */ name) => { + const m = performance.getEntriesByName(name, 'mark')[0] + return m ? Math.round(m.startTime) : null + } + const nav = /** @type {PerformanceNavigationTiming | undefined} */ (performance.getEntriesByType('navigation')[0]) + const timing = { + html: nav ? Math.round(nav.domInteractive) : null, + main: at('stroke:main'), + settings: at('stroke:settings'), + connections: at('stroke:connections'), + mount: at('stroke:mount'), + lockReady: at('stroke:lock-ready'), + shell: at('stroke:shell'), + reveal: Math.round(performance.now()), + } + localStorage.setItem('stroke:last-boot', JSON.stringify(timing)) + } catch { /* storage blocked: nothing to record */ } } /** Never leave a blank window: reveal regardless if no screen claimed it. */ diff --git a/src/lib/components/AdvisorPage.svelte b/src/lib/components/AdvisorPage.svelte index 117b0a49..58f2111b 100644 --- a/src/lib/components/AdvisorPage.svelte +++ b/src/lib/components/AdvisorPage.svelte @@ -283,7 +283,7 @@ bind:value={query} placeholder="Filter by table, check…" aria-label="Filter findings" - class="field-surface h-7 w-52 bg-background pl-7 pr-2 text-ui-xs text-foreground placeholder:text-muted-foreground focus:border-primary/40 focus:outline-none" + class="search-box no-field-frame h-7 w-52 pl-7 pr-2 text-ui-xs" /> diff --git a/src/lib/components/AppLockGate.svelte b/src/lib/components/AppLockGate.svelte index 2ea5426b..35eb3ae6 100644 --- a/src/lib/components/AppLockGate.svelte +++ b/src/lib/components/AppLockGate.svelte @@ -49,6 +49,7 @@ // and says locked, the screen still comes up - just a beat later. const giveUp = setTimeout(() => (ready = true), 1500) initAppLock().finally(() => { + performance.mark('stroke:lock-ready') clearTimeout(giveUp) ready = true teardown = installAutoLock() diff --git a/src/lib/components/AppLockScreen.svelte b/src/lib/components/AppLockScreen.svelte index f65aa3c7..17d7c971 100644 --- a/src/lib/components/AppLockScreen.svelte +++ b/src/lib/components/AppLockScreen.svelte @@ -79,7 +79,7 @@ >
-
+
diff --git a/src/lib/components/AppMenuBar.svelte b/src/lib/components/AppMenuBar.svelte index 7b273d78..a95e680e 100644 --- a/src/lib/components/AppMenuBar.svelte +++ b/src/lib/components/AppMenuBar.svelte @@ -40,7 +40,7 @@ const contentCls = "data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 duration-75 " + - "border border-border/60 bg-popover text-popover-foreground min-w-60 max-w-(--menu-max-w) rounded-[10px] p-1 elevate-2-rim z-[60] outline-none"; + "chrome-font border border-border/60 bg-popover text-popover-foreground min-w-60 max-w-(--menu-max-w) rounded-[10px] p-1 elevate-2-rim z-[60] outline-none"; // A fixed h-7 row (the compact control height) instead of py-1: padding-based // rows grew with the font and zoom, and every menu came out a different diff --git a/src/lib/components/ConnectOverlay.svelte b/src/lib/components/ConnectOverlay.svelte index 2a6361a6..71273690 100644 --- a/src/lib/components/ConnectOverlay.svelte +++ b/src/lib/components/ConnectOverlay.svelte @@ -82,12 +82,19 @@