Skip to main content

Tunnels

A tunnel gives a technician a port on their own computer that leads to an address inside a customer's network: a remote desktop server, a database, the web page of a router. Nothing is opened in the customer's firewall and no VPN is involved. The agent on one of the customer's hosts connects outwards, and so does the technician's client; the gateway joins the two.

A tunnel carries any TCP protocol and does not look inside it. RDP, VNC, SSH, HTTP and database protocols all work with their ordinary clients, pointed at the local port.

There are two routes. A tunnel through another agent reaches an address in the customer's network and is bounded by that agent's tunnel.allow. A desktop session through a host's own agent reaches only the host's own loopback and needs the desktop action instead; see Tunnels for access entries.

How a tunnel opens​

  1. The technician asks the dashboard, from the command line or the Remote Connection Manager, for a tunnel to a named target or to a port of a host.
  2. The dashboard checks that the account may (see What is checked), asks the gateway to sign a grant for exactly that host, address and port, and tells the agent over its live link to open it. The technician's client gets a token for the relay that is good for two minutes.
  3. The agent verifies the grant against the job key pinned in its configuration file, checks the address against its own tunnel.allow, connects to it, and connects to the relay with the grant.
  4. The client connects to the relay with its token. The gateway pairs the two ends and copies bytes both ways.

Every connection made through the local port is a tunnel of its own, with its own grant and token. A remote desktop client that reconnects, or a browser that opens a dozen connections to a web interface, therefore opens many tunnels in turn; they all appear on the Tunnels page and in the audit log. For a stored entry they are grouped as one launch, which is asked about once; see Tunnels for access entries.

Setting it up​

A tunnel needs four things, and each is somebody's decision:

WhereWhatWho decides
The instanceThe gateway runs, GATEWAY_PUBLIC_URL is set and the relay paths are reachable from outside. See Gateway.The operator of the instance
The host's config.yamlactions contains tunnel, job_key is set, and tunnel.allow lists the networks a tunnel through this host may reach.Whoever installs the agent
The accountThe access.tunnel permission at the customer. The Technician template carries it. See Users and Permissions.An administrator
The customerNamed targets, and if wanted the policies that ask for a reason or an approval.A manager

A host that does not have tunnel in actions, or has it with an empty tunnel.allow, is shown as not allowing tunnels: the agent switches the action off and says so in its log, and the dashboard refuses before anything is signed.

# /etc/selvara-agent/config.yaml
actions: [tunnel]
tunnel:
allow:
- 192.168.1.0/24 # the customer's office network
- 10.20.0.5/32 # one server in another network

tunnel.allow is read only from this file. The dashboard cannot widen it, which is the point: a dashboard that is taken over can ask for a tunnel anywhere, and the host still refuses every address outside its list. Host names in a target are resolved by the agent, and every address a name resolves to must lie inside the list, so a name that also points outside is refused whatever order the resolver answers in.

Named targets​

A target is a destination with a name, so that technicians choose Router web interface at Acme instead of remembering an address. Managers keep them under Access → Tunnels → Targets.

FieldMeaning
CustomerThe customer the target belongs to. Who may open it follows from the permission at this customer.
AgentThe host of this customer the tunnel runs through. The page marks agents that are offline or do not allow tunnels.
NameWhat technicians choose from. Unique within the customer, ignoring case.
Host and portWhere the agent connects, as the agent sees the network: a name or an IP address, without scheme or port.
ProtocolA hint (SSH, RDP, VNC, HTTP, HTTPS or other) for the interface; it does not change how the tunnel behaves.
DescriptionFree text shown beside the name.

The list is live: a target created on one screen appears on every other, and in the command line and desktop app the next time they list targets. Deleting a target leaves tunnels that are already open running until they end. Deleting the agent's host deletes its targets.

A target is fixed to its port. Asking for a different one is refused.

The host itself​

Without a named target, a tunnel can only lead to the host the agent runs on, at a port you name:

selvara tunnel acme/web-01:3389 -L 13389

This is what selvara proxy uses for SSH without a VPN. The agent connects to 127.0.0.1 on that host, so its tunnel.allow must contain 127.0.0.1/32 (or 127.0.0.0/8) for these to work. Anything else in the customer's network has to be a named target, so that nobody can aim a tunnel at an address that no manager chose.

