Skip to main content

Java

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

  • Two ways in: Tuna.listen hands you connections as streams, Tuna.forward proxies them to a local address.
  • Survives network failures: reconnects and keeps the same public address.
  • Built on Apache MINA SSHD and the JDK HTTP client.
info

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

The package is am.tuna:tuna-sdk, source code is on GitHub.

Installation​

code.tuna.am only for now

The package will be on Maven Central a bit later. For now, add the code.tuna.am package registry as a Maven repository.

Maven:

<repositories>
<repository>
<id>tuna</id>
<url>https://code.tuna.am/api/packages/tuna/maven</url>
</repository>
</repositories>

<dependency>
<groupId>am.tuna</groupId>
<artifactId>tuna-sdk</artifactId>
<version>0.1.0</version>
</dependency>

Gradle:

repositories {
maven { url = uri("https://code.tuna.am/api/packages/tuna/maven") }
}

dependencies {
implementation("am.tuna:tuna-sdk:0.1.0")
}

Requires Java 17 or later. Besides MINA SSHD, the SDK depends on Bouncy Castle (bcprov-jdk18on) for Ed25519 keys. MINA logs through SLF4J: to see the logs, add a binding such as slf4j-simple; to silence the missing-binding warning, add slf4j-nop.

Authentication​

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

Forward to a local service​

Options options = new Options()
.protocol("tcp")
.port("my-db")
.listener(e -> {
if (e.type() == EventType.CONNECTED) {
System.out.println("forwarding " + e.url() + " -> 127.0.0.1:5432");
}
});

try (Tunnel tunnel = Tuna.forward(options, "127.0.0.1:5432")) {
tunnel.await();
}

The local address is host:port or unix:///path/to.sock (Unix sockets are not available on Windows). forward returns once the tunnel is up; await blocks until the tunnel is closed or ends for good.

Handle connections yourself​

Tunnel tunnel = Tuna.listen(new Options().protocol("http"));
System.out.println("public URL: " + tunnel.url());

while (true) {
Connection c = tunnel.accept();
executor.submit(() -> handle(c.getInputStream(), c.getOutputStream()));
}

A Connection is a pair of blocking streams, like a Socket: writes go out immediately, closing the output stream signals end of data, and close() closes the connection in both directions. Tunnel.close() stops accepting and reconnecting, but the tunnel stays connected until the last accepted connection is closed, so in-flight requests get to finish.

Options​

Options methodMeaning
protocolhttp or tcp, required
apiKeyAPI key, defaults to $TUNA_API_KEY
subdomain, domainHTTP: reserved subdomain or custom domain
portTCP: reserved port or its alias
proxyProtocolTCP: PROXY protocol version, 1 or 2
cidrAllow, cidrDenyTCP: client subnets allowed or rejected by the Tuna node
listenercallback for tunnel events
connectTimeoutlimit on the wait for the first connection; zero means no limit
serverUrl, sslContext, clientNameAPI address, TLS settings for it, client name reported to the server

Events and errors​

The listener receives CONNECTED, RECONNECTING, RECONNECTED, BANNER, SESSION_EXPIRING, TERMINATED and LOCAL_ERROR. 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 Tuna.isPermanent:

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

If the tunnel never comes up, listen and forward throw the exception; after it connects, accept and await do.

Behavior worth knowing​

  • On a tunnel from forward, Tunnel.close() cuts off forwarded connections; on a tunnel from listen, it waits for your code to close the accepted connections.
  • The Host header and the PROXY protocol header reach your service as sent.
  • The API client is java.net.http.HttpClient; it uses the JVM proxy settings (https.proxyHost, ProxySelector), not HTTPS_PROXY. Connections to Tuna nodes are plain SSH and do not use the proxy.
  • For a private CA, set sslContext.