A classic reverse tunneling problem! You've come to the right place. I'll outline a few approaches, and recommend the most suitable technologies for your Rust-based setup.
The Goal:
Establish a secure, reverse tunnel from the kiosk (behind a firewall) to the manager server (publicly accessible). Then, allow the client (maintenance worker) to connect to the manager server's IP:Port, which will forward the traffic to the kiosk, creating a seamless tunnel.
Options:
- SSH Reverse Tunneling: Use SSH to create a reverse tunnel from the kiosk to the manager server. The kiosk establishes an SSH connection to the manager server, and then creates a reverse tunnel (-R option) that forwards a port on the manager server to a port on the kiosk.
- Pros: Well-established, secure (if you use SSH keys), and easy to set up.
- Cons: Requires SSH server on the manager, might add overhead.
- WebSockets over TLS: Establish a WebSocket connection from the kiosk to the manager server over TLS (wss://). The manager server can then expose a WebSocket endpoint, which the client connects to. The manager server relays the traffic between the client and kiosk WebSocket connections.
- Pros: Firewall-friendly (uses HTTPS ports), bi-directional communication, and relatively lightweight.
- Cons: More complex to implement than SSH; you'll need WebSocket libraries.
- STUN + TURN: Use Session Traversal Utilities for NAT (STUN) to help the kiosk discover its public IP and port. Then, employ Traversal Using Relays around NAT (TURN) to create a relay connection from the kiosk to the manager server. The client connects to the TURN server (manager), which forwards traffic to the kiosk.
- Pros: Specifically designed for NAT traversal; standardized (RFC 5389/5766).
- Cons: More complex setup; requires additional infrastructure (STUN/TURN servers).
- Custom TCP Tunneling: Implement a custom TCP tunneling protocol using a secure, encrypted connection (e.g., TLS) between the kiosk and manager server. The kiosk connects to the manager, which allocates a port for the client to connect to. The manager server then relays traffic between the client and kiosk.
- Pros: Fine-grained control, lightweight, and tailored to your needs.
- Cons: More development effort; you'll need to handle connection management, encryption, and errors.
Recommendation:
Considering your Rust-based setup and the need for simplicity, I'd suggest Option 1: SSH Reverse Tunneling or Option 2: WebSockets over TLS. Both are viable, secure, and relatively easy to implement.
Here's a more detailed outline for each:
SSH Reverse Tunneling (Recommended)
- On the manager server, ensure SSH is installed and running (e.g., OpenSSH).
- On the kiosk, use the
ssh command with -R option to create a reverse tunnel:
ssh -R 8080:localhost:8080 manager_user@manager_server_ip -N -f
This forwards manager_server_ip:8080 to kiosk:8080. -N prevents command execution, and -f runs SSH in the background.
3. In your Rust daemon on the kiosk, execute the above ssh command when the user requests a tunnel.
4. On the client-side, simply connect to manager_server_ip:8080, and the traffic will be forwarded to the kiosk's localhost:8080.
In Rust, you can use the std::process::Command API to execute the ssh command:
use std::process::Command;
fn create_tunnel() -> std::io::Result<()> {
let status = Command::new("ssh")
.arg("-R")
.arg("8080:localhost:8080")
.arg("manager_user@manager_server_ip")
.arg("-N")
.arg("-f")
.status()?;
Ok(())
}
WebSockets over TLS
- Choose a WebSocket library for Rust, such as
tokio-tungstenite or websocket-rs.
- On the kiosk, establish a WebSocket connection to the manager server (e.g.,
wss://manager_server_ip:443/ws).
- On the manager server, set up a WebSocket endpoint (e.g.,
/ws) using a library like actix-web or tokio-tungstenite. When a client connects, relay the traffic to the corresponding kiosk WebSocket connection.
- On the client-side, connect to
wss://manager_server_ip:443/ws, and the manager server will forward the traffic to the kiosk.
Here's some sample Rust code using tokio-tungstenite for the kiosk:
use tokio_tungstenite::{connect_async, tungstenite::Message};
async fn create_tunnel() -> Result<(), tokio_tungstenite::tungstenite::Error> {
let url = "wss://manager_server_ip:443/ws";
let (ws_stream, _) = connect_async(url).await?;
// Handle WebSocket messages and relay traffic from the manager server
}
And on the manager server (using actix-web):
use actix_web::{web, App, HttpResponse, HttpServer};
use tokio_tungstenite::{tungstenite::Message, WebSocket};
async fn ws_handler(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, actix_web::Error> {
// Handle WebSocket connection from client, relay traffic to kiosk
let res = WebSocket::new(stream, |msg| {
// Relay msg to kiosk WebSocket connection
});
Ok(res)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/ws", web::get().to(ws_handler))
})
.bind("manager_server_ip:443")?
.run()
.await
}
Both approaches require some development effort, but SSH Reverse Tunneling is likely faster to implement, as it leverages existing SSH infrastructure. WebSockets over TLS offer more flexibility and scalability, but require additional library integration.
Lastly, ensure you handle:
- Authentication: Verify the user's identity before establishing the tunnel.
- Authorization: Control which kiosks and users can create tunnels.
- Encryption: Use SSH keys or TLS certificates to secure the connections.
- Error handling: Manage connection drops, timeouts, and other edge cases.
Choose the approach that best fits your project's requirements and your team's expertise. Good luck!