Using a tunnel​

selvara tunnel acme/office-rdp -L 13389 # prints: Forwarding 127.0.0.1:13389 -> acme/office-rdp

Leave the command running, point the client at the local port and press Ctrl+C when you are done. The desktop app does the same from its Tunnels page and shows the address to copy.

ProtocolClientAddress to give it
RDPRemote Desktop Connection, mstsc /v:127.0.0.1:13389; Remmina, xfreerdp /v:127.0.0.1:13389127.0.0.1:13389
VNCAny VNC viewer127.0.0.1:5900, or the display form 127.0.0.1::5900 that some viewers want for a port
SSHssh -p 2222 user@127.0.0.1127.0.0.1:2222
Web interfaceA browserhttp://127.0.0.1:8080
Databasepsql -h 127.0.0.1 -p 5432127.0.0.1:5432

The local port listens on the loopback interface only, so nobody else on the network can use it; -L 0.0.0.0:13389 changes that and should be done on purpose.

Some clients check the name they connect to. A web interface with a certificate for router.office.local warns when it is opened as 127.0.0.1; RDP may ask to confirm the certificate of the server. That is the client noticing it is not talking to the name in the certificate, and it is expected: the tunnel does not rewrite anything. Where a browser insists on the real name, add that name to your hosts file pointing at 127.0.0.1.

Tunnels for access entries​

An access entry of a host that is not reached directly opens through an agent: the dashboard decides which route from the host's paths and the entry's protocol (see Opening a host), and the desktop app and the browser both ask for it with the entry's id. There are two kinds of launch, and the agent treats them differently:

KindWhenWhich agentWhat the agent checks
tunnelthe path is the tunnel host path: the host's tunnel switch is on, with a tunnel hostthe tunnel hostits tunnel action, and its own tunnel.allow against the entry's address and port
desktopthe path is the agent path: the host has an agentthe host's own agentits desktop action only: it connects to its own loopback on the granted port

The launch names its path in the body as path: agent or tunnel. Without it the server uses the path older apps used: the tunnel host path when the switch is on, else the agent path when the host has an agent. The direct path needs no launch: the desktop app opens its address itself, and the server answers 409 tunnel_not_needed if it is asked. A path that does not exist is refused with no_agent or tunnel_off.

For a tunnel launch the destination is the one saved in the entry: the entry's address and port (22, 3389 or 5900 by default) for SSH, RDP, VNC and VS Code over SSH, and the host and port of the URL for a web entry (80 and 443 by default; an IPv6 literal works). Entries of a web link host, local VS Code and WSL entries, and entries of a host without a service contract cannot be tunnelled. A host name the agent would refuse (an underscore, brackets, host:port) is refused with invalid_tunnel_host. SMB is not offered through a tunnel, and a web page through a tunnel is best effort: a page that insists on its own host name may not work. A web entry of a host that is reached through an agent opens the same way, with its own launch.

For a desktop launch the address is only a label (the name SSH keeps its host key under, and the title of an .rdp file): the gateway signs a grant with the loopback address and the port the dashboard names, and the agent dials only its own 127.0.0.1 (then ::1) on that port. The protocols are rdp, vnc, ssh and web. The agent refuses a port it keeps for itself (port not allowed), and nothing about the grant reads tunnel.allow. What that shifts is described under The desktop action.

Nothing is looser than for any other tunnel in the rest: the account needs access.tunnel at the customer, the agent must be online, enrolled and allow the action, and a tunnel carrier's own tunnel.allow must contain the network. The customer's policies (a reason, approval) apply to every launch opened this way, of either kind. They do not apply to an entry opened directly or to copying a password, because the server is not in the path.

Tunnel hosts. Only an agent that is marked as a tunnel host can be chosen as a host's tunnel host, and the server refuses any other with 422 invalid_tunnel_system; a launch whose tunnel host has lost the mark or the action is refused with tunnel_host_required or tunnel_not_allowed. Mark an agent on its page (Configuration, Tunnel host) or in bulk through the hosts API; a tunnel host that other hosts still reach through cannot be unmarked (409 tunnel_host_in_use, with the number of hosts and entries). A customer without a tunnel host offers no tunnel host choice on the Access tab, only the hint where to mark one. Named targets and tunnels opened by hand are not affected: they go through whichever agent they name.

One launch, one approval. Starting an RDP session or loading a web page opens many TCP connections, and asking about each would make it unusable. The first socket opens the launch: it asks for the reason and approval, writes the tunnel.open audit entry (which names the entry) and gets the first relay token. Each later socket names that first tunnel and gets a token of its own, valid for two minutes and one connection as always, with no further question and no further entry. The approval is still checked for each: if it was withdrawn or ran out, the launch is over and the next socket starts a new one, which asks again.

  • At most 64 sockets after the first per launch, 20 open at once per person, and 5 launches with a socket open per person (429 too_many_sockets_in_launch, too_many_sockets, too_many_launches).
  • A launch lasts while any of its sockets is open, and 15 minutes after the last one closed; it ends in any case after 8 hours, or sooner with the approval's or policy's window, or when a person terminates it or loses the right.
  • The launch is closed by one tunnel.close entry with the number of sockets (and how many failed to open), written when the launch is over rather than when a socket closes.
  • The Tunnels page shows a launch as one row, named after the host and protocol of the entry; Terminate ends it with all its sockets.

Waiting for approval. When the customer requires approval, the first call answers 202 with status: "pending_approval" and sessionId, which is the access request's id. The client polls GET /api/access-entries/{id}/tunnel/{sessionId} until status is approved (then it calls again), or denied, cancelled, expired or ended; DELETE on the same address withdraws the request. The approver of a decided request is named in approver.

From the command line, the same with selvara tunnel --entry ID (see Command Line); the desktop app starts it for an entry of a host that is reached through an agent.

Opening in the browser​

An RDP or VNC entry of a host that is reached through an agent (and SSH, when it goes through a tunnel host) can also be opened in the browser, with no client on your side: in browser next to in desktop app in the Connect menu. It opens the same kind of launch, with the same reason, approval and tunnel.open entry, but the other end of the relay is the dashboard's guacd rather than a helper on your computer. See Remote Desktop.

Opening from the desktop app​

An entry of a host that is reached through an agent is launched like any other, with one step in front: the app starts the selvara helper that is installed next to it, waits until the launch is open, and points the client at the helper's port on 127.0.0.1. The real address never reaches the client program.

  • Before the client starts. The helper opens the launch first, so the reason the customer asks for (the app asks for it in a dialog) and the approval (Waiting for somebody to approve the access request..., with Cancel, which withdraws the request) are settled before RDP or the browser has a connection that could time out. A launch that is already open is reused without a dialog.

  • One port per entry. The port comes from the entry's id, between 20000 and 45000, and is the same at every launch, so the certificate decision RDP remembers and a browser's cookies and origin stay with the entry. Entries that were connections before 3.4.0 keep their ids, so the decision they remembered stays valid. If something else holds the port the helper uses a free one, and the launch follows it.

  • What the client is given.

    TypeLaunched as
    SSHssh -p <port> -o HostKeyAlias=<real host> -o CheckHostIP=no user@127.0.0.1. The agent reaches the target from its own network; for the host's own agent the real address is only the HostKeyAlias.
    RDPan .rdp file for 127.0.0.1:<port>
    VNC127.0.0.1:<port> (RealVNC) or vnc://user@127.0.0.1:<port> (Remmina)
    Webthe address with host and port replaced by 127.0.0.1:<port>; best effort, see above. A web entry takes its own launch (/api/access-entries/{id}/tunnel)
    VS Code over SSHssh-remote+user@<alias>-tunnel, with a block of its own for that alias in the managed ssh config file: HostName 127.0.0.1, the port, HostKeyAlias and CheckHostIP no

    A target on port 22 keeps the bare host name as HostKeyAlias, any other port the [host]:port form that ssh itself writes, so a tunnelled session is checked against the same known_hosts entry as a direct one. How strictly that is checked stays your own StrictHostKeyChecking; the app never lowers it. A real key change is shown as ssh reports it.

  • Certificate warnings are expected. mstsc and browsers see a certificate for the real host on 127.0.0.1. The warning appears once per connection because the port stays the same.

  • How long it stays open. One helper runs per connection. It ends after 15 minutes in which no socket was open, when the app quits, when the app locks or the account signs out, and when the app dies without a goodbye (the helper notices its standard input closing). A dropped connection to the dashboard does not end it.

  • Version. The app and the helper come from the same build. Before a tunnel the app asks the helper for selvara version --json and refuses a helper whose major version differs from its own or that is older than the app needs, naming the version it found. The helper is looked for next to the app (selvara or selvara.exe, also in a bin folder there or a macOS bundle's Resources); SELVARA_CLI names another path. A helper that was replaced by hand is the usual cause of a version error.

  • When something goes wrong the launch snackbar says why in the app's language: no helper found, wrong helper version, a refusal of the dashboard (for example that the agent is offline or lacks the tunnel action) or a failure of the helper itself. A host outside the agent's tunnel.allow list shows up only when the client connects, because the agent decides it then; the app reports it as outside tunnel.allow. The helper is unsigned like the rest of the app: if an endpoint product quarantines selvara.exe, allow it by its path next to the app.

Limits​

  • Idle: a tunnel nothing has moved through for 15 minutes ends.
  • Length: a tunnel ends after 8 hours, whatever is moving, or when the approval or access policy that let it open ends, if that is sooner. A tunnel also ends when the person loses the tunnel permission at the customer, is removed, or withdraws the approval.
  • Waiting: a side whose counterpart does not arrive within 60 seconds is closed. The client token is valid for two minutes.
  • Once: neither a grant nor a token opens a second connection.
  • Dashboard restart: a tunnel's bytes go through the gateway, so a hard restart of the app does not cut one; but the app learns that a tunnel ended only from the agent. When a host links again after the app was down, tunnels still open on its record for that host are closed as Agent connection lost, and a tunnel past its time limit is closed as Reached its time limit by a sweep that runs every minute, whatever the agent managed to report.
  • Gateway restart: the technician's connection is cut with it. The agent tells it from an ordinary close (the technician ended it, or the target hung up) and records Gateway connection lost; a new connection through the same selvara proxy listener works as soon as the gateway is back.
  • Link: a tunnel cannot outlive the agent's live link. When the link drops the agent closes its tunnels, and the page shows them as ended with Agent connection lost.

What is checked​

Before anything is signed, the dashboard checks, in this order, and the first failure answers:

  1. The target or host exists and belongs to a customer the account reaches. Otherwise it is answered as not found, the same as for one that does not exist.
  2. The account holds access.tunnel at that customer. A refusal is recorded in the audit log as tunnel.open with the outcome denied.
  3. The host is not waiting for approval after enrolling with a token that requires it.
  4. The agent's link is up and its actions include tunnel (or desktop, for a launch through the host's own agent, which also needs an agent that is new enough to know it).
  5. The customer's policies, below.

The gateway then applies its own limits (an eight-hour ceiling and a rate per host), and the agent checks the grant and tunnel.allow once more. The audit record is written before the agent is told, and if it cannot be written no tunnel opens.

Reason​

When the customer's Require a reason policy is on, a tunnel needs a reason of at least three characters, kept with the audit entry and shown in the list of open tunnels:

selvara tunnel acme/office-rdp --reason "Ticket 4711: replace the switch"
SELVARA_REASON="Ticket 4711" ssh acme/web-01 # for proxy, which ssh starts

The desktop app has a field for it on each target.

Approval​

When Require approval is on, the first attempt to open a tunnel to a host does not open one. It files an access request, and the command fails with the message that a request is waiting. Someone else who may open shells or tunnels at the customer approves or denies it under Access → Requests; nobody decides their own request. Once approved, the requester may open tunnels to any destination behind that host, as many as they need, until the approval ends (an hour unless an access policy sets another limit). The same approval covers shells and SSH certificates on the host. A request nobody answers lapses after a day.

Recording​

Record sessions does not record the content of a tunnel: the dashboard does not see inside a protocol it cannot read. What is kept is who opened which tunnel, when and why, how it ended, and how many bytes moved each way (in the gateway's own log, see Gateway).

Open and ended tunnels​

Access → Tunnels → Open tunnels lists every tunnel open now: where it leads, who opened it, when it started and when it ends, and the reason given. A manager can Terminate one; so can the person who opened it, through the API. The agent is told to close its ends and the relay closes the client's. Below, the tunnels that ended in the last day show how they ended: closed, idle, at their time limit, terminated, connection lost, or could not be opened with what the agent said, such as the address being outside its tunnel.allow.

The page, like the rest of the interface, is live. A tunnel reaching its time limit leaves the list with no one doing anything.

Audit log​

ActionWhen
tunnel.openA tunnel or a desktop session was opened, refused for lack of permission, or failed at the gateway. For an entry it names the entry and its host and carries the kind (tunnel or desktop), the protocol and how the host was reached; for a web address, the web address too.
tunnel.closeThe agent reported the end of a tunnel, or its link dropped. For a launch of an access entry, one entry when the whole launch is over, with its number of sockets.
tunnel.terminateSomeone terminated a tunnel.
tunnel_target.create, .update, .deleteA target changed.
system.tunnel_host_changeAn agent was marked as a tunnel host, or the mark was removed.
access_request.create, .decideA request was filed or decided, including a refused attempt to decide one's own.

See Audit Log. The gateway keeps its own record of every grant it signs and every relayed session.

API​

The command line and the desktop app use these; they take the same API tokens as the rest of the API and answer in the usual { success, data } envelope.

RequestMeaning
GET /api/access/targetsThe named targets at customers where the account holds access.tunnel: id, name, description, customer, systemId, host, port, protocol.
POST /api/access/tunnelsBody { "targetId" } or { "systemId", "port" }, and "reason" where one is required. Answers { sessionId, relayUrl, token }. The relay URL may be a path, to resolve against the dashboard.
DELETE /api/access/tunnels/{id}Terminates a tunnel; for the first tunnel of an entry's launch, the whole launch.
POST /api/access-entries/{id}/tunnelOpens a launch for an access entry, over a tunnel host or the host's own agent. Body { "reason", "parentSessionId", "path" }, all optional (path is agent or tunnel). 201 with { status: "open", sessionId, relayUrl, token, host, port, protocol, kind, via } (kind is tunnel or desktop; for desktop, host and port are the entry's own and nothing dials them), or 202 with { status: "pending_approval", sessionId, requestId }. Refusals are codes: no_rights (403), offline, waiting_for_enrollment, desktop_not_allowed, agent_outdated, tunnel_not_allowed, no_agent, tunnel_off, tunnel_host_required, tunnel_not_needed (the path is the direct one), invalid_tunnel_host (422, with details.reason), reason_required, parent_expired, approval_required, the three too_many_ limits above, 423 locked. A script's token gets 403 token_kind_not_allowed.
GET, DELETE /api/access-entries/{id}/tunnel/{sessionId}Where a waiting request or a launch stands; withdraws the request or ends the launch.
POST /api/web-addresses/{id}/tunnel, GET, DELETE …/tunnel/{sessionId}The route older clients use for a web address. Web addresses are web entries now and keep their ids, so it answers like the entry's route above.
POST /api/access/targets, PATCH and DELETE /api/access/targets/{id}Manage targets (managers).

Refusals carry a status that says whose doing it is: 400 for a request that cannot be satisfied (a missing port or reason), 403 for a missing permission or a request waiting for approval, 404 for an unknown target, 409 for an agent that is offline or does not allow tunnels, 429 when the gateway limits the host, 502 or 503 when the gateway cannot be reached or is not configured.

Troubleshooting​

  • "The agent on this host is not connected": the host's live link is down. The host's page shows since when.
  • "This host does not allow tunnels": the host's config.yaml lacks tunnel in actions, or has no tunnel.allow, and the agent was restarted after the change.
  • The tunnel opens and nothing arrives, and it ends a minute later: look at Ended in the last day. Could not be opened with outside tunnel.allow means the address is not in the host's list; with cannot reach the host cannot connect to the address. The dashboard does not know the host's list, so it cannot refuse this earlier.
  • selvara proxy fails for SSH: the host's tunnel.allow needs 127.0.0.1/32 for tunnels to the host itself.
  • The gateway is not configured: set GATEWAY_URL, GATEWAY_TOKEN and GATEWAY_PUBLIC_URL; see Configuration.
  • A browser says the connection is not private: see Using a tunnel.