Packages
etunnel
1.7.53
1.7.54
1.7.53
1.7.52
1.7.51
1.7.50
1.7.49
1.7.48
1.7.47
1.7.46
1.7.45
1.7.44
1.7.43
1.7.42
1.7.41
1.7.40
1.7.39
1.7.38
1.7.37
1.7.36
1.7.35
1.7.34
1.7.33
1.7.32
1.7.31
1.7.30
1.7.29
1.7.28
1.7.27
1.7.26
1.7.25
1.7.24
1.7.23
1.7.22
1.7.21
1.7.20
1.7.19
1.7.18
1.7.17
1.7.16
1.7.15
1.7.14
1.7.13
1.7.12
1.7.11
1.7.10
1.7.9
1.7.8
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.6.16
1.6.15
1.6.14
1.6.13
1.6.12
1.6.11
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.0
1.5.0
1.4.0
1.3.1
1.3.0
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.0
Expose local services to the internet in one line of code: instant public HTTPS URLs with automatic TLS, through NAT and firewalls, no signup. Webhook-friendly ngrok-style reverse tunnel, with SDKs in 20 languages and a single-binary Go server.
Current section
Files
Jump to
Current section
Files
README.md
# etunnel for Elixir
[](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.7.36"}]
end
```
Requires Elixir >= 1.12 / OTP 24+. Deps: `websockex` (~> 0.4) and `jason`
(~> 1.3). The Hex package contains only `lib/` + `mix.exs` (`runner.exs`, the
CLI, is **not** shipped). Require `~> 1.7.36`: 1.7.35 crashed on every
connect (`KeyError :last_activity`), fixed in 1.7.36.
## AI agent quick-start (copy this prompt)
````text
Integrate etunnel (Elixir SDK) into this project to expose the local service on port <PORT> at a public HTTPS URL.
- Package: `etunnel` on hex.pm (Elixir >= 1.12; deps websockex + jason) · Install: `{:etunnel, "~> 1.7.36"}` in mix.exs deps, then `mix deps.get`
- Server: wss://t.etun.uk/tunnel (override with ETUNNEL_SERVER). Anonymous tier = random subdomain, 4h TTL; custom subdomain / longer TTL need a paid token (opts.token or ETUNNEL_TOKEN).
- Minimal embed (verified against the SDK source):
```elixir
{:ok, t} = Etunnel.start_link(server: "wss://t.etun.uk/tunnel", local: 3000)
IO.puts("TUNNEL_URL: " <> Etunnel.url(t))
Process.sleep(:infinity)
```
- Rules: print the public URL as `TUNNEL_URL: <url>` (Etunnel.url/1 blocks up to 10 s for welcome) and keep the process alive; call `Etunnel.close(pid)` on shutdown to release the subdomain. This SDK is buffer-based (no WebSocket upgrades, no streaming) - fine for webhooks/APIs/dashboards.
- Do not invent other options. Full API/options: #embed and #multi-target-routing. Wire protocol: https://github.com/topcheer/etunnel/blob/main/PROTOCOL.md
````
## CLI
```
cd sdks/elixir && mix deps.get && mix run --no-halt runner.exs 3000 # source checkout only (runner.exs is not in the Hex package)
```
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
IO.puts("TUNNEL_URL: " <> url)
# the server's signed resume token arrives at the start_link caller:
tok =
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)
```
Options (`Etunnel.start_link/1`): `server:` (required), `local:` (port or
`"host:port"`; omit when `routes:` is given), `token:`, `subdomain:`, `resume:`,
`routes:` (see below), `tls_verify:`, `edge_probe_url:`, `edge_probe_interval:`
(edge liveness, see below).
`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.
**Edge liveness probe.** `ping`/`pong` only proves the control socket is
alive; the public hostname can stop routing while the socket stays up
(PROTOCOL.md 3a). Every tunnel GETs `<url>/.well-known/etunnel-health` every
60 s (first probe after 3 s, 10 s timeout, through `:httpc`'s default profile -
not the local replay transport) and `Etunnel.edge_state(t)` returns `:unknown`,
`:ok` or `:unreachable`; transitions are logged to stderr (`edge reachable:
<host> routes to this tunnel`, `edge unreachable: ... (the public URL may not
work from this host; tunnel kept)`, then at most every 5 min `edge still
unreachable: ...`). Only when the edge itself answers `404` twice in a row does
the tunnel send `bye` with reason `"edge lost tunnel"`, deliver
`{:etunnel_closed, "edge lost tunnel"}` to the `start_link` caller and exit
with that reason, so a supervisor reconnects and re-lands the subdomain.
Options: `edge_probe_url: "https://edge.internal:8443"` (dial that base URL
instead, still sending `host: <welcome.hostname>` - the edge routes by Host)
and `edge_probe_interval: 30_000` (ms; `0` = 60 s default, negative = disabled).
Replayed requests carry the visitor's public host in `host` (restored from
`X-Forwarded-Host`, which is kept too - Go SDK parity), so absolute URLs and
Origin/Host same-origin checks work behind the tunnel.
Tests: `mix test` (unit + route suites; the `:edge_probe`-tagged fixture tests
are excluded from a plain `mix test`) and `sh tests/edge_probe.sh` (mix compile
gate + cases 1-4 of `sdks/testtools/EDGE_PROBE.md` against the shared Go
fake-edge fixture - needs go).
```sh
cd sdks/elixir && sh tests/edge_probe.sh
# PASS: sdks/elixir edge probe: mix compile gate + cases 1-4 (4/4)
```
## 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).
## Multi-target routing
One public hostname, several local services - e.g. `/` to a built frontend
and `/api` to a backend - without CORS or a second tunnel. Routing is
client-side; the protocol is unchanged (PROTOCOL.md 2).
```elixir
{:ok, pid} = Etunnel.start_link(
server: "wss://t.example.com/tunnel",
routes: [
%{prefix: "/api", addr: "127.0.0.1:8080", strip: true},
%{prefix: "/", addr: "127.0.0.1:3000"}
])
```
Semantics (identical in every SDK, pinned by tests): longest prefix
wins; matching is on segment boundaries (`/api` never matches `/apiv2`);
`strip` removes only the path part - the query string is carried over
byte-for-byte, an exact hit bottoms out at `/`; no matching route (only
possible without a `/` fallback) answers `404` at the SDK edge.
> Redirect note: with `strip`, a backend 302 to an absolute path (`/users/next`) lands on the fallback route on the next hop - backends should return relative redirects or keep the prefix (same as nginx `proxy_pass` with trailing slash; see #155).