Skip to content

Relay Linux desktops over a private socket and notice them without a restart (851-4484) - #1485

Merged
pondorasti merged 5 commits into
mainfrom
alexandru/851-4484-linux-desktop-step-1-relay-over-a-private
Nov 26, 2026
Merged

pondorasti merged 5 commits into
mainfrom
alexandru/851-4484-linux-desktop-step-1-relay-over-a-private

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4484
Part of 851-4479 (Linux desktops: screen sharing that works after installing codevisor-server). Step 1 of the approved design.

What changes

  • Private socket instead of a local port. The VNC relay connects to the Unix socket named in screen-sharing.json ({ "vnc": { "socket", "display", "xauthority", ... } }). It never connects to a port: any account on the machine can dial 127.0.0.1:5901.
  • Old configs are not silent. A config that names a port and no socket (a desktop set up for an older Codevisor) stays listed, and viewers are told "This machine's desktop was set up for an older version of Codevisor. Set it up again to share it." The server logs one line when it starts seeing such a config. A vnc section with no usable socket that isn't such an old config is not offered, and is logged too. Replacing an old desktop is step 2.
  • The socket has to be the server's own. Before every connection, and when a viewer asks what it can view, the server checks that the path is a socket, owned by the account the server runs as, closed to group and others, with every folder above it owned by that account or root and closed to writes by others (or sticky like /tmp) (routes/private-socket.ts). Links in the folder path are resolved first and the checked path is the one dialled. If not, nothing is relayed and the viewer is told which it is: "The desktop on this machine isn't running." (nothing at the path), "The desktop on this machine isn't run by the account Codevisor runs as, so Codevisor won't share it." (not a socket, another account's socket, or a path the server is denied access to: the old root-desktop case), or "Other accounts on this machine can reach its desktop, so Codevisor won't share it." (the socket or a folder above it is open to others).
  • The server log says why. When a desktop isn't shared, the log names the path and what is wrong with it, for example [desktop] not shared: /home/dev/.codevisor can be changed by other accounts (mode 775, belongs to uid 1000). It is written when the reason changes, not on every request.
  • No restart to notice a desktop. The config is read on every request (once per /v1/info, so the two features it lists can't disagree). /v1/info lists screen-sharing-v1 only while the config names a desktop, and a request to a machine with no desktop answers "This machine has no desktop to share".
  • Dev container desktop. Xvnc listens on a socket in the container's runtime folder with -rfbport -1 -localhost (no TCP port), and X11 itself needs a cookie (-auth). The script then asks ss, twice a second apart, whether anything listens on the desktop's TCP port on any address. The config is written last, and an exit trap cleans up unless the script got that far: whatever stops it (an open port, ss failing, the cookie, Xvnc, D-Bus, the config write), Xvnc and D-Bus are stopped and the socket, cookie and config are removed. The server gets the cookie through the config and its environment, so the clipboard bridge, 2× scaling and Computer Use keep working.
  • scripts/vnc-desktop.sh is deleted. It could only write the port config the server no longer reads. Its replacement is step 2 (851-4485, codevisor desktop setup).
  • A bug found on the way: a viewer's setScale request was rejected with "Invalid Screen Sharing display" before it reached the desktop, because the route only accepted Mac display ids. So 2× scaling of Linux desktops has not worked since the live picture-in-picture change (606acbd). The route now accepts the desktop's own display id.

apps/server/README.md documents the config and the check.

What a viewer sees

Nothing new. The display id changes from vnc:<port> to vnc:desktop. A Mac that remembers vnc:5901 for the machine opens vnc:desktop by itself: the connection picks the remembered display only if the machine still offers it, otherwise the first offered one, and the socket and scale requests use the offered id. A new Swift test shows this (ScreenSharingVNCRememberedDisplayTests). No Swift source changes.

One gap left for step 2: the apps re-read a machine's features when they reconnect or come to the foreground, so a desktop that appears while the app is open and connected shows up at the next of those, not at once.

Tests

In apps/server unless noted, and they fail without the change:

  • private-socket.test.ts: a private socket passes, also inside a sticky shared folder and through a linked folder (the real path is dialled). Refused as missing: nothing at the path. Refused as not this account's: another owner, a regular file, a link to a socket, a folder the server may not look into. Refused as exposed: modes 660/606/666, a folder others can write to at any level.
  • screen-sharing-vnc.test.ts: the relay tests now run against a real Unix socket and the real config file. New: a socket opened to others is not dialled and the viewer is told why; a desktop is offered once its config is written and withdrawn once it is removed, on one running server; a config that names a port stays listed, answers "set it up again", is logged once and is never dialled; a stray file at the socket path gets the "isn't run by the account" message; setScale with the desktop's display id reaches the desktop (400 before the fix).
  • vnc-desktops.test.ts: scaling uses the desktop's own display and cookie; 2× is offered only for an Xfce desktop whose display is known.

Checked by hand

In the dev container with CODEVISOR_DEV_CONTAINER_DESKTOP=1 bun run dev:linux, against the container's real server:

  • The only listening TCP port in the container is the server's own; Xvnc runs with the socket (mode 600, folder 700) and the cookie; X11 refuses a client without the cookie.

  • /v1/info lists screen sharing, capabilities answer available, and the WebSocket relays the RFB 003.008 greeting.

  • With the server left running: moving the config away removes the feature and answers 501; moving it back restores both; chmod 666 on the socket gives the "other accounts" message and closes the socket with 1011 (checked before the close reason carried the message); chmod 600 restores it.

  • setScale 2 and 1 through the server change Xfce's UI scale (read back with xfconf-query).

  • In the Mac dev app: the inspector's Desktop card shows the live desktop, the Desktop window opens, and a click in it launched the terminal in the container.

  • scripts/dev-container-desktop.sh on its own in a throwaway node:22-bookworm container, thirteen runs: a normal start (no listening TCP socket at all); something listening on 127.0.0.1:5901; something listening on [::1]:5901 only; a port that opens just after Xvnc is up; ss failing; xauth failing; mcookie failing; Xvnc unable to start; dbus-launch failing; a config folder that can't be created; a read-only config folder; and a normal start after all that. Every failing run printed its own reason, exited 1, left no Xvnc process, no D-Bus daemon, no socket, no cookie and no config.

Desktop card in the Mac dev app

Desktop window after a click in the viewer opened the terminal

Not checked by hand: typing and clipboard copy/paste through the viewer (the server's clipboard watcher was running against the desktop), Computer Use on the container desktop, the iPhone, and any machine outside the dev container.

Known limits, left as they are

  • "Without a restart" holds for new connections. A viewer that is already connected keeps streaming after the config is removed or the socket stops being private, until it disconnects. Closing live relays then is part of step 2 (851-4485).

  • The folder rule refuses a folder a group can write to. On Fedora and RHEL (own group per account, umask 002) a home-made folder is mode 775 and is refused even though the group has one member; the log names the folder. Whether to relax that is a step 3 question, noted in the design doc. A socket under /run/user/<uid> is unaffected.

  • A socket file left behind by an Xvnc that was killed still passes the check, so capabilities answer available and the viewer's connection then closes with "VNC server unavailable". Telling a dead socket from a live one means connecting to it, which is the relay's job.

  • The config is read from disk, synchronously, on every /v1/info and screen sharing request. It is one small local file next to the server's database. If it ever shows up in a profile, cache it on the file's modification time.

Reviews

First code review (medium), fixed with a test or a run: the dev container's Xvnc had lost -localhost; the socket check looked only at the socket's own folder; the script's start-up check ignored a missing X11 socket; a refused connection always said "VNC server unavailable".

Review of 73a3557, fixed:

  1. The script left Xvnc running (no password) when its TCP check failed. Every failure path now stops Xvnc and removes the socket and cookie.
  2. The TCP check could pass without checking (bash or /dev/tcp missing looked like a closed port) and only probed IPv4. It now uses ss, on any address, and fails closed when ss can't answer. iproute2 is installed with the desktop.
  3. An old port-only config lost its desktop silently. It now stays listed with a reason, and is logged once.
  4. The socket check gave wrong explanations (a file, a link or another account's socket read as "exposed"; an unreadable folder as "missing"). It now tells missing, not this account's, and exposed apart.
  5. The "couldn't write the desktop's X11 cookie" message was unreachable. It is reached now, for xauth and mcookie failures.

Code review (medium) of that delta (8382c5e), fixed:

  • The TCP check ran once, the moment Xvnc's sockets appeared. It runs again a second later.
  • A failed start left a D-Bus daemon behind. D-Bus now starts after the checks.
  • A config with a mistyped socket next to a leftover port, or an invalid port, was taken for an old config. Only "no socket and a valid port" is.
  • Any filesystem error other than "not there" read as another account's desktop. Only a denied path does.
  • An old config also listed computer-use-stream-v1. It lists screen-sharing-v1 only.
  • The old-config log line was written once per server process. It is written each time such a config starts being seen.

Not fixed, as agreed: the stale socket file case under "Known limits".

Review of e67642a:

  1. Checked, not a problem: a viewer holding a saved vnc:5901 opens the offered vnc:desktop. Shown by a Swift test.
  2. /v1/info looked at the config twice; it looks once.
  3. A valid port next to an unusable socket vanished silently; it is logged.
  4. The log names the folder that fails the check, with its mode and owner. README and design doc note the strictness.
  5. The script cleans up through an exit trap.
  6. README says "without a restart" is about new connections; closing live relays is on 851-4485.

🤖 Generated with Claude Code

@pondorasti
pondorasti force-pushed the alexandru/851-4484-linux-desktop-step-1-relay-over-a-private branch 2 times, most recently from 73a3557 to 8382c5e Compare November 26, 2026 05:21
pondorasti and others added 5 commits November 25, 2026 21:43
…restart (851-4484)

The server's VNC relay connects to the Unix socket named in
screen-sharing.json instead of a localhost port, and only when that
socket is private to the server's own account. The config is read on
every request, so a desktop appears or goes away while the server keeps
running. A config that names a port is ignored.

The dev container's desktop listens on a socket with no TCP port and
locks X11 with a cookie. scripts/vnc-desktop.sh wrote the port config
and is gone.

A viewer's request to set a desktop's scale was rejected before it
reached the desktop; it is accepted for the desktop's own display.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…r's VNC off TCP

Review follow-ups for 851-4484. The socket check walks every folder up to
the root and dials the path it checked, so a folder higher up that others
can write to no longer passes. A refused connection's close reason says
whether the desktop isn't running or is reachable by other accounts.

The dev container's Xvnc keeps -localhost next to -rfbport -1, and the
script stops without writing a config when a TCP port is open or the X11
socket is missing.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…nc when its checks fail

Review follow-ups for 851-4484.

A desktop whose config names a local port (set up for an older
Codevisor) stays listed and tells viewers it has to be set up again,
with one log line; the server never connects to the port.

The socket check tells three things apart: nothing there, something that
isn't this account's socket (a file, a link, another account's socket,
or a path the server may not look at), and a socket or folder that other
accounts can open or change.

The dev container's desktop script stops Xvnc and removes its socket and
cookie on every failure path, checks the TCP port with ss on any address
and fails closed when ss can't answer, and reports a cookie that
couldn't be written.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ner's TCP port twice

Second review follow-ups for 851-4484.

Only a config with no socket and a valid port is treated as set up for
an older Codevisor. It no longer lists computer-use-stream-v1, and the
log line is written each time such a config starts being seen. Only a
denied path reads as another account's desktop; a path that can't be
resolved reads as not running.

The dev container's desktop script checks the TCP port again a second
after Xvnc is up, and starts D-Bus only once the checks have passed, so a
failed start leaves no daemon behind.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…desktop on any failed start

Third review follow-ups for 851-4484.

The server log names the path that keeps a desktop from being shared and
what is wrong with it (for example a folder a group can write to, with
its mode and owner), and a vnc section with no usable socket is logged
instead of vanishing. Both are logged when the reason changes. /v1/info
looks at the desktop once and derives both features from that look.

The dev container's desktop script cleans up through an exit trap unless
it reached its last line, reports a D-Bus session that didn't start, and
writes the config last.

A test shows a viewer that remembers the old display id (vnc:5901)
opening the display the machine offers now.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4484-linux-desktop-step-1-relay-over-a-private branch from e67642a to 0117fe5 Compare November 26, 2026 05:47
@pondorasti
pondorasti merged commit bcd7dd8 into main Nov 26, 2026
@pondorasti
pondorasti deleted the alexandru/851-4484-linux-desktop-step-1-relay-over-a-private branch November 26, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant