Termix icon indicating copy to clipboard operation
Termix copied to clipboard

Feature: let a connect policy end the connections already open to a host

Open Li-Lee opened this issue 2 weeks ago • 3 comments

What problem would this solve?

A connect policy (ctx.hosts.connectPolicy) is asked only when a connection opens. When its answer for a host changes, the connections that are already open stay up:

  • Access that ran out. An approvals or just-in-time plugin denies a host once the grant has expired. New connections are refused. But the open terminal, the pooled background connections (Tmux Monitor, Host Metrics), file transfers and tunnels opened under the grant keep running. A pooled connection closes after 10 idle minutes, and a terminal stays up for as long as its tab is open.
  • A Mac put to sleep from Termix. A sleeping Mac wakes for TCP connections to the services it advertises over Bonjour, SSH on port 22 included. The macOS driver log gives the wake reason as mDNS TCP. Termix's own traffic undoes the sleep within 30 to 40 seconds: the ssh2 keepalive on the pooled connection, and Tmux Monitor polling while a Termix window is open. The status check's TCP probe to port 22 wakes it too. A plugin can deny new background connections to the Mac, but it can't close the open ones, and status checks never ask the connect policy. Linux hosts that only wake on a magic packet aren't affected: I tested one in S3, and ordinary traffic didn't wake it.

A plugin can't fix this itself. ctx.ssh.dropPooled only drops the calling plugin's own pool, which is right, so other plugins' connections and open terminals are out of its reach.

What would you like?

1. ctx.hosts.connectPolicy.disconnect(hostId, { reason })

It ends every connection core holds open to the host, for all users and plugins, and resolves to how many it ended.

  • It needs hosts:connect-policy. This adds no new capability: a plugin that can already refuse every new connection to a host can now also end the open ones.
  • Each call is audited, with the plugin, the host, the count and the reason.
  • A policy calls it right after its answer for the host changes: a grant ran out, or the host was put to sleep.

2. Optional probes: true on connectPolicy.provide(...)

A policy that opts in is also asked before status checks, with a new profile "probe". ctx.hosts.probeTargets then leaves out the hosts it denies, so status providers don't contact them. It's opt-in so that an existing policy which denies background connections never hides hosts from the status dots. It also keeps a policy that throws, which counts as a deny, from hiding every host.

How I'd build it

A small core registry of open connections, keyed by host id. It is fed at the three places core opens a connection to a host:

  • connectHost: core features, plugins' ctx.ssh.connect and the connection pool;
  • the socket the sandbox bridge opens for ctx.ssh.openTransport: plugins that run their own ssh2 client, such as SSH Terminal;
  • the SSH relay.

A connection adds itself once it is ready and removes itself when it closes. A per-host generation counter covers the race: a connection still in its handshake when disconnect runs ends itself as soon as it is ready, and fails with access-denied. Core never names a plugin. The registry costs one Map/Set entry and one close listener per connection, with no timers and no database writes.

Points I'd like your view on

  1. Ending live sessions is new. Is "audited, and only for plugins with hosts:connect-policy" enough? Or do you want the user to see a reason in the terminal (the reason could be shown there)?
  2. Remote desktop sessions (guacd) aren't covered. Should they be in the first version or a follow-up?
  3. Probe opt-in. Is probes: true on the existing provider the right fit, or would you rather have a separate hook?

Alternatives

  • ctx.ssh.dropPooled: reaches only the calling plugin's own pool.
  • Waiting for the pool's idle close: that takes 10 minutes, open terminals stay up, and on a Mac the keepalives in the meantime keep waking it.
  • Each plugin closing what it opened: the policy plugin doesn't know which plugins hold connections, and a terminal belongs to the user, not to a plugin.

Anything else?

I built a prototype against dev-26.11.2 (at 1368e650) to see what it would cost:

Part Lines
Core (8 files) +222 / −4
SDK types and createMockCtx (it records disconnects) +48
CHANGELOGs and PLUGIN-GUIDE +9
Tests (20 new) +349
  • type-check, lint and format:check pass.
  • The full test suite has no new failures.

If the direction works for you, I'm happy to open a PR against the current dev branch. I'll wait for your answers on the three points first.

I tested the Mac behaviour with Termix 26.11.0 against a Mac Studio on macOS 27.2.

Li-Lee avatar Nov 17 '26 22:11 Li-Lee

Auto-triage

Add a method to the connect policy API to disconnect all open connections to a host when access is revoked, and an opt-in hook to prevent status probes from waking sleeping hosts.

Field Value
Type Feature request
Difficulty 3/5 (Moderate)
Priority Low

A maintainer will take a look. If anything above looks wrong, just say so.

termix-triage-bot[bot] avatar Nov 17 '26 22:11 termix-triage-bot[bot]

Built for Termix 26.11.3 (SDK 2.11, not released yet), the way you described:

  • ctx.hosts.connectPolicy.disconnect?.(hostId, { reason }) needs hosts:connect-policy, ends every connection core holds to the host for all users and plugins, resolves to the count, and is audited with the plugin, host, count and reason.
  • A small registry in hosts/open-connections.ts is fed by connectHost (ctx.ssh.connect and the pool), the openTransport socket for plugins with their own client (SSH Terminal), and the SSH relay. A per host generation ends a connection still in its handshake once it is ready.
  • provide({ check, probes: true }) is also asked before status checks with profile "probe", and probeTargets drops the hosts it denies. Opt in only, so other policies never hide status dots.

On your points: audited and gated by the capability is enough for now, no reason in the terminal yet; remote desktop sessions are a follow up; and probes: true on the provider. createMockCtx records disconnects. Thanks for the prototype numbers, they made this quick.

LukeGus avatar Nov 20 '26 05:11 LukeGus

This shipped in v26.11.3. Update Termix to the latest version to get it. Thanks for the report!

termix-triage-bot[bot] avatar Nov 21 '26 13:11 termix-triage-bot[bot]