lua/

directory
v1.7.53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT

README

etunnel for Lua

LuaRocks

luasocket + luasec (hand-rolled WebSocket with client SNI; lua-http is used for wss when that rock is installed).

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

Install

luarocks install ggcode.etunnel     # 1.7.36-1: pulls luasocket + luasec, installs ggcode/etunnel.lua
# single file (needs the LuaSocket + LuaSec rocks, Lua 5.3+):
curl -O https://raw.githubusercontent.com/topcheer/etunnel/main/sdks/lua/etunnel.lua
# from a source checkout - run make from the repo root (the rockspec maps sdks/lua/etunnel.lua):
luarocks make sdks/lua/ggcode.etunnel-1.7.36-1.rockspec   # -> <tree>/share/lua/<ver>/ggcode/etunnel.lua

Requires Lua 5.3+ (string.pack). Fixed in 1.7.36-1: the 1.7.35-1 rock failed with Failed copying etunnel.lua (its rockspec pointed at an old tag).

AI agent quick-start (copy this prompt)

Integrate etunnel (Lua SDK) into this project to expose the local service on port <PORT> at a public HTTPS URL.

- Package: `ggcode.etunnel` on LuaRocks (1.7.36-1) · Install: `luarocks install ggcode.etunnel`; the installed file is <tree>/share/lua/<ver>/ggcode/etunnel.lua - copy it next to your code to embed
- 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; append to a copy of etunnel.lua named myapp.lua, run `lua myapp.lua`):
```lua
local t = connect('wss://t.etun.uk/tunnel', 3000, nil, nil, nil, nil)
print('TUNNEL_URL: ' .. tostring(t.url))
io.stdout:flush()
t.run()
```
- Rules: print the public URL as `TUNNEL_URL: <url>` and keep the process alive (t.run() blocks); call t.close() on shutdown to release the subdomain. This SDK is buffer-based (no WebSocket upgrades, no streaming, sequential requests) - 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

lua etunnel.lua 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.

  • Edge liveness probe (PROTOCOL.md 3a): every 60s the tunnel GETs its own url + /.well-known/etunnel-health (first probe after 3s, 10s timeout; plain LuaSocket/LuaSec, environment proxies are not honoured). A 200 from the edge sets t.edge_state = "ok"; an edge that cannot be reached from this host (offline, DNS, captive portal) is "unreachable" and logged - the tunnel stays up; when the edge answers 404 twice in a row it no longer routes this hostname, so the tunnel closes with bye reason edge lost tunnel and the CLI reconnects. Options (7th connect argument, a table): edge_probe_url (probe another base URL; Host: stays the tunnel hostname), edge_probe_interval (seconds; 0 = 60s, negative = disabled), or the ETUNNEL_EDGE_PROBE_URL / ETUNNEL_EDGE_PROBE_INTERVAL env vars; on_log / on_edge_state callbacks observe the lines and state transitions. State before the first probe: "unknown".

Embed

etunnel.lua exports no module table: connect is file-local and the CLI block only runs when etunnel.lua itself is executed. Copy the file next to your code, rename it (e.g. myapp.lua) and append your main; run lua myapp.lua.

local t = connect('wss://t.etun.uk/tunnel', 3000, nil, nil, nil, nil)
print('TUNNEL_URL: ' .. tostring(t.url))
io.stdout:flush()
t.run()   -- blocking serve loop (one request at a time); returns when the tunnel drops
-- on shutdown: t.close()   (sends `bye`, frees the subdomain)

connect(server, local_port, token, subdomain, resume, routes) blocks until welcome and returns {url, hostname, resumeToken, edge_state, close_reason, run(), close()}; run() must be called to serve requests (sequential). local_port is a port or "127.0.0.1:port"; replay t.resumeToken via the resume argument to reclaim the same subdomain. An optional 7th argument, a table, carries the edge probe options (edge_probe_url, edge_probe_interval, on_log, on_edge_state); run() returns the close reason when the SDK closed the tunnel itself ("edge lost tunnel"), and t.edge_state is "unknown"|"ok"|"unreachable".

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
ETUNNEL_EDGE_PROBE_URL override the edge probe base URL (default: the tunnel's public URL; Host: stays the tunnel hostname) -
ETUNNEL_EDGE_PROBE_INTERVAL edge probe interval in seconds (fractions allowed); 0 = 60s, negative disables the probe 60

License

MIT — see LICENSE. Buffer-based protocol client: request and response bodies are buffered per stream (fine for webhooks/dashboards/demos; use the Go 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 and Privacy Policy.

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).

local t = connect("wss://t.example.com/tunnel", nil, nil, nil, nil, {
  { 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).

Environment

Runs on Lua 5.3-5.5 (verified on 5.5.1 with LuaSocket 3.1 + LuaSec 1.3, without lua-http). luac -p (5.4) is the CI gate.

Directories

Path Synopsis
testtools
wssserver command
wssserver is a minimal WebSocket fixture for the Lua SDK tests (issue #52 review must-fix): it answers the first connection with a welcome carrying a resumeToken, closes it, and verifies that the supervisor's reconnect replays the token in hello.
wssserver is a minimal WebSocket fixture for the Lua SDK tests (issue #52 review must-fix): it answers the first connection with a welcome carrying a resumeToken, closes it, and verifies that the supervisor's reconnect replays the token in hello.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL