vite-plugin-ssh-tunnel-advanced
v1.0.7
Published
Vite plugin to create SSH tunnels using system SSH for exposing local dev server remotely.
Maintainers
Readme
Vite Plugin SSH Tunnel Advanced
A Vite plugin to easily create an SSH tunnel from your local development server to a remote server. This allows you to expose your local Vite dev server (e.g., localhost:5173) on a port on a remote machine, making it accessible via the remote machine's IP or a domain pointing to it.
This plugin uses the system's ssh command-line utility to establish and manage the tunnel.
Features
- Sets up an SSH reverse tunnel (
ssh -R). - Uses a private key for authentication.
- Automatically manages the tunnel lifecycle with the Vite dev server.
- Gracefully closes the tunnel on server shutdown or process exit.
- Configurable remote port.
- Option to display a custom proxy URL in logs.
- Colored logging for better readability.
Prerequisites
- SSH Client: An SSH client must be installed and available in your system's
PATH.- Linux/macOS: Usually available by default.
- Windows: OpenSSH client is often included in modern Windows 10/11 versions or can be installed with Git for Windows.
- SSH Server Configuration:
- The remote SSH server must be configured to allow TCP forwarding. Ensure
AllowTcpForwarding yes(orAllowTcpForwarding all) is set in your server's/etc/ssh/sshd_config. - For the remote port to be accessible from external networks (not just
localhoston the remote server),GatewayPorts yesorGatewayPorts clientspecifiedmust be set in/etc/ssh/sshd_configon the remote server. Restart thesshdservice after changes.
- The remote SSH server must be configured to allow TCP forwarding. Ensure
Installation
npm install vite-plugin-ssh-tunnel-advanced --save-dev
# or
yarn add vite-plugin-ssh-tunnel-advanced --dev
# or
pnpm add vite-plugin-ssh-tunnel-advanced --save-devYou might also need picocolors if not already present (though often a transitive dependency):
npm install picocolorsUsage
Import and add the plugin to your vite.config.js or vite.config.ts:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'; // Example, use your framework plugin
import { sshTunnel } from 'vite-plugin-ssh-tunnel-advanced';
export default defineConfig({
plugins: [
react(),
sshTunnel({
host: 'your-remote-server.com', // SSH server address
username: 'ssh_user', // SSH username
privateKey: '~/.ssh/id_rsa_remote', // Path to your SSH private key
remotePort: 9000, // Port to listen on the remote server
// Optional: If you access the tunnel via a custom domain/proxy
// proxyUrl: 'https://dev.your-app.com'
}),
],
});When you run your Vite dev server (npm run dev or yarn dev), the plugin will:
- Wait for the Vite server to start listening.
- Attempt to establish an SSH tunnel to your remote server.
- Traffic to
your-remote-server.com:9000(or yourproxyUrl) will be forwarded to your local Vite dev server.
Configuration
The sshTunnel plugin accepts a configuration object with the following properties:
| Option | Type | Required | Default | Description |
|--------------|----------|----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------|
| host | string | Yes | | SSH server host address (e.g., 'example.com', '192.168.1.100'). |
| username | string | Yes | | Username for the SSH connection (e.g., 'root', 'deploy_user'). |
| privateKey | string | Yes | | Path to the SSH private key file. Can use ~ for home directory (e.g., '~/.ssh/id_rsa'). |
| remotePort | number | No | 3000 | Remote port on the SSH server to listen on. Traffic to this port will be forwarded to the local Vite server. |
| proxyUrl | string | No | | Optional URL to display in logs after tunnel setup. Useful if the remote server is accessed via a domain/proxy (e.g., 'https://dev.example.com'). |
How it Works
The plugin executes a command similar to this:
ssh -i "<privateKeyPath>" \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-f -N -M -S "<socketPath>" \
-R 0.0.0.0:<remotePort>:localhost:<localVitePort> \
<username>@<host>-R 0.0.0.0:<remotePort>:localhost:<localVitePort>: Sets up remote port forwarding.-M -S <socketPath>: Uses SSH ControlMaster for managing the connection via a socket file, allowing for clean termination.-f -N: Runs SSH in the background and prevents execution of remote commands.
Troubleshooting
- "Private key file not found":
- Ensure the path provided in
privateKeyis correct. - The plugin resolves
~to your home directory. - Check file permissions.
- Ensure the path provided in
- "Failed to create SSH tunnel" / SSH stderr output:
- Permission denied (publickey):
- Verify the private key is authorized on the remote server for the specified user (i.e., its corresponding public key is in
~/.ssh/authorized_keyson the server). - Check permissions of your
~/.sshdirectory (should be700) and~/.ssh/authorized_keys(should be600or644) on the server.
- Verify the private key is authorized on the remote server for the specified user (i.e., its corresponding public key is in
- Connection refused/timeout:
- Ensure the
hostis correct and the SSH server is running and accessible. - Check firewalls (both local and on the remote server).
- Ensure the
- "Warning: remote port forwarding failed for listen port ":
- The
remotePortmight already be in use on the remote server. Try a different port. AllowTcpForwardingmight be disabled on the server.
- The
- Permission denied (publickey):
- Tunnel connects, but
proxyUrlorhttp://<host>:<remotePort>doesn't work:- Ensure
GatewayPorts yes(orclientspecified) is set in the remote server'ssshd_configif you want to access theremotePortfrom outside the remote server'slocalhost. - Check firewalls on the remote server that might be blocking incoming connections to
remotePort. - If using a domain/proxy (like Nginx), ensure it's correctly configured to forward traffic to
localhost:<remotePort>on the remote server.
- Ensure
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
License
This project is MIT licensed.
