Implementation:2giosangmitom Sqmeow nvim tunnel open
| Knowledge Sources | |
|---|---|
| Domains | Networking, Connection_Management |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
Concrete tool for opening SSH tunnels to reach databases behind a bastion host provided by the sqmeow-core engine.
Description
The tunnel::open function forwards a free local port through ssh to a bastion host, then rewrites the database URL to point at 127.0.0.1:<local_port>. It parses the database host and port from the URL (using dialect-specific defaults), parses the SSH target user@host[:port], spawns ssh -N -L with ExitOnForwardFailure=yes and BatchMode=yes, and waits for the local port to accept connections (up to CONNECT_TIMEOUT = 10s). The returned Tunnel struct holds the SSH child process and closes it on drop.
File-based databases (SQLite, DuckDB), MongoDB SRV addresses, and multi-host cluster URLs are rejected because they cannot go through a single tunnel.
Usage
Use this function when a database connection specifies an SSH jump target (via option) and the database URL points to a host reachable only through that bastion.
Code Reference
Source Location
- Repository: sqmeow.nvim
- File: core/sqmeow-core/src/server/tunnel.rs
- Lines: L19-111
Signature
pub struct Tunnel {
_child: Child,
}
pub async fn open(
program: &str,
url: &str,
via: &str,
) -> Result<(String, Tunnel), String>
Import
use crate::server::tunnel;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| program | &str | Yes | SSH binary path (e.g. "ssh") |
| url | &str | Yes | Database URL to tunnel to (scheme identifies dialect for default port) |
| via | &str | Yes | SSH target: "user@host" or "user@host:port" |
Outputs
| Name | Type | Description |
|---|---|---|
| url | String | Rewritten URL pointing at 127.0.0.1:<local_port> |
| tunnel | Tunnel | Guard that closes the SSH process on drop |
| Err | String | Error message (ssh exit, timeout, unsupported URL) |
Usage Examples
Tunneling to a PostgreSQL database
let (rewritten, tunnel) = tunnel::open(
"ssh",
"postgres://app:pw@db.internal/app",
"me@bastion:2222",
).await?;
// rewritten → "postgres://app:pw@127.0.0.1:54321/app"
// tunnel holds the ssh process; closing it when dropped
Default port selection
// Ports are inferred from the dialect when the URL omits one:
// PostgreSQL → 5432, MySQL → 3306, Redis → 6379, MongoDB → 27017,
// Scylla → 9042, ClickHouse → 8123 (HTTP) / 8443 (TLS),
// Oracle → 1521 / 2484 (TCPS), MSSQL → 1433, SurrealDB → 8000