netbridge: Reverse SSH Tunnels for Larzscript, No Inbound Port Needed
2026-08-02 · Larzscript
Every machine behind a home router, a phone's carrier NAT, or a locked-down office firewall shares the same problem: nothing on the outside can dial in. The usual fixes are a commercial VPN, port-forwarding rules you don't control, or a bespoke relay server nobody trusts. [netbridge](https://larzos.com/stack/larznetbridge/) is a new Larzscript package that does it the boring, reliable way instead: a reverse SSH tunnel to a relay server you already own.
Two tunnel kinds, one primitive
Everything here is built on plain ssh -R - no VPN client, no extra daemon on the relay beyond the sshd it almost certainly already runs.
- `forward` - a classic reverse port-forward.
relay:PORTbecomes a door straight to one service on this machine (its own sshd, a database, a dev server). One port in, one port out. - `socks` - a reverse *dynamic* forward, no destination. The relay gets a SOCKS5 listener, and this machine's own
sshclient resolves every connection through its own routing table. One tunnel reaches your whole reachable network, not just one port.
import "netbridge" as netbridge
let srv = netbridge.default_server()
srv["host"] = "relay.example.com"
srv["identity"] = "~/.ssh/netbridge_key"
let shell = netbridge.tunnel_forward("my-shell", 2200, "127.0.0.1", 22)
print(netbridge.route_label(shell))
print(netbridge.build_ssh_command(srv, shell))
relay:2200 -> 127.0.0.1:22
'ssh' -N -T -p 22 -i '~/.ssh/netbridge_key' -R '2200:127.0.0.1:22' -o BatchMode=yes -o ExitOnForwardFailure=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile='known_hosts' 'tunnel@relay.example.com'
That's the real command a caller would hand to ssh - netbridge builds it correctly for both tunnel kinds so you never have to remember ssh -R's slightly awkward [bind_address:]port[:host:hostport] syntax by hand.
Why it's silent when it works
A reverse tunnel run with -N (no remote command) is deliberately quiet on success - it just sits there forwarding traffic. That means "did it connect" can't be answered by waiting for output or an exit code that may never come while the tunnel is healthy. Instead netbridge scans whatever ssh *does* print for a fixed set of known failure phrases - permission denied, remote port forwarding failed, connection refused, host key verification failed, and a few others - and treats silence as success:
print(netbridge.is_error_line("debug1: Authenticating to relay.example.com")) # false
print(netbridge.is_error_line("Permission denied (publickey).")) # true
Reconnecting without hammering the relay
supervise() runs one attempt, and if it fails, waits before trying again - doubling the wait each time (retry_min up to retry_max), and resetting back to retry_min the moment a connection stays up for more than a minute. That's the same shape a lot of production reconnect logic uses, just written out in the open instead of buried in a framework.
The one honest limitation worth naming: Larzscript has no OS threads, so supervise() blocks the process it runs in for as long as the tunnel needs to stay up. The design this package is modeled on (a real Windows tool called NetBridge) runs one supervisor thread per tunnel inside a single GUI process. Larzscript can't do that - so the pattern here is one larzscript process per tunnel, backgrounded by your own OS (nohup larzscript run_tunnel.lz my-shell &, a systemd unit, a scheduled task) - which is honestly closer to what's actually happening under the hood anyway: each tunnel is *already* its own separate ssh child process, thread or no thread.
A whole-network tunnel in one line
let net = netbridge.tunnel_socks("internal-net", 1080)
print(netbridge.route_label(net))
# SOCKS5 @ relay:1080 -> (whole reachable network)
Point any SOCKS5-aware client at relay:1080 and it reaches everything this machine can reach - handy for reaching an internal service from outside without punching a hole for each one individually.
Try it
larzscript pkg install netbridge
import "netbridge" as netbridge
let srv = netbridge.default_server()
srv["host"] = "your-relay.example.com"
let shell = netbridge.tunnel_forward("my-shell", 2200, "127.0.0.1", 22)
netbridge.supervise(srv, shell, {"log_fn": fn(name, line) { print(line) }})
Full source, the config-persistence layer, and the exact error-marker list are on GitHub - or see it alongside the rest of the Larz Stack.