Files
Termix/plugins/ssh-terminal/src/backend/terminal-socket.ts
T
3643e7af97 release-2.9.0 (#1532)
* fix(macos): skip single-instance lock in Mac App Store builds (#1454)

fix(macos): skip single-instance lock in Mac App Store builds

* Fix/backend optional sharp (#1455)

Fix/backend optional sharp

* feat: issue #1218 (#1218)

https://github.com/Termix-SSH/Support/issues/1218

* feat: Add option to use saved global custom theme under Connection Defaults (#1209)

https://github.com/Termix-SSH/Support/issues/1209

* feat: Add Fleet Sharing functionality to the UI (#1228)

https://github.com/Termix-SSH/Support/issues/1228

* feat: Add a list view to the Docker management page (#1237)

https://github.com/Termix-SSH/Support/issues/1237

* feat: issue #1264 (#1264)

https://github.com/Termix-SSH/Support/issues/1264

* feat: 2FA With FortiToken (#1288)

https://github.com/Termix-SSH/Support/issues/1288

* feat: Allow to disable showing paths to folders (#1274)

https://github.com/Termix-SSH/Support/issues/1274

* feat: Sync Network Graph between Desktop and Remote Server (#1245)

https://github.com/Termix-SSH/Support/issues/1245

* feat: Version Number on offline servers (#1291)

https://github.com/Termix-SSH/Support/issues/1291

* feat: Support OrbStack Docker socket path on macOS / Fix "Docker is not installed" on mac... (#1302)

https://github.com/Termix-SSH/Support/issues/1302

* feat: Sidebar host click should focus existing tab instead of opening a new connection (#1289)

https://github.com/Termix-SSH/Support/issues/1289

* fix: Search in side bar shows overlapping hosts when grouped by tags (#1303)

https://github.com/Termix-SSH/Support/issues/1303

* fix: I can't send a file on its own. (#1304)

https://github.com/Termix-SSH/Support/issues/1304

* fix: sudo password not autofilling (#1248)

https://github.com/Termix-SSH/Support/issues/1248

* fix: node-MainThread (#1300)

https://github.com/Termix-SSH/Support/issues/1300

* fix: Tunnel authentication with shared credentials (#1295)

https://github.com/Termix-SSH/Support/issues/1295

* fix: host key updates not syncing, metrics 404 race on first connect

Host key writes never bumped updatedAt so sync never picked them up.
Also retry the first metrics fetch briefly instead of failing right away.

* chore: increment ver

* feat: add category and icon fields to plugin manifest schema

* ci(deps): bump the github-actions group with 2 updates (#1458)

Bumps the github-actions group with 2 updates: [crowdin/github-action](https://github.com/crowdin/github-action) and [actions/github-script](https://github.com/actions/github-script).


Updates `crowdin/github-action` from 2 to 3
- [Release notes](https://github.com/crowdin/github-action/releases)
- [Commits](https://github.com/crowdin/github-action/compare/v2...v3)

Updates `actions/github-script` from 7 to 9
- [Release notes](https://github.com/actions/github-script/releases)
- [Commits](https://github.com/actions/github-script/compare/v7...v9)

---
updated-dependencies:
- dependency-name: crowdin/github-action
  dependency-version: '3'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: actions/github-script
  dependency-version: '9'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps-dev): bump the dev-patch-updates group with 13 updates (#1459)

Bumps the dev-patch-updates group with 13 updates:

| Package | From | To |
| --- | --- | --- |
| [@codemirror/commands](https://github.com/codemirror/commands) | `6.11.0` | `6.11.1` |
| [@codemirror/search](https://github.com/codemirror/search) | `6.7.1` | `6.7.2` |
| [@codemirror/view](https://github.com/codemirror/view) | `6.43.9` | `6.43.12` |
| [@testing-library/dom](https://github.com/testing-library/dom-testing-library) | `10.4.1` | `10.4.2` |
| [@testing-library/react](https://github.com/testing-library/react-testing-library) | `16.3.2` | `16.3.3` |
| [@testing-library/user-event](https://github.com/testing-library/user-event) | `14.6.5` | `14.6.7` |
| [@types/ssh2](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/ssh2) | `1.15.5` | `1.15.6` |
| [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/tree/HEAD/packages/plugin-react) | `6.1.0` | `6.1.1` |
| [cytoscape](https://github.com/cytoscape/cytoscape.js) | `3.34.1` | `3.34.3` |
| [eslint-plugin-react-refresh](https://github.com/ArnaudBarre/eslint-plugin-react-refresh) | `0.5.4` | `0.5.7` |
| [i18next](https://github.com/i18next/i18next) | `26.4.0` | `26.4.2` |
| [prettier](https://github.com/prettier/prettier) | `3.9.6` | `3.9.8` |
| [react-i18next](https://github.com/i18next/react-i18next) | `17.0.12` | `17.0.14` |


Updates `@codemirror/commands` from 6.11.0 to 6.11.1
- [Changelog](https://github.com/codemirror/commands/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codemirror/commands/commits)

Updates `@codemirror/search` from 6.7.1 to 6.7.2
- [Changelog](https://github.com/codemirror/search/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codemirror/search/commits)

Updates `@codemirror/view` from 6.43.9 to 6.43.12
- [Changelog](https://github.com/codemirror/view/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codemirror/view/commits)

Updates `@testing-library/dom` from 10.4.1 to 10.4.2
- [Release notes](https://github.com/testing-library/dom-testing-library/releases)
- [Changelog](https://github.com/testing-library/dom-testing-library/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/dom-testing-library/compare/v10.4.1...v10.4.2)

Updates `@testing-library/react` from 16.3.2 to 16.3.3
- [Release notes](https://github.com/testing-library/react-testing-library/releases)
- [Changelog](https://github.com/testing-library/react-testing-library/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/react-testing-library/compare/v16.3.2...v16.3.3)

Updates `@testing-library/user-event` from 14.6.5 to 14.6.7
- [Release notes](https://github.com/testing-library/user-event/releases)
- [Changelog](https://github.com/testing-library/user-event/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/user-event/compare/v14.6.5...v14.6.7)

Updates `@types/ssh2` from 1.15.5 to 1.15.6
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/ssh2)

Updates `@vitejs/plugin-react` from 6.1.0 to 6.1.1
- [Release notes](https://github.com/vitejs/vite-plugin-react/releases)
- [Changelog](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite-plugin-react/commits/plugin-react@6.1.1/packages/plugin-react)

Updates `cytoscape` from 3.34.1 to 3.34.3
- [Release notes](https://github.com/cytoscape/cytoscape.js/releases)
- [Commits](https://github.com/cytoscape/cytoscape.js/compare/v3.34.1...v3.34.3)

Updates `eslint-plugin-react-refresh` from 0.5.4 to 0.5.7
- [Release notes](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/releases)
- [Changelog](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/blob/main/CHANGELOG.md)
- [Commits](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/compare/v0.5.4...v0.5.7)

Updates `i18next` from 26.4.0 to 26.4.2
- [Release notes](https://github.com/i18next/i18next/releases)
- [Changelog](https://github.com/i18next/i18next/blob/master/CHANGELOG.md)
- [Commits](https://github.com/i18next/i18next/compare/v26.4.0...v26.4.2)

Updates `prettier` from 3.9.6 to 3.9.8
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/3.9.6...3.9.8)

Updates `react-i18next` from 17.0.12 to 17.0.14
- [Changelog](https://github.com/i18next/react-i18next/blob/master/CHANGELOG.md)
- [Commits](https://github.com/i18next/react-i18next/compare/v17.0.12...v17.0.14)

---
updated-dependencies:
- dependency-name: "@codemirror/commands"
  dependency-version: 6.11.1
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@codemirror/search"
  dependency-version: 6.7.2
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@codemirror/view"
  dependency-version: 6.43.12
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@testing-library/dom"
  dependency-version: 10.4.2
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@testing-library/react"
  dependency-version: 16.3.3
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@testing-library/user-event"
  dependency-version: 14.6.7
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@types/ssh2"
  dependency-version: 1.15.6
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: "@vitejs/plugin-react"
  dependency-version: 6.1.1
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: cytoscape
  dependency-version: 3.34.3
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: eslint-plugin-react-refresh
  dependency-version: 0.5.7
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: i18next
  dependency-version: 26.4.2
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: prettier
  dependency-version: 3.9.8
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
- dependency-name: react-i18next
  dependency-version: 17.0.14
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dev-patch-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump the prod-patch-updates group with 6 updates (#1461)

Bumps the prod-patch-updates group with 6 updates:

| Package | From | To |
| --- | --- | --- |
| [@tanstack/react-virtual](https://github.com/TanStack/virtual/tree/HEAD/packages/react-virtual) | `3.14.10` | `3.14.13` |
| [compression](https://github.com/expressjs/compression) | `1.8.1` | `1.8.2` |
| [jose](https://github.com/panva/jose) | `6.2.9` | `6.2.12` |
| [jszip](https://github.com/Stuk/jszip) | `3.10.1` | `3.10.2` |
| [socks](https://github.com/JoshGlazebrook/socks) | `2.8.9` | `2.8.10` |
| [undici](https://github.com/nodejs/undici) | `8.10.0` | `8.10.2` |


Updates `@tanstack/react-virtual` from 3.14.10 to 3.14.13
- [Release notes](https://github.com/TanStack/virtual/releases)
- [Changelog](https://github.com/TanStack/virtual/blob/main/packages/react-virtual/CHANGELOG.md)
- [Commits](https://github.com/TanStack/virtual/commits/@tanstack/react-virtual@3.14.13/packages/react-virtual)

Updates `compression` from 1.8.1 to 1.8.2
- [Release notes](https://github.com/expressjs/compression/releases)
- [Changelog](https://github.com/expressjs/compression/blob/master/HISTORY.md)
- [Commits](https://github.com/expressjs/compression/compare/v1.8.1...v1.8.2)

Updates `jose` from 6.2.9 to 6.2.12
- [Release notes](https://github.com/panva/jose/releases)
- [Changelog](https://github.com/panva/jose/blob/main/CHANGELOG.md)
- [Commits](https://github.com/panva/jose/compare/v6.2.9...v6.2.12)

Updates `jszip` from 3.10.1 to 3.10.2
- [Changelog](https://github.com/Stuk/jszip/blob/main/CHANGES.md)
- [Commits](https://github.com/Stuk/jszip/compare/v3.10.1...v3.10.2)

Updates `socks` from 2.8.9 to 2.8.10
- [Release notes](https://github.com/JoshGlazebrook/socks/releases)
- [Commits](https://github.com/JoshGlazebrook/socks/compare/2.8.9...2.8.10)

Updates `undici` from 8.10.0 to 8.10.2
- [Release notes](https://github.com/nodejs/undici/releases)
- [Commits](https://github.com/nodejs/undici/compare/v8.10.0...v8.10.2)

---
updated-dependencies:
- dependency-name: "@tanstack/react-virtual"
  dependency-version: 3.14.13
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
- dependency-name: compression
  dependency-version: 1.8.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
- dependency-name: jose
  dependency-version: 6.2.12
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
- dependency-name: jszip
  dependency-version: 3.10.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
- dependency-name: socks
  dependency-version: 2.8.10
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
- dependency-name: undici
  dependency-version: 8.10.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: prod-patch-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: add plugin worker protocol, bootstrap and plugin_storage table

Worker-side ctx proxy plus the message envelope it talks over, and the
per-plugin key/value table behind ctx.storage with migrations for all
three dialects. Rows cascade with the plugin so uninstalling leaves
nothing behind.

* feat: add allowlisted host view for plugins

An allowlist rather than a list of fields to strip. A blocklist rots:
the moment a new secret-bearing column lands on SSHHost it starts
leaking silently. With an allowlist a new field stays invisible to
plugins until someone adds it here on purpose.

* feat: add plugin loader with manifest validation and crash restart

Validates an unpacked plugin, spawns a worker_threads worker and manages
activate/deactivate/crash-restart with backoff, disabling after three
tries.

The crash counter clears only after a plugin stays up past a stability
window, not on activation. Clearing it on activation let a plugin that
activates cleanly and then dies restart forever, because every attempt
looked like the first.

loader.ts documents what the worker boundary does and does not buy you.
Node's permission model is process-wide, not per-worker, so it cannot be
enabled for plugin workers alone and is not claimed here.

* feat: route internal events through a shared plugin event bus

Formalises three ad-hoc publish paths that already existed:
automation-events.ts, metrics/automation-bridge.ts and the
hostSessionStatus singleton, now the host.session.status topic.

Both invariants the old code relied on are preserved. Publishing stays
fire-and-forget, so a failing subscriber cannot disturb a metrics poll or
a host delete. The automations engine subscribes to the bus at scheduler
start instead of being imported ad hoc, which keeps the edge
one-directional: automations may import repositories, hosts modules must
not import automations.

* feat: add plugin ctx broker with permission gate and audit

Every ctx call a plugin makes is checked against its granted
capabilities, performed by the main thread, then audited. A capability
must be both declared in the manifest and granted in the database, so
widening a plugin's reach always needs a new manifest the user can see,
never just a database row.

Audit attribution is set by the broker, not the plugin. The plugin
supplies only the details, so it cannot forge the actor.

fix: stop plugin workers seeing plaintext credentials in error messages

A failing ctx.ssh.connect handed the worker the raw error from deep in
the SSH stack, which embeds the connect config. The worker received, in
full:

  Authentication failed for root using password <SECRET>

where <SECRET> was the host's real plaintext password. Any plugin with
ssh.exec could read it by catching the error, defeating the whole point
of the handle-based ssh API.

Errors crossing the boundary are now sanitised. Only PluginFacingError
and PluginPermissionError, both authored here, reach a plugin verbatim;
anything else becomes a generic message and the real one goes to the
log. This uses a marker class rather than matching on message text,
because matching would start leaking again the moment an upstream error
happened to contain a familiar phrase.

Covered by credential-isolation.test.ts, which plants a sentinel in
every secret-bearing field and asserts it appears in no message the
worker ever receives.

* feat: dispatch plugin HTTP routes and wire the runtime into startup

ctx.http.route now registers a real router on the /plugin-api/:pluginId
dispatcher, replacing its stub 404. A plugin never touches the Express
req/res: it gets a plain summary and returns a plain object, so sockets
and session cookies stay on this side of the boundary. Authorization and
cookie headers are not forwarded.

Plugins load last in the start-up sequence, after the server is fully
wired, and are terminated before the database on shutdown so none can
outlive it. A plugin that fails to load cannot stop the backend.

fix: correct the nginx plugin-api location regex

The pattern was ^/plugin-api/(/.*)?$, which needs a double slash and so
never matched /plugin-api/<id>/<route>. Every other block in the file
uses ^/prefix(/.*)?$. Left alone, plugin routes would work in dev and
404 only behind Docker.

* fix: update stale SFTP transfer test for single file destination paths

The test still asserted transferToHost was called with the destination
directory "/srv". Commit 7727fa09 (#1304) deliberately changed a single
file transfer to send the full destination path including the filename,
so the correct expectation is "/srv/remote-1.txt". The code was right
and the test was left behind.

* feat: ship the ssh terminal as a first-party in-process plugin

Adds a hardcoded process:transport-owner tier so the terminal can own its
WebSocket server and ssh2 clients, which cannot cross the worker boundary.

* feat: ship docker as a first-party in-process plugin

Moves docker backend and frontend into plugins/docker/, alongside
ssh-terminal. Adds a host editor tab seam, a dashboard card registry,
and a build step to compile plugin backends into dist.

* feat: ship host metrics as a first-party in-process plugin

* fix: harden plugin auth and add per-request identity for ctx calls

Require auth on /plugin-api, gate plugin enable/grant/revoke routes
behind admin.plugins.manage, and thread the calling user through
worker ctx.hosts/ctx.ssh calls instead of always using the install
owner. Also adds the plugin permissions grant/revoke UI.

* feat: ship the AI assistant as a first-party in-process plugin

Moves src/backend/ai and its routes into plugins/ai, mounted through a
small /ai dispatcher instead of its own port. Moves the ai-enabled and
ai-private-endpoints settings routes out of user-settings-routes.ts
into the plugin too, so it has no leftover core dependents.

* fix: remove compiled plugin JS left behind in plugins/ after build

copy-bundled-plugins.cjs compiled and rewrote plugin backend TS in
place, copied it to dist/plugins, but never deleted the .js it had
just emitted next to the .ts sources. Those files shadowed the real
source for vitest/tsx and had import paths rewritten for the dist
layout, so tests failed with "Cannot find module" once a local build
had run. Affects docker, host-metrics and ai, not just this one.

* feat: add a permission-gated backend service registry for plugins

* feat: add a permission-gated UI action registry and action slots for plugins

Moves the terminal toolbar's hardwired AI button into a slot the ssh-terminal
plugin offers and the ai plugin fills, gated on ai.services.use.

* feat: add cross-plugin secret references gated by the provider's RBAC permission

* feat: move proxmox discovery/import/sync into a first-party plugin

* feat: move remote desktop (RDP/VNC/Telnet) into a first-party plugin

Relocates guacamole backend/frontend source and tests into
plugins/remote-desktop, following the ssh-terminal/docker pattern.

* feat: wire remote-desktop plugin into core and rename i18n keys

Repoints core consumers (dispatcher, collab, session-sharing, tab
utils) at the plugin, adds isTabTypeAvailable gating for rdp/vnc/
telnet, widens hostCapability to accept an array, and renames the
guacamole i18n namespace to remoteDesktop.

* feat: move fleets into a first-party plugin

* feat: move automations into a first-party plugin

* feat: remove legacy alert rules, superseded by automations

Alert rules already auto-migrate into automations on boot and the old
engine stands down after. Deletes the rule/firing tables, routes, UI,
and AI tools; keeps notification channels since automations still use
them. Also fixes automations' notify step, which still called the
deleted repository.

* feat: move network topology into a first-party plugin

* feat: move tailscale device discovery and settings into a plugin

Adds plugins/tailscale/ with a devices sidebar tab, admin settings
section, and re-auth dialog. Host Metrics tailscale manager card and
SSH re-auth banner parsing stay in core.

* feat: move workspaces into a first-party plugin

* feat: move web endpoint into a first-party plugin

* feat: collapse plugin runtime to one tier behind a plugin SDK

* feat: make each plugin an npm workspace built by a termix-plugin CLI

* feat: give plugins their own tables, migrations and sync entities

* feat: serve plugin http and websockets from one mount instead of ports

* feat: namespace plugin permissions and keep them valid when a plugin is off

* feat: give plugins their own settings and one settings screen

* feat: load plugin frontends at runtime and remove plugin ids from the shell

* fix: mock the ai status fetch in its activate test

* feat: add auth extension points and one ssh connect pipeline

* feat: rebuild workspaces as the reference plugin

* fix: gate second factors after SSO/LDAP behind an admin setting

* feat: convert network-topology to a full plugin (B1)

* feat: move snippets backend and tables into a plugin (B2)

* feat: point core frontend snippets pickers at the plugin (B2)

* feat: add the snippets plugin (B2)

* feat: wire ai, automations, fleets and ssh-terminal to snippets (B2)

* feat: finish the tailscale plugin, including SSH auth (B3)

* feat: finish the fleets plugin, add ctx.hosts and host-commands SDK (B4)

* chore: update proxmox and host-metrics locale files for B5

* chore: regenerate drizzle migration journals for B5

* feat: add ctx.hosts create/update to the SDK, host-metrics drops proxmox

* feat: move proxmox node stats and collectors into the proxmox plugin

* feat: drop proxmox host columns, use plugin settings instead

* feat: finish the fleets plugin, add ctx.hosts and host-commands SDK (B4)

* feat: convert file manager into a plugin

* feat: convert ssh tunnels into a plugin

* feat: finish the web-endpoint plugin, add ctx.desktop (B8)

* feat: move the terminal fully into the ssh-terminal plugin (B9)

* feat: convert tmux monitoring into a plugin (B10)

* feat: convert serial console into the serial plugin

* feat: move session sharing and collab rooms into a plugin (B12)

* feat: turn session recording into a plugin (B13)

* feat: finish the remote-desktop plugin (B14)

* feat: finish the host-metrics plugin, move host status into core (B16)

* feat: finish the docker plugin (B15)

* feat: finish the automations plugin (B17)

* feat: finish the ai plugin (B18)

* feat: finish the homepage plugin, move service links and canvas out of core (B19)

* feat: convert wake-on-lan into a plugin (B20)

* feat: convert secret-sources into a plugin (B20)

* feat: move totp and passkeys into plugins (C1)

* feat: move sso and ldap logins into plugins (C2)

* feat: move opkssh and warpgate into plugins (C3)

* feat: move step-ca into a plugin (C4)

* feat: move vault ssh signing into a plugin (C5)

* feat: move termix identity into a plugin (C6)

* feat: move acme certificate automation into a plugin (C7)

* fix: finish 2.9.0 plugin moves and get every check green

* chore: lock the plugin boundary and remove 2.9.0 leftovers

* fix: harden the plugin runtime after the 2.9.0 security review

* feat: add plugin packing, signing, pinned trust and tmxplug bundling

* test: prove 2.8 to 2.9.0 upgrade is lossless and fix what it caught

* fix: give bundled plugin backends a real require for CJS deps

* feat: improve UI loading time and jitter

* feat: show credential sharing inline in the sidebar like host sharing

* feat: tailscale add host and copy ip, fix outbound fetch hang and plugin ESM warning

* feat: unify alerts into one plugin, channels, rules and inbox

* feat: rebuild desktop sync as linked server accounts, fix packaged plugin bundles and UI scaling

* fix: set blacksmith builder cache-key to stop broken gha cache fallback

* fix: link plugin-sdk workspace before npm ci in docker deps stage

* fix: link every plugin workspace before npm ci in docker deps stage

* fix(host-metrics): exclude image and firmware filesystems (#1469)

* fix(host-metrics): exclude image and firmware filesystems

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(hosts): normalize imported jump host references (#1470)

* fix(hosts): normalize imported jump host references

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(ci): build plugin SDK and repair MySQL settings migration (#1471)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(file-manager): keep inline rename consistent (#1476)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(file-manager): keep inline rename consistent

* fix(dashboard): reuse translated host status labels (#1475)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(dashboard): reuse translated host status labels

* docs(api): clarify local and remote version fields (#1474)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* docs(api): clarify local and remote version fields

* fix(file-manager): preserve selected backgrounds on hover (#1473)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(file-manager): preserve selection feedback on hover

* test(ui): await dialog focus cleanup before teardown

* fix(file-manager): restore Ctrl/Cmd+F file search (#1472)

* fix(ci): build plugin SDK before dependent checks

* fix(mysql): keep plugin settings scope indexes within limits

* test(mysql): refresh reviewed plugin upgrade SQL

* fix(file-manager): focus file search with Ctrl or Cmd F

* fix: keep rounded-full circular in production builds

* fix(file-manager): cancel uploads and stop remote writes (#1477)

* fix(file-manager): cancel uploads and stop remote writes

* test(css): exercise the configured PostCSS plugin chain

* fix(file-manager): apply compact density to directory tree (#1478)

* fix(file-manager): apply compact density to directory tree

* test(css): exercise the configured PostCSS plugin chain

* fix(file-manager): preserve font sizes in compact mode (#1480)

* fix(file-manager): preserve font sizes in compact mode

* test(css): exercise the configured PostCSS plugin chain

* fix(file-manager): prioritize file names in narrow lists (#1483)

* fix(file-manager): prioritize file names in narrow lists

* test(css): exercise the configured PostCSS plugin chain

* test(identity): preserve full Ed25519 public keys in fixtures

* test(remote-desktop): always mutate ciphertext in tamper test

* fix(remote-desktop): probe the saved host guacd endpoint (#1481)

* fix(remote-desktop): probe the saved host guacd endpoint

* test(css): exercise the configured PostCSS plugin chain

* test(identity): preserve full Ed25519 public keys in fixtures

* fix(homepage): check allowlisted private service reachability (#1479)

* fix(homepage): check allowlisted private service reachability

* test(css): exercise the configured PostCSS plugin chain

* fix(remote-desktop): stop unsolicited clipboard read prompts (#1482)

* fix(remote-desktop): read clipboard only on explicit paste

* test(css): exercise the configured PostCSS plugin chain

* test(identity): preserve full Ed25519 public keys in fixtures

* test(remote-desktop): always mutate ciphertext in tamper test

* chore: simplify plugin readmes, changelogs and manifest descriptions

* fix: keep plugins loaded after signing in from the login screen

* fix(file-manager): accept OS file drops over existing rows (#1484)

* fix(file-manager): accept OS file drops over existing rows

* test(file-manager): target the file label in drop tests

* test(css): exercise the configured PostCSS plugin chain

* test(identity): preserve full Ed25519 public keys in fixtures

* test(remote-desktop): always mutate ciphertext in tamper test

* fix(file-manager): pipeline SFTP download reads (#1485)

* feat: add plugin sdk build to beta release

* fix: make link-store secret check immune to random base64 matches

* fix(ldap): preserve the directory-provided bind DN (#1486)

* fix: compute beta changes across dev branches and move the beta tag

* chore: untrack plugin sdk ARCHITECTURE.md

* chore: remove references to plugin sdk ARCHITECTURE.md

* feat: merge host editor terminal tabs into one owned by ssh-terminal

* feat: add descriptions to plugin host enable toggles

* fix(file-manager): show download progress in file viewers (#1487)

* fix(electron): signal backend readiness over IPC (#1488)

* fix(split-screen): allow layout creation without randomUUID (#1490)

* fix(split-screen): allow layout creation without randomUUID

* test(credentials): avoid flaky Ed25519 key generation

* fix(sidebar): remeasure visible rows after density changes (#1489)

* fix(sidebar): remeasure visible rows after density changes

* test(credentials): avoid flaky Ed25519 key generation

* feat(split-screen): redesign split screen with free-form panes and pane picker

* feat(sidebar): add host click behavior setting, default to always opening a new tab

* feat(snippets): redesign panel to match the host list

* fix(session-recording): stop session logs panel from refetching in a loop

* feat(snippets): bring back sharing, target hosts and drag reorder

* feat(ssh-terminal): add toolbar position, start state, display and fade host settings

* feat(ssh-terminal): add host setting to turn off password prompt auto-fill

* fix(ai): stop streams getting cut off and keep tool steps across messages

* feat: improve quick connect system using plugin api

* fix: proxmox guest sync, startup snippet sync, drop dead cloudflare ssh transport

* feat: move terminal settings, look and ssh tools into ssh-terminal plugin

* feat: move snippets, keybinding actions and macros out of core into plugins

* feat: store rdp, vnc and telnet logins in a generic per-protocol table

* feat: type the plugin host record, move plugin strings out of core and add lint checks

* feat: replace core homepage widget registry with generic extension points

* feat: plugins only see their own host settings in the browser

* feat: harden plugin api for separate repos, signed updates and generic external login

* fix: keep plugin css below core utilities and version plugin assets by their css

* fix: load and save plugin host settings in the editor and tighten settings row spacing

* feat: redesign user host defaults system

* fix(remote-desktop): release held keys on focus loss (#1498)

* fix(session-sharing): replace local presentation after takeover (#1500)

* fix(file-manager): refresh disk usage with directory listing (#1503)

* fix(ssh-terminal): preserve input order during local echo reconciliation (#1505)

* feat(remote-desktop): expose display zoom controls for RDP (#1514)

* fix(file-manager): restore text selection and copying in Markdown previews (#1518)

* fix(sidebar): keep compact host addresses visible (#1524)

* fix(hosts): preserve plugin settings when reopening the editor (#1499)

* fix(ssh-terminal): close owner sockets when presentations unmount (#1501)

* fix(session-sharing): synchronize shared SSH terminal dimensions (#1502)

* fix(session-sharing): show participant presence in member terminals (#1504)

* fix(ai): sync provider configuration and keys across linked devices (#1506)

* feat(ssh-terminal): add a duplicate tab action (#1510)

* feat(file-manager): allow disabling move-to-trash confirmation (#1511)

* feat(file-manager): default to the remote login directory (#1512)

* feat(homepage): synchronize canvas layouts across devices (#1515)

* fix(shortcuts): prevent accidental double-Shift palette activation (#1519)

* feat(tmux-monitor): add a unified multi-host view (#1520)

* fix(electron): allow explicit quit with active connections (#1522)

* feat: reconnect all disconnected terminal tabs (#1523)

* fix(hosts): preserve recording and sharing settings in the dev editor (#1509)

* fix(database): preserve RDP domain during startup backfill (#1497)

* feat(sidebar): add keyboard type-ahead search for hosts (#1516)

* feat(automations): add per-host maintenance mode and recurring schedules (#1521)

* feat(tmux): add a per-host mouse toggle and preserve remote configuration (#1513)

* fix(session-sharing): use the linked server for desktop meetings and links (#1507)

* fix(tunnels): support TOTP for client tunnels and jump hosts (#1508)

* fix: type errors and lint failures from merged plugin prs

* feat(installer): make the Windows desktop shortcut optional (#1517)

* fix: ship the plugin sdk and serialport in desktop builds

* fix: prefer ipv4 when resolving ssh host names (#1365)

* fix: readable terminal selection with the light theme (#1319)

* fix: build desktop session share links on the linked server (#1350)

* fix: show meeting guests to members and hide the roster from guests (#1353)

* fix: reliable file manager saves with sudo prompt and no base64 guessing (#1335)

* fix: show disk usage for the mount being browsed (#1347)

* fix: load the linux tray icon reliably and log tray failures (#1332)

* fix: cover the gnome close path in the desktop quit test (#1332)

* fix: prompt for jump host TOTP in terminal, file manager and metrics (#1312)

* feat: optional auto reconnect for dropped ssh sessions (#1321)

* feat: tighter compact host rows with inline tags (#1325)

* feat: bring back the alert feed homepage widget

* feat: pick target terminals for snippets again

* feat: share button on connection tabs again

* feat: recent servers list when linking the desktop app

* feat: list the fixed app shortcuts in keyboard shortcuts

* fix: translate hardcoded toasts and drop em dashes from ui text

* fix: translate leftover hardcoded ui text

* fix: toolbar files button opens at the shell directory again

* fix: show the remote desktop note under connection origin again

* chore: openapi docs for host metrics manager routes

* chore: openapi docs for undocumented routes and fix two broken doc blocks

* fix: declare terminal.listSessions use for snippet targets

* revert: drop the alert feed widget, the notification center replaces it

* feat: restore the alert feed homepage widget

* fix: quick connect fills the host address from plugin auth editors like tailscale

* fix: docker container logs use the base background color

* chore: fix dependabot alerts and apply safe npm upgrades

* fix: proxmox stats tab reads its switch from plugin host settings

* fix: count plugin ssh logins toward host status and stop showing reachable as a warning

* fix: hide the built-in source pill in the host editor

* fix: host status shows online when the port answers and only warns on a failed login

* fix: drop the top border on the split layout menu

* fix: host defaults tabs sit where the host editor puts them and skip plugins with nothing to default

* fix: remote desktop auth method labels and notes for vnc and telnet

* fix: add description to the web endpoint enable switch

* fix: crowdin sync and pretranslate for plugin locales, run plugin tests on release

* feat: move telemetry out of core into a usage statistics plugin

* fix: recent activity card no longer cuts off host names early

* fix: remove duplicate top border on split screen menu

* fix: redesign session log player controls, fix tooltip overlap and drop dot separators

* fix: file manager and sftp text showing raw i18n keys

* fix: onboarding rerunning for existing users after upgrading

* fix: fleet detail buttons overflowing the sidebar

* fix: session log player scrolling off screen horizontally

* fix: network graph lag when adding hosts and on every render

* fix: maintenance index name, shared host read only view and badge polling

* fix: meeting tabs, workspace tab merging, rail panels as tabs and general ui lag

* chore: update release notes

* fix: remote desktop tabs reconnecting on every tab switch

* fix: build plugin sdk before linting in release workflow

* chore: lint, format, and bump version to 2.9.0

* chore: skip crowdin pre-translate in release for now

* chore: sync Crowdin translations for 2.9.0

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Koi <38512047+datlt4@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: ZacharyZcR <zacharyzcr1984@gmail.com>
2026-10-01 16:20:16 -05:00

2972 lines
100 KiB
TypeScript

import { AsyncResource } from "async_hooks";
import { StringDecoder } from "string_decoder";
import { WebSocket, type RawData } from "ws";
import ssh2Pkg, {
type Client as SSHClientType,
type ClientChannel,
type PseudoTtyOptions,
} from "ssh2";
import {
PluginSshInteractionError,
type HostSshOptions,
type PluginContext,
type PluginSshHost,
type PluginSshPrepared,
type PluginSshPromptChannel,
type PluginWebSocketConnection,
} from "@termix/plugin-sdk/backend";
import {
asObject,
asString,
getErrorMessage,
hostAddressMismatch,
HOST_ADDRESS_MISMATCH_MESSAGE,
HOST_NOT_ON_THIS_SERVER_MESSAGE,
isRetriableDnsError,
isWindowsSftpPath,
MemoryAgent,
parseWsMessage,
resolveHostForSshConnect,
resolveServerHostId,
resolveServerJumpHosts,
sftpPathToLocalPath,
toTerminalDimension,
type TerminalLogger,
} from "./helpers.js";
import { SSHAuthManager } from "./keyboard-prompt.js";
import { HOST_KEYS } from "./settings.js";
import {
isMessageAllowedForParticipant,
type TerminalSessionManager,
} from "./session-manager.js";
import type {
SessionGuestsV1,
SessionSharingV1,
SharedSessionRef,
TmuxSessionsV1,
} from "./services.js";
const { Client, utils: ssh2Utils } = ssh2Pkg;
interface ConnectToHostData {
cols: number;
rows: number;
hostConfig: {
id: number;
/** Names the host across a sync pair; `id` only names it locally. */
syncId?: string | null;
instanceId?: string;
ip: string;
port: number;
username: string;
password?: string;
key?: string;
keyPassword?: string;
keyType?: string;
authType?: string;
credentialId?: number;
userId?: string;
forceKeyboardInteractive?: boolean;
jumpHosts?: Array<{ hostId: number }>;
useSocks5?: boolean;
socks5Host?: string;
socks5Port?: number;
socks5Username?: string;
socks5Password?: string;
socks5ProxyChain?: unknown;
portKnockSequence?: Array<{
port: number;
protocol?: "tcp" | "udp";
delay?: number;
}>;
/** Keepalive, legacy algorithms, agent and environment options. */
sshOptions?: HostSshOptions | null;
/** When true, ignore key material and force password auth (fallback path). */
passwordFallbackOnly?: boolean;
};
initialPath?: string;
executeCommand?: string;
/** Attach straight to this tmux session once the shell is ready
* (tmux monitor opens its panes through a real PTY this way). */
tmuxAttachSession?: string;
}
interface ResizeData {
cols: number;
rows: number;
}
interface TOTPResponseData {
code?: string;
}
export interface TerminalSocketDeps {
ctx: PluginContext;
log: TerminalLogger;
sessionManager: TerminalSessionManager;
/** The optional tmux.sessions service, as the acting user. */
getTmux: () => TmuxSessionsV1 | null;
/** The optional sessions.sharing service, as the acting user. */
getSharing: () => SessionSharingV1 | null;
/** Guest link resolution, published on ctx.registry by session sharing. */
getGuests: () => SessionGuestsV1 | null;
}
/**
* Serves /plugin-ws/ssh-terminal/terminal.
*
* The route is public with optional auth: a signed-in user arrives with a
* token core verifies, and a share-link or room guest arrives with a token of
* its own that is resolved here. Socket and ssh2 listeners are bound with
* AsyncResource so every ctx call inside them still runs as the socket's user.
*/
export function createTerminalSocket(deps: TerminalSocketDeps) {
const { ctx, log: sshLogger, sessionManager } = deps;
const authLogger = sshLogger;
const bind = <A extends unknown[], R>(fn: (...args: A) => R) =>
AsyncResource.bind(fn);
const sockets = new Set<WebSocket>();
function closeAll(): void {
for (const ws of sockets) {
try {
ws.close(1001, "Terminal plugin disabled");
} catch {
// Already gone.
}
}
sockets.clear();
}
/** Joins an anonymous guest socket to a live shared session, read-only or not per the share. */
async function attachShareGuest(
ws: WebSocket,
share: SharedSessionRef,
guests: SessionGuestsV1,
): Promise<void> {
const session = sessionManager.getSession(share.sessionId);
if (!session || !session.isConnected) {
ws.close(1008, "Session has ended");
return;
}
const joined = sessionManager.joinAsParticipant(share.sessionId, ws, {
userId: null,
permissionLevel: share.permissionLevel,
guestLabel: "Guest",
shareId: share.id,
});
if (!joined) {
ws.close(1008, "Session is no longer active");
return;
}
guests.recordJoin(share.id).catch(() => {});
const buffered = sessionManager.getBuffer(joined);
if (buffered) {
ws.send(JSON.stringify({ type: "data", data: buffered }));
}
ws.send(
JSON.stringify({ type: "sessionAttached", sessionId: share.sessionId }),
);
ws.send(JSON.stringify({ type: "connected", message: "Joined session" }));
const currentSessionId: string = share.sessionId;
let wsAlive = true;
ws.on("pong", () => {
wsAlive = true;
});
const wsPingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
if (!wsAlive) {
ws.terminate();
return;
}
wsAlive = false;
ws.ping();
} else {
clearInterval(wsPingInterval);
}
}, 30000);
ws.on("close", () => {
clearInterval(wsPingInterval);
sessionManager.removeParticipant(currentSessionId, ws);
sshLogger.info("Guest left shared terminal session", {
operation: "terminal_guest_disconnect",
sessionId: currentSessionId,
shareId: share.id,
});
});
ws.on("message", (msg: RawData) => {
let type: string;
let data: unknown;
try {
({ type, data } = parseWsMessage(msg));
} catch {
return;
}
const liveSession = sessionManager.getSession(currentSessionId);
const participant = liveSession
? sessionManager.getParticipantForWs(liveSession, ws)
: null;
// A guest is only ever a participant. Once it has left (or was never
// added) it may send nothing, or a read-only guest could "disconnect"
// and then type into the owner's shell as nobody in particular.
if (!participant || !isMessageAllowedForParticipant(participant, type)) {
return;
}
switch (type) {
case "input": {
if (typeof data !== "string") break;
const inputData = data;
sessionManager.bufferInput(currentSessionId, inputData);
const inputStream = liveSession?.sshStream;
if (inputStream) {
try {
inputStream.write(Buffer.from(inputData, "utf8"));
} catch {
inputStream.write(Buffer.from(inputData, "latin1"));
}
}
break;
}
case "ping":
ws.send(JSON.stringify({ type: "pong" }));
break;
case "disconnect":
sessionManager.removeParticipant(currentSessionId, ws);
ws.close(1000, "Left the session");
break;
default:
break;
}
});
}
/**
* A share-link (?shareToken) or collab room (?roomGuestToken) guest. Never
* touches user credentials: guests join an already-live stream. Session
* sharing resolves the token; without it guest links are refused.
*/
async function handleGuestConnection(
ws: WebSocket,
connection: PluginWebSocketConnection,
tokens: { shareToken?: string; roomGuestToken?: string },
): Promise<void> {
const guests = deps.getGuests();
if (!guests) {
ws.close(1008, "Session sharing is not available");
return;
}
const resolved = await guests.resolve({
...tokens,
clientIp: connection.clientIp,
});
if ("reason" in resolved) {
ws.close(1008, resolved.reason);
return;
}
return attachShareGuest(ws, resolved.share, guests);
}
async function handleConnection(
connection: PluginWebSocketConnection,
): Promise<void> {
const ws = connection.socket as WebSocket;
const req = connection.request as import("http").IncomingMessage;
const sessionId: string | undefined = undefined;
sockets.add(ws);
ws.once("close", () => sockets.delete(ws));
ws.on("error", (error) => {
sshLogger.error("WebSocket connection error", error, {
operation: "ws_error",
sessionId,
});
});
const urlObj = new URL(req.url || "", "http://localhost");
const shareToken = urlObj.searchParams.get("shareToken");
if (shareToken) {
await handleGuestConnection(ws, connection, { shareToken });
return;
}
const roomGuestToken = urlObj.searchParams.get("roomGuestToken");
if (roomGuestToken) {
await handleGuestConnection(ws, connection, { roomGuestToken });
return;
}
if (!connection.userId) {
ws.close(1008, "Authentication required");
return;
}
const userId = connection.userId;
if (!connection.isDataUnlocked()) {
ws.send(
JSON.stringify({
type: "error",
message: "Data locked - re-authenticate with password",
code: "DATA_LOCKED",
}),
);
ws.close(1008, "Data access required");
return;
}
sshLogger.info("Terminal WebSocket connection established", {
operation: "terminal_ws_connect",
sessionId,
userId,
});
let currentSessionId: string | null = null;
let sshConn: SSHClientType | null = null;
let sshStream: ClientChannel | null = null;
let lastJumpClient: SSHClientType | null = null;
let keyboardInteractiveFinish: ((responses: string[]) => void) | null =
null;
let totpPromptSent = false;
let totpTimeout: NodeJS.Timeout | null = null;
let isKeyboardInteractive = false;
let keyboardInteractiveResponded = false;
let isConnecting = false;
let isConnected = false;
let isCleaningUp = false;
let isShellInitializing = false;
let isDuplicateConnDiscarded = false;
// The keyboard-interactive handler whose browser sign-in is pending.
let browserSignInId: string | null = null;
let browserSignInTimeout: NodeJS.Timeout | null = null;
let isAwaitingAuthCredentials = false;
let wsAlive = true;
ws.on("pong", () => {
wsAlive = true;
});
const wsPingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
if (!wsAlive) {
sshLogger.warn(
"WebSocket pong timeout - terminating zombie connection",
{
operation: "ws_pong_timeout",
userId,
sessionId: currentSessionId,
},
);
ws.terminate();
return;
}
wsAlive = false;
ws.ping();
}
}, 30000);
ws.on(
"close",
bind(() => {
clearInterval(wsPingInterval);
void deps
.getSharing()
?.unsubscribeRoom(ws)
.catch(() => {});
sshLogger.info("Terminal WebSocket disconnected", {
operation: "terminal_ws_disconnect",
sessionId,
userId,
});
if (currentSessionId) {
const session = sessionManager.getSession(currentSessionId);
if (session?.isConnected) {
const participant = sessionManager.getParticipantForWs(session, ws);
if (participant && !participant.isOwner) {
sessionManager.removeParticipant(currentSessionId, ws);
} else {
// Only detach if this WS is still the owner's attached socket, or
// no owner is currently attached. If a refresh reconnected and
// reattached a new WS before this close event fired, we must not
// clobber that new attachment.
const ownerStillAttached = Array.from(
session.participants.values(),
).some((p) => p.isOwner && p.ws !== ws);
if (!ownerStillAttached) {
sessionManager.detachWs(currentSessionId);
}
}
} else {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
}
cleanupAuthState();
}),
);
function resetConnectionState() {
isConnecting = false;
isConnected = false;
isKeyboardInteractive = false;
keyboardInteractiveResponded = false;
keyboardInteractiveFinish = null;
totpPromptSent = false;
browserSignInId = null;
}
ws.on(
"message",
bind(async (msg: RawData) => {
if (!connection.isDataUnlocked()) {
ws.send(
JSON.stringify({
type: "error",
message: "Data access expired - please re-authenticate",
code: "DATA_EXPIRED",
}),
);
ws.close(1008, "Data access expired");
return;
}
let type: string;
let data: unknown;
try {
({ type, data } = parseWsMessage(msg));
} catch (e) {
sshLogger.warn("Rejected malformed WebSocket message", {
operation: "websocket_message_invalid",
userId,
error: getErrorMessage(e),
});
ws.send(
JSON.stringify({ type: "error", message: "Invalid message" }),
);
return;
}
// Server-side gate: non-owner participants (read-only or read-write
// guests/joiners) may only send input/ping/disconnect - everything else
// (auth flows, tmux, resize, etc.) is owner-only and silently ignored.
if (type !== "joinSharedSession") {
const gateSession = currentSessionId
? sessionManager.getSession(currentSessionId)
: null;
const gateParticipant = gateSession
? sessionManager.getParticipantForWs(gateSession, ws)
: null;
if (!isMessageAllowedForParticipant(gateParticipant, type)) {
return;
}
}
try {
switch (type) {
case "connectToHost": {
const connectData = data as ConnectToHostData;
if (!connectData?.hostConfig) {
ws.send(
JSON.stringify({
type: "error",
message: "Missing host configuration",
}),
);
break;
}
connectData.hostConfig.userId = userId;
handleConnectToHost(connectData).catch((error) => {
const errMsg = getErrorMessage(error);
if (
errMsg.includes("Cannot parse privateKey") &&
errMsg.includes("no passphrase")
) {
isAwaitingAuthCredentials = true;
ws.send(
JSON.stringify({
type: "passphrase_required",
message:
"The SSH key is encrypted. Please enter the passphrase to unlock it.",
}),
);
return;
}
sshLogger.error("Failed to connect to host", error, {
operation: "ssh_connect",
userId,
hostId: connectData.hostConfig?.id,
ip: connectData.hostConfig?.ip,
});
ws.send(
JSON.stringify({
type: "error",
message: "Failed to connect to host: " + errMsg,
}),
);
});
break;
}
case "attachSession": {
const attachData = data as {
sessionId: string;
cols: number;
rows: number;
tabInstanceId?: string;
};
sshLogger.info("Attempting to attach session", {
operation: "terminal_attach_session",
sessionId: attachData.sessionId,
tabInstanceId: attachData.tabInstanceId,
userId,
requestedCols: attachData.cols,
requestedRows: attachData.rows,
});
const session = sessionManager.attachWs(
attachData.sessionId,
userId,
ws,
attachData.tabInstanceId,
);
if (session) {
sshLogger.success("Session attached successfully", {
operation: "terminal_attach_success",
sessionId: attachData.sessionId,
sessionCreatedAt: session.createdAt,
wasDetached: !!session.lastDetachedAt,
detachedDuration: session.lastDetachedAt
? Date.now() - session.lastDetachedAt
: 0,
});
currentSessionId = attachData.sessionId;
sshStream = session.sshStream;
sshConn = session.sshConn;
isConnecting = false;
isConnected = true;
const buffered = sessionManager.getBuffer(session);
if (buffered) {
ws.send(JSON.stringify({ type: "data", data: buffered }));
}
const attachCols = toTerminalDimension(attachData.cols);
const attachRows = toTerminalDimension(attachData.rows);
if (
attachCols &&
attachRows &&
(attachCols !== session.cols || attachRows !== session.rows)
) {
session.sshStream?.setWindow(
attachRows,
attachCols,
attachRows,
attachCols,
);
sessionManager.resizeSession(
session.id,
attachCols,
attachRows,
);
}
ws.send(
JSON.stringify({
type: "sessionAttached",
sessionId: attachData.sessionId,
}),
);
ws.send(
JSON.stringify({
type: "connected",
message: "Session reattached",
}),
);
} else {
sshLogger.warn(
"Session attachment failed - will create new connection",
{
operation: "terminal_attach_failed",
sessionId: attachData.sessionId,
tabInstanceId: attachData.tabInstanceId,
userId,
reason: "session_not_found_or_invalid",
},
);
ws.send(
JSON.stringify({
type: "sessionExpired",
sessionId: attachData.sessionId,
}),
);
}
break;
}
case "listSessions": {
const sessions = sessionManager.getUserSessions(userId);
ws.send(
JSON.stringify({
type: "sessionList",
sessions: sessions.map((s) => ({
id: s.id,
hostId: s.hostId,
hostName: s.hostName,
createdAt: s.createdAt,
lastDetachedAt: s.lastDetachedAt,
tmuxSessionName: s.tmuxSessionName,
})),
}),
);
break;
}
case "resize": {
const resizeData = data as ResizeData;
handleResize(resizeData);
break;
}
case "disconnect": {
const disconnectSession = currentSessionId
? sessionManager.getSession(currentSessionId)
: null;
const disconnectParticipant = disconnectSession
? sessionManager.getParticipantForWs(disconnectSession, ws)
: null;
if (disconnectParticipant && !disconnectParticipant.isOwner) {
if (currentSessionId) {
sessionManager.removeParticipant(currentSessionId, ws);
currentSessionId = null;
}
break;
}
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState();
sshConn = null;
sshStream = null;
break;
}
case "get_cwd": {
const activeConn =
sessionManager.getSession(currentSessionId)?.sshConn ?? sshConn;
if (!activeConn) {
ws.send(JSON.stringify({ type: "cwd", path: "/" }));
break;
}
activeConn.exec("pwd", (err, execStream) => {
if (err) {
ws.send(JSON.stringify({ type: "cwd", path: "/" }));
return;
}
let stdout = "";
execStream.on("data", (chunk: Buffer) => {
stdout += chunk.toString("utf-8");
});
execStream.stderr.on("data", () => {});
execStream.on("close", () => {
const cwd = stdout.trim() || "/";
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "cwd", path: cwd }));
}
});
});
break;
}
case "open_file_in_editor": {
const requestedPath = asString(asObject(data).path);
const activeConn =
sessionManager.getSession(currentSessionId)?.sshConn ?? sshConn;
if (!activeConn || !requestedPath) {
ws.send(
JSON.stringify({
type: "open_file_in_editor",
path: requestedPath || "/",
}),
);
break;
}
const escapedPath = requestedPath.replace(/'/g, "'\\''");
activeConn.exec(
`realpath '${escapedPath}' 2>/dev/null || echo '${escapedPath}'`,
(err, execStream) => {
if (err) {
ws.send(
JSON.stringify({
type: "open_file_in_editor",
path: requestedPath,
}),
);
return;
}
let stdout = "";
execStream.on("data", (chunk: Buffer) => {
stdout += chunk.toString("utf-8");
});
execStream.stderr.on("data", () => {});
execStream.on("close", () => {
const resolvedPath = stdout.trim() || requestedPath;
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "open_file_in_editor",
path: resolvedPath,
}),
);
}
});
},
);
break;
}
case "input": {
if (typeof data !== "string") break;
const inputData = data;
if (currentSessionId) {
sessionManager.bufferInput(currentSessionId, inputData);
}
const inputStream =
sessionManager.getSession(currentSessionId)?.sshStream ??
sshStream;
if (inputStream) {
if (inputData === "\t") {
inputStream.write(inputData);
} else if (
typeof inputData === "string" &&
inputData.startsWith("\x1b")
) {
inputStream.write(inputData);
} else {
try {
inputStream.write(Buffer.from(inputData, "utf8"));
} catch (error) {
sshLogger.error(
"Error writing input to SSH stream",
error,
{
operation: "ssh_input_encoding",
userId,
dataLength: inputData.length,
},
);
inputStream.write(Buffer.from(inputData, "latin1"));
}
}
}
break;
}
case "ping":
ws.send(JSON.stringify({ type: "pong" }));
break;
case "tmux_attach": {
const tmuxData = data as { sessionName: string };
const session = currentSessionId
? sessionManager.getSession(currentSessionId)
: null;
const tmux = deps.getTmux();
if (session?.sshStream && tmux) {
const existingName = tmuxData.sessionName || undefined;
if (existingName) {
void tmux
.attachOrCreate(
session.sshStream,
existingName,
undefined,
session.hostId,
)
.catch(() => {});
session.tmuxSessionName = existingName;
sshLogger.info("User selected tmux session to attach", {
operation: "tmux_user_attach",
sessionName: existingName,
hostId: session.hostId,
});
ws.send(
JSON.stringify({
type: "tmux_session_attached",
sessionName: existingName,
}),
);
} else {
const newName = `termix-${session.hostId}-${Date.now().toString(36).slice(-4)}`;
void tmux
.attachOrCreate(
session.sshStream,
undefined,
newName,
session.hostId,
)
.catch(() => {});
const sshConn = session.sshConn;
if (sshConn) {
(async () => {
const confirmed = await tmux.waitForSession(
sshConn,
newName,
);
session.tmuxSessionName = confirmed;
sshLogger.info("User requested new tmux session", {
operation: "tmux_user_create",
sessionName: confirmed,
hostId: session.hostId,
});
ws.send(
JSON.stringify({
type: "tmux_session_created",
sessionName: confirmed,
}),
);
})();
}
}
}
break;
}
case "tmux_detach": {
const session = currentSessionId
? sessionManager.getSession(currentSessionId)
: null;
if (session?.sshConn && session.tmuxSessionName) {
const tmuxName = session.tmuxSessionName;
session.sshStream?.write("\x02d");
session.tmuxSessionName = null;
sshLogger.info("User detached from tmux session", {
operation: "tmux_user_detach",
sessionName: tmuxName,
hostId: session.hostId,
});
ws.send(
JSON.stringify({
type: "tmux_detached",
sessionName: tmuxName,
}),
);
}
break;
}
case "totp_response": {
const totpData = data as TOTPResponseData;
if (keyboardInteractiveFinish && totpData?.code) {
if (totpTimeout) {
clearTimeout(totpTimeout);
totpTimeout = null;
}
const totpCode = totpData.code;
keyboardInteractiveFinish([totpCode]);
keyboardInteractiveFinish = null;
totpPromptSent = false;
} else {
sshLogger.warn(
"TOTP response received but no callback available",
{
operation: "totp_response_error",
userId,
hasCallback: !!keyboardInteractiveFinish,
hasCode: !!totpData?.code,
},
);
ws.send(
JSON.stringify({
type: "error",
message:
"TOTP authentication state lost. Please reconnect.",
}),
);
}
break;
}
case "password_response": {
const passwordData = data as TOTPResponseData;
if (keyboardInteractiveFinish && passwordData?.code) {
if (totpTimeout) {
clearTimeout(totpTimeout);
totpTimeout = null;
}
const password = passwordData.code;
keyboardInteractiveFinish([password]);
keyboardInteractiveFinish = null;
} else {
sshLogger.warn(
"Password response received but no callback available",
{
operation: "password_response_error",
userId,
hasCallback: !!keyboardInteractiveFinish,
hasCode: !!passwordData?.code,
},
);
ws.send(
JSON.stringify({
type: "error",
message:
"Password authentication state lost. Please reconnect.",
}),
);
}
break;
}
case "reconnect_with_credentials": {
const credentialsData = data as {
cols: number;
rows: number;
hostConfig: ConnectToHostData["hostConfig"];
password?: string;
sshKey?: string;
keyPassword?: string;
};
if (!credentialsData?.hostConfig) {
ws.send(
JSON.stringify({
type: "error",
message: "Missing host configuration",
}),
);
break;
}
if (credentialsData.password) {
credentialsData.hostConfig.password = credentialsData.password;
credentialsData.hostConfig.authType = "password";
(
credentialsData.hostConfig as Record<string, unknown>
).userProvidedPassword = true;
} else if (credentialsData.sshKey) {
credentialsData.hostConfig.key = credentialsData.sshKey;
credentialsData.hostConfig.keyPassword =
credentialsData.keyPassword;
credentialsData.hostConfig.authType = "key";
} else if (credentialsData.keyPassword) {
credentialsData.hostConfig.keyPassword =
credentialsData.keyPassword;
}
isAwaitingAuthCredentials = false;
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState();
sshConn = null;
sshStream = null;
const reconnectData: ConnectToHostData = {
cols: credentialsData.cols,
rows: credentialsData.rows,
hostConfig: credentialsData.hostConfig,
};
handleConnectToHost(reconnectData).catch((error) => {
const errMsg = getErrorMessage(error);
if (
errMsg.includes("Cannot parse privateKey") &&
errMsg.includes("no passphrase")
) {
isAwaitingAuthCredentials = true;
ws.send(
JSON.stringify({
type: "passphrase_required",
message:
"The SSH key is encrypted. Please enter the passphrase to unlock it.",
}),
);
return;
}
sshLogger.error("Failed to reconnect with credentials", error, {
operation: "ssh_reconnect_with_credentials",
userId,
hostId: credentialsData.hostConfig?.id,
ip: credentialsData.hostConfig?.ip,
});
ws.send(
JSON.stringify({
type: "error",
message:
"Failed to connect with provided credentials: " + errMsg,
}),
);
});
break;
}
case "collab_subscribe": {
const { roomId } = (data ?? {}) as { roomId?: string };
if (typeof roomId !== "string" || !roomId) break;
try {
const sharing = deps.getSharing();
const joined = sharing
? await sharing.subscribeRoom(roomId, ws)
: false;
if (!joined) {
ws.send(
JSON.stringify({
type: "error",
message: "Room not found",
}),
);
}
} catch (error) {
sshLogger.error("Failed to subscribe to collab room", error, {
operation: "collab_subscribe_error",
userId,
});
}
break;
}
case "collab_unsubscribe": {
const { roomId } = (data ?? {}) as { roomId?: string };
void deps
.getSharing()
?.unsubscribeRoom(
ws,
typeof roomId === "string" ? roomId : undefined,
)
.catch(() => {});
break;
}
case "joinSharedSession": {
const joinData = data as {
shareId: string;
tabInstanceId?: string;
};
try {
const sharing = deps.getSharing();
const authorized = sharing
? await sharing.authorizeJoin(joinData.shareId)
: null;
if (!sharing || !authorized) {
ws.send(
JSON.stringify({
type: "error",
message: "Share not found or not accessible",
}),
);
break;
}
const { share, displayName } = authorized;
const joinedSession = sessionManager.joinAsParticipant(
share.sessionId,
ws,
{
userId,
permissionLevel: share.permissionLevel,
displayName,
tabInstanceId: joinData.tabInstanceId,
shareId: share.id,
},
);
if (!joinedSession) {
ws.send(
JSON.stringify({
type: "error",
message: "Shared session is no longer active",
}),
);
break;
}
currentSessionId = share.sessionId;
sshStream = joinedSession.sshStream;
sshConn = joinedSession.sshConn;
isConnecting = false;
isConnected = true;
sharing.recordJoin(share.id).catch(() => {});
const buffered = sessionManager.getBuffer(joinedSession);
if (buffered) {
ws.send(JSON.stringify({ type: "data", data: buffered }));
}
ws.send(
JSON.stringify({
type: "sessionAttached",
sessionId: share.sessionId,
}),
);
ws.send(
JSON.stringify({
type: "connected",
message: "Joined session",
}),
);
} catch (error) {
sshLogger.error("Failed to join shared session", error, {
operation: "terminal_join_shared_session_error",
userId,
shareId: joinData.shareId,
});
ws.send(
JSON.stringify({
type: "error",
message: "Failed to join shared session",
}),
);
}
break;
}
default:
if (await handleSignInMessage(type, data)) break;
sshLogger.warn("Unknown message type received", {
operation: "websocket_message_unknown_type",
userId,
messageType: type,
});
}
} catch (error) {
// A malformed payload must never escape into the process-level
// unhandledRejection handler, which exits the server.
sshLogger.error("Error handling WebSocket message", error, {
operation: "websocket_message_handler_error",
userId,
messageType: type,
});
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "error",
message: "Failed to process message",
}),
);
}
}
}),
);
/**
* Starts the browser sign-in behind an "<interaction>_auth_required"
* message. The provider for the host's auth type wins; otherwise the first
* provider that owns this interaction.
*/
async function startAuthInteraction(
interaction: string,
hostId: number,
payload: unknown,
): Promise<void> {
await ctx.ssh.startInteraction(interaction, {
hostId,
socket: ws,
requestOrigin: connection.requestOrigin,
payload: (payload ?? {}) as Record<string, unknown>,
});
}
/**
* Messages a sign-in's UI sends: "<interaction>_start_auth", "_cancel"
* and "_auth_completed" for a provider's browser step, and
* "<handler>_auth_continue" for a pending keyboard-interactive browser
* round. False for anything else.
*/
async function handleSignInMessage(
type: string,
data: unknown,
): Promise<boolean> {
const match =
/^([a-z0-9-]+)_(start_auth|cancel|auth_completed|auth_continue)$/.exec(
type,
);
if (!match) return false;
const [, name, action] = match;
const payload = (data ?? {}) as Record<string, unknown>;
const hostId = Number(payload.hostId);
if (action === "start_auth") {
try {
await startAuthInteraction(name, hostId, payload);
} catch (error) {
sshLogger.error("Failed to start sign-in", error, {
operation: "sign_in_start_error",
interaction: name,
userId,
hostId,
});
ws.send(
JSON.stringify({
type: `${name}_error`,
requestId: "",
hostId,
error:
error instanceof PluginSshInteractionError
? error.message
: getErrorMessage(error, "Failed to start authentication"),
}),
);
}
return true;
}
if (action === "cancel") {
try {
await ctx.ssh.cancelInteraction(name, {
requestId:
typeof payload.requestId === "string"
? payload.requestId
: undefined,
hostId: Number.isInteger(hostId) ? hostId : undefined,
});
resetConnectionState();
} catch (error) {
sshLogger.error("Failed to cancel sign-in", error, {
operation: "sign_in_cancel_error",
interaction: name,
userId,
});
}
return true;
}
if (action === "auth_continue") {
if (name === browserSignInId && keyboardInteractiveFinish) {
if (browserSignInTimeout) {
clearTimeout(browserSignInTimeout);
browserSignInTimeout = null;
}
keyboardInteractiveFinish([""]);
keyboardInteractiveFinish = null;
browserSignInId = null;
}
return true;
}
const completed = payload as {
hostId: number;
cols?: number;
rows?: number;
hostConfig?: ConnectToHostData["hostConfig"];
};
resetConnectionState();
const reconnectConfig: ConnectToHostData = {
cols: completed.cols || 80,
rows: completed.rows || 24,
hostConfig:
completed.hostConfig ||
({
id: completed.hostId,
ip: "",
port: 22,
username: "",
userId,
} as ConnectToHostData["hostConfig"]),
};
handleConnectToHost(reconnectConfig).catch((error) => {
sshLogger.error("Failed to reconnect after sign-in", error, {
operation: "sign_in_reconnect_error",
interaction: name,
userId,
hostId: completed.hostId,
});
ws.send(
JSON.stringify({
type: "error",
message:
"Failed to connect after authentication: " +
getErrorMessage(error),
}),
);
});
return true;
}
async function handleConnectToHost(data: ConnectToHostData) {
const { hostConfig, initialPath, executeCommand, tmuxAttachSession } =
data;
const {
id,
syncId: hostSyncId,
ip: rawIp,
port: clientPort,
username: clientUsername,
password,
key,
keyPassword,
keyType,
authType,
credentialId,
} = hostConfig;
const clientIp = rawIp?.replace(/^\[|\]$/g, "").trim() || rawIp;
let ip = clientIp;
let port = clientPort;
let username = clientUsername;
sshLogger.info("Resolving SSH host configuration", {
operation: "terminal_host_resolve",
sessionId,
userId,
hostId: id,
});
const sendLog = (
stage: string,
level: string,
message: string,
details?: Record<string, unknown>,
) => {
ws.send(
JSON.stringify({
type: "connection_log",
data: { stage, level, message, details },
}),
);
};
if (!username || typeof username !== "string" || username.trim() === "") {
sshLogger.error("Invalid username provided", undefined, {
operation: "ssh_connect",
hostId: id,
ip,
});
ws.send(
JSON.stringify({
type: "error",
message: "Invalid username provided",
}),
);
return;
}
if (!ip || typeof ip !== "string" || ip.trim() === "") {
sshLogger.error("Invalid IP provided", undefined, {
operation: "ssh_connect",
hostId: id,
username,
});
ws.send(
JSON.stringify({ type: "error", message: "Invalid IP provided" }),
);
return;
}
if (!port || typeof port !== "number" || port <= 0) {
sshLogger.error("Invalid port provided", undefined, {
operation: "ssh_connect",
hostId: id,
ip,
username,
port,
});
ws.send(
JSON.stringify({ type: "error", message: "Invalid port provided" }),
);
return;
}
if (isConnecting || isConnected) {
sshLogger.warn("Connection already in progress or established", {
operation: "ssh_connect",
hostId: id,
isConnecting,
isConnected,
});
ws.send(
JSON.stringify({
type: "error",
message: "Connection already in progress",
code: "DUPLICATE_CONNECTION",
}),
);
return;
}
isConnecting = true;
sshConn = new Client();
sendLog("dns", "info", `Starting address resolution of ${ip}`);
sendLog("tcp", "info", `Connecting to ${ip} port ${port}`);
const onConnectionTimeout = () => {
if (sshConn && isConnecting && !isConnected) {
sshLogger.error("SSH connection timeout", undefined, {
operation: "ssh_connect",
hostId: id,
ip,
port,
username,
});
ws.send(
JSON.stringify({
type: "error",
message: "SSH connection timeout",
}),
);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
}
};
// Reassigned when an auth provider's onBanner holds the connection open
// (Tailscale SSH check mode), so the short connect timeout does not tear
// down a connection the server is deliberately waiting on.
let connectionTimeout = setTimeout(onConnectionTimeout, 120000);
let bannerHoldPending = false;
let authRetries = 0;
let isAuthRetrying = false;
let prepared: PluginSshPrepared | null = null;
let connectTarget: PluginSshHost | null = null;
let clearOnlineStatus: (() => void) | null = null;
let resolvedHostData:
| (Record<string, unknown> & {
id?: number;
ip?: string;
port?: number;
username?: string;
password?: string;
key?: string;
keyPassword?: string;
keyType?: string;
authType?: string;
jumpHosts?: Array<{ hostId: number }>;
useSocks5?: boolean;
socks5Host?: string;
socks5Port?: number;
socks5Username?: string;
socks5Password?: string;
socks5ProxyChain?: unknown;
portKnockSequence?: ConnectToHostData["hostConfig"]["portKnockSequence"];
sshOptions?: HostSshOptions | null;
})
| null = null;
if (id && userId) {
try {
// Prefer the sync identity. A numeric id belongs to whichever database
// produced it, so on a sync server it names a different host than the
// desktop app meant; syncId is the same string on both sides.
resolvedHostData = (await ctx.ssh.resolveHost(id, {
syncId: hostSyncId,
})) as unknown as typeof resolvedHostData | null;
if (hostSyncId && !resolvedHostData) {
sshLogger.error(
"Refusing to connect: host is not known to this server",
undefined,
{
operation: "ssh_connect_host_sync_id_unknown",
hostId: id,
userId,
},
);
ws.send(
JSON.stringify({
type: "error",
message: HOST_NOT_ON_THIS_SERVER_MESSAGE,
}),
);
cleanupAuthState(connectionTimeout);
return;
}
// Older clients send only the numeric id, which cannot be trusted to
// mean the same host here. Everything below is taken from the row it
// lands on -- the address, the credentials, the jump hosts, the stored
// host key -- so compare the address before using any of it.
if (
!hostSyncId &&
hostAddressMismatch(clientIp, resolvedHostData?.ip)
) {
sshLogger.error(
"Refusing to connect: host id resolves to a different address here",
undefined,
{
operation: "ssh_connect_host_id_mismatch",
hostId: id,
userId,
clientIp,
resolvedIp: resolvedHostData?.ip,
},
);
ws.send(
JSON.stringify({
type: "error",
message: HOST_ADDRESS_MISMATCH_MESSAGE,
}),
);
cleanupAuthState(connectionTimeout);
return;
}
if (resolvedHostData) {
const resolvedJumpHosts = resolveServerJumpHosts(
hostConfig.jumpHosts,
resolvedHostData.jumpHosts,
hostSyncId,
);
if (resolvedJumpHosts !== hostConfig.jumpHosts) {
hostConfig.jumpHosts = resolvedJumpHosts;
sendLog(
"jump",
"info",
`Loaded ${resolvedJumpHosts?.length ?? 0} jump host(s) from server-side host data`,
);
}
if (resolvedHostData.useSocks5) {
hostConfig.useSocks5 = resolvedHostData.useSocks5;
hostConfig.socks5Host = resolvedHostData.socks5Host;
hostConfig.socks5Port = resolvedHostData.socks5Port;
hostConfig.socks5Username = resolvedHostData.socks5Username;
hostConfig.socks5Password = resolvedHostData.socks5Password;
hostConfig.socks5ProxyChain = resolvedHostData.socks5ProxyChain;
}
if (!hostConfig.sshOptions && resolvedHostData.sshOptions) {
hostConfig.sshOptions = resolvedHostData.sshOptions;
}
if (
(!hostConfig.portKnockSequence ||
hostConfig.portKnockSequence.length === 0) &&
resolvedHostData.portKnockSequence &&
resolvedHostData.portKnockSequence.length > 0
) {
hostConfig.portKnockSequence = resolvedHostData.portKnockSequence;
sendLog(
"port_knock",
"info",
`Loaded ${resolvedHostData.portKnockSequence.length} port knock(s) from server-side host data`,
);
}
}
} catch (error) {
sshLogger.warn(`Failed to resolve server-side host data for ${id}`, {
operation: "ssh_host_data",
hostId: id,
error: getErrorMessage(error),
});
}
}
const serverHostId = resolveServerHostId(id, resolvedHostData);
let autoTmuxEnabled = false;
if (serverHostId != null) {
try {
autoTmuxEnabled =
(await ctx.settings.getHost(serverHostId, HOST_KEYS.autoTmux)) ===
true;
} catch {
autoTmuxEnabled = false;
}
}
// Resolve credentials server-side when frontend doesn't provide them
let resolvedCredentials = {
username,
password,
key,
keyPassword,
keyType,
authType,
certPublicKey: undefined as string | undefined,
};
const authMethodNotAvailable = false;
if (id && userId && !password && !key) {
try {
if (resolvedHostData) {
ip = resolvedHostData.ip || ip;
port = resolvedHostData.port || port;
username = resolvedHostData.username || username;
resolvedCredentials = {
username: resolvedHostData.username || username,
password: resolvedHostData.password,
key: resolvedHostData.key,
keyPassword: keyPassword || resolvedHostData.keyPassword,
keyType: resolvedHostData.keyType,
authType: resolvedHostData.authType,
certPublicKey: resolvedHostData.certPublicKey as
string | undefined,
};
sendLog(
"auth",
"info",
"Credentials resolved from server-side host data",
);
}
} catch (error) {
sshLogger.warn(`Failed to resolve host credentials for ${id}`, {
operation: "ssh_credentials",
hostId: id,
error: getErrorMessage(error),
});
}
} else if (credentialId && id && userId) {
try {
if (resolvedHostData) {
ip = resolvedHostData.ip || ip;
port = resolvedHostData.port || port;
username = resolvedHostData.username || username;
resolvedCredentials = {
username: resolvedHostData.username || username,
password: resolvedHostData.password,
key: resolvedHostData.key,
// Preserve user-supplied keyPassword (e.g. from passphrase dialog) over the empty DB value
keyPassword: keyPassword || resolvedHostData.keyPassword,
keyType: resolvedHostData.keyType,
authType: resolvedHostData.authType,
certPublicKey: resolvedHostData.certPublicKey as
string | undefined,
};
}
} catch (error) {
sshLogger.warn(`Failed to resolve credentials for host ${id}`, {
operation: "ssh_credentials",
hostId: id,
credentialId,
error: getErrorMessage(error),
});
}
}
if (hostConfig.passwordFallbackOnly && resolvedCredentials.password) {
resolvedCredentials = {
...resolvedCredentials,
key: undefined,
keyPassword: undefined,
keyType: undefined,
certPublicKey: undefined,
authType: "password",
};
}
const connectsViaJumpHosts = !!(
hostConfig.jumpHosts &&
hostConfig.jumpHosts.length > 0 &&
hostConfig.userId
);
let connectHost = ip;
if (connectsViaJumpHosts) {
// The target is only reachable through the jump host's network (e.g. a
// VPN-only address), so DNS must be resolved there, not on this host.
sendLog(
"dns",
"info",
`Skipping local address resolution of ${ip} (resolved by jump host)`,
);
} else {
sendLog("dns", "info", `Starting address resolution of ${ip}`);
try {
const resolution = await resolveHostForSshConnect(ip);
connectHost = resolution.host;
if (resolution.resolvedAddress && resolution.resolvedAddress !== ip) {
sendLog(
"dns",
"success",
`Resolved ${ip} to ${resolution.resolvedAddress}`,
{ attempts: resolution.attempts },
);
}
} catch (error) {
const message = getErrorMessage(error);
sshLogger.error("SSH hostname resolution failed", error, {
operation: "terminal_dns_resolve",
hostId: id,
ip,
port,
transient: isRetriableDnsError(error),
});
sendLog(
"dns",
"error",
`DNS resolution failed for ${ip}: ${message}`,
);
ws.send(
JSON.stringify({
type: "error",
message: isRetriableDnsError(error)
? "SSH error: DNS lookup temporarily failed. Check the Docker/container DNS configuration or try again."
: "SSH error: Could not resolve hostname from the Termix server container.",
}),
);
cleanupAuthState(connectionTimeout);
return;
}
}
sendLog("tcp", "info", `Connecting to ${ip} port ${port}`);
// A provider can hold the handshake open pending an out-of-band step
// (Tailscale SSH check mode delivers its re-auth URL as an auth banner,
// then blocks while the user logs in via the browser).
sshConn.on(
"banner",
bind((banner: string) => {
if (!prepared?.onBanner) return;
const decision = prepared.onBanner(banner);
if (!decision) return;
const authType = prepared.authType;
if (decision.action === "hold") {
bannerHoldPending = true;
clearTimeout(connectionTimeout);
connectionTimeout = setTimeout(
onConnectionTimeout,
decision.timeoutMs,
);
sendLog("auth", "info", decision.message);
ws.send(
JSON.stringify({
type: `${authType}_check_required`,
hostId: id,
...decision.details,
}),
);
return;
}
if (bannerHoldPending) {
bannerHoldPending = false;
sendLog("auth", "info", `${authType} check completed`);
ws.send(
JSON.stringify({
type: `${authType}_check_completed`,
hostId: id,
...decision.details,
}),
);
}
}),
);
sshConn.on(
"ready",
bind(() => {
if (serverHostId != null) {
clearOnlineStatus ??= ctx.hosts.trackSession(serverHostId);
}
clearTimeout(connectionTimeout);
isAuthRetrying = false;
if (bannerHoldPending) {
bannerHoldPending = false;
ws.send(
JSON.stringify({
type: `${prepared?.authType}_check_completed`,
hostId: id,
}),
);
}
sshLogger.success("SSH connection established", {
operation: "terminal_ssh_connected",
sessionId,
userId,
hostId: id,
ip,
});
void ctx.audit.record({
action: "ssh_connect",
resourceType: "host",
resourceId: String(id),
resourceName: `${username}@${ip}:${port}`,
success: true,
});
if (totpPromptSent) {
authLogger.success("TOTP verification successful for SSH session", {
operation: "terminal_totp_success",
sessionId,
userId,
hostId: id,
});
}
sendLog("handshake", "success", "SSH handshake completed");
sendLog(
"auth",
"success",
`Authentication successful for ${username}`,
);
sendLog("connected", "success", "Connection established");
const hostDisplayName = `${username}@${ip}:${port}`;
const tabInstanceId = hostConfig.instanceId;
// The recordings.writer provider decides per host and user.
const sessionLoggingEnabled = true;
currentSessionId = sessionManager.createSession(
userId,
id,
hostDisplayName,
data.cols,
data.rows,
tabInstanceId,
sessionLoggingEnabled,
);
// If createSession returned an existing live session (duplicate tabInstanceId),
// close the newly-established SSH connection and attach this WS to the live session instead.
const existingSession = sessionManager.getSession(currentSessionId);
if (
existingSession &&
existingSession.sshStream &&
!existingSession.sshStream.destroyed &&
existingSession.sshConn !== sshConn
) {
const reusedSessionId = currentSessionId;
sshLogger.info(
"Reusing existing live session after duplicate connectToHost, closing new SSH conn",
{
operation: "terminal_reuse_existing_session",
sessionId: reusedSessionId,
tabInstanceId,
userId,
},
);
// Null out currentSessionId before ending the duplicate connection so
// the sshConn "close" handler does not destroy the reused session.
// Set isDuplicateConnDiscarded so the close handler does not send a
// "disconnected" message to the new WS that is now attached to the live session.
// Null out currentSessionId before ending the duplicate connection so
// the sshConn "close" handler does not destroy the reused session.
// Set isDuplicateConnDiscarded so the close handler exits without
// sending a "disconnected" message to the new WS.
currentSessionId = null;
isDuplicateConnDiscarded = true;
clearTimeout(connectionTimeout);
try {
sshConn?.end();
} catch {
/* ignore */
}
sshConn = null;
sshStream = null;
// Point this WS handler's closure at the live session so the input
// handler can forward keystrokes via currentSessionId.
currentSessionId = reusedSessionId;
sshStream = existingSession.sshStream;
sshConn = existingSession.sshConn;
isConnecting = false;
isConnected = true;
sessionManager.attachWs(reusedSessionId, userId, ws, tabInstanceId);
const buffered = sessionManager.getBuffer(existingSession);
if (buffered) {
ws.send(JSON.stringify({ type: "data", data: buffered }));
}
ws.send(
JSON.stringify({
type: "sessionCreated",
sessionId: reusedSessionId,
}),
);
ws.send(
JSON.stringify({
type: "sessionAttached",
sessionId: reusedSessionId,
}),
);
ws.send(
JSON.stringify({
type: "connected",
message: "Session reattached",
}),
);
return;
}
sshLogger.info("Terminal session created after SSH ready", {
operation: "terminal_session_created",
sessionId: currentSessionId,
userId,
hostId: id,
tabInstanceId,
ip,
port,
});
const conn = sshConn;
if (!conn || isCleaningUp || !sshConn) {
sshLogger.warn(
"SSH connection was cleaned up before shell could be created",
{
operation: "ssh_shell",
hostId: id,
ip,
port,
username,
isCleaningUp,
connNull: !conn,
sshConnNull: !sshConn,
},
);
ws.send(
JSON.stringify({
type: "error",
message:
"SSH connection was closed before terminal could be created",
}),
);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
return;
}
isShellInitializing = true;
isConnecting = false;
isConnected = true;
if (!sshConn) {
sshLogger.error(
"SSH connection became null right before shell creation",
{
operation: "ssh_shell",
hostId: id,
},
);
ws.send(
JSON.stringify({
type: "error",
message: "SSH connection lost during setup",
}),
);
isShellInitializing = false;
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
return;
}
sshLogger.info("Creating shell", {
operation: "ssh_shell_start",
hostId: id,
ip,
port,
username,
});
let shellCallbackReceived = false;
const shellTimeout = setTimeout(() => {
if (!shellCallbackReceived && isShellInitializing) {
sshLogger.error(
"Shell creation timeout - no response from server",
{
operation: "ssh_shell_timeout",
hostId: id,
ip,
port,
username,
},
);
isShellInitializing = false;
ws.send(
JSON.stringify({
type: "error",
message:
"Shell creation timeout. The server may not support interactive shells or the connection was interrupted.",
}),
);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
}
}, 15000);
conn.shell(
{
rows: data.rows,
cols: data.cols,
term: "xterm-256color",
} as PseudoTtyOptions,
bind((err: Error | undefined, stream: ClientChannel) => {
shellCallbackReceived = true;
clearTimeout(shellTimeout);
isShellInitializing = false;
if (err) {
sshLogger.error("Shell error", err, {
operation: "ssh_shell",
hostId: id,
ip,
port,
username,
});
ws.send(
JSON.stringify({
type: "error",
message: "Shell error: " + err.message,
}),
);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
return;
}
sshStream = stream;
sshLogger.success("Terminal shell channel opened", {
operation: "terminal_shell_opened",
sessionId,
userId,
hostId: id,
termType: "xterm-256color",
});
if (currentSessionId) {
sessionManager.setSSHState(
currentSessionId,
sshConn!,
stream,
lastJumpClient,
);
sessionManager.attachWs(currentSessionId, userId, ws);
ws.send(
JSON.stringify({
type: "sessionCreated",
sessionId: currentSessionId,
}),
);
sshLogger.info("Session ready for persistence", {
operation: "session_ready",
sessionId: currentSessionId,
userId,
hostId: id,
});
}
const boundSessionId = currentSessionId;
// A single TCP/SSH packet boundary can split a multi-byte UTF-8
// character (e.g. the box-drawing glyphs mc/htop use for borders).
// Buffer.toString("utf-8") on each chunk independently replaces the
// split bytes with U+FFFD, which shows up as corrupted/inserted
// characters. StringDecoder carries incomplete trailing bytes over
// to the next chunk so multi-byte characters decode correctly.
const decoder = new StringDecoder("utf-8");
stream.on("data", (data: Buffer) => {
try {
const utf8String = decoder.write(data);
if (!utf8String) return;
const session = sessionManager.getSession(boundSessionId);
if (session) {
sessionManager.bufferOutput(boundSessionId!, utf8String);
sessionManager.broadcast(boundSessionId!, {
type: "data",
data: utf8String,
});
}
} catch (error) {
sshLogger.error("Error encoding terminal data", error, {
operation: "terminal_data_encoding",
hostId: id,
dataLength: data.length,
});
const fallback = data.toString("latin1");
const session = sessionManager.getSession(boundSessionId);
if (session) {
sessionManager.bufferOutput(boundSessionId!, fallback);
sessionManager.broadcast(boundSessionId!, {
type: "data",
data: fallback,
});
}
}
});
stream.on("close", (code: number | null) => {
const session = sessionManager.getSession(boundSessionId);
if (session) {
if (code != null) {
sessionManager.broadcast(boundSessionId!, {
type: "session_ended",
code,
});
} else {
sessionManager.broadcast(boundSessionId!, {
type: "disconnected",
message: "Connection lost",
graceful: true,
});
}
}
if (boundSessionId) {
sessionManager.destroySession(boundSessionId);
if (currentSessionId === boundSessionId) {
currentSessionId = null;
}
}
});
stream.on("error", (err: Error) => {
sshLogger.error("SSH stream error", err, {
operation: "ssh_stream",
hostId: id,
ip,
port,
username,
});
const session = sessionManager.getSession(boundSessionId);
if (session) {
sessionManager.broadcast(boundSessionId!, {
type: "error",
message: "SSH stream error: " + err.message,
});
}
});
const autoTmux = autoTmuxEnabled;
// Helper to run initialPath/executeCommand after the shell
// (or tmux session) is ready
const runPostShellCommands = (delay: number) => {
setTimeout(() => {
if (initialPath && initialPath.trim() !== "") {
let cdCommand: string;
if (isWindowsSftpPath(initialPath)) {
const winPath = sftpPathToLocalPath(initialPath);
const escaped = winPath.replace(/"/g, '""');
cdCommand = `cd "${escaped}"\r`;
} else {
cdCommand = `cd "${initialPath.replace(/"/g, '\\"')}"\r`;
}
stream.write(cdCommand);
}
if (executeCommand && executeCommand.trim() !== "") {
setTimeout(() => {
stream.write(`${executeCommand}\r`);
}, 300);
}
}, delay);
};
const tmux = deps.getTmux();
if (tmuxAttachSession && conn && tmux) {
// Direct attach (tmux monitor): the session is known to exist, so
// skip detection and reuse the same path as the manual
// "tmux_attach" websocket message.
void tmux
.attachOrCreate(stream, tmuxAttachSession, undefined, id)
.catch(() => {});
{
const session = sessionManager.getSession(boundSessionId);
if (session) session.tmuxSessionName = tmuxAttachSession;
}
sshLogger.info("Attached to requested tmux session", {
operation: "tmux_direct_attach",
sessionName: tmuxAttachSession,
hostId: id,
});
ws.send(
JSON.stringify({
type: "tmux_session_attached",
sessionName: tmuxAttachSession,
}),
);
} else if (autoTmux && conn && tmux) {
(async () => {
try {
const detection = await tmux.detect(conn);
if (!detection.available) {
sshLogger.warn("tmux not found on remote host", {
operation: "tmux_detection",
hostId: id,
});
ws.send(
JSON.stringify({
type: "tmux_unavailable",
message:
"tmux is not installed on the remote host. Falling back to standard shell.",
}),
);
runPostShellCommands(0);
} else if (detection.sessions.length === 0) {
const newName = `termix-${id}-${Date.now().toString(36).slice(-4)}`;
void tmux
.attachOrCreate(stream, undefined, newName, id)
.catch(() => {});
const confirmed = await tmux.waitForSession(
conn,
newName,
);
const session = sessionManager.getSession(boundSessionId);
if (session) {
session.tmuxSessionName = confirmed;
}
sshLogger.info("Created new tmux session", {
operation: "tmux_new_session",
sessionName: confirmed,
hostId: id,
});
ws.send(
JSON.stringify({
type: "tmux_session_created",
sessionName: confirmed,
}),
);
runPostShellCommands(0);
} else {
sshLogger.info(
"Multiple tmux sessions found, sending list to frontend",
{
operation: "tmux_sessions_available",
sessions: detection.sessions,
hostId: id,
},
);
ws.send(
JSON.stringify({
type: "tmux_sessions_available",
sessions: detection.sessions,
}),
);
// Commands deferred until user picks a session
}
} catch (error) {
sshLogger.error("tmux detection failed", error, {
operation: "tmux_detection_error",
hostId: id,
});
// Fallback: run commands in plain shell
runPostShellCommands(0);
}
})();
} else {
// No tmux -- run commands directly as before
runPostShellCommands(0);
}
ws.send(
JSON.stringify({ type: "connected", message: "SSH connected" }),
);
if (id && hostConfig.userId) {
try {
ctx.events.emit("host.login", {
hostId: id,
userId: hostConfig.userId,
sshUser: username,
fromIp: connection.clientIp,
});
} catch (error) {
sshLogger.warn("Failed to dispatch login event", {
operation: "login_event_dispatch_error",
hostId: id,
error: getErrorMessage(error),
});
}
const hostName =
resolvedHostData?.userId === hostConfig.userId &&
typeof resolvedHostData?.name === "string" &&
resolvedHostData.name
? resolvedHostData.name
: `${username}@${ip}:${port}`;
ctx.hosts
.recordActivity(id, "terminal", hostName)
.catch((error) => {
sshLogger.warn("Failed to log terminal activity", {
operation: "activity_log_error",
userId: hostConfig.userId,
hostId: id,
error: getErrorMessage(error),
});
});
}
}),
);
}),
);
sshConn.on(
"error",
bind((err: Error) => {
clearTimeout(connectionTimeout);
sendLog("error", "error", `Connection error: ${err.message}`);
sshLogger.error("SSH connection error", err, {
operation: "ssh_connect",
hostId: id,
ip,
port,
username,
authType: resolvedCredentials.authType,
browserSignInId,
isKeyboardInteractive,
hasKeyboardInteractiveFinish: !!keyboardInteractiveFinish,
keyboardInteractiveResponded,
});
// The provider decides what an auth failure means: clear a cached
// certificate and ask for a new sign-in, retry once, or give up with
// its own message.
const authFollowUp = prepared?.onAuthFailed
? prepared.onAuthFailed({
error: err,
retries: authRetries,
canRetry: !connectConfig.sock && !bannerHoldPending,
methodNotAvailable: authMethodNotAvailable,
})
: undefined;
if (authFollowUp?.status === "interaction-required") {
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
sendLog("auth", "error", authFollowUp.message);
ws.send(
JSON.stringify({
type: `${authFollowUp.interaction}_auth_required`,
hostId: id,
message: authFollowUp.message,
}),
);
return;
}
if (
err.message.includes("Cannot parse privateKey") &&
err.message.includes("no passphrase")
) {
sendLog(
"auth",
"error",
"SSH key is encrypted but no passphrase was provided",
);
isAwaitingAuthCredentials = true;
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
ws.send(
JSON.stringify({
type: "passphrase_required",
message:
"The SSH key is encrypted. Please enter the passphrase to unlock it.",
}),
);
return;
}
if (authFollowUp?.status === "retry") {
authRetries++;
sendLog("auth", "info", authFollowUp.message);
sshLogger.info("Retrying SSH auth with provider changes", {
operation: "ssh_auth_retry",
hostId: id,
userId,
});
clearTimeout(connectionTimeout);
connectionTimeout = setTimeout(
onConnectionTimeout,
(connectConfig.readyTimeout as number | undefined) ?? 120000,
);
Object.assign(connectConfig, authFollowUp.patch);
// ssh2's connect() ends an open socket and reconnects on close, keeping
// every listener attached, so the same client can be reused here.
isAuthRetrying = true;
sshConn.connect(connectConfig);
return;
}
if (authFollowUp?.status === "error") {
sendLog("auth", "error", authFollowUp.message);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
ws.send(
JSON.stringify({ type: "error", message: authFollowUp.message }),
);
return;
}
if (
authMethodNotAvailable &&
resolvedCredentials.authType === "none" &&
!isKeyboardInteractive
) {
sendLog(
"auth",
"error",
"Server does not support keyboard-interactive authentication",
);
isAwaitingAuthCredentials = true;
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
ws.send(
JSON.stringify({
type: "auth_method_not_available",
message:
"The server does not support keyboard-interactive authentication. Please provide credentials.",
}),
);
return;
}
if (
resolvedCredentials.authType === "none" &&
err.message.includes(
"All configured authentication methods failed",
) &&
!isKeyboardInteractive &&
!keyboardInteractiveResponded
) {
isAwaitingAuthCredentials = true;
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
ws.send(
JSON.stringify({
type: "auth_method_not_available",
message:
"The server does not support keyboard-interactive authentication. Please provide credentials.",
}),
);
return;
}
if (
isKeyboardInteractive &&
keyboardInteractiveFinish &&
err.message.includes("All configured authentication methods failed")
) {
sshLogger.warn(
"Authentication error during keyboard-interactive - SKIPPING cleanup, waiting for user response",
{
operation: "ssh_error_during_keyboard_interactive_skip_cleanup",
hostId: id,
error: err.message,
},
);
resetConnectionState();
return;
}
sshLogger.error("Proceeding with cleanup after error", {
operation: "ssh_error_cleanup",
hostId: id,
error: err.message,
});
if (
err.message.includes("authentication") ||
err.message.includes("Authentication")
) {
authLogger.error("SSH authentication failed", err, {
operation: "terminal_ssh_auth_failed",
sessionId,
userId,
hostId: id,
authType: resolvedCredentials.authType,
});
sendLog("auth", "error", `Authentication failed: ${err.message}`);
} else {
sendLog("error", "error", `Connection failed: ${err.message}`);
}
let errorMessage = "SSH error: " + err.message;
if (err.message.includes("No matching key exchange algorithm")) {
errorMessage =
"SSH error: No compatible key exchange algorithm found. This may be due to an older SSH server or network device.";
} else if (err.message.includes("No matching cipher")) {
errorMessage =
"SSH error: No compatible cipher found. This may be due to an older SSH server or network device.";
} else if (err.message.includes("No matching MAC")) {
errorMessage =
"SSH error: No compatible MAC algorithm found. This may be due to an older SSH server or network device.";
} else if (
err.message.includes("ENOTFOUND") ||
err.message.includes("ENOENT")
) {
errorMessage =
"SSH error: Could not resolve hostname or connect to server.";
} else if (err.message.includes("ECONNREFUSED")) {
errorMessage =
"SSH error: Connection refused. The server may not be running or the port may be incorrect.";
} else if (err.message.includes("ENETUNREACH")) {
const isIPv6 = ip && ip.includes(":");
errorMessage = isIPv6
? "SSH error: Network unreachable. IPv6 may not be available in this environment. If running in Docker, enable IPv6 in the Docker daemon and network configuration."
: "SSH error: Network unreachable. Check your network configuration and routing.";
} else if (err.message.includes("ETIMEDOUT")) {
errorMessage =
"SSH error: Connection timed out. Check your network connection and server availability.";
} else if (
err.message.includes("ECONNRESET") ||
err.message.includes("EPIPE")
) {
errorMessage =
"SSH error: Connection was reset. This may be due to network issues or server timeout.";
} else if (
err.message.includes("authentication failed") ||
err.message.includes("Permission denied")
) {
errorMessage =
"SSH error: Authentication failed. Please check your username and password/key.";
}
ws.send(JSON.stringify({ type: "error", message: errorMessage }));
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
}),
);
sshConn.on(
"close",
bind(() => {
// The +password retry ends the socket before reconnecting; that close is
// part of the retry, not a disconnect.
if (isAuthRetrying) {
return;
}
clearOnlineStatus?.();
clearOnlineStatus = null;
clearTimeout(connectionTimeout);
sshLogger.info("SSH connection closed", {
operation: "terminal_ssh_disconnected",
sessionId,
userId,
hostId: id,
});
if (isDuplicateConnDiscarded) {
cleanupAuthState(connectionTimeout);
return;
}
if (isAwaitingAuthCredentials) {
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
return;
}
if (isShellInitializing || (isConnected && !sshStream)) {
sshLogger.warn(
"SSH connection closed during shell initialization",
{
operation: "ssh_close_during_init",
hostId: id,
ip,
port,
username,
isShellInitializing,
hasStream: !!sshStream,
},
);
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "error",
message:
"Connection closed during shell initialization. The server may have rejected the shell request.",
}),
);
}
} else {
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "disconnected",
message: "Connection closed",
}),
);
}
}
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
}),
);
const sshAuthManager = new SSHAuthManager({
ssh: ctx.ssh,
log: sshLogger,
userId,
ws,
hostId: id || 0,
isKeyboardInteractive,
keyboardInteractiveResponded,
keyboardInteractiveFinish,
totpPromptSent,
browserSignInId,
totpTimeout,
browserSignInTimeout,
totpAttempts: 0,
});
sshConn.on(
"keyboard-interactive",
bind(
(
name: string,
instructions: string,
instructionsLang: string,
prompts: Array<{ prompt: string; echo: boolean }>,
finish: (responses: string[]) => void,
) => {
if (connectionTimeout) {
clearTimeout(connectionTimeout);
}
sshAuthManager.handleKeyboardInteractive(
name,
instructions,
instructionsLang,
prompts,
finish,
resolvedCredentials as unknown as Parameters<
typeof sshAuthManager.handleKeyboardInteractive
>[5],
// The resolved host carries the plugin host settings that
// keyboard-interactive handlers read.
connectTarget ?? hostConfig,
);
isKeyboardInteractive =
sshAuthManager.context.isKeyboardInteractive;
keyboardInteractiveResponded =
sshAuthManager.context.keyboardInteractiveResponded;
keyboardInteractiveFinish =
sshAuthManager.context.keyboardInteractiveFinish;
totpPromptSent = sshAuthManager.context.totpPromptSent;
browserSignInId = sshAuthManager.context.browserSignInId;
totpTimeout = sshAuthManager.context.totpTimeout;
browserSignInTimeout = sshAuthManager.context.browserSignInTimeout;
},
),
);
const effectiveAuthType =
resolvedCredentials.authType ||
(resolvedCredentials.key
? "key"
: resolvedCredentials.password
? "password"
: undefined);
if (!effectiveAuthType) {
sshLogger.error("No valid authentication method provided");
ws.send(
JSON.stringify({
type: "error",
message: "No valid authentication method provided",
}),
);
return;
}
connectTarget = {
...(resolvedHostData ?? {}),
id: serverHostId ?? id,
ip,
port,
username,
userId: hostConfig.userId,
authType: effectiveAuthType,
password: resolvedCredentials.password,
key: resolvedCredentials.key,
keyPassword: resolvedCredentials.keyPassword,
keyType: resolvedCredentials.keyType,
certPublicKey: resolvedCredentials.certPublicKey,
forceKeyboardInteractive: hostConfig.forceKeyboardInteractive,
sshOptions: hostConfig.sshOptions,
jumpHosts: hostConfig.jumpHosts,
useSocks5: hostConfig.useSocks5,
socks5Host: hostConfig.socks5Host,
socks5Port: hostConfig.socks5Port,
socks5Username: hostConfig.socks5Username,
socks5Password: hostConfig.socks5Password,
socks5ProxyChain: hostConfig.socks5ProxyChain,
portKnockSequence: hostConfig.portKnockSequence,
} as PluginSshHost;
const built = await ctx.ssh.prepare(connectTarget, {
purpose: "terminal",
profile: "terminal",
client: sshConn,
serverHostId: serverHostId ?? undefined,
hostKeySocket: ws,
interactive: true,
log: (level, message) => sendLog("auth", level, message),
});
const connectConfig = built.config;
// DNS was resolved above, and skipped when jump hosts do it instead.
connectConfig.host = connectHost;
prepared = built;
if (built.outcome.status !== "ready") {
const outcome = built.outcome;
if (outcome.status === "interaction-required") {
ws.send(
JSON.stringify({
type: `${outcome.interaction}_auth_required`,
hostId: id,
}),
);
return;
}
if (
outcome.status === "error" &&
outcome.code === "passphrase-required"
) {
sendLog(
"auth",
"error",
"SSH key is encrypted but no passphrase was provided",
);
isAwaitingAuthCredentials = true;
cleanupAuthState(connectionTimeout);
ws.send(
JSON.stringify({
type: "passphrase_required",
message: outcome.message,
}),
);
return;
}
sshLogger.error("SSH auth could not be prepared", {
operation: "terminal_auth_prepare",
hostId: id,
authType: effectiveAuthType,
error: outcome.message,
});
sendLog("auth", "error", outcome.message);
ws.send(JSON.stringify({ type: "error", message: outcome.message }));
return;
}
if (hostConfig.sshOptions?.agentForwarding) {
if (connectConfig.privateKey) {
try {
const parsed = ssh2Utils.parseKey(
connectConfig.privateKey as Buffer,
connectConfig.passphrase as string | undefined,
);
if (parsed && !(parsed instanceof Error)) {
connectConfig.agent = new MemoryAgent(parsed);
connectConfig.agentForward = true;
sendLog("auth", "info", "SSH agent forwarding enabled");
}
} catch {
sshLogger.warn("Failed to set up agent forwarding", {
operation: "agent_forward_setup",
hostId: id,
});
}
} else if (connectConfig.agent) {
connectConfig.agentForward = true;
sendLog(
"auth",
"info",
"SSH agent forwarding enabled (external agent)",
);
}
}
const proxyChain = Array.isArray(connectTarget.socks5ProxyChain)
? connectTarget.socks5ProxyChain
: [];
const viaProxy =
!!connectTarget.useSocks5 &&
(!!connectTarget.socks5Host || proxyChain.length > 0);
// A jump host asking for TOTP or input uses the same messages as the
// target host, answered through totp_response / password_response.
const jumpHostPrompt: PluginSshPromptChannel = {
ask: (request) =>
new Promise((resolve) => {
if (
(request.kind !== "totp" && request.kind !== "input") ||
ws.readyState !== WebSocket.OPEN
) {
resolve(null);
return;
}
if (totpTimeout) clearTimeout(totpTimeout);
totpTimeout = setTimeout(() => {
keyboardInteractiveFinish = null;
totpPromptSent = false;
resolve(null);
}, 180000);
keyboardInteractiveFinish = (answers) =>
resolve((answers[0] ?? "").trim());
if (request.kind === "totp") {
totpPromptSent = true;
sendLog("auth", "info", "Jump host TOTP verification required");
ws.send(
JSON.stringify(
request.retry
? { type: "totp_retry" }
: { type: "totp_required", prompt: request.prompt },
),
);
} else {
ws.send(
JSON.stringify({
type: "password_required",
prompt: request.prompt,
echo: request.echo,
}),
);
}
}),
};
let transport: Awaited<ReturnType<typeof ctx.ssh.openTransport>>;
try {
transport = await ctx.ssh.openTransport(connectTarget, connectConfig, {
// Resolved above, or deliberately left to the jump host.
resolveDns: false,
log: (level, message) => sendLog("handshake", level, message),
prompt: jumpHostPrompt,
});
} catch (error) {
sshLogger.error("SSH transport failed", error, {
operation: "ssh_transport",
hostId: id,
ip,
port,
});
ws.send(
JSON.stringify({
type: "error",
message:
error instanceof Error && error.name === "JumpHostChainError"
? `Failed to connect through jump hosts: ${error.message}`
: getErrorMessage(error),
}),
);
if (currentSessionId) {
sessionManager.destroySession(currentSessionId);
currentSessionId = null;
}
cleanupAuthState(connectionTimeout);
return;
}
lastJumpClient = transport.jumpClient as SSHClientType | null;
sendLog(
"handshake",
"info",
transport.via === "jump"
? "Starting SSH session through jump host" +
(viaProxy ? " (via proxy)" : "")
: transport.via === "proxy"
? "Starting SSH session (via proxy)"
: "Starting SSH session",
);
sendLog("auth", "info", `Authenticating as ${username}`);
sshLogger.info("Initiating SSH connection", {
operation: "terminal_ssh_connect_attempt",
sessionId,
userId,
hostId: id,
ip,
port,
username,
authType: resolvedCredentials.authType,
via: transport.via,
});
sshConn.connect(connectConfig);
}
function handleResize(data: ResizeData) {
const cols = toTerminalDimension(data?.cols);
const rows = toTerminalDimension(data?.rows);
if (!cols || !rows) return;
const resizeStream =
sessionManager.getSession(currentSessionId)?.sshStream ?? sshStream;
if (resizeStream && resizeStream.setWindow) {
resizeStream.setWindow(rows, cols, rows, cols);
const session = sessionManager.getSession(currentSessionId);
if (session) {
sessionManager.resizeSession(session.id, cols, rows);
}
}
}
function cleanupAuthState(timeoutId?: NodeJS.Timeout) {
if (timeoutId) {
clearTimeout(timeoutId);
}
if (totpTimeout) {
clearTimeout(totpTimeout);
totpTimeout = null;
}
if (browserSignInTimeout) {
clearTimeout(browserSignInTimeout);
browserSignInTimeout = null;
}
sshStream = null;
sshConn = null;
lastJumpClient = null;
resetConnectionState();
isCleaningUp = false;
isAwaitingAuthCredentials = false;
}
// Note: PTY-level keepalive (writing \x00 to the stream) was removed.
// It was causing ^@ characters to appear in terminals with echoctl enabled.
// SSH-level keepalive is configured via connectConfig (keepaliveInterval,
// keepaliveCountMax, tcpKeepAlive), which handles connection health monitoring
// without producing visible output on the terminal.
//
// See: https://github.com/Termix-SSH/Support/issues/232
// See: https://github.com/Termix-SSH/Support/issues/309
}
return { handleConnection, closeAll };
}