# Native handoff

Native programs that already have their own peer-to-peer transport (such as a WireGuard tailnet) do not need to send application traffic through a vagus relay. Native handoff seals a connection route into a 5-minute encrypted invitation URL that the other peer resolves through the handoff service and decrypts locally, keeping live credentials out of chat history and off the relay.

## When to use it

Use native handoff whenever two native programs need to pass a short-lived connection credential over a channel that keeps history (team chat, email, issue trackers, synced clipboards, or photographed QR codes).

For example, when bootstrapping a peer-to-peer WireGuard tunnel, machine A seals its route (such as a WireGuard public key, pre-shared key, and relay endpoint) and prints an invitation URL or QR code. Machine B resolves the link, decrypts the route locally, and dials the tunnel. The relay never sees the route or pre-shared key, traffic stays off the relay once connected, and the link left in chat stops working after 5 minutes. The same flow works for pairing a mobile app via QR code or sharing a one-off database URL or SSH target in chat.

A route is 1 to 1,024 printable ASCII characters with no spaces. vagus treats it as opaque data: your programs define the format, and the receiver must validate that the resolved route is safe to dial.

Anyone with the full invitation URL can redeem it repeatedly until it expires, so peers must still authenticate each other (for example, with mutual approval or application credentials) when they connect.

## Run the handoff service

Enable native handoff on a relay with `-native-handoff`. It shares the relay's HTTPS listener and certificate with route statements and [browser admission](https://vagus.neuroplast.io/docs/admission):

```sh
vagus relay -public :443 -control :19451 -domain relay.example.com \
  -https :443 -allow-origins https://app.example.com -native-handoff \
  -cert control.pem -key control.key
```

To run native handoff without browser admission, pass `-https :443 -native-handoff` and omit `-allow-origins`.

Handoff offers and redemptions use `POST /v1/handoffs`. The handler rejects any request with an `Origin` header so web pages cannot call it.

In Docker Compose (`examples/compose`), set `NATIVE_HANDOFF=true` (the HTTPS listener is already enabled).

To run handoff standalone without a relay:

```sh
vagus handoff serve -listen :8443 -cert service.pem -key service.key
```

## Offer and resolve from the CLI

On the offering machine, pipe the route into `vagus handoff offer`:

```sh
printf '%s\n' "$route" | vagus handoff offer -relay https://handoff.example.com
```

Send the printed invitation URL to the other peer. On the receiving machine, pipe the invitation into `vagus handoff resolve`:

```sh
printf '%s\n' "$invitation" | vagus handoff resolve -relay https://handoff.example.com
```

`resolve` prints the original route to stdout. Keep both outputs private. `-relay` pins the expected HTTPS origin: `resolve` rejects invitations for any other origin before making a network request.

## How it works

1. The issuer sends an empty `POST /v1/handoffs` over TLS (`Content-Type: application/octet-stream`).
2. The service returns a random 256-bit key and a ticket containing that key plus a 5-minute expiry, sealed under a random in-memory wrapping key kept by the service process.
3. The issuer encrypts the route locally with AES-GCM using the returned key, binding the ticket as associated data, and puts the ticket and ciphertext in the invitation URL's fragment (`#...`).
4. The recipient sends only the fixed-size ticket to `POST /v1/handoffs`. The service unseals it, checks the expiry, and returns the key. The recipient decrypts the route locally.

Neither HTTP request includes the plaintext or encrypted route, and HTTP redirects are rejected.

Tickets are bound to the process that issued them. Restarting the service or changing its origin invalidates unredeemed invitations. Behind a load balancer, redemption requests must hit the same process that issued the ticket.

## Embed in Go

Module `neuroplast.io/vagus/pkg/handoff` uses only the Go standard library:

```go
invitation, err := handoff.Offer(ctx, client, origin, route)
route, err := handoff.Resolve(ctx, client, origin, invitation)
```

Set a deadline on `ctx`. Passing a `nil` client uses `http.DefaultClient`. Always pass `origin` from trusted configuration, never parsed out of the invitation URL.

To mount handoff inside your own HTTPS server, register `handoff.NewServer()` at `handoff.Path`. Embedded relays enable it with `relay.Options.NativeHandoff = true` alongside `relay.Options.HTTPS`.
