Tunnel Agent

A small Python script you run on your network so PyCorner can reach private services and egress from your IP.

The tunnel agent is what makes private-network connectivity possible. It’s a single-file Python script that opens an outbound WebSocket to PyCorner and waits for instructions: open this TCP socket, make this HTTP call. All bytes are encrypted between the agent and PyCorner (RSA-2048 handshake + AES-256-GCM on top of WSS/TLS), and the agent connects only to the destinations you list for it.

What it gives you

Install

Sign in to app.pycorner.dev, go to Resources → Tunnel Agents, click New Agent, name it (something descriptive — office-pg, customer-prod, etc.) and pick how long its token is valid (90 days unless you choose otherwise, a year at most; see Token expiry). Then open Reachable destinations for the new agent and list what it may connect to: a new agent reaches nothing until something is listed (see What an agent may reach). You’ll get a download for a zip containing:

Unzip and run:

unzip pycorner-agent-<name>.zip
cd pycorner-agent

# Recommended: pixi
pixi run start

# Or with pip
pip install -r requirements.txt
python pycorner_agent.py

The terminal will show:

[INFO] Connecting to wss://app.pycorner.dev/ws/tunnel/
[INFO] WebSocket connected
[INFO] Handshake successful, AES key established
[INFO] Authenticated as agent <uuid>

Once you see “Authenticated”, the Resources → Tunnel Agents page will show a green online dot for that agent. The agent reconnects automatically on network blips with exponential backoff.

What an agent may reach

Which hosts and ports an agent may connect to inside your network is decided centrally, on the Resources page (Tunnel Agents → Reachable destinations) or through the API, and applies at once: the connected agent gets the new list without a restart.

A destination is a host, a port and a protocol:

Examples
Hostdb.internal · *.corp.internal (every name below that domain) · 10.0.0.5 · 10.0.0.0/24 · * (any host)
Port5432 · 8000-8100 · * (any port)
ProtocolTCP (databases, SSH, web) or UDP (DNS, syslog, SNMP)

The rules:

A resource whose target is not on its agent’s list says so on the Resources page, with a button that adds it.

# API: read and replace the list (the Bearer token is your own API token)
curl https://app.pycorner.dev/api/v1/data/tunnel-agents/<AGENT_ID>/reach/ -H "Authorization: Bearer $TOKEN"
curl -X PUT https://app.pycorner.dev/api/v1/data/tunnel-agents/<AGENT_ID>/reach/ \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"entries": ["db.internal:5432", {"host": "10.0.0.0/24", "ports": "53", "proto": "udp"}]}'

Who may use an agent

An agent is used by its owner; an agent of a team by the team’s members. Under Access you can narrow that: to yourself only, and to certain apps (then a resource reaches something through the agent only when it is used from one of them).

A link joins two of your agents: one listens on a port on its own machine, the other connects on its side. Both only dial out, so nobody opens an inbound port. Create them under Resources → Links.

Pools: more than one agent per network

Run two or more agents in the same network and put them in a pool (Resources → Pools). Links and resources can point at the pool instead of one agent. Each new connection goes to a connected member, the one with the fewest open connections first; when a member is down the others take over at once, and connections that were open through it end, as TCP connections do. Set the reachable destinations on the pool so every member reaches the same. Each agent counts for billing as before; a pool costs nothing.

Capabilities

The agent advertises its capabilities to the server at handshake. Current set:

An older agent keeps working with what it has; it just cannot be given UDP or proxy links. Re-downloading the bundle and restarting picks up new capabilities.

Run as a service

The agent installs itself as a service that starts at boot, restarts on failure and runs as an unprivileged user:

sudo python3 pycorner_agent.py install        # Linux, system-wide (systemd)
python3 pycorner_agent.py install --user      # Linux, for your own user, no sudo
python pycorner_agent.py install              # Windows, from an Administrator prompt
python3 pycorner_agent.py status              # is it running?  (add --user where installed so)
python3 pycorner_agent.py uninstall           # stop and remove

install takes the config.json of the current folder (--config FILE for another), refuses one without a token, and --dry-run prints every step without doing anything. On macOS, run python3 pycorner_agent.py run --config … --log-file … from your own launchd plist. The agent is a single process, doesn’t fork, and exits cleanly on SIGINT/SIGTERM.

Token expiry

Agent tokens always expire. No token is valid for more than one year, and nothing overrides that. You pick the lifetime when a token is made (1 to 365 days, 90 days unless you choose otherwise). Under Token rules on the Resources page an account can set a lower maximum, the usual lifetime, and how long a replaced token keeps working.

When a token’s day comes the server disconnects the agent and refuses the token from then on (Token expired). So that this does not come as a surprise:

Token rotation

On the Tunnel Agents tab, New token replaces the token without downtime:

  1. Click New token: choose how long the new one is valid, and how long the old one keeps working (the grace period: 24 hours unless set otherwise, 0 to 7 days).
  2. The new token is shown once; the bundle you download afterwards has it in.
  3. Put it into the agent’s config.json and restart the agent. Until the grace period ends both tokens work; then the old one stops, and an agent still connected with it is disconnected.

Every replaced token keeps working until its own grace period ends, also when you make another new token in the meantime (up to 10 replaced tokens work at the same time; one more ends the oldest). A new token made with a grace period of 0 ends all earlier tokens at once, and only the new one works from then on: that is what to do when a token has leaked.

Rotate via the API

curl -X POST https://app.pycorner.dev/api/v1/data/tunnel-agents/<AGENT_ID>/regenerate-token/ \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"expires_in_days": 90, "grace_hours": 24}'
# → { ..., "token": "<new>", "expires_at": "...", "previous_token_valid_until": "..." }

Leave expires_in_days out for the account’s usual lifetime, and grace_hours for its usual grace period. More than the account’s maximum (365 days at most) is refused with a sentence that says so.

Self-rotation (the agent rotates its own token)

For unattended deployments, the running agent can rotate its own credential by presenting its current token — no dashboard login. Useful in a pre-expiry cron job that updates config.json and restarts the agent:

curl -X POST https://app.pycorner.dev/api/v1/data/tunnel-agents/rotate/ \
  -H "Authorization: Bearer $CURRENT_AGENT_TOKEN" \
  -H "Content-Type: application/json" -d '{}'
# → { ..., "token": "<new-secret>", "expires_at": "..." }

Without a body the new token keeps the date the old one expires on; send {"expires_in_days": 90} for a new lifetime (within the account’s maximum). The presented token keeps working for the grace period. An already-expired token can’t self-rotate — make a new one from the dashboard instead.

Security model

Diagnostics

The agent logs every TCP session open/close and every WebSocket reconnect. To see the agent’s view of an incoming request:

# In its terminal — you'll see:
[INFO] TCP session a8d06686 opened to 127.0.0.1:5433
[INFO] TCP session a8d06686 closed

On the server side, daphne logs every tunnel WS connect + handshake step plus a one-line summary when bytes start flowing. Tail with:

pixi run -e supervisor supervisorctl -c conf/supervisord.conf tail -f daphne stderr

Setting PYCORNER_DEBUG=1 in the agent’s environment turns on per-message logging.