Skip to main content

Rust

The Rust SDK opens Tuna tunnels straight from your code: expose a local HTTP or TCP service at a public address without installing tuna.

  • Two ways in: listen returns a Tunnel that accepts connections as byte streams (AsyncRead + AsyncWrite), forward proxies them to a local address.
  • Survives network failures: reconnects and keeps the same public address.
  • Runs on Tokio. SSH is russh, TLS is rustls, cryptography is ring.
info

The API is not stable yet: it may change before v1.

The package is tuna-sdk on crates.io, source code is on GitHub.

Installation​

cargo add tuna-sdk

The library name is tuna_sdk. Requires Rust 1.89 or later and a C compiler for ring (gcc or clang; neither cmake nor Go is needed).

Authentication​

Create an API key with Run tunnels enabled (the For tunnels preset) and pass it in Options::api_key or the TUNA_API_KEY environment variable.

Accept connections​

use tuna_sdk::{Options, Protocol};

let tunnel = tuna_sdk::listen(Options {
protocol: Protocol::Http,
..Default::default()
})
.await?;
println!("public URL: {}", tunnel.url());

while let Ok(mut visitor) = tunnel.accept().await {
tokio::spawn(async move {
let mut local = tokio::net::TcpStream::connect("127.0.0.1:8080").await?;
tokio::io::copy_bidirectional(&mut visitor, &mut local).await?;
Ok::<_, std::io::Error>(())
});
}

close stops accepting and reconnecting, but the tunnel stays connected until the last accepted stream is released; wait returns after that. abort cuts off all accepted streams at once. Dropping the Tunnel closes it the same way as close.

Forward to a local service​

forward proxies connections from the tunnel to a local address, host:port or unix:///path/to.sock:

use std::sync::Arc;
use tuna_sdk::{Event, Options, Protocol};

tuna_sdk::forward(
Options {
protocol: Protocol::Tcp,
port: "my-db".into(),
on_event: Some(Arc::new(|e: &Event| {
if let Event::Connected { url } = e {
eprintln!("forwarding {url} -> 127.0.0.1:5432");
}
})),
..Default::default()
},
"127.0.0.1:5432",
)
.await?;

forward runs until the tunnel ends for good. To stop it, drop the future, for example with tokio::select! on a shutdown signal.

Options​

Options fieldMeaning
protocolProtocol::Http (default) or Protocol::Tcp
api_keyAPI key, defaults to $TUNA_API_KEY
subdomain, domainHTTP: reserved subdomain or custom domain
portTCP: reserved port or its alias
proxy_protocolTCP: PROXY protocol version, 1 or 2
cidr_allow, cidr_denyTCP: client subnets allowed or rejected by the Tuna node
on_eventcallback for tunnel events
connect_timeoutlimit on the wait for the first connection; None means no limit
server_url, client_nameAPI address and client name reported to the server

Events and errors​

on_event receives Event::Connected, Reconnecting, Reconnected, Banner, SessionExpiring, Terminated and LocalError; Event::name returns the snake_case name. Calls never overlap and run on the tunnel's tasks, so keep the callback short.

Network failures and temporary server errors are retried: the pause grows from 1 to 15 seconds with random jitter, and Retry-After is respected. If the node does not respond for 15 seconds, Reconnecting arrives with Error::KeepAliveTimeout and the tunnel reconnects. The tunnel ends with an error when retrying is pointless; check it with Error::is_permanent:

  • Error::Server: the API rejected the request — bad or revoked key, missing permission, unknown reserved name, unpaid plan;
  • Error::Terminated: the server closed the tunnel — it was deleted, suspended or hit the session time limit.

Behavior worth knowing​

  • When the visitor closes its side of a forwarded connection, the connection is closed in both directions: a response sent after that is lost.
  • The Host header and the PROXY protocol header reach your service as sent.
  • The API client uses the system root certificates and does not go through an HTTP proxy. Connections to Tuna nodes are plain SSH.