| Crates.io | reverse-ssh |
| lib.rs | reverse-ssh |
| version | 0.2.0 |
| created_at | 2025-10-29 19:01:08.760897+00 |
| updated_at | 2025-12-09 21:00:29.061313+00 |
| description | A Rust library for creating reverse SSH tunnels with automatic URL capture from services like localhost.run |
| homepage | https://github.com/aovestdipaperino/rrp |
| repository | https://github.com/aovestdipaperino/rrp |
| max_upload_size | |
| id | 1907195 |
| size | 979,559 |
A Rust library for creating reverse SSH tunnels using the russh crate.
Reverse SSH (also called remote port forwarding) allows you to:
This is particularly useful for:
Internet -> SSH Server:8080 -> [SSH Tunnel] -> Your Local Service:3000
The fastest way to try reverse SSH tunneling:
# 1. Clone and build
git clone <your-repo-url>
cd reverse-ssh
# 2. Start the test server
cargo run --example simple_server
# 3. In another terminal, test with localhost.run
cargo run --example localhost_run
That's it! You'll get a public URL that forwards to your local server.
Add this to your Cargo.toml:
[dependencies]
reverse-ssh = "0.1.0"
use reverse_ssh::{ReverseSshClient, ReverseSshConfig};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = ReverseSshConfig {
server_addr: "your-server.com".to_string(),
server_port: 22,
username: "your-username".to_string(),
key_path: Some("/path/to/private/key".to_string()),
password: None,
remote_port: 8080,
local_addr: "127.0.0.1".to_string(),
local_port: 3000,
};
let mut client = ReverseSshClient::new(config);
client.run().await?;
Ok(())
}
server_addr: SSH server hostname or IPserver_port: SSH server port (usually 22)username: SSH usernamekey_path: Path to private key (for key-based auth)password: Password (for password-based auth)remote_port: Port on SSH server to listen onlocal_addr: Local address to forward to (usually 127.0.0.1)local_port: Local port to forward toYou can use either key-based or password authentication:
// Key-based authentication
let config = ReverseSshConfig {
// ...
key_path: Some("/home/user/.ssh/id_rsa".to_string()),
password: None,
// ...
};
// Password authentication
let config = ReverseSshConfig {
// ...
key_path: None,
password: Some("your-password".to_string()),
// ...
};
For reverse port forwarding to work, your SSH server must allow it. Add this to /etc/ssh/sshd_config:
GatewayPorts yes
AllowTcpForwarding yes
Then restart the SSH service:
sudo systemctl restart sshd
This library includes several examples to help you get started:
Start a local HTTP server to test your tunnel:
cargo run --example simple_server
This runs a web server on localhost:8080 that displays connection information. Perfect for testing!
Expose your local service to the internet using localhost.run:
# First, start the test server
cargo run --example simple_server
# In another terminal, create the tunnel (default: port 8080, ~/.ssh/id_rsa)
cargo run --example localhost_run
# Or specify custom SSH key and port
cargo run --example localhost_run -- --key ~/.ssh/my_custom_key --port 3000
# Or use environment variables
SSH_KEY=~/.ssh/my_key LOCAL_PORT=3000 cargo run --example localhost_run
# See all options
cargo run --example localhost_run -- --help
Command-line Options:
--key, -k <path> - Path to SSH private key (default: ~/.ssh/id_rsa)--port, -p <port> - Local port to forward (default: 8080)--help, -h - Show help messageEnvironment Variables:
SSH_KEY - Path to SSH private keyLOCAL_PORT - Local port to forwardlocalhost.run is a free SSH tunneling service (similar to ngrok) that requires no registration.
How it works:
Internet SSH Tunnel Your Machine
↓ ↓ ↓
http://xxx.localhost.run → ssh.localhost.run:22 → localhost:8080
Automatic Features:
⚠ SSH key not found: /home/user/.ssh/id_rsa
Would you like to generate a new SSH keypair?
This will create /home/user/.ssh/id_rsa and /home/user/.ssh/id_rsa.pub [Y/n]:
Just press Enter or type 'Y' to generate a keypair automatically.
How it works:
╔══════════════════════════════════════════════════════╗
║ 🌐 TUNNEL ACTIVE 🌐 ║
╠══════════════════════════════════════════════════════╣
║ Your local service is now accessible at: ║
║ ║
║ https://abc123def456.localhost.run ║
║ ║
║ Local: http://127.0.0.1:8080 ║
║ Connected in: 2s ║
╚══════════════════════════════════════════════════════╝
The example automatically parses server messages to extract and display your public URL!
Alternative: You can also use the traditional SSH command for comparison:
ssh -R 80:localhost:8080 localhost.run
Test with your own SSH server:
export SSH_HOST=your-server.com
export SSH_USER=your-username
export SSH_KEY=~/.ssh/id_rsa
export REMOTE_PORT=9999
export LOCAL_PORT=8080
cargo run --example local_test
This example:
http://your-server.com:9999Minimal example showing the library usage:
# Edit examples/basic.rs with your SSH server details
cargo run --example basic
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.