# Connector, relay and registry

> How Minekube makes every server reachable without an open port: a Rust connector with iroh inside the server's process through Java's FFM API, relays that only pass bytes, and a registry on Cloudflare Durable Objects that makes every network an OpenID Connect issuer.

> **Preview: Connect in the Minekube plugin:**
> This page describes networks in the Minekube plugin, the next generation of Connect. Today's Connect plugin and its `<endpoint>.play.minekube.net` addresses are described in the [Connect docs](https://minekube.com/guide/) and keep working. [Networks in the Minekube plugin](https://minekube.com/guide/networks/) compares the two.

Every endpoint, a Paper server or a Velocity proxy, at home behind NAT or in a cluster, should be reachable for players without an open port and without Minekube running anything per server. Three parts do that.

> **Status:**
> Built. The registry on Cloudflare and a first relay run on staging, where the Minekube hub runs; production at `g.minekube.net` and relays in several regions come next.

## The connector, inside the server's process

The connector is a Rust library with [iroh](https://iroh.computer), loaded into the JVM through Java's Foreign Function and Memory API (FFM). It runs inside Paper and Velocity, with the Minekube plugin.

- **One connection, a stream per player.** The connector holds one iroh connection to its relay. Each player is a stream on it: no handshake per join, and one player does not hold up another.
- **Into the platform as a local connection.** Each stream reaches the server as a local Netty connection, through the server's own channel setup, so ViaVersion and Velocity's handlers apply, and with the player's real address.
- **A key is its identity.** On its first start the connector creates a key in the plugin folder. The registry knows which network the key belongs to. Code addresses servers by their labels, not their keys, so setting a server up again, moving it or changing its key changes no code.
- **No Connect plugin needed.** A server or proxy with the Minekube plugin brings its own connector, so it does not need the Connect plugin and should not load it as well. The Connect plugin stays the way online for servers without the Minekube plugin.
- **A narrow boundary.** Bytes cross between Java and Rust through a handful of calls, with backpressure in both directions and no thread per player. No panic crosses the boundary.

Why in the process and not next to it: hosting panels that run one container per server, such as Pterodactyl and Pelican, end every process in the container when Java exits. A companion process would not outlive anything there; it would only cost memory. In the process, the fixed price is one iroh connection per endpoint.

FFM is final from Java 22 on, so the connector loads on servers running Java 22 or newer. The plugin jar carries native libraries for Linux (x64 and arm64), macOS (arm64 and x64) and Windows (x64), built in CI; loading them is not yet tested on every platform.

## Relays: they pass bytes and understand nothing

A relay takes the player's TCP connection, reads the hostname from the handshake, much like SNI in HTTPS, picks a healthy instance of that network, opens a stream to its connector and copies bytes both ways.

- **Every network shares a few relay addresses.** A network needs neither a public IP nor an open port.
- **Players' traffic stays theirs.** In online mode the connection between client and server is encrypted end to end, so the relay cannot read or inject anything. It sees metadata: addresses, hostnames, times and amounts.
- **It runs no customer code.** Network rules run in the network's own runtimes.
- **It keeps working without the registry.** It caches the registry's answers and serves joins from the cache when the registry is unreachable.
- **Endpoints without outbound UDP** reach it through iroh's relay service over HTTPS, which runs inside the relay too.
- **Low latency comes from where the relay is,** not from anycast. The route from player to server is fixed; a relay close to the server lies almost on it. The connector measures the latency to every relay at start and takes the nearest as its home.

A relay is small, so relays are small virtual servers in a few regions, two per region behind a health check. Anycast is planned as a paid tier for failover, not for latency.

## The registry, on Cloudflare

The registry is a Worker with two kinds of Durable Objects: one directory that maps every key to its network and holds pending claim codes and the relays, and one object per network with its instances, their labels, load and home relay. A request goes from the Worker to exactly one object; no object calls another in a player's path.

- **Joining a network.** Always with the server's consent: an invite it shows once (`mk network invite`), a token its environment gives it that a trust rule covers ([as below](#every-network-is-an-openid-connect-issuer)), or the code a new connector logs while it belongs to no network (`mk claim <code> --network <name>`; 8 characters without look-alikes, valid for 15 minutes). An owner cannot add a key without the server, since a key alone is public. [Let servers in](https://minekube.com/guide/networks/let-servers-in) shows each way.
- **System labels** such as platform and version are set by the registry, not by the connector.
- **A signed control channel.** Each instance keeps a WebSocket to its network's object, signed with its key, and reports its load every 10 seconds. Hibernation keeps an idle channel almost free.
- **One source for types.** The messages between Cloudflare and Rust are one Effect Schema, turned into JSON Schema and from there into Rust types; CI fails if they drift.

## Every network is an OpenID Connect issuer

An instance's identity should be checkable outside Minekube too, with standard tools and without asking us. So every network is an [OpenID Connect](https://openid.net/connect/) issuer, hosted by the registry:

- **Its keys are public.** `https://<registry>/n/<network>/.well-known/openid-configuration` and `/jwks` list the Ed25519 keys of the network's members, each with its instance ID as `kid`. A token an instance signs with its own key can be checked anywhere with any JWT library, for as long as the instance belongs to the network.
- **Servers join with the token their environment gives them.** Instead of a claim code, a server can show the token it already has: a Kubernetes service account token, or a CI job's OIDC token. The network keeps trust rules: the issuer, the audience, the subject (exact or by prefix) and, for an issuer the registry cannot reach, such as a private cluster's, that issuer's key set. `mk trust add`, `mk trust list` and `mk trust remove` manage them. In `connector.json`, `"join": {"network": "<network>", "token": "<file>"}` names the token file. The connector reads the file fresh on each attempt, since projected tokens rotate, and posts the token signed with its own key; the registry checks it against the issuer's published keys, and when a rule matches, the key becomes a member. No Minekube secret is stored on the server.
- **Keep such tokens narrow.** The environment's token is not bound to the instance's key, so a rule should require an audience of its own (`minekube`) and short-lived tokens, as Kubernetes projects them.
- **Labels from the rule.** `mk trust add --labels game=parkour` (and `mk network invite --labels`) gives every server it lets in these labels from the registry, so one rule decides which servers a revision reaches.
- **Planned:** tokens for players, so a network's website, shop or forum can offer "sign in with our server"; and a signing key of the network's own for statements the network makes itself.

Rights stay primitives here too: a verified token says who is calling; what a network does with that is up to its own routing.

## Measured

Latency through the relay, the connector's memory, the relay under load, start and restart times and a change of network mid-session were measured on one machine. The numbers, with what, where and when, are on [Performance](https://minekube.com/games/platform/performance#networking).
