Skip to content

fix: support mongodb+srv:// connection URLs with SSH tunnels - #1433

Open
nick22985 wants to merge 5 commits into
dbgate:masterfrom
nick22985:fix/mongodb-srv-ssh-tunnel
Open

fix: support mongodb+srv:// connection URLs with SSH tunnels#1433
nick22985 wants to merge 5 commits into
dbgate:masterfrom
nick22985:fix/mongodb-srv-ssh-tunnel

Conversation

@nick22985

Copy link
Copy Markdown

Summary

  • mongodb+srv:// connection URLs crashed with MongoParseError: mongodb+srv URI cannot have port number when used with SSH tunnels
  • The existing SSH tunnel creates a local port forward and rewrites the URL with a port, but SRV URLs cannot have ports since they use DNS SRV records to discover replica set members
  • Added a SOCKS5 proxy mode to the SSH tunnel infrastructure so the MongoDB driver can route all connections (including DNS-discovered shards) through the tunnel without modifying the URL
  • Regular mongodb:// connections with SSH tunnels are unchanged

How it works

  • Drivers can implement getSSHTunnelMode(connection) returning 'socks' or 'forward'
  • The mongo driver returns 'socks' for mongodb+srv:// URLs
  • In SOCKS mode, a local SOCKS5 server routes each connection through SSH's forwardOut
  • The MongoDB driver's built-in SOCKS5 support (proxyHost/proxyPort) handles the rest. DNS SRV resolution and connections to all replica set members go through the proxy transparently

Closes: #1343

Copilot AI review requested due to automatic review settings April 16, 2026 03:43
@nick22985
nick22985 force-pushed the fix/mongodb-srv-ssh-tunnel branch from 6431730 to 57c46d8 Compare April 16, 2026 03:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds SSH SOCKS5 proxy support so MongoDB mongodb+srv:// connections can work through SSH tunneling without rewriting the URL (SRV URIs cannot include a port). This integrates with the existing SSH tunnel infrastructure and lets drivers choose between traditional local port-forwarding and SOCKS mode.

Changes:

  • Introduced a driver hook (getSSHTunnelMode) and implemented it in the MongoDB driver to select SOCKS mode for mongodb+srv:// URLs.
  • Extended SSH tunnel plumbing (getSshTunnel*, forked forward process) to support a new mode: 'socks'.
  • Implemented a minimal local SOCKS5 server in SSHConnection and wired the MongoDB driver to pass proxyHost/proxyPort to the MongoDB Node driver.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
plugins/dbgate-plugin-mongo/src/backend/drivers.js Adds getSSHTunnelMode and uses MongoDB driver SOCKS proxy options when tunneling SRV URLs.
packages/api/src/utility/sshTunnelProxy.js Passes tunnel “options” through IPC proxy layer.
packages/api/src/utility/sshTunnel.js Adds tunnel mode selection + caching keying and forwards mode to forked process.
packages/api/src/utility/connectUtility.js Chooses tunnel mode per-driver and sets either local forward host/port or SOCKS proxy host/port on the connection.
packages/api/src/utility/SSHConnection.js Adds SOCKS5 server implementation using forwardOut per CONNECT.
packages/api/src/proc/sshForwardProcess.js Starts either forward() or socksForward() based on mode.
packages/api/src/controllers/databaseConnections.js Applies the same tunnel mode selection for native op command args flow.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/api/src/utility/SSHConnection.js
Comment thread packages/api/src/utility/SSHConnection.js
Comment thread packages/api/src/utility/SSHConnection.js
Comment thread packages/api/src/utility/SSHConnection.js
Comment thread packages/api/src/utility/SSHConnection.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: MongoDB srv connections dont work with SSH tunnel

2 participants