Current section

Files

Jump to
etunnel README.md
Raw

README.md

# etunnel for Elixir

[![Hex](https://img.shields.io/hexpm/v/etunnel?label=Hex%20etunnel)](https://hex.pm/packages/etunnel)

websockex + jason; local requests via :httpc.

Zero registration: every SDK client speaks the same tiny wire protocol
([PROTOCOL.md](../../PROTOCOL.md)) against any etunnel server
(default `wss://t.etun.uk/tunnel`).

## Install

```
# mix.exs
defp deps do
  [{:etunnel, "~> 1.0"}]
end
```

Requires Elixir >= 1.12 / OTP 24+.

## CLI

```
mix run --no-halt runner.exs 3000
```

Prints `TUNNEL_URL: https://<subdomain>.t.etun.uk` — that URL now routes to
your local service. Ctrl-C closes the tunnel.

**Reconnects automatically.** If the connection drops (including a server
restart) the CLI re-dials with exponential backoff (500ms → 30s, ±20%
jitter, reset on success) and prints `TUNNEL_URL:` again. The subdomain is
**reclaimed**, not re-randomised: the client replays the server's signed
`resumeToken` in `hello.resume`, so links already shared keep working (the
server needs a stable `SESSION_SECRET`; see PROTOCOL.md §1). Set
`ETUNNEL_RECONNECT=0` to restore the old one-shot behaviour.

## Embed

```elixir
{:ok, t} = Etunnel.start_link(server: "wss://t.etun.uk/tunnel", local: 3000)
url = Etunnel.url(t)              # waits ≤10s for welcome; {:error, reason} on failure

# the server's signed resume token arrives at the start_link caller:
receive do {:etunnel_resume_token, tok} -> tok end

Etunnel.close(t)                  # sends bye and tears down the connection
{:ok, t2} = Etunnel.start_link(server: "wss://t.etun.uk/tunnel", local: 3000, resume: tok)
```

`Etunnel.url/1` waits up to 10s for the welcome frame and returns the URL, or
`{:error, reason}` — including the structured `{code, message}` of a server
error frame, which also stops the tunnel process. Reconnecting with the
`resumeToken` reclaims the same subdomain.

TLS verifies the peer against the OS CA store by default (`verify_peer` +
SNI). Pass `tls_verify: :verify_none` only for throwaway self-signed servers.

## Environment variables

| Variable | Meaning | Default |
|---|---|---|
| `ETUNNEL_SERVER` | tunnel server WebSocket URL | `wss://t.etun.uk/tunnel` |
| `ETUNNEL_LOCAL` | local port or `host:port` (or 1st CLI arg) | `3000` |
| `ETUNNEL_TOKEN` | paid-plan API key (optional) | - |
| `ETUNNEL_RECONNECT` | `0` disables auto-reconnect (one-shot CLI) | enabled |
| `ETUNNEL_SUBDOMAIN` | custom subdomain (paid plans) | random |

## License

MIT — see [LICENSE](../../LICENSE). Buffer-based protocol client: request and
response bodies are buffered per stream (fine for webhooks/dashboards/demos;
use the [Go SDK](../../sdk) for streaming and WebSocket pass-through).

## Service terms

Use of the etunnel relay service (default `wss://t.etun.uk/tunnel`) is governed
by its [Terms of Service](https://t.etun.uk/terms) and
[Privacy Policy](https://t.etun.uk/privacy).