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
- On-prem database reach — your DB stays on your network; the agent dials it from inside.
- Fixed egress IP — outbound API calls go out from your network, not PyCorner’s. Cleaner upstream allowlists.
- Reach to internal APIs — what you list as reachable from the agent’s network is callable from your apps.
- Links — join two agents (your laptop and the office) over TCP or UDP, or open a SOCKS5/HTTP proxy port to a whole private network.
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:
pycorner_agent.py— the agent script.config.json— your token + the server URL, pre-filled.pixi.toml— declares the agent’s deps (websockets,cryptography).requirements.txt— pip alternative.README.md.
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 | |
|---|---|
| Host | db.internal · *.corp.internal (every name below that domain) · 10.0.0.5 · 10.0.0.0/24 · * (any host) |
| Port | 5432 · 8000-8100 · * (any port) |
| Protocol | TCP (databases, SSH, web) or UDP (DNS, syslog, SNMP) |
The rules:
- Nothing is reachable until something is listed. For every agent made since these lists exist, the list is the rule and an empty list reaches nothing.
- Two lists, and a destination has to be on both. The agent also has an optional
allowlist in its ownconfig.json. The agent enforces the intersection: the local file can only narrow what is listed here, never widen it. An agent without a localallowfollows the central list alone. - The server checks too. PyCorner refuses anything outside the list before it even asks the agent, so an agent that was modified to ignore the list is not asked to reach more.
- Agents from before keep working as they did (only their own
allowdecides) until you tick “make the list the rule” in the editor. That cannot be switched off again; to let the agent’s ownconfig.jsondecide alone, list host*, port*. - A name that is only covered by a network (you listed
10.0.0.0/24, a resource asks fordb.internal) is resolved by the agent, which connects only to an address inside the network. The server cannot know private names, so list names by name where it matters.
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).
Links
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.
TCP port — one service:
psql -h localhost -p 15432on your laptop reachesdb.internal:5432in the office.UDP port — DNS, syslog, SNMP:
dig @127.0.0.1 -p 15353 intranet.corp. Each sender is one flow, forgotten after 60 seconds of silence (configurable, 5 seconds to an hour). Datagrams keep their boundaries; those above 16 KiB are dropped. The target has to be listed as UDP. Agents from before UDP cannot have UDP links: update them.Proxy — a whole network through one port. The port speaks SOCKS5 and HTTP CONNECT; every connection names the host it wants, and the far agent connects there if its reachable destinations allow it. Names are resolved at the far end:
export ALL_PROXY=socks5h://localhost:11080 # curl, git, pip, most tools (note the h) export HTTPS_PROXY=http://localhost:11080 # tools that only know HTTP proxies curl http://wiki.corp.internal/What does not go through a proxy link: UDP (make a UDP link),
HTTP_PROXY=http://…for plainhttp://URLs (the proxy carries CONNECT only;ALL_PROXY=socks5h://…covers them), programs that ignore proxy settings (wrap them withproxychains, or make a plain link), inbound connections, SOCKS4. A destination that is not allowed is answered “not allowed” (HTTP 403), one that is down “host unreachable” (502). A proxy link needs the far agent’s reachable destinations to be listed.
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:
tcp_proxy— open raw TCP sockets to a target host:port (used by Tunnel resources and DB resources withagent_id).http_egress— make outbound HTTP requests (used by API resources withagent_id).forwards— listen for links.reach— take the list of reachable destinations live. (The server enforces the list for older agents too.)notices— log what the server tells it: the token expires soon, was replaced, has expired.udp,proxy— carry UDP, listen for proxy links.
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
- Linux, system-wide: a systemd unit running as the system user
pycorner-agent, the script in/opt/pycorner-agent/, the config in/etc/pycorner-agent/config.jsonreadable by that user alone (mode 600), the log in the journal (journalctl -u pycorner-agent -f). - Linux,
--user: the same as a unit of your own systemd, config in~/.config/pycorner-agent/config.json(mode 600). - Windows: a scheduled task (
PyCornerAgent, made withschtasks, nothing beyond Python needed) that starts at boot and runs as the built-in unprivileged accountLOCAL SERVICE; files and a rotating log in%ProgramData%\PyCornerAgent. This path has not been run on a Windows machine by us yet: check the first install withstatusand the log.
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:
- every list shows the expiry date, in amber from 14 days and red from 3 days before, and the Resources page shows a notice;
- the owner gets one e-mail 14 days and one 3 days before;
- the agent logs it at start and when the server tells it.
Token rotation
On the Tunnel Agents tab, New token replaces the token without downtime:
- 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).
- The new token is shown once; the bundle you download afterwards has it in.
- Put it into the agent’s
config.jsonand 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
- The agent is outbound-only. Your network’s inbound firewall stays closed; no port forwards, no public IP required.
- The agent holds a token, not your customer’s data. Compromising the token gets an attacker the ability to be that agent — they can serve requests for resources scoped to it, but they can’t read existing data unless the underlying upstream service grants them access.
- The agent does not persist anything to disk by default.
config.jsonis the only state file and it only contains config you’ve provided. It holds the token: keep it readable by the agent’s user alone (chmod 600; the agent warns otherwise). - What the agent may reach is the central list and, when you set one, the
allowlist of its ownconfig.json: both have to agree. - Encryption: RSA-2048 key exchange per connection, AES-256-GCM for message payloads, on top of WSS (TLS). It ends at the PyCorner server, which relays the bytes: use TLS or SSH inside the tunnel when the server should not read them.
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.