.NET
The .NET SDK opens Tuna tunnels straight from your code: expose a local HTTP or TCP service at a public address without installing tuna.
TunaTunnel.ForwardAsyncproxies connections from the public address to a localhost:port.- Survives network failures: reconnects and keeps the same public address.
- Dependencies are only SSH.NET and the standard library.
The API is not stable yet: it may change before v1.
The package is Tuna.Sdk, source code is on GitHub.
Installation
The package will be on nuget.org a bit later. For now, add the code.tuna.am package registry as a NuGet source.
dotnet nuget add source https://code.tuna.am/api/packages/tuna/nuget/index.json --name tuna
dotnet add package Tuna.Sdk
The namespace is Tuna.Sdk. Requires .NET 8 or later.
Authentication
Create an API key with Run tunnels enabled (the For tunnels preset) and pass it in TunaOptions.ApiKey or the TUNA_API_KEY environment variable.
Forward to a local service
using Tuna.Sdk;
var options = new TunaOptions
{
Protocol = "http",
OnEvent = e =>
{
if (e.Type == TunaEventType.Connected)
{
Console.WriteLine($"forwarding {e.Url} -> 127.0.0.1:5000");
}
},
};
await TunaTunnel.ForwardAsync(options, "127.0.0.1:5000", cancellationToken);
ForwardAsync runs until cancellationToken is cancelled or the tunnel ends for good. To get a tunnel object, start it with StartAsync and await Completion:
await using var tunnel = await TunaTunnel.StartAsync(options, "127.0.0.1:5432");
Console.WriteLine(tunnel.Url);
await tunnel.Completion;
The local address is host:port. Unix sockets and listen mode are not supported.
Options
TunaOptions property | Meaning |
|---|---|
Protocol | http or tcp, required |
ApiKey | API key, defaults to $TUNA_API_KEY |
Subdomain, Domain | HTTP: reserved subdomain or custom domain |
Port | TCP: reserved port or its alias |
ProxyProtocol | TCP: PROXY protocol version, 1 or 2 |
CidrAllow, CidrDeny | TCP: client subnets allowed or rejected by the Tuna node |
OnEvent | callback for tunnel events |
ConnectTimeout | limit on the wait for the first connection; zero means no limit |
ServerUrl, ClientName | API address and client name reported to the server |
Events and errors
OnEvent receives Connected, Reconnecting, Reconnected, Banner, SessionExpiring, Terminated and LocalError. Calls never overlap and run on the tunnel's threads, 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 connection to the node is lost, Reconnecting arrives after 15 seconds with a KeepAliveTimeoutException. The tunnel ends with an exception when retrying is pointless; check it with TunaTunnel.IsPermanent:
TunaServerException: the API rejected the request — bad or revoked key, missing permission, unknown reserved name, unpaid plan. The details are inStatus,CodeandRequestId;TunaTerminatedException: the server closed the tunnel — it was deleted, suspended or hit the session time limit. The reason is inReason.
Behavior worth knowing
- The cancellation token passed to
StartAsyncorForwardAsyncbounds the whole life of the tunnel, not only the first connection. Cancelling it or disposing the tunnel closes all connections. To give up early when the tunnel does not come up, setConnectTimeout. - The
Hostheader and the PROXY protocol header reach your service as sent. - The API client is
HttpClientand honorsHTTPS_PROXY. Connections to Tuna nodes are plain SSH and do not use the proxy.