paulrbrt / nord-single-egress
One egress process for a single-device VPN account: shared login gate, warm tunnel reuse, handoff and rotation, over a local socket. Keeps a NordVPN-style account within its concurrent-session and login-rate limits while many workers keep using it.
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
One egress process for a single-device VPN account. Every worker asks it for a request over a local socket; none of them opens its own tunnel. That is how a NordVPN-style account — one login per window, a hard cap on concurrent sessions — serves an entire fleet without getting throttled, penalised, or billed for extra devices.
PHP 8.1+, ext-curl, no runtime dependencies. Composer-installable, and the
test suite runs offline.
composer require paulrbrt/nord-single-egress
The problem this solves
A single-device VPN account gives you one credential and two limits:
- a login rate (NordVPN: about one login per 60 seconds per account), and
- a session cap (a small number of concurrent tunnels; opening more is treated as account sharing).
The naive shape — each worker opens its own tunnel when it needs one — spends a login per worker, re-logs after every recycle, and walks straight into the cap. People then buy more devices, more accounts, or more proxies to get around a limit that is really an architecture problem.
The shape that works is the one a phone uses: one process holds the tunnels; everything else is a client of it. Logins are metered in one place, warm tunnels are reused, and a restart hands over instead of starting cold.
What you get
| Piece | What it does |
|---|---|
LoginGate |
Cross-process gate: at most login_limit logins per login_window_seconds, enforced with flock + a state file. Every tunnel opens through it. |
Manager |
Owns the warm tunnels. Reuses a warm tunnel for its origin, bounds the set to slots with LRU eviction, rotates on pressure, and performs a declared handoff. |
CurlTransport |
The real network transport: one reusable curl handle per tunnel, keep-alive, proxy credential, HTTP/2. |
Server / Client |
A unix-socket JSON-line protocol. A worker can be restarted freely — it never held a tunnel. |
bin/nord-egress |
serve, request, stats, handoff, rotate. |
Quick start
cp examples/config.php config.php # then put real credentials in it chmod 0600 config.php bin/nord-egress serve --config=config.php # the one process bin/nord-egress request --config=config.php \ --url=https://api.example.com/v1/status --json bin/nord-egress stats --config=config.php
From a worker:
use NordSingleEgress\Client; use NordSingleEgress\Config; $config = Config::fromFile('/etc/myapp/egress.php'); $client = new Client($config->socketPath); $response = $client->request('https://api.example.com', 'GET', 'https://api.example.com/v1/status'); echo $response->status, ' ', $response->body;
Nothing in that snippet opens a connection to the VPN, so nothing in it can spend a login.
The rules it encodes
- A login is a shared, rationed resource. The gate is the only place a login is spent, and it is spent before the tunnel opens — a failed open still costs what the provider saw.
- Reuse beats re-open. A request to an origin with a warm tunnel rides it. Fresh tunnels are for new origins and for rotations only.
- A bound, not a pile.
slotscaps the warm set; the least-recently-used closes before a new one opens, so the account's session cap is respected by construction. - Rotation is rare and measured. It happens on pressure (two straight
transport deaths, or an empty-answer wave you report) and never more often
than
rotation_dwell_seconds. - A handoff is declared, not discovered.
handoff()closes the tunnels and returns the warm-up cost (warm_origins × login window), so a recycle is a planned degradation instead of an outage.
Observability
stats() (or nord-egress stats) answers the questions that matter in
production:
{
"warm": [{"origin": "https://api.example.com", "exit": "nord=us-miami", "age_s": 412.7, "strikes": 0}],
"warm_count": 1,
"slots": 8,
"rotations": 2,
"gate_wait_s": 0.0
}
handoff() returns the successor's bill before you pay it:
{"warm_origins": 3, "warmup_budget_s": 180, "warm": [...]}
What this library deliberately does not decide
It moves bytes through one exit. It has no opinion about your application:
- No budgets by default.
request_timeout_secondsis required, and a request with no timeout is refused rather than silently given one. Tuning a timeout is a configuration change in your project, never an edit to egress code — which is the point of keeping the two separate. - No retry policy beyond the tunnel. A first transport failure is retried on the same warm tunnel; a second means the tunnel is gone, so it rotates and tries once more. What counts as an application failure is yours.
- No provider knowledge. It does not know which API you call, what a payload
means, or how your app signs it. Any provider-specific adapter belongs in your
code, on top of
Manager. - No queue of requests. Callers are admitted by
slotsand the login gate; there is no work queue inside the process, and nothing is buffered on your behalf.
Runs on shared hosting
This was built for the kind of box where you get PHP and a cron entry and nothing else — no root, no systemd, no supervisor, no ability to install a daemon.
-
No extensions beyond
curlandjson. Nopcntl, noposix, no sockets extension: the unix socket is plainstream_socket_server. -
One process, no fork, no threads.
serveis foreground; if it dies, the next cron tick restarts it. -
Cron-safe relaunch.
serverefuses to start when a live server already answers on the socket, so a* * * * *entry cannot unlink a working socket out from under live traffic. Use--runtime=NNto bound each run below the host's process cap:* * * * * /usr/bin/php /path/bin/nord-egress serve --runtime=55 --config=/path/config.php
-
State lives in your app directory. The socket, the gate lock and its state file all live under
state_dir; nothing needs a system path. -
Small footprint: the process holds one tunnel table and a handful of descriptors, so it fits a cheap shared plan alongside the website.
Testing
composer test # or: php tests/run.php
The suite uses an in-memory transport and a temporary state directory: no network, no VPN, no credentials. It covers the gate's spacing, tunnel reuse, slot eviction, rotation dwell, the retry ladder, and a real client/server roundtrip over a unix socket.
When not to use this
- Multiple accounts. This library meters ONE credential. If you legitimately
have several, run one process per account and give each its own
state_dir. - Per-request IP rotation. That is the opposite shape: you want a new login per request, and this library is built to minimise logins.
- A shared multi-tenant host where you cannot supervise a long-lived
process.
serveis a foreground daemon; it belongs under systemd, s6 or supervisor.
Notes on providers
docs/ carries the provider notes this design was measured against, including
nord_proxy.md (endpoints, credential handling, what the limits actually are).
The library itself is provider-agnostic: any HTTP/SOCKS proxy with a username
and password works, because ExitPath is just host, port and credential.
License
MIT.