Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Remote desktop: WebRTC only, with ICE over TCP, TURN over TCP/TLS, ICE restarts and clear errors

Federico Pereira43420b6bd4e0
diff --git a/apps/settings/Sharing.jsx b/apps/settings/Sharing.jsx
index e813c44..3db09cb 100644
--- a/apps/settings/Sharing.jsx
+++ b/apps/settings/Sharing.jsx
@@ -4,7 +4,7 @@
// server is installed, the first time, with the package engine's own pkg-install action.
import {
View, Text, Button, Toggle, Segmented, TextInput, useTheme, useLocale, useStore, admin, remote, remoteViewers, sshLogin, remoteCidr,
- remoteMinutesLeft, remoteVideo, remoteIceServer, statusText, useSystemInfo, spacing, typeScale,
+ remoteMinutesLeft, remoteVideo, remoteIceServer, remoteFingerprint, statusText, useSystemInfo, spacing, typeScale,
} from '@lordos/sdk';
import { useEffect, useState } from 'react';
import { Stack, Section, SettingRow, Note, Message, StatusDot, tint } from './ui.jsx';
@@ -103,69 +103,78 @@ function AccessSection({ st, isAdmin, busy, act }) {
);
}
-// The video path (docs/REMOTE.md, "Video (WebRTC)"): on by default; the built-in STUN server covers the
-// local network; external STUN and a TURN relay are for connecting across networks. The TURN password
-// goes to the admin helper once and is never read back (only "saved" is shown).
-function VideoSection({ st, isAdmin, busy, run }) {
+// The WebRTC connection (docs/REMOTE.md, "Video (WebRTC)"), the only way the screen reaches a browser:
+// the built-in STUN server and ICE over TCP on the remote desktop port cover the local network and VPNs;
+// external STUN and a TURN relay (over UDP, TCP or TLS) are for connecting across networks. The TURN
+// password goes to the admin helper once and is never read back (only "saved" is shown).
+function ConnectionSection({ st, isAdmin, busy, run }) {
const t = useTheme();
const { t: tr } = useLocale();
const [stun, setStun] = useState(st.stun.join(', '));
const [turn, setTurn] = useState(st.turn);
const [user, setUser] = useState(st.turnUser);
const [pass, setPass] = useState('');
+ const [transport, setTransport] = useState(st.turnTransport);
+ const [fp, setFp] = useState(st.turnFp);
const [bad, setBad] = useState('');
- useEffect(() => { setStun(st.stun.join(', ')); setTurn(st.turn); setUser(st.turnUser); }, [st.stun.join(','), st.turn, st.turnUser]);
+ useEffect(() => { setStun(st.stun.join(', ')); setTurn(st.turn); setUser(st.turnUser); setTransport(st.turnTransport); setFp(st.turnFp); },
+ [st.stun.join(','), st.turn, st.turnUser, st.turnTransport, st.turnFp]);
const list = stun.split(',').map((x) => x.trim()).filter(Boolean);
const stunOk = list.length <= 4 && list.every((x) => remoteIceServer(x));
+ const fpOk = transport !== 'tls' || !fp.trim() || remoteFingerprint(fp);
const turnOk = !turn.trim() || (remoteIceServer(turn) && /^[\x21-\x7e]{1,128}$/.test(user) && !/["\\]/.test(user) && (pass || st.turnKey));
const saveStun = () => { if (!stunOk) { setBad('stun'); return; } setBad(''); run(() => remoteVideo.setStun(list)); };
const saveTurn = () => {
if (!turnOk) { setBad('turn'); return; }
+ if (!fpOk) { setBad('fp'); return; }
setBad('');
- run(() => (turn.trim() ? remoteVideo.setTurn(turn.trim(), user, pass) : remoteVideo.clearTurn())).then(() => setPass(''));
+ run(() => (turn.trim() ? remoteVideo.setTurn(turn.trim(), user, pass, transport, transport === 'tls' ? fp.trim() : '') : remoteVideo.clearTurn())).then(() => setPass(''));
};
const label = { fontSize: typeScale.caption, color: t.textDim, textAlign: 'start' };
- const err = (k) => (bad === k ? <Text style={{ fontSize: typeScale.caption, color: tint.error, textAlign: 'start' }}>{tr(k === 'stun' ? 'errors.remote.badIceServer' : 'errors.remote.badTurnUser')}</Text> : null);
+ const msgKey = { stun: 'errors.remote.badIceServer', turn: 'errors.remote.badTurnUser', fp: 'errors.remote.badTurnFingerprint' };
+ const err = (k) => (bad === k ? <Text style={{ fontSize: typeScale.caption, color: tint.error, textAlign: 'start' }}>{tr(msgKey[k])}</Text> : null);
const off = !isAdmin || busy;
return (
<>
- <Section title={tr('settings.sharing.video')}>
- <SettingRow label={tr('settings.sharing.videoToggle')} hint={tr(st.video ? 'settings.sharing.videoHint' : 'settings.sharing.videoOffHint')}>
- <Toggle small on={st.video} onChange={(on) => !off && run(() => remoteVideo.setVideo(on))} />
- </SettingRow>
+ <Section title={tr('settings.sharing.video')} footer={tr('settings.sharing.videoFooter', { port: st.port })}>
+ <SettingRow label={tr('settings.sharing.videoRow')} hint={tr('settings.sharing.videoHint')} />
</Section>
- {st.video && (
- <Section title={tr('settings.sharing.stun')} footer={tr('settings.sharing.stunHint')}>
- <View style={{ paddingVertical: spacing.s, gap: 4 }}>
- <View style={{ flexDirection: 'row', alignItems: 'center', gap: spacing.s }}>
- <TextInput value={stun} placeholder={tr('settings.sharing.stunPlaceholder')} style={{ flex: 1, direction: 'ltr' }}
- onChange={(v) => { setStun(v.slice(0, 400)); setBad(''); }} onSubmit={saveStun} />
- {isAdmin && <Button small label={tr('settings.sharing.save')} disabled={off} onClick={saveStun} />}
- </View>
- {err('stun')}
- </View>
- </Section>
- )}
- {st.video && (
- <Section title={tr('settings.sharing.turn')} footer={tr('settings.sharing.turnFooter')}>
- <View style={{ paddingVertical: spacing.s, gap: 6 }}>
- <Text style={label}>{tr('settings.sharing.turnServer')}</Text>
- <TextInput value={turn} placeholder={tr('settings.sharing.turnServerPlaceholder')} style={{ direction: 'ltr' }} onChange={(v) => { setTurn(v.slice(0, 95)); setBad(''); }} />
- <Text style={label}>{tr('settings.sharing.turnUser')}</Text>
- <TextInput value={user} style={{ direction: 'ltr' }} onChange={(v) => { setUser(v.slice(0, 128)); setBad(''); }} />
- <Text style={label}>{tr('settings.sharing.turnPass')}</Text>
- <TextInput secure value={pass} placeholder={st.turnKey ? tr('settings.sharing.turnPassSaved') : ''} style={{ direction: 'ltr' }}
- onChange={(v) => { setPass(v.slice(0, 128)); setBad(''); }} onSubmit={saveTurn} />
- {err('turn')}
- {isAdmin && (
- <View style={{ flexDirection: 'row', gap: spacing.s, justifyContent: 'flex-end' }}>
- {st.turn ? <Button small label={tr('settings.sharing.turnRemove')} disabled={off} onClick={() => run(() => remoteVideo.clearTurn())} /> : null}
- <Button small label={tr('settings.sharing.save')} disabled={off} onClick={saveTurn} />
- </View>
- )}
+ <Section title={tr('settings.sharing.stun')} footer={tr('settings.sharing.stunHint')}>
+ <View style={{ paddingVertical: spacing.s, gap: 4 }}>
+ <View style={{ flexDirection: 'row', alignItems: 'center', gap: spacing.s }}>
+ <TextInput value={stun} placeholder={tr('settings.sharing.stunPlaceholder')} style={{ flex: 1, direction: 'ltr' }}
+ onChange={(v) => { setStun(v.slice(0, 400)); setBad(''); }} onSubmit={saveStun} />
+ {isAdmin && <Button small label={tr('settings.sharing.save')} disabled={off} onClick={saveStun} />}
</View>
- </Section>
- )}
+ {err('stun')}
+ </View>
+ </Section>
+ <Section title={tr('settings.sharing.turn')} footer={tr('settings.sharing.turnFooter')}>
+ <View style={{ paddingVertical: spacing.s, gap: 6 }}>
+ <Text style={label}>{tr('settings.sharing.turnServer')}</Text>
+ <TextInput value={turn} placeholder={tr('settings.sharing.turnServerPlaceholder')} style={{ direction: 'ltr' }} onChange={(v) => { setTurn(v.slice(0, 95)); setBad(''); }} />
+ <Text style={label}>{tr('settings.sharing.turnTransport')}</Text>
+ <Segmented small value={transport} onChange={(v) => { setTransport(v); setBad(''); }}
+ options={[['udp', tr('settings.sharing.turnUdp')], ['tcp', tr('settings.sharing.turnTcp')], ['tls', tr('settings.sharing.turnTls')]]} />
+ {transport === 'tls' && <Text style={label}>{tr('settings.sharing.turnFp')}</Text>}
+ {transport === 'tls' && <TextInput value={fp} placeholder={tr('settings.sharing.turnFpPlaceholder')} style={{ direction: 'ltr', fontFamily: 'monospace' }}
+ onChange={(v) => { setFp(v.slice(0, 95)); setBad(''); }} />}
+ {transport === 'tls' && <Text style={label}>{tr('settings.sharing.turnFpHint')}</Text>}
+ <Text style={label}>{tr('settings.sharing.turnUser')}</Text>
+ <TextInput value={user} style={{ direction: 'ltr' }} onChange={(v) => { setUser(v.slice(0, 128)); setBad(''); }} />
+ <Text style={label}>{tr('settings.sharing.turnPass')}</Text>
+ <TextInput secure value={pass} placeholder={st.turnKey ? tr('settings.sharing.turnPassSaved') : ''} style={{ direction: 'ltr' }}
+ onChange={(v) => { setPass(v.slice(0, 128)); setBad(''); }} onSubmit={saveTurn} />
+ {err('turn')}
+ {err('fp')}
+ {isAdmin && (
+ <View style={{ flexDirection: 'row', gap: spacing.s, justifyContent: 'flex-end' }}>
+ {st.turn ? <Button small label={tr('settings.sharing.turnRemove')} disabled={off} onClick={() => run(() => remoteVideo.clearTurn())} /> : null}
+ <Button small label={tr('settings.sharing.save')} disabled={off} onClick={saveTurn} />
+ </View>
+ )}
+ </View>
+ </Section>
</>
);
}
@@ -255,7 +264,7 @@ export function SharingSettings() {
(r) => { setMsg({ ok: true, text: ok || statusText(r) }); setTimeout(() => setSt(remote.status()), 300); return true; },
(e) => { setMsg({ ok: false, text: e.message }); return false; }).finally(() => setBusy(false));
};
- // an admin call from a section with its own fields (the video settings)
+ // an admin call from a section with its own fields (the connection settings)
const run = (fn) => {
setBusy(true);
setMsg(null);
@@ -293,7 +302,7 @@ export function SharingSettings() {
{!st.installed && <Note tone="warn">{tr('settings.sharing.notInstalled')}</Note>}
<Message msg={msg} />
<AccessSection st={st} isAdmin={isAdmin} busy={busy} act={act} />
- <VideoSection st={st} isAdmin={isAdmin} busy={busy} run={run} />
+ <ConnectionSection st={st} isAdmin={isAdmin} busy={busy} run={run} />
<Section title={tr('settings.sharing.connected')} footer={tr('settings.sharing.security')}>
{!st.clients.length ? <SettingRow label={tr('settings.sharing.nobody')} labelStyle={{ color: t.textDim }} /> : st.clients.map((c) => (
<SettingRow key={c.pid} icon={<StatusDot color={tint.ok} />} label={`${c.user} · ${c.ip}`}
diff --git a/assets/locales/en/errors.json b/assets/locales/en/errors.json
index 25676ab..6a4b648 100644
--- a/assets/locales/en/errors.json
+++ b/assets/locales/en/errors.json
@@ -569,5 +569,6 @@
"remote.listFull": "The list is full (64 entries)",
"remote.badIceServer": "Not a valid server: use name:port, like stun.example.org:3478 (at most 4)",
"remote.badTurnUser": "Not a valid relay user name (no spaces, at most 128 characters)",
- "remote.badTurnPassword": "Not a valid relay password (at most 128 characters, no line breaks)"
+ "remote.badTurnPassword": "Not a valid relay password (at most 128 characters, no line breaks)",
+ "remote.badTurnFingerprint": "Not a valid certificate fingerprint: 64 hexadecimal digits (pairs may be separated by colons)"
}
diff --git a/assets/locales/en/settings.json b/assets/locales/en/settings.json
index 3c6c13f..8f157d2 100644
--- a/assets/locales/en/settings.json
+++ b/assets/locales/en/settings.json
@@ -326,10 +326,8 @@
"sharing.blocklistFooter": "Always refused, before anything else, until you remove them.",
"sharing.listEmpty": "The list is empty.",
"sharing.blockForever": "Block permanently",
- "sharing.video": "Video",
- "sharing.videoToggle": "Stream as video (WebRTC)",
- "sharing.videoHint": "Smoother and sharper than images, with sound. Falls back to images when the browser or the network can’t.",
- "sharing.videoOffHint": "The browser gets the screen as images.",
+ "sharing.video": "Connection",
+ "sharing.videoHint": "The screen, its sound and your input travel encrypted over WebRTC: the only way the remote desktop works.",
"sharing.stun": "STUN servers",
"sharing.stunPlaceholder": "stun.example.org:3478",
"sharing.stunHint": "This computer answers STUN itself, which is enough on the local network. Add a public server only to connect across networks (up to 4, separated by commas).",
@@ -342,5 +340,14 @@
"sharing.turnRemove": "Remove relay",
"sharing.turnFooter": "Only when neither side can reach the other directly. The relay only sees encrypted video and input; its password stays on this computer, readable only by the system.",
"sharing.save": "Save",
- "sharing.noneSet": "None"
+ "sharing.noneSet": "None",
+ "sharing.videoRow": "Video over WebRTC",
+ "sharing.videoFooter": "Works across the local network and VPNs over UDP, or over TCP port {port} where UDP is blocked. To connect across networks, add a STUN server or a relay below.",
+ "sharing.turnTransport": "Reach the relay over",
+ "sharing.turnUdp": "UDP",
+ "sharing.turnTcp": "TCP",
+ "sharing.turnTls": "TLS",
+ "sharing.turnFp": "Certificate fingerprint (optional)",
+ "sharing.turnFpPlaceholder": "SHA-256, like 3A:7F:…",
+ "sharing.turnFpHint": "Empty: the relay’s certificate must be valid for its name. For a relay with its own certificate, paste its SHA-256 fingerprint here."
}
diff --git a/assets/locales/es/errors.json b/assets/locales/es/errors.json
index 2047d66..b7a5158 100644
--- a/assets/locales/es/errors.json
+++ b/assets/locales/es/errors.json
@@ -569,5 +569,6 @@
"remote.listFull": "La lista está llena (64 entradas)",
"remote.badIceServer": "No es un servidor válido: usá nombre:puerto, como stun.ejemplo.org:3478 (como mucho 4)",
"remote.badTurnUser": "No es un usuario de relé válido (sin espacios, hasta 128 caracteres)",
- "remote.badTurnPassword": "No es una contraseña de relé válida (hasta 128 caracteres, sin saltos de línea)"
+ "remote.badTurnPassword": "No es una contraseña de relé válida (hasta 128 caracteres, sin saltos de línea)",
+ "remote.badTurnFingerprint": "No es una huella de certificado válida: 64 dígitos hexadecimales (los pares pueden ir separados por dos puntos)"
}
diff --git a/assets/locales/es/settings.json b/assets/locales/es/settings.json
index 8c18078..ba34e60 100644
--- a/assets/locales/es/settings.json
+++ b/assets/locales/es/settings.json
@@ -326,10 +326,8 @@
"sharing.blocklistFooter": "Siempre rechazadas, antes que cualquier otra regla, hasta que las quites.",
"sharing.listEmpty": "La lista está vacía.",
"sharing.blockForever": "Bloquear siempre",
- "sharing.video": "Video",
- "sharing.videoToggle": "Transmitir como video (WebRTC)",
- "sharing.videoHint": "Más fluido y nítido que con imágenes, y con sonido. Vuelve a imágenes cuando el navegador o la red no pueden.",
- "sharing.videoOffHint": "El navegador recibe la pantalla como imágenes.",
+ "sharing.video": "Conexión",
+ "sharing.videoHint": "La pantalla, su sonido y lo que escribís viajan cifrados por WebRTC: es la única forma en que funciona el escritorio remoto.",
"sharing.stun": "Servidores STUN",
"sharing.stunPlaceholder": "stun.ejemplo.org:3478",
"sharing.stunHint": "Esta computadora responde STUN por sí misma, y en la red local alcanza. Agregá un servidor público solo para conectarte entre redes (hasta 4, separados por comas).",
@@ -342,5 +340,14 @@
"sharing.turnRemove": "Quitar el relé",
"sharing.turnFooter": "Solo cuando ninguno de los dos lados puede llegar al otro directamente. El relé solo ve video y entrada cifrados; su contraseña queda en esta computadora y solo el sistema puede leerla.",
"sharing.save": "Guardar",
- "sharing.noneSet": "Ninguno"
+ "sharing.noneSet": "Ninguno",
+ "sharing.videoRow": "Video por WebRTC",
+ "sharing.videoFooter": "Funciona en la red local y por VPN con UDP, o por el puerto TCP {port} donde UDP está bloqueado. Para conectarse entre redes distintas, agregá un servidor STUN o un relé abajo.",
+ "sharing.turnTransport": "Llegar al relé por",
+ "sharing.turnUdp": "UDP",
+ "sharing.turnTcp": "TCP",
+ "sharing.turnTls": "TLS",
+ "sharing.turnFp": "Huella del certificado (opcional)",
+ "sharing.turnFpPlaceholder": "SHA-256, como 3A:7F:…",
+ "sharing.turnFpHint": "Vacío: el certificado del relé tiene que ser válido para su nombre. Para un relé con certificado propio, pegá acá su huella SHA-256."
}
diff --git a/assets/locales/pt/errors.json b/assets/locales/pt/errors.json
index 7096b0f..29f6794 100644
--- a/assets/locales/pt/errors.json
+++ b/assets/locales/pt/errors.json
@@ -569,5 +569,6 @@
"remote.listFull": "A lista está cheia (64 entradas)",
"remote.badIceServer": "Não é um servidor válido: use nome:porta, como stun.exemplo.org:3478 (no máximo 4)",
"remote.badTurnUser": "Não é um usuário de retransmissor válido (sem espaços, até 128 caracteres)",
- "remote.badTurnPassword": "Não é uma senha de retransmissor válida (até 128 caracteres, sem quebras de linha)"
+ "remote.badTurnPassword": "Não é uma senha de retransmissor válida (até 128 caracteres, sem quebras de linha)",
+ "remote.badTurnFingerprint": "Não é uma impressão digital de certificado válida: 64 dígitos hexadecimais (os pares podem ser separados por dois-pontos)"
}
diff --git a/assets/locales/pt/settings.json b/assets/locales/pt/settings.json
index f9ae0fe..a561538 100644
--- a/assets/locales/pt/settings.json
+++ b/assets/locales/pt/settings.json
@@ -326,10 +326,8 @@
"sharing.blocklistFooter": "Sempre recusados, antes de qualquer outra regra, até você removê-los.",
"sharing.listEmpty": "A lista está vazia.",
"sharing.blockForever": "Bloquear sempre",
- "sharing.video": "Vídeo",
- "sharing.videoToggle": "Transmitir como vídeo (WebRTC)",
- "sharing.videoHint": "Mais fluido e nítido que com imagens, e com som. Volta para imagens quando o navegador ou a rede não conseguem.",
- "sharing.videoOffHint": "O navegador recebe a tela como imagens.",
+ "sharing.video": "Conexão",
+ "sharing.videoHint": "A tela, o som e o que você digita viajam criptografados por WebRTC: é a única forma de funcionamento da área de trabalho remota.",
"sharing.stun": "Servidores STUN",
"sharing.stunPlaceholder": "stun.exemplo.org:3478",
"sharing.stunHint": "Este computador responde STUN sozinho, o que basta na rede local. Adicione um servidor público só para conectar entre redes (até 4, separados por vírgulas).",
@@ -342,5 +340,14 @@
"sharing.turnRemove": "Remover retransmissor",
"sharing.turnFooter": "Só quando nenhum dos lados consegue chegar ao outro diretamente. O retransmissor só vê vídeo e entrada criptografados; a senha fica neste computador e só o sistema pode lê-la.",
"sharing.save": "Salvar",
- "sharing.noneSet": "Nenhum"
+ "sharing.noneSet": "Nenhum",
+ "sharing.videoRow": "Vídeo por WebRTC",
+ "sharing.videoFooter": "Funciona na rede local e por VPN com UDP, ou pela porta TCP {port} onde o UDP está bloqueado. Para conectar entre redes diferentes, adicione um servidor STUN ou um retransmissor abaixo.",
+ "sharing.turnTransport": "Chegar ao retransmissor por",
+ "sharing.turnUdp": "UDP",
+ "sharing.turnTcp": "TCP",
+ "sharing.turnTls": "TLS",
+ "sharing.turnFp": "Impressão digital do certificado (opcional)",
+ "sharing.turnFpPlaceholder": "SHA-256, como 3A:7F:…",
+ "sharing.turnFpHint": "Vazio: o certificado do retransmissor precisa ser válido para o nome dele. Para um retransmissor com certificado próprio, cole aqui a impressão digital SHA-256."
}
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 5282ab3..4ff3c87 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -85,10 +85,10 @@ Besides the kernel and libc, only these are allowed:
| Mesa: `libgbm`, `libEGL`, `libGLESv2` | GPU composition and dmabuf buffers from apps (games); optional: without a 3D GPU or without the `-dev` packages it builds and runs without Mesa (approved by the user on 2026-10-16, see `GPU.md`) | system |
| QuickJS | ES2023 JavaScript engine, ~1 MB, written in C | vendored|
| `stb_truetype`, `stb_image` | fonts and images; public domain, a single header | vendored|
-| `stb_image_write` | PNG and JPEG for the remote desktop (`lord-sessiond` only); public domain | vendored|
+| `stb_image_write` | PNG for the remote desktop's lossless refresh of still areas and cursor images (`lord-sessiond` only); public domain | vendored|
| OpenSSL (`libssl`, `libcrypto`) | TLS. **Only two binaries link it**: `lord-sessiond` (remote desktop on the LAN, `REMOTE.md`) and `lord-maild` (IMAP/SMTP/CalDAV/CardDAV, certificates checked against the system CA store, `MAIL.md`). Never `lord-server` | system |
| `libcrypt` | verifying passwords against `/etc/shadow` (`lord-sessiond`, `admin.c`) | system |
-| `libsrtp2`, `usrsctp` | the remote desktop's video (WebRTC, 0.8.4): SRTP and the data channels' SCTP, in `lord-sessiond`'s bridge only (as the user, after it dropped root); with OpenSSL's DTLS and our own ICE-lite, SDP and RTP code (`remote/rtc.c`, `remote/rtcwire.c`) because Debian 13 packages no libdatachannel. Optional: without them the remote desktop is tiles only. Never `lord-server` | system |
+| `libsrtp2`, `usrsctp` | the remote desktop's video (WebRTC, 0.8.4): SRTP and the data channels' SCTP, in `lord-sessiond`'s bridge only (as the user, after it dropped root); with OpenSSL's DTLS and our own ICE-lite, SDP and RTP code (`remote/rtc.c`, `remote/rtcwire.c`) because Debian 13 packages no libdatachannel. Required for `lord-sessiond` since the remote desktop shows the screen only over WebRTC (the image path was removed in 0.8.4). Never `lord-server` | system |
| FFmpeg (`libavcodec`, `libavformat`, `libavutil`, `libswscale`, `libswresample`) and `libva` (+ `libva-drm`) | Decoding and encoding video, audio and images; VA-API for hardware decoding (M9, approved by the user). **Only `lord-media` links them**, the sandboxed helper; the player, synchronization and interface are our own. Never `lord-server` (`MEDIA.md`) | system |
| Poppler (`libpoppler-glib`) and libarchive | PDF and archives (M9, approved by the user). **Only `lord-doc` links them**, the sandboxed helper; Poppler brings cairo, FreeType and fontconfig, which `lord-doc` also uses for font samples and for building PDFs. Never `lord-server` (`DOCUMENTS.md`) | system |
| `clang` (build time only) | the BPF programs of `lord-netperm` (`np.bpf.c`, `clang -target bpf`); they are embedded in the binary. Without clang (`BPF=0`) the service runs without BPF. Neither libbpf nor bcc is used | build machine |
diff --git a/docs/ISO.md b/docs/ISO.md
index cc5db61..0776219 100644
--- a/docs/ISO.md
+++ b/docs/ISO.md
@@ -332,7 +332,7 @@ COPYFILE_DISABLE=1 tar czf - --no-xattrs --exclude build --exclude node_modules
package.json README.md server third_party assets themes tools | \
ssh [email protected] 'rm -rf ~/LordOS-iso && mkdir ~/LordOS-iso && cd ~/LordOS-iso && tar xzf -'
# on VM 116
-make -C server -j4 # lord-server (GPU=1) and lord-sessiond (needs libssl-dev)
+make -C server -j4 # lord-server (GPU=1) and lord-sessiond (needs libssl-dev, libsrtp2-dev, libusrsctp-dev)
sudo tools/iso/build.sh /var/tmp/lordos-iso # → /var/tmp/lordos-iso/out/
```
@@ -384,7 +384,7 @@ sudo apt-get install -y --no-install-recommends debootstrap squashfs-tools xorri
| `libpoppler-glib-dev`, `libarchive-dev` | compiling `lord-doc` (PDF and archives); without them `make` skips it and the package ships without a PDF viewer |
| `libavcodec-dev`, `libavformat-dev`, `libavutil-dev`, `libswscale-dev`, `libswresample-dev` | compiling `lord-media` (video, photos, camera, recorder); without them `make` skips it |
| `libva-dev` | optional: GPU decoding (VA-API) in `lord-media` |
-| `libsrtp2-dev`, `libusrsctp-dev` | the remote desktop's video (WebRTC: SRTP and the data channels) in `lord-sessiond`; without them `make` builds the tile path only and the package doesn't depend on `libsrtp2-1`/`libusrsctp2` |
+| `libsrtp2-dev`, `libusrsctp-dev` | the remote desktop (WebRTC: SRTP and the data channels) in `lord-sessiond`; required: without them `make` stops with an error for `lord-sessiond` (or build with `SESSIOND=0`), and the package always depends on `libsrtp2-1` and `libusrsctp2` |
| `clang` | compiling `lord-netperm`'s BPF programs (per-app network permissions); without it the build still works, but no rules are applied |
| `debootstrap` | the Debian base system |
| `squashfs-tools` | `mksquashfs` |
@@ -675,7 +675,7 @@ M8 step (live ISO), on the development VM:
```sh
sudo apt-get install -y --no-install-recommends debootstrap squashfs-tools xorriso grub-pc-bin \
- grub-efi-amd64-bin grub-common mtools dosfstools lintian libssl-dev
+ grub-efi-amd64-bin grub-common mtools dosfstools lintian libssl-dev libsrtp2-dev libusrsctp-dev
```
In Proxmox, VM 117 `lordos-live` is used to test the ISO. It has 4 GB, 4 cores, q35, `vga: virtio`,
diff --git a/docs/RASPBERRY.md b/docs/RASPBERRY.md
index 36a5f77..76d1066 100644
--- a/docs/RASPBERRY.md
+++ b/docs/RASPBERRY.md
@@ -693,7 +693,7 @@ There are two possible systems on the board:
2. **Packages for compiling** (the same as in `VM-SETUP.md` plus Mesa):
```sh
sudo apt-get install -y --no-install-recommends build-essential pkg-config libxkbcommon-dev libwayland-dev \
- wayland-protocols libgbm-dev libegl-dev libgles-dev libssl-dev libcrypt-dev xkb-data \
+ wayland-protocols libgbm-dev libegl-dev libgles-dev libssl-dev libcrypt-dev libsrtp2-dev libusrsctp-dev xkb-data \
libgl1-mesa-dri libegl1 libgles2 libgbm1 mesa-utils glmark2-es2-wayland
```
3. **The tree,** from the development machine (as for the VM; the themes are built there):
diff --git a/sdk/index.js b/sdk/index.js
index 0bdcee3..2d7567b 100644
--- a/sdk/index.js
+++ b/sdk/index.js
@@ -20,7 +20,7 @@ import { ImeOverlay } from './ime.js';
import { ClipboardPanel, ClipboardToast, clipboard as clipboardApi } from './clipboard.js';
import { SpotlightPanel } from './spotlight.js';
import { RemoteBadge } from './remote.js';
-export { remote, remoteViewers, sshLogin, remoteCidr, remoteMinutesLeft, remoteAccess, remoteVideo, remoteIceServer } from './remote.js';
+export { remote, remoteViewers, sshLogin, remoteCidr, remoteMinutesLeft, remoteAccess, remoteVideo, remoteIceServer, remoteFingerprint } from './remote.js';
export { perms, permsRev, requestPermission, markDeviceUse, useIndicators, PrivacyIndicators, PermissionPrompt, registerPrivacySection, privacySections, PERMISSIONS, onPermissionRevoked, micStart, micStop, micLevel } from './privacy.js';
import { PermissionPrompt } from './privacy.js';
import { Powerbox } from './powerbox.jsx';
diff --git a/sdk/remote.js b/sdk/remote.js
index 5c4f4b8..309bd5c 100644
--- a/sdk/remote.js
+++ b/sdk/remote.js
@@ -7,7 +7,7 @@
// useStore(remoteViewers) → [{ ip, user }]
// remote.status() → { enabled, installed, port, allow, net: 'lan'|'list'|'any', fingerprint, clients: [...],
// sessions: [...], monitor, blocked: [{ ip, until, strikes, manual }], allowed: ['cidr'] }
-// remoteVideo.setVideo(on) / setStun([...]) / setTurn(server, user, pass) / clearTurn() (remote-ice)
+// remoteVideo.setStun([...]) / setTurn(server, user, pass, 'udp'|'tcp'|'tls', fingerprint) / clearTurn() (remote-ice)
// remoteCidr(text) → canonical-looking text if it's an address or network the admin actions accept, else ''
// (lord-sessiond's helper checks again, strictly: server/src/remote/ipnet.c)
// lord-sessiond's notice "this source was blocked after failed logins" arrives as __lordRemoteNotice and is
@@ -82,10 +82,11 @@ export const remoteAccess = {
};
function parseConfig(text) {
- const c = { port: 5673, allow: 'all', net: 'lan', video: true, stun: [], turn: '', turnUser: '', turnKey: false };
+ const c = { port: 5673, allow: 'all', net: 'lan', stun: [], turn: '', turnUser: '', turnKey: false, turnTransport: 'udp', turnFp: '' };
for (const line of String(text || '').split('\n')) {
const m = /^(\w+)=(.*)$/.exec(line.trim());
- if (m && m[1] === 'video') c.video = m[2] !== '0';
+ if (m && m[1] === 'turn_transport' && ['udp', 'tcp', 'tls'].includes(m[2])) c.turnTransport = m[2];
+ if (m && m[1] === 'turn_fp') c.turnFp = remoteFingerprint(m[2]);
if (m && m[1] === 'stun' && c.stun.length < 4) c.stun.push(m[2]);
if (m && m[1] === 'turn') c.turn = m[2];
if (m && m[1] === 'turn_user') c.turnUser = m[2];
@@ -109,7 +110,7 @@ export const remote = {
installed: !!s.installed, enabled: !!(st && st.running), port: st?.port || cfg.port, allow: st?.allow || cfg.allow,
fingerprint: s.fingerprint || '', clients: st?.clients || [], sessions: st?.sessions || [], monitor: st?.monitor || '',
net: st?.net || cfg.net, blocked: Array.isArray(st?.blocked) ? st.blocked : [], allowed: Array.isArray(st?.allowed) ? st.allowed : [],
- video: cfg.video, stun: cfg.stun, turn: cfg.turn, turnUser: cfg.turnUser, turnKey: cfg.turnKey,
+ stun: cfg.stun, turn: cfg.turn, turnUser: cfg.turnUser, turnKey: cfg.turnKey, turnTransport: cfg.turnTransport, turnFp: cfg.turnFp,
};
},
};
@@ -121,12 +122,20 @@ export function remoteIceServer(text) {
return m && Number(m[2]) <= 65535 && !m[1].includes('..') ? s : '';
}
-// The video path's settings (docs/REMOTE.md, "Video (WebRTC)"), through the remote-ice admin action;
-// the TURN password goes quietly (never into the visible admin log) and is never read back.
+// Mirrors rw_fp_parse (rtcwire.c): a SHA-256 certificate fingerprint, 64 hex digits, optionally in pairs
+// separated by ':'. Returns the text (trimmed) or ''.
+export function remoteFingerprint(text) {
+ const s = String(text || '').trim();
+ return /^[0-9a-fA-F]{64}$/.test(s) || /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){31}$/.test(s) ? s : '';
+}
+
+// The WebRTC connection's settings (docs/REMOTE.md, "Video (WebRTC)"), through the remote-ice admin
+// action; the TURN password goes quietly (never into the visible admin log) and is never read back.
export const remoteVideo = {
- setVideo: (on) => admin.run('remote-ice', `video=${on ? 'on' : 'off'}`),
setStun: (list) => admin.run('remote-ice', `stun=${list.join(',')}`),
- setTurn: (server, user, pass) => admin.run('remote-ice', [`turn=${server}`, `turn_user=${user}`, ...(pass ? [`turn_pass=${pass}`] : [])].join('\n'), undefined, { quiet: true }),
+ setTurn: (server, user, pass, transport = 'udp', fp = '') => admin.run('remote-ice',
+ [`turn=${server}`, `turn_transport=${transport}`, `turn_fp=${transport === 'tls' ? fp : ''}`, `turn_user=${user}`, ...(pass ? [`turn_pass=${pass}`] : [])].join('\n'),
+ undefined, { quiet: true }),
clearTurn: () => admin.run('remote-ice', 'turn='),
};
diff --git a/server/Makefile b/server/Makefile
index e6cccca..b22b2a6 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -106,7 +106,8 @@ HDR += $(wildcard src/tp/*.h)
.PHONY: all clean test test-full
# lord-sessiond: sessions, login screen and remote desktop (docs/SESSIONS.md). It's the
-# only one that links OpenSSL (TLS); without libssl-dev, only lord-server is built.
+# only one that links OpenSSL (TLS), and with it libsrtp2 and usrsctp (the remote desktop's WebRTC);
+# without libssl-dev, only lord-server is built.
SESSIOND ?= $(shell pkg-config --exists openssl 2>/dev/null && echo 1 || echo 0)
ALL := build/lord-server
ifeq ($(SESSIOND),1)
@@ -458,14 +459,16 @@ build/lord-server: $(OBJ) build/obj/ui/stb.o $(QJS_OBJ) $(WL_OBJ)
SESSIOND_OBJ := build/obj/remote/sessiond.o build/obj/remote/sess.o build/obj/remote/monitor.o build/obj/remote/conn.o \
build/obj/remote/enc.o build/obj/remote/pages.o build/obj/sec/cgroup.o build/obj/remote/netsup.o build/obj/remote/cgsup.o \
- build/obj/remote/rsec.o build/obj/remote/ipnet.o build/obj/remote/lockout.o build/obj/remote/rtcwire.o
-# The remote desktop's video (docs/REMOTE.md, "Video (WebRTC)"): our own WebRTC peer in the bridge, on
-# Debian's libsrtp2 and usrsctp (and OpenSSL's DTLS). Without them, the tile path only.
-RTC ?= $(shell pkg-config --exists libsrtp2 usrsctp 2>/dev/null && echo 1 || echo 0)
-ifeq ($(RTC),1)
-SESSIOND_OBJ += build/obj/remote/rtc.o
+ build/obj/remote/rsec.o build/obj/remote/ipnet.o build/obj/remote/lockout.o build/obj/remote/rtcwire.o build/obj/remote/rtc.o
+# The remote desktop shows the screen only over WebRTC (docs/REMOTE.md, "Video (WebRTC)"): our own peer in
+# the bridge, on Debian's libsrtp2 and usrsctp (and OpenSSL's DTLS). lord-sessiond can't be built without
+# them (libsrtp2-dev, libusrsctp-dev).
+ifeq ($(SESSIOND),1)
+ifneq ($(shell pkg-config --exists libsrtp2 usrsctp 2>/dev/null && echo 1),1)
+$(error lord-sessiond needs libsrtp2 and usrsctp for the remote desktop: apt-get install libsrtp2-dev libusrsctp-dev (or SESSIOND=0))
+endif
SESSIOND_LIBS := $(shell pkg-config --libs libsrtp2 usrsctp)
-build/obj/remote/rtc.o build/obj/remote/conn.o: CPPFLAGS += -DLORD_RTC $(shell pkg-config --cflags libsrtp2 usrsctp)
+build/obj/remote/rtc.o: CPPFLAGS += $(shell pkg-config --cflags libsrtp2 usrsctp)
endif
build/obj/remote/pages.o: src/remote/login.html src/remote/client.html
@mkdir -p $(dir $@)
diff --git a/server/src/main.c b/server/src/main.c
index aaf3750..9c9e650 100644
--- a/server/src/main.c
+++ b/server/src/main.c
@@ -538,7 +538,7 @@ static int gpu_start(Server *s);
* read back once (gpu_read_next) for every viewer. Only protected windows still need the CPU path
* (they are painted black in a CPU copy of the scene, media/capture.c); LORD_REMOTE_GPU=0 forces it. */
static int remote_needs_cpu(void) {
- if (!mirror_clients()) return 0;
+ if (!mirror_watchers()) return 0;
const char *e = getenv("LORD_REMOTE_GPU");
return (e && !strcmp(e, "0")) || media_protected_count() > 0;
}
@@ -1239,7 +1239,7 @@ static void present_cpu(Server *s, Out *o, int index, Rect r, int feed) {
/* remote desktop (without the cursor) and recording: with protected windows in black */
MediaCursor mc = { px_, py_, k.mode, k.px, k.w, k.h, k.hx, k.hy };
view_of(o, index);
- const Canvas *seen = media_composed(&out, mirror_clients() > 0, &mc);
+ const Canvas *seen = media_composed(&out, mirror_watchers() > 0, &mc);
view_reset();
mirror_frame(seen->px, seen->stride, W, H);
mirror_cursor(px_, py_, k.mode, k.px, k.w, k.h, k.hx, k.hy, k.gen);
@@ -1295,7 +1295,7 @@ static void present_memory(Server *s, Out *o, int index, int feed) {
if (feed) {
/* what the browser sees and what is recorded: with the protected windows in black */
MediaCursor mc = { x, y, k.mode, k.px, k.w, k.h, k.hx, k.hy };
- const Canvas *seen = media_composed(&out, mirror_clients() > 0, &mc);
+ const Canvas *seen = media_composed(&out, mirror_watchers() > 0, &mc);
mirror_frame(seen->px, seen->stride, o->scene.w, o->scene.h);
mirror_cursor(x, y, k.mode, k.px, k.w, k.h, k.hx, k.hy, k.gen);
}
@@ -1366,7 +1366,7 @@ static void render_frame(Server *s) {
/* the remote desktop watches: the composed frame is read back once for every viewer */
static uint32_t *rb;
static size_t rb_n;
- int readback = o == feed && mirror_clients() > 0;
+ int readback = o == feed && mirror_watchers() > 0;
if (readback) {
size_t need = (size_t)o->scene.w * o->scene.h;
if (need > rb_n) { free(rb); rb = malloc(need * 4); rb_n = rb ? need : 0; }
@@ -1401,7 +1401,7 @@ static void render_frame(Server *s) {
}
/* every display in one frame for the remote desktop and recording (capture view) */
Out *c = &s->cap;
- if (feed == c && (mirror_clients() || media_needs_cpu())) {
+ if (feed == c && (mirror_watchers() || media_needs_cpu())) {
if (!c->on || c->v.w != s->W || c->v.h != s->H) {
c->on = 1;
c->v = out_view(0, 0, s->W, s->H, 100);
@@ -1744,7 +1744,7 @@ int main(int argc, char **argv) {
double mrd = mirror_timeout();
if (mrd >= 0 && (delay < 0 || mrd < delay)) delay = mrd;
/* sin monitor y sin nadie mirando no se dibuja (al conectarse alguien llega un cuadro entero) */
- int unseen = s.headless && !mirror_clients() && !media_needs_cpu(); /* grabando la pantalla, sí */
+ int unseen = s.headless && !mirror_watchers() && !media_needs_cpu(); /* recording the screen: yes */
if (!s.headless && display_is_off()) unseen = 1; /* screen off after idle (DPMS): nothing to draw */
/* nothing is drawn: the Linux apps waiting for a frame get it at most once a second (wl.c) */
if (unseen) {
diff --git a/server/src/remote/client.html b/server/src/remote/client.html
index b392471..b575985 100644
--- a/server/src/remote/client.html
+++ b/server/src/remote/client.html
@@ -14,14 +14,32 @@
video { position: absolute; inset: 0; display: block; visibility: hidden; background: #000; pointer-events: none; }
#bar { position: fixed; top: 0; left: 50%; transform: translate(-50%, -34px); transition: transform .18s ease; z-index: 3;
display: flex; gap: 10px; align-items: center; padding: 6px 10px 6px 12px; border-radius: 0 0 12px 12px;
- background: rgba(28,26,44,.92); border: 1px solid rgba(255,255,255,.12); border-top: 0; }
+ background: rgba(28,26,44,.92); border: 1px solid rgba(255,255,255,.12); border-top: 0; white-space: nowrap; }
#bar.show, #bar:hover { transform: translate(-50%, 0); }
#bar button { border: 0; border-radius: 7px; padding: 5px 10px; color: #fff; background: #3a3754; cursor: pointer; font-size: 12px; }
#bar button.red { background: #b4232f; }
- #lat { color: #a9a6c3; min-width: 70px; }
- #msg { position: fixed; inset: 0; display: none; place-items: center; z-index: 4; background: rgba(8,8,16,.82); }
- #msg div { padding: 22px 26px; border-radius: 14px; background: #1f1c33; border: 1px solid rgba(255,255,255,.14); text-align: center; }
- #msg button { margin-top: 14px; border: 0; border-radius: 8px; padding: 8px 16px; color: #fff; background: #6d28d9; cursor: pointer; }
+ #lat { color: #a9a6c3; min-width: 52px; }
+ #mode { color: #a9a6c3; }
+ #again-note { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 4; display: none; padding: 7px 14px;
+ border-radius: 16px; background: rgba(28,26,44,.94); border: 1px solid rgba(255,255,255,.16); color: #fff; }
+ #again-note i { display: inline-block; width: 10px; height: 10px; margin-inline-end: 8px; border-radius: 50%;
+ border: 2px solid #a77bf3; border-top-color: transparent; animation: spin 0.9s linear infinite; vertical-align: -1px; }
+ @keyframes spin { to { transform: rotate(360deg); } }
+ #msg { position: fixed; inset: 0; display: none; place-items: center; z-index: 5; background: rgba(8,8,16,.88); padding: 16px; }
+ #card { width: min(560px, 100%); max-height: calc(100vh - 32px); overflow: auto; padding: 22px 26px; border-radius: 14px;
+ background: #1f1c33; border: 1px solid rgba(255,255,255,.14); line-height: 1.45; }
+ #card h1 { margin: 0 0 8px; font-size: 18px; font-weight: 650; color: #fff; }
+ #card p { margin: 6px 0; color: #d8d5ea; }
+ #card ul { margin: 6px 0 4px; padding-inline-start: 4px; list-style: none; color: #d8d5ea; }
+ #card li { margin: 3px 0; }
+ #card li.no { color: #ffb4b4; }
+ #card .what { margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: rgba(167,123,243,.12); color: #ece6ff; }
+ #card .det { margin-top: 10px; font: 11px ui-monospace, Menlo, monospace; color: #8f8ba8; word-break: break-word; }
+ #card .btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
+ #card button { border: 0; border-radius: 8px; padding: 8px 16px; color: #fff; background: #6d28d9; cursor: pointer; font-size: 13px; }
+ #card button.plain { background: #3a3754; }
+ #card .spin { display: inline-block; width: 14px; height: 14px; margin-inline-end: 10px; border-radius: 50%; border: 2px solid #a77bf3;
+ border-top-color: transparent; animation: spin 0.9s linear infinite; vertical-align: -2px; }
#rcur { position: fixed; width: 14px; height: 20px; pointer-events: none; display: none; z-index: 2;
background: no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='20'%3E%3Cpath d='M1 1v16l4-4 3 6 2-1-3-6h6z' fill='white' stroke='black'/%3E%3C/svg%3E"); }
</style>
@@ -31,21 +49,76 @@
<div id="rcur"></div>
<div id="bar"><span id="who">LordOS</span><span id="lat">…</span><span id="mode"></span>
<button id="snd" hidden>🔇</button><button id="fs">⛶</button><button id="out" class="red">✕</button></div>
-<div id="msg"><div><div id="msgtext"></div><button id="again"></button></div></div>
+<div id="again-note"><i></i><span id="again-text"></span></div>
+<div id="msg"><div id="card" role="alertdialog" aria-live="assertive"></div></div>
<script>
'use strict';
+// The remote desktop arrives only as WebRTC video (docs/REMOTE.md, "Video (WebRTC)"). If the
+// connection breaks, the page restarts ICE, then makes a new peer, then reconnects the WebSocket, on
+// its own and without signing in again; if it truly can't connect, it says why and what to do.
const L = {
- en: { full: 'Full screen', out: 'Disconnect', bye: 'The session ended or the connection was lost.', again: 'Reconnect', conn: 'Connecting…',
- sndOn: 'Turn sound on', sndOff: 'Turn sound off', video: 'Video', tiles: 'Images' },
- es: { full: 'Pantalla completa', out: 'Desconectar', bye: 'La sesión terminó o se cortó la conexión.', again: 'Reconectar', conn: 'Conectando…',
- sndOn: 'Activar el sonido', sndOff: 'Silenciar', video: 'Video', tiles: 'Imágenes' },
- pt: { full: 'Tela cheia', out: 'Desconectar', bye: 'A sessão terminou ou a conexão caiu.', again: 'Reconectar', conn: 'Conectando…',
- sndOn: 'Ativar o som', sndOff: 'Silenciar', video: 'Vídeo', tiles: 'Imagens' },
+ en: {
+ full: 'Full screen', out: 'Disconnect', conn: 'Connecting…', reconn: 'Reconnecting…', retry: 'Try again', signout: 'Sign out',
+ sndOn: 'Turn sound on', sndOff: 'Turn sound off', video: 'Video', bye: 'The session ended.',
+ unsupTitle: 'This browser can’t show the remote desktop',
+ unsupText: 'The remote desktop arrives as live video over WebRTC. This browser is missing something it needs:',
+ reqRtc: 'WebRTC (RTCPeerConnection) with data channels', reqCodec: 'H.264 or VP8 video decoding', reqSecure: 'A secure (https) page — this one is',
+ unsupWhat: 'Open this address in an up-to-date desktop or mobile browser with WebRTC turned on. Privacy extensions or settings that turn WebRTC off also cause this.',
+ failTitle: 'Couldn’t connect to the remote desktop', why: 'Why', what: 'What to do', details: 'Details',
+ noroute: 'The video connection never reached this computer: no connection check arrived over UDP, nor over TCP port {port}. A firewall, a VPN or the network between you is blocking both.',
+ norouteWhat: 'Allow UDP, or at least TCP port {port}, between this browser and the computer. To connect across networks, an administrator can add a relay (TURN) in Settings → Sharing on the computer.',
+ dtls: 'The connection was reached, but the encrypted video channel (DTLS) couldn’t be set up.',
+ dtlsWhat: 'Try again. If it keeps happening, something between you (a proxy or security software) is altering the connection.',
+ lost: 'The connection was lost and didn’t come back.', lostWhat: 'Check the network on both sides. When it is back, try again: you stay signed in.',
+ server: 'The computer couldn’t start the video connection.', serverWhat: 'Try again. If it keeps happening, turn the remote desktop off and on in Settings → Sharing.',
+ offer: 'The computer refused this browser’s connection offer.', offerWhat: 'Update the browser and try again.',
+ media: 'The connection is up, but no picture arrived.', mediaWhat: 'This browser may not be able to play H.264 or VP8 video right now (hardware decoding turned off, or a codec blocked). Try another browser.',
+ wsdown: 'The computer can’t be reached: the connection to it was lost.', wsdownWhat: 'Check that the computer is on and on the network. This page keeps trying on its own.',
+ },
+ es: {
+ full: 'Pantalla completa', out: 'Desconectar', conn: 'Conectando…', reconn: 'Reconectando…', retry: 'Reintentar', signout: 'Cerrar sesión',
+ sndOn: 'Activar el sonido', sndOff: 'Silenciar', video: 'Video', bye: 'La sesión terminó.',
+ unsupTitle: 'Este navegador no puede mostrar el escritorio remoto',
+ unsupText: 'El escritorio remoto llega como video en vivo por WebRTC. A este navegador le falta algo que necesita:',
+ reqRtc: 'WebRTC (RTCPeerConnection) con canales de datos', reqCodec: 'Decodificar video H.264 o VP8', reqSecure: 'Una página segura (https): esta lo es',
+ unsupWhat: 'Abrí esta dirección en un navegador actualizado, de escritorio o del teléfono, con WebRTC activado. Las extensiones o ajustes de privacidad que apagan WebRTC también causan esto.',
+ failTitle: 'No se pudo conectar al escritorio remoto', why: 'Por qué', what: 'Qué hacer', details: 'Detalles',
+ noroute: 'La conexión de video nunca llegó a esta computadora: no llegó ninguna comprobación de conexión por UDP ni por el puerto TCP {port}. Un firewall, una VPN o la red entre ustedes bloquea los dos.',
+ norouteWhat: 'Permití UDP, o al menos el puerto TCP {port}, entre este navegador y la computadora. Para conectarse entre redes distintas, un administrador puede agregar un relé (TURN) en Ajustes → Compartir de la computadora.',
+ dtls: 'La conexión llegó, pero no se pudo establecer el canal de video cifrado (DTLS).',
+ dtlsWhat: 'Reintentá. Si sigue pasando, algo entre ustedes (un proxy o un programa de seguridad) está alterando la conexión.',
+ lost: 'La conexión se cortó y no volvió.', lostWhat: 'Revisá la red de los dos lados. Cuando vuelva, reintentá: la sesión sigue iniciada.',
+ server: 'La computadora no pudo iniciar la conexión de video.', serverWhat: 'Reintentá. Si sigue pasando, apagá y encendé el escritorio remoto en Ajustes → Compartir.',
+ offer: 'La computadora rechazó la propuesta de conexión de este navegador.', offerWhat: 'Actualizá el navegador y reintentá.',
+ media: 'La conexión está establecida, pero no llegó imagen.', mediaWhat: 'Puede que este navegador no pueda reproducir video H.264 o VP8 ahora (decodificación por hardware apagada o un códec bloqueado). Probá con otro navegador.',
+ wsdown: 'No se puede llegar a la computadora: se perdió la conexión.', wsdownWhat: 'Comprobá que la computadora esté encendida y en la red. Esta página sigue intentando sola.',
+ },
+ pt: {
+ full: 'Tela cheia', out: 'Desconectar', conn: 'Conectando…', reconn: 'Reconectando…', retry: 'Tentar de novo', signout: 'Sair',
+ sndOn: 'Ativar o som', sndOff: 'Silenciar', video: 'Vídeo', bye: 'A sessão terminou.',
+ unsupTitle: 'Este navegador não consegue mostrar a área de trabalho remota',
+ unsupText: 'A área de trabalho remota chega como vídeo ao vivo por WebRTC. Falta a este navegador algo de que ela precisa:',
+ reqRtc: 'WebRTC (RTCPeerConnection) com canais de dados', reqCodec: 'Decodificar vídeo H.264 ou VP8', reqSecure: 'Uma página segura (https): esta é',
+ unsupWhat: 'Abra este endereço em um navegador atualizado, de computador ou celular, com o WebRTC ativado. Extensões ou ajustes de privacidade que desligam o WebRTC também causam isso.',
+ failTitle: 'Não foi possível conectar à área de trabalho remota', why: 'Por quê', what: 'O que fazer', details: 'Detalhes',
+ noroute: 'A conexão de vídeo nunca chegou a este computador: nenhuma verificação de conexão chegou por UDP nem pela porta TCP {port}. Um firewall, uma VPN ou a rede entre vocês bloqueia as duas.',
+ norouteWhat: 'Permita UDP, ou pelo menos a porta TCP {port}, entre este navegador e o computador. Para conectar entre redes diferentes, um administrador pode adicionar um retransmissor (TURN) em Ajustes → Compartilhar no computador.',
+ dtls: 'A conexão chegou, mas o canal de vídeo criptografado (DTLS) não pôde ser estabelecido.',
+ dtlsWhat: 'Tente de novo. Se continuar, algo entre vocês (um proxy ou um programa de segurança) está alterando a conexão.',
+ lost: 'A conexão caiu e não voltou.', lostWhat: 'Verifique a rede dos dois lados. Quando voltar, tente de novo: a sessão continua iniciada.',
+ server: 'O computador não conseguiu iniciar a conexão de vídeo.', serverWhat: 'Tente de novo. Se continuar, desligue e ligue a área de trabalho remota em Ajustes → Compartilhar.',
+ offer: 'O computador recusou a proposta de conexão deste navegador.', offerWhat: 'Atualize o navegador e tente de novo.',
+ media: 'A conexão está estabelecida, mas nenhuma imagem chegou.', mediaWhat: 'Este navegador talvez não consiga reproduzir vídeo H.264 ou VP8 agora (decodificação por hardware desligada ou um codec bloqueado). Tente outro navegador.',
+ wsdown: 'Não é possível alcançar o computador: a conexão caiu.', wsdownWhat: 'Verifique se o computador está ligado e na rede. Esta página continua tentando sozinha.',
+ },
};
const T = L[(navigator.language || 'en').slice(0, 2)] || L.en;
const $ = (id) => document.getElementById(id);
const cv = $('c'), ctx = cv.getContext('2d'), vid = $('v'), stage = $('stage');
-$('fs').title = T.full; $('out').title = T.out; $('again').textContent = T.again;
+const PORT = location.port || '443';
+$('fs').title = T.full; $('out').title = T.out;
+// what the page knows, for tests and support: state, the last reason, the media path
+const rd = window.__rd = { state: 'start', why: '', path: '', restarts: 0, peers: 0, wsOpens: 0 };
// KeyboardEvent.code → evdev code (linux/input-event-codes.h)
const K = { Escape: 1, Digit1: 2, Digit2: 3, Digit3: 4, Digit4: 5, Digit5: 6, Digit6: 7, Digit7: 8, Digit8: 9, Digit9: 10, Digit0: 11,
@@ -88,36 +161,116 @@ function sendIn(o) {
if (dcIn && dcIn.readyState === 'open' && j.length < 200000) dcIn.send(j); else send(o);
}
-// ---- video (WebRTC): H.264/VP8 + Opus from the session's encoder; input and the lossless tiles of
-// still areas over data channels. The tiles over the WebSocket stay until the video plays, and come
-// back if it fails (docs/REMOTE.md, "Video (WebRTC)").
-let pc = null, dcIn = null, vmode = false, rtcTimer = 0, offered = false, codec = '';
-function setMode() { $('mode').textContent = vmode ? `${T.video} · ${codec.toUpperCase()}` : T.tiles; }
-function giveUp(tell) {
- clearTimeout(rtcTimer);
- if (pc) { try { pc.close(); } catch (e) {} }
- pc = null; dcIn = null; offered = false;
+// ---- messages: a small "reconnecting" note, or a full card (error, unsupported browser) ----
+function note(on) { $('again-text').textContent = T.reconn; $('again-note').style.display = on ? 'block' : 'none'; }
+function el(tag, text, cls) { const e = document.createElement(tag); if (text) e.textContent = text; if (cls) e.className = cls; return e; }
+function card(build) {
+ const c = $('card');
+ c.textContent = '';
+ build(c);
+ $('msg').style.display = 'grid';
+}
+function hideCard() { $('msg').style.display = 'none'; }
+function waiting(text) { card((c) => { const p = el('p'); p.append(el('span', '', 'spin'), text); c.append(p); }); }
+const fill = (s) => s.replace(/\{port\}/g, PORT);
+
+// What this browser has: WebRTC, data channels and a video codec the computer sends (H.264 or VP8).
+function support() {
+ const rtc = typeof RTCPeerConnection === 'function' && typeof RTCDataChannel === 'function';
+ let codec = rtc;
+ try {
+ const caps = typeof RTCRtpReceiver === 'function' && RTCRtpReceiver.getCapabilities && RTCRtpReceiver.getCapabilities('video');
+ if (caps && Array.isArray(caps.codecs)) codec = caps.codecs.some((c) => /^video\/(h264|vp8)$/i.test(c.mimeType));
+ } catch (e) {}
+ return { rtc, codec, secure: window.isSecureContext !== false };
+}
+function unsupported(s) {
+ rd.state = 'unsupported';
+ card((c) => {
+ c.append(el('h1', T.unsupTitle), el('p', T.unsupText));
+ const ul = el('ul');
+ for (const [ok, text] of [[s.rtc, T.reqRtc], [s.codec, T.reqCodec], [s.secure, T.reqSecure]]) ul.append(el('li', (ok ? '✓ ' : '✗ ') + text, ok ? '' : 'no'));
+ c.append(ul, el('div', T.unsupWhat, 'what'));
+ const b = el('div', '', 'btns'), so = el('button', T.signout, 'plain');
+ so.onclick = signOut;
+ b.append(so);
+ c.append(b);
+ });
+}
+function failure(why, detail) {
+ rd.state = 'failed';
+ rd.why = why;
+ note(false);
+ const key = T[why] ? why : 'server';
+ card((c) => {
+ c.append(el('h1', T.failTitle));
+ c.append(el('p', `${T.why}: ${fill(T[key])}`));
+ c.append(el('div', `${T.what}: ${fill(T[key + 'What'])}`, 'what'));
+ if (detail) c.append(el('div', `${T.details}: ${detail}`, 'det'));
+ const b = el('div', '', 'btns'), so = el('button', T.signout, 'plain'), again = el('button', T.retry);
+ so.onclick = signOut;
+ again.onclick = () => { hideCard(); retryAll(); };
+ b.append(so, again);
+ c.append(b);
+ setTimeout(() => again.focus(), 0);
+ });
+}
+
+// ---- the WebRTC peer: H.264/VP8 + Opus from the session's encoder; input and the lossless refresh
+// of still areas over data channels ----
+let pc = null, dcIn = null, vmode = false, codec = '', iceServers = [], offering = false, offersSent = 0, answersGot = 0;
+let playTimer = 0, discTimer = 0, restartTimer = 0, restartTries = 0, peerFails = 0, everPlayed = false, serverWhy = '';
+function setMode() { $('mode').textContent = vmode ? `${T.video} · ${codec.toUpperCase()}${rd.path ? ' · ' + rd.path.toUpperCase() : ''}` : ''; }
+function clearTimers() { clearTimeout(playTimer); clearTimeout(discTimer); clearTimeout(restartTimer); }
+function closePeer() {
+ clearTimers();
+ if (pc) { pc.onconnectionstatechange = pc.oniceconnectionstatechange = pc.onnegotiationneeded = pc.onicecandidate = pc.ontrack = null; try { pc.close(); } catch (e) {} }
+ pc = null; dcIn = null; offering = false; offersSent = answersGot = 0;
const was = vmode;
vmode = false;
vid.style.visibility = 'hidden'; vid.srcObject = null; $('snd').hidden = true;
- if (was) { ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H); }
- if (tell) send({ t: 'vmode', on: 0 });
+ if (was) queue = queue.then(() => ctx.clearRect(0, 0, W, H));
setMode();
}
function enterVideo() {
if (vmode || !pc) return;
- clearTimeout(rtcTimer);
+ clearTimeout(playTimer);
vmode = true;
+ everPlayed = true;
+ peerFails = 0;
+ rd.why = '';
+ restartTries = 0;
+ rd.state = 'playing';
+ hideCard();
+ note(false);
queue = queue.then(() => ctx.clearRect(0, 0, W, H));
vid.style.visibility = 'visible';
$('snd').hidden = false;
send({ t: 'vmode', on: 1 });
setMode();
}
-function startRtc(ice) {
+const gathered = (p) => new Promise((ok) => {
+ if (p.iceGatheringState === 'complete') return ok();
+ const t = setTimeout(ok, 1500);
+ p.addEventListener('icegatheringstatechange', () => { if (p.iceGatheringState === 'complete') { clearTimeout(t); ok(); } });
+});
+// the browser's offer (the first one, or an ICE restart's) over the authenticated WebSocket
+function offer(restart) {
+ if (!pc || offering) return;
+ const p = pc;
+ offering = true;
+ p.createOffer(restart ? { iceRestart: true } : {}).then((o) => p.setLocalDescription(o)).then(() => gathered(p))
+ .then(() => { if (pc === p) { offersSent++; send({ t: 'rtc', sdp: p.localDescription.sdp }); } })
+ .catch(() => { offering = false; if (pc === p) peerFailed('offer', 'createOffer'); });
+}
+function startPeer() {
+ closePeer();
+ rd.peers++;
+ rd.state = 'connecting';
+ serverWhy = '';
try {
- pc = new RTCPeerConnection({ iceServers: (ice || []).map((u) => ({ urls: u })), bundlePolicy: 'max-bundle', rtcpMuxPolicy: 'require' });
- } catch (e) { pc = null; return; }
+ pc = new RTCPeerConnection({ iceServers: iceServers.map((u) => ({ urls: u })), bundlePolicy: 'max-bundle', rtcpMuxPolicy: 'require' });
+ } catch (e) { pc = null; unsupported(support()); return; }
pc.addTransceiver('video', { direction: 'recvonly' });
pc.addTransceiver('audio', { direction: 'recvonly' });
dcIn = pc.createDataChannel('input', { ordered: true });
@@ -132,17 +285,94 @@ function startRtc(ice) {
if (vid.srcObject !== ms) vid.srcObject = ms;
vid.play().catch(() => {});
};
- pc.onicecandidate = (e) => { if (offered && e.candidate && e.candidate.candidate) send({ t: 'ice', c: e.candidate.candidate }); };
- pc.onconnectionstatechange = () => { if (pc && (pc.connectionState === 'failed' || pc.connectionState === 'closed')) giveUp(true); };
- const gathered = () => new Promise((ok) => {
- if (pc.iceGatheringState === 'complete') return ok();
- const t = setTimeout(ok, 1500);
- pc.addEventListener('icegatheringstatechange', () => { if (pc && pc.iceGatheringState === 'complete') { clearTimeout(t); ok(); } });
- });
- pc.createOffer().then((o) => pc.setLocalDescription(o)).then(gathered)
- .then(() => { if (pc) { send({ t: 'rtc', sdp: pc.localDescription.sdp }); offered = true; } })
- .catch(() => giveUp(true));
- rtcTimer = setTimeout(() => { if (!vmode) giveUp(true); }, 12000);
+ pc.onicecandidate = (e) => { if (e.candidate && e.candidate.candidate && !offering) send({ t: 'ice', c: e.candidate.candidate }); };
+ pc.oniceconnectionstatechange = () => iceState(pc && pc.iceConnectionState);
+ offer(false);
+ // no picture within 15 s: say why (the server's reason if it gave one, else what this side saw)
+ const p = pc;
+ playTimer = setTimeout(() => { if (pc === p && !vmode) diagnose(p).then((d) => peerFailed(d.why, d.text)); }, 15000);
+}
+// ICE lost its path: restart it (new credentials, the same encrypted channel), then a new peer
+function iceState(st) {
+ if (!pc) return;
+ if (st === 'connected' || st === 'completed') {
+ clearTimeout(discTimer); clearTimeout(restartTimer);
+ restartTries = 0;
+ offering = false;
+ if (vmode) rd.state = 'playing';
+ note(false);
+ pathKind();
+ return;
+ }
+ if (!vmode) return; // still connecting: the play timer decides
+ if (st === 'disconnected') {
+ note(true);
+ clearTimeout(discTimer);
+ discTimer = setTimeout(() => restartIce(), 2000);
+ } else if (st === 'failed') { note(true); restartIce(); }
+}
+function restartIce() {
+ if (!pc) return;
+ clearTimeout(restartTimer);
+ // the WebSocket can't deliver our last offer yet (its path is down too): wait, don't pile up offers
+ if (offering || (ws && ws.bufferedAmount > 0)) { restartTimer = setTimeout(restartIce, 1000); return; }
+ if (restartTries >= 6) { renewPeer('lost'); return; } // about 40 s of restarts: a new peer
+ restartTries++;
+ rd.restarts++;
+ rd.state = 'restarting';
+ try { if (pc.restartIce) pc.restartIce(); } catch (e) {}
+ offer(true);
+ restartTimer = setTimeout(() => {
+ if (!pc) return;
+ const st = pc.iceConnectionState;
+ if (st !== 'connected' && st !== 'completed') { offering = false; restartIce(); }
+ }, Math.min(8000, 2000 * restartTries));
+}
+// this peer is done: ask the bridge for a new one ({"t":"vmode","on":0}); its "ready" starts it
+function renewPeer(why) {
+ serverWhy = why;
+ closePeer();
+ note(true);
+ rd.state = 'renewing';
+ if (ws && ws.readyState === 1) send({ t: 'vmode', on: 0 });
+ else connect();
+}
+// A peer failed before (or after) playing. Twice in a row without a picture: the card, with Retry.
+function peerFailed(why, detail) {
+ peerFails++;
+ if (why === 'codec') { closePeer(); unsupported({ rtc: true, codec: false, secure: window.isSecureContext !== false }); return; }
+ if (why === 'noroute' && everPlayed) why = 'lost'; // it worked before: the network went away
+ if (peerFails >= 2) { closePeer(); failure(why, detail); if (ws && ws.readyState === 1) send({ t: 'vmode', on: 0 }); return; }
+ renewPeer(why);
+}
+// What went wrong, from the browser's side: the ICE state and the candidate pairs it tried.
+function diagnose(p) {
+ const base = { why: serverWhy || 'noroute', text: '' };
+ if (!p || !p.getStats) return Promise.resolve(base);
+ return p.getStats().then((stats) => {
+ let pairs = 0, ok = 0, udp = 0, tcp = 0, gotFrames = 0;
+ stats.forEach((s) => {
+ if (s.type === 'candidate-pair') { pairs++; if (s.state === 'succeeded') ok++; }
+ if (s.type === 'remote-candidate') { if (s.protocol === 'tcp') tcp++; else udp++; }
+ if (s.type === 'inbound-rtp' && s.kind === 'video') gotFrames = s.framesDecoded || 0;
+ });
+ const ice = p.iceConnectionState, dtls = p.connectionState;
+ let why = serverWhy;
+ if (!why) why = ok === 0 ? 'noroute' : dtls !== 'connected' ? 'dtls' : 'media';
+ if (why === 'noroute' && ok > 0) why = 'dtls';
+ return { why, text: `ICE ${ice}, ${dtls}; ${pairs} candidate pairs (${ok} succeeded), the computer offered ${udp} UDP and ${tcp} TCP addresses still listed; ${gotFrames} frames decoded${serverWhy ? '; the computer said: ' + serverWhy : ''}` };
+ }).catch(() => base);
+}
+// which path the media takes (shown in the top bar): UDP, TCP or a relay
+function pathKind() {
+ if (!pc || !pc.getStats) return;
+ pc.getStats().then((stats) => {
+ let sel = '';
+ stats.forEach((s) => { if (s.type === 'transport' && s.selectedCandidatePairId) sel = s.selectedCandidatePairId; });
+ const pair = sel && stats.get(sel);
+ const rc = pair && stats.get(pair.remoteCandidateId);
+ if (rc) { rd.path = rc.candidateType === 'relay' ? 'relay' : rc.protocol || ''; setMode(); }
+ }).catch(() => {});
}
const onFrame = () => { if (vid.videoWidth > 0) enterVideo(); else vid.requestVideoFrameCallback?.(onFrame); };
vid.addEventListener('playing', () => { if (vid.videoWidth > 0) enterVideo(); });
@@ -167,28 +397,92 @@ $('snd').onclick = () => {
$('snd').title = vid.muted ? T.sndOn : T.sndOff;
};
-function show(text) { $('msgtext').textContent = text; $('msg').style.display = 'grid'; }
-
+// ---- the WebSocket: sign-in, signaling, cursor, clipboard. Reconnects with backoff, without a new
+// sign-in while the cookie lasts; if the cookie is gone, /desk sends the browser to the sign-in page ----
+let wsTries = 0, wsTimer = 0, helloSeen = false, wantedClose = false;
function connect() {
- $('msg').style.display = 'none';
- const w = Math.round(innerWidth * devicePixelRatio > 2560 ? innerWidth : innerWidth), h = Math.round(innerHeight);
- giveUp(false);
- const rtcOk = typeof RTCPeerConnection === 'function' ? 1 : 0;
- ws = new WebSocket(`wss://${location.host}/ws?w=${w}&h=${h}&v=${rtcOk}`);
+ clearTimeout(wsTimer);
+ if (ws) { ws.onclose = null; try { ws.close(); } catch (e) {} ws = null; }
+ closePeer();
+ const w = Math.round(innerWidth), h = Math.round(innerHeight);
+ helloSeen = false;
+ ws = new WebSocket(`wss://${location.host}/ws?w=${w}&h=${h}&v=1`);
ws.binaryType = 'arraybuffer';
- ws.onopen = () => send({ t: 'resize', w: w & ~1, h: h & ~1 }); // an already existing session takes this size
+ ws.onopen = () => { rd.wsOpens++; send({ t: 'resize', w: w & ~1, h: h & ~1 }); }; // an existing session takes this size
ws.onmessage = (e) => (typeof e.data === 'string' ? onText(JSON.parse(e.data)) : onBin(new Uint8Array(e.data)));
- ws.onclose = () => { ws = null; giveUp(false); show(T.bye); };
+ ws.onclose = () => {
+ ws = null;
+ closePeer();
+ if (wantedClose) return;
+ wsTries++;
+ if (!helloSeen && wsTries >= 2) {
+ // refused twice before the hello: is the sign-in still valid? (a redirect means it isn't)
+ fetch('/desk', { redirect: 'manual', cache: 'no-store' }).then((r) => {
+ if (r.type === 'opaqueredirect' || r.status === 303) location.replace('/');
+ else later();
+ }, later);
+ } else later();
+ };
+}
+function later() {
+ const d = Math.min(30000, 1000 * 2 ** Math.min(wsTries, 5));
+ if (wsTries >= 3) failure('wsdown', `WebSocket closed ${wsTries} times; next try in ${Math.round(d / 1000)} s`);
+ else note(true);
+ rd.state = 'offline';
+ wsTimer = setTimeout(() => { if (rd.state === 'offline') connect(); }, d);
+}
+function retryAll() {
+ peerFails = 0; restartTries = 0; wsTries = 0;
+ if (ws && ws.readyState === 1 && iceServers.length) { waiting(T.conn); renewPeer(''); }
+ else { waiting(T.conn); connect(); }
}
function onText(m) {
- if (m.t === 'size') { W = m.w; H = m.h; cv.width = W; cv.height = H; ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H); fit(); }
- else if (m.t === 'hello') { $('who').textContent = `${m.user}@${location.hostname}`; setMode(); if (m.rtc && Array.isArray(m.ice)) startRtc(m.ice); }
- else if (m.t === 'rtc') {
- if (m.fail || !pc) giveUp(false);
- else if (m.sdp) { codec = m.codec || ''; pc.setRemoteDescription({ type: 'answer', sdp: m.sdp }).catch(() => giveUp(true)); }
+ if (m.t === 'size') { W = m.w; H = m.h; cv.width = W; cv.height = H; ctx.clearRect(0, 0, W, H); fit(); }
+ else if (m.t === 'hello') {
+ helloSeen = true;
+ wsTries = 0;
+ $('who').textContent = `${m.user}@${location.hostname}`;
+ if (!m.rtc || !Array.isArray(m.ice)) { failure('server', 'the computer has no WebRTC peer for this connection'); return; }
+ iceServers = m.ice;
+ startPeer();
+ } else if (m.t === 'rtc') {
+ if (m.fail) {
+ // the bridge closed this peer and says why (RTC_WHY_* in rtc.h, or codec, offer, restart, or
+ // browser: the page asked for it); a new peer comes with "ready" (or in a later message)
+ if (m.ready && Array.isArray(m.ice)) iceServers = m.ice;
+ const mine = m.fail === 'browser' || m.fail === 'restart';
+ if (!mine) {
+ const was = pc;
+ diagnose(was).then((d) => {
+ closePeer();
+ if (m.fail === 'codec') { peerFailed('codec'); return; }
+ serverWhy = m.fail;
+ peerFails++;
+ let why = m.fail === 'noroute' && everPlayed ? 'lost' : m.fail;
+ if (peerFails >= 2) { failure(why, `the computer said: ${m.fail}; ${d.text}`); return; }
+ note(true);
+ if (m.ready) setTimeout(() => { if (!pc && rd.state !== 'failed') startPeer(); }, 1000);
+ });
+ return;
+ }
+ if (m.why2 === 'server') { closePeer(); failure('server', 'the computer could not create a new peer'); return; }
+ if (m.ready && rd.state !== 'failed') { closePeer(); startPeer(); }
+ return;
+ }
+ if (m.ready && Array.isArray(m.ice)) { // a peer that had to wait (the 2 s limit) is there now
+ iceServers = m.ice;
+ if (!pc && rd.state !== 'failed') startPeer();
+ return;
+ }
+ if (m.sdp && pc) {
+ const p = pc;
+ if (++answersGot < offersSent) return; // an answer to an older offer of ours: the newest one follows
+ if (!m.restart) codec = m.codec || '';
+ p.setRemoteDescription({ type: 'answer', sdp: m.sdp }).then(() => { offering = false; }, () => { offering = false; if (pc === p) peerFailed('offer', 'setRemoteDescription'); });
+ }
}
- else if (m.t === 'pong') $('lat').textContent = `${Math.max(0, Math.round(performance.now() - m.ts))} ms`;
+ else if (m.t === 'pong') { $('lat').textContent = `${Math.max(0, Math.round(performance.now() - m.ts))} ms`; if (vmode) pathKind(); }
else if (m.t === 'cur') {
// cursor moved by someone else (the monitor or another connection): drawn on top
const rc = $('rcur');
@@ -199,20 +493,16 @@ function onText(m) {
} else if (m.t === 'clip') {
if (navigator.clipboard && document.hasFocus()) navigator.clipboard.writeText(m.text).catch(() => {});
lastSent = m.text;
- } else if (m.t === 'bye' && m.lock) location.href = '/?e=3'; // the session got locked: log in again
- else if (m.t === 'bye') show(T.bye);
+ } else if (m.t === 'bye' && m.lock) { wantedClose = true; location.href = '/?e=3'; } // the session got locked: sign in again
+ else if (m.t === 'bye') {
+ wantedClose = true;
+ closePeer();
+ card((c) => { c.append(el('h1', T.bye)); const b = el('div', '', 'btns'), a = el('button', T.retry); a.onclick = () => { wantedClose = false; hideCard(); retryAll(); }; b.append(a); c.append(b); });
+ }
}
function onBin(d) {
- const kind = d[0];
- if (kind === 1) { // rectangle
- const x = d[1] << 8 | d[2], y = d[3] << 8 | d[4], w = d[5] << 8 | d[6], h = d[7] << 8 | d[8];
- const blob = new Blob([d.subarray(10)], { type: d[9] ? 'image/jpeg' : 'image/png' });
- const p = createImageBitmap(blob);
- queue = queue.then(() => p).then((bmp) => { ctx.drawImage(bmp, x, y, w, h); bmp.close(); }, () => {});
- } else if (kind === 3) { // end of frame: acknowledged once it's drawn
- queue = queue.then(() => send({ t: 'ack' }));
- } else if (kind === 2) { // an app's cursor image
+ if (d[0] === 2) { // an app's cursor image
const w = d[1] << 8 | d[2], hx = d[5] << 8 | d[6], hy = d[7] << 8 | d[8];
const url = URL.createObjectURL(new Blob([d.subarray(9)], { type: 'image/png' }));
const k = scale === 1 ? 1 : scale;
@@ -294,16 +584,20 @@ addEventListener('mousemove', (e) => {
if (e.clientY < 8) { $('bar').classList.add('show'); clearTimeout(hideT); hideT = setTimeout(() => $('bar').classList.remove('show'), 2500); }
});
$('fs').onclick = () => (document.fullscreenElement ? document.exitFullscreen() : document.documentElement.requestFullscreen()).then(() => setTimeout(fit, 200));
-// logging out is a POST from this page (the server checks its Origin), never a link another site could open
-$('out').onclick = () => {
+// signing out is a POST from this page (the server checks its Origin), never a link another site could open
+function signOut() {
+ wantedClose = true;
if (ws) ws.close();
+ closePeer();
const f = document.createElement('form');
f.method = 'post'; f.action = '/logout';
document.body.appendChild(f); f.submit();
-};
-$('again').onclick = connect;
+}
+$('out').onclick = signOut;
$('bar').classList.add('show'); hideT = setTimeout(() => $('bar').classList.remove('show'), 3000);
-show(T.conn); connect(); cv.focus();
+const sup = support();
+if (!sup.rtc || !sup.codec || !sup.secure) unsupported(sup);
+else { waiting(T.conn); connect(); cv.focus(); }
</script>
</body>
</html>
diff --git a/server/src/remote/conn.c b/server/src/remote/conn.c
index 7b745b4..843b126 100644
--- a/server/src/remote/conn.c
+++ b/server/src/remote/conn.c
@@ -14,20 +14,20 @@
* shared state, closes every other descriptor and DROPS root for good: TLS, compression and
* the bridge run as the user. This root process only waits, and when the bridge says the
* session was locked it ends every cookie of that user.
- * 4. Bridge: session rectangles → PNG/JPEG → browser; mouse, keyboard and clipboard from the
- * browser (validated, rsec.c rs_browser_input) → session. Flow control: the browser
- * acknowledges each frame ("ack") and only then does the session build the next one.
- * 5. Video (rtc.c, docs/REMOTE.md "Video (WebRTC)"): if the browser can, it sends a WebRTC offer
- * over this same WebSocket; the bridge answers, the session's shared encoder feeds it H.264 or
- * VP8 and Opus (RP_VFRAME, RP_AFRAME), input and the lossless tiles of still areas go over data
- * channels. The tiles above stay as the fallback until the video plays, and if it fails.
+ * 4. Bridge: the screen only ever reaches the browser over WebRTC (rtc.c, docs/REMOTE.md "Video
+ * (WebRTC)"). The browser sends its offer over this WebSocket; the bridge answers, the session's
+ * shared encoder feeds it H.264 or VP8 and Opus (RP_VFRAME, RP_AFRAME), and input and the
+ * lossless PNG refresh of still areas go over data channels. The WebSocket carries the sign-in,
+ * the signaling, the cursor, the clipboard and pings; mouse, keyboard and clipboard from either
+ * are validated by rsec.c rs_browser_input. When a peer fails the bridge says why and makes a new
+ * one; the page restarts ICE or reconnects on its own. There is no other way to show the screen.
+ * 5. ICE over TCP: a connection to this same port that starts with a framed STUN check instead of
+ * a TLS ClientHello is handed, by this root child, to the bridge its check names (ice_tcp()).
* Browser protocol and the security analysis: docs/REMOTE.md. */
#include "sessiond.h"
#include "proto.h"
#include "rsec.h"
-#ifdef LORD_RTC
#include "rtc.h"
-#endif
#include <arpa/inet.h>
#include <ctype.h>
@@ -51,6 +51,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
+#include <sys/un.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
@@ -71,10 +72,10 @@ static char peer[48]; /* address as shown */
static char peer_key[48]; /* lockout key (IPv4, or the IPv6 /64) */
static double deadline;
static int lock_fd = -1; /* bridge → root parent: "L" = the session was locked */
-#ifdef LORD_RTC
-static int want_rtc; /* the page can do WebRTC (/ws?v=1) and Settings allows video */
-#endif
static char *turn_pass; /* read as root before the bridge drops root; the bridge's rtc.c owns it */
+static int ice_lfd = -1; /* ICE-TCP handoff: root's /run/lordos/rtc/<route>, kept by the bridge */
+static char ice_route[9];
+static char ice_path[64];
static double mono(void) {
struct timespec t;
@@ -598,19 +599,14 @@ static int ws_handshake(const RsReq *r) {
/* ---- bridge (runs as the user) ---- */
static int sess = -1; /* session socket */
-static int vmode; /* video mode: tiles go over the data channel, lossless, once still */
-#ifdef LORD_RTC
-static int tiles_lost; /* a tile didn't fit the data channel: ask for them all again */
+static int vmode; /* the video plays: the lossless refresh goes over the data channel */
+static int tiles_lost; /* a refresh tile didn't fit the data channel: ask for them all again */
static double tiles_lost_at;
static Rtc *rtc;
-static int rtc_dead;
-#endif
-static uint32_t *fb; /* copy of the screen (to replace with PNG) */
+static const char *rtc_dead; /* RTC_WHY_* of a peer that just failed */
+static int resets;
+static double reset_at;
static int W, H;
-typedef struct { uint16_t x, y, w, h; } Box16;
-static Box16 jpegs[512];
-static int njpegs;
-static double last_frame;
static int locked, closing;
static void sess_send(int type, const void *a, size_t an) {
@@ -622,16 +618,16 @@ static void sess_send(int type, const void *a, size_t an) {
if (an && write(sess, a, an) != (ssize_t)an) return;
}
-#ifdef LORD_RTC
-/* A lossless tile over the "tiles" data channel: [1][x, y, w, h u16 BE][png]; halved until it fits. */
-static void dc_tile(int x, int y, int w, int h) {
- int len = 0, jpeg = 0;
- unsigned char *img = rd_encode(fb + (size_t)y * W + x, W, w, h, 0, &len, &jpeg);
+/* A lossless tile over the "tiles" data channel: [1][x, y, w, h u16 BE][png]; halved until it fits.
+ * px: the rectangle's own pixels (stride w), straight from the session's message. */
+static void dc_tile(const uint32_t *px, int stride, int x, int y, int w, int h) {
+ int len = 0;
+ unsigned char *img = rd_encode_png(px, stride, w, h, &len);
if (!img) return;
if (len > 250000 && h > 1) {
free(img);
- dc_tile(x, y, w, h / 2);
- dc_tile(x, y + h / 2, w, h - h / 2);
+ dc_tile(px, stride, x, y, w, h / 2);
+ dc_tile(px + (size_t)(h / 2) * stride, stride, x, y + h / 2, w, h - h / 2);
return;
}
unsigned char *m = malloc((size_t)len + 9);
@@ -645,18 +641,6 @@ static void dc_tile(int x, int y, int w, int h) {
}
free(img);
}
-#endif
-
-static void send_rect(int x, int y, int w, int h, int allow_jpeg) {
- int len = 0, jpeg = 0;
- unsigned char *img = rd_encode(fb + (size_t)y * W + x, W, w, h, allow_jpeg, &len, &jpeg);
- if (!img) return;
- unsigned char hd[10] = { 1, (unsigned char)(x >> 8), (unsigned char)x, (unsigned char)(y >> 8), (unsigned char)y,
- (unsigned char)(w >> 8), (unsigned char)w, (unsigned char)(h >> 8), (unsigned char)h, (unsigned char)jpeg };
- ws_frame(2, hd, 10, img, (size_t)len);
- free(img);
- if (jpeg && njpegs < 512) jpegs[njpegs++] = (Box16){ (uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h };
-}
/* Session message → browser. */
static void from_session(int type, const unsigned char *d, uint32_t n) {
@@ -669,41 +653,30 @@ static void from_session(int type, const unsigned char *d, uint32_t n) {
if (!s[0] || !s[1] || s[0] > 8192 || s[1] > 8192) break;
W = s[0];
H = s[1];
- free(fb);
- fb = calloc((size_t)W * H, 4);
- njpegs = 0;
ws_text("{\"t\":\"size\",\"w\":%d,\"h\":%d}", W, H);
}
break;
case RP_RECT: {
- if (n < 8 || !fb) break;
+ /* the session sends rectangles only to a viewer whose video plays, once the screen is still:
+ * the lossless refresh over the video (mirror.c) */
+ if (n < 8 || !vmode || !rtc) break;
uint16_t r[4];
memcpy(r, d, 8);
- if ((uint32_t)r[0] + r[2] > (uint32_t)W || (uint32_t)r[1] + r[3] > (uint32_t)H || n < 8 + (uint32_t)r[2] * r[3] * 4) break;
- for (int j = 0; j < r[3]; j++) memcpy(fb + (size_t)(r[1] + j) * W + r[0], d + 8 + (size_t)j * r[2] * 4, (size_t)r[2] * 4);
- /* whatever gets overwritten no longer needs replacing */
- for (int i = 0; i < njpegs; i++)
- if (jpegs[i].x >= r[0] && jpegs[i].y >= r[1] && jpegs[i].x + jpegs[i].w <= r[0] + r[2] && jpegs[i].y + jpegs[i].h <= r[1] + r[3]) jpegs[i--] = jpegs[--njpegs];
-#ifdef LORD_RTC
- if (vmode) { dc_tile(r[0], r[1], r[2], r[3]); break; } /* the screen is still: lossless */
-#endif
- /* screen in motion (consecutive frames): straight JPEG, replaced with PNG once it's still */
- send_rect(r[0], r[1], r[2], r[3], rd_now() - last_frame < 0.25 ? 2 : 1);
+ if (!r[2] || !r[3] || (uint32_t)r[0] + r[2] > (uint32_t)W || (uint32_t)r[1] + r[3] > (uint32_t)H || n < 8 + (uint32_t)r[2] * r[3] * 4) break;
+ uint32_t *px = malloc((size_t)r[2] * r[3] * 4); /* aligned copy of the message's pixels */
+ if (!px) break;
+ memcpy(px, d + 8, (size_t)r[2] * r[3] * 4);
+ dc_tile(px, r[2], r[0], r[1], r[2], r[3]);
+ free(px);
break;
}
case RP_END:
-#ifdef LORD_RTC
- if (vmode) { /* the data channel is reliable: no browser ack needed */
+ if (vmode && rtc) { /* the data channel is reliable: no browser ack needed */
unsigned char e = 3;
rtc_send(rtc, RTC_CH_TILES, &e, 1, 1);
- sess_send(RP_READY, NULL, 0);
- break;
}
-#endif
- ws_frame(2, (unsigned char[]){ 3 }, 1, NULL, 0);
- last_frame = rd_now();
+ sess_send(RP_READY, NULL, 0);
break;
-#ifdef LORD_RTC
case RP_VFRAME:
if (n > 8 && rtc && d[0] == rtc_codec(rtc)) {
uint32_t ms;
@@ -732,7 +705,6 @@ static void from_session(int type, const unsigned char *d, uint32_t n) {
rtc_send(rtc, RTC_CH_TILES, m, 1 + n, 1);
}
break;
-#endif
case RP_CURSOR:
if (n >= 5) {
int16_t x, y;
@@ -773,18 +745,15 @@ static void from_session(int type, const unsigned char *d, uint32_t n) {
}
}
-#ifdef LORD_RTC
-static void video_off(const char *why);
-static void video_mode(int on) {
- if (on && (!rtc || !rtc_media_ready(rtc) || !rtc_dc_open(rtc, RTC_CH_TILES))) return;
- if (on == vmode) return;
- vmode = on;
- njpegs = 0;
- unsigned char v = (unsigned char)on;
+static void video_reset(const char *code, const char *why);
+/* The browser says the video plays: the lossless refresh of still areas starts (RP_VMODE). */
+static void video_mode(void) {
+ if (!rtc || !rtc_media_ready(rtc) || !rtc_dc_open(rtc, RTC_CH_TILES) || vmode) return;
+ vmode = 1;
+ unsigned char v = 1;
sess_send(RP_VMODE, &v, 1);
- sd_log("video: %s", on ? "playing in the browser; tiles only for still areas" : "back to tiles");
+ sd_log("video: playing in the browser (%s)", rtc_path_kind(rtc));
}
-#endif
/* One validated browser message → session. From a data channel (dc) only the input messages count. */
static void from_browser(const char *j, size_t n, int dc) {
@@ -794,7 +763,7 @@ static void from_browser(const char *j, size_t n, int dc) {
RsInput in;
if (rs_json_parse(j, n, &o, sb, n + 1) == 0) {
int type = rs_browser_input(&o, W, H, &in);
- if (dc && (type == RS_IN_ACK || type == RS_IN_PING || type == RS_IN_RTC || type == RS_IN_ICE || type == RS_IN_VMODE)) type = RS_IN_NONE;
+ if (dc && (type == RS_IN_PING || type == RS_IN_RTC || type == RS_IN_ICE || type == RS_IN_VMODE)) type = RS_IN_NONE;
switch (type) {
case RS_IN_PTR: { int16_t p[2] = { (int16_t)in.a, (int16_t)in.b }; sess_send(RP_PTR, p, 4); break; }
case RS_IN_BTN: { unsigned char p[2] = { (unsigned char)in.a, (unsigned char)in.b }; sess_send(RP_BTN, p, 2); break; }
@@ -816,17 +785,18 @@ static void from_browser(const char *j, size_t n, int dc) {
sess_send(RP_KEY, p, 3);
break;
}
- case RS_IN_ACK: sess_send(RP_READY, NULL, 0); break;
case RS_IN_RESIZE: { uint16_t sz[2] = { (uint16_t)in.a, (uint16_t)in.b }; sess_send(RP_RESIZE, sz, 4); break; }
case RS_IN_PING: ws_text("{\"t\":\"pong\",\"ts\":%lld}", in.ts); break;
case RS_IN_CLIP: sess_send(RP_SETCLIP, in.text, in.tlen); break;
-#ifdef LORD_RTC
case RS_IN_RTC:
- if (rtc && rtc_codec(rtc) == 0 && rtc_offer(rtc, in.text, in.tlen, 1) < 0) video_off("the offer was refused");
+ if (rtc) {
+ int first = rtc_codec(rtc) == 0, e = rtc_offer(rtc, in.text, in.tlen, 1);
+ if (e < 0) video_reset(e == RW_ECODEC ? "codec" : first ? "offer" : "restart",
+ e == RW_ECODEC ? "the browser offers neither H.264 nor VP8" : first ? "the offer was refused" : "the ICE restart was refused");
+ }
break;
case RS_IN_ICE: if (rtc) rtc_candidate(rtc, in.text, in.tlen); break;
- case RS_IN_VMODE: if (in.a) video_mode(1); else video_off("the browser gave up"); break;
-#endif
+ case RS_IN_VMODE: if (in.a) video_mode(); else video_reset("browser", "the browser closed this peer"); break;
default: break;
}
}
@@ -896,7 +866,6 @@ static int conns_full(void) {
return n >= RD_MAX_CONNS;
}
-#ifdef LORD_RTC
static void rtc_signal(void *ud, const char *json) { (void)ud; ws_frame(1, json, strlen(json), NULL, 0); }
static void rtc_state(void *ud, int st, const char *why) {
(void)ud;
@@ -906,7 +875,7 @@ static void rtc_state(void *ud, int st, const char *why) {
uint32_t k = 4000;
memcpy(v + 4, &k, 4);
sess_send(RP_VIDEO, v, 8);
- } else if (st == RTC_FAILED) { rtc_dead = 1; (void)why; }
+ } else if (st == RTC_FAILED) rtc_dead = why ? why : RTC_WHY_SERVER;
}
static void rtc_message(void *ud, int chan, const uint8_t *p, size_t n, int binary) {
(void)ud;
@@ -915,22 +884,29 @@ static void rtc_message(void *ud, int chan, const uint8_t *p, size_t n, int bina
static void rtc_key(void *ud) { (void)ud; sess_send(RP_KEYFRAME, NULL, 0); }
static void rtc_rate(void *ud, unsigned kbps) { (void)ud; uint32_t k = kbps; sess_send(RP_RATE, &k, 4); }
-/* Back to the tiles for good (the video failed or the browser gave up). */
-static void video_off(const char *why) {
- if (!rtc) return;
- char st[160];
- rtc_stats(rtc, st, sizeof st);
- sd_log("video: off (%s; %s)", why, st);
- if (vmode) { unsigned char v = 0; sess_send(RP_VMODE, &v, 1); vmode = 0; }
- unsigned char v[8] = { 0 };
- sess_send(RP_VIDEO, v, 8);
- rtc_free(rtc);
- rtc = NULL;
- ws_text("{\"t\":\"rtc\",\"fail\":1}");
+/* The ICE servers the page may use: our own STUN server (this connection's UDP port, at the address
+ * the page used) and the external ones from Settings. The TURN relay is never given to the browser. */
+static void ice_list(char *ice, size_t cap) {
+ char ip[64] = "";
+ size_t o = 0;
+ RwAddr la;
+ struct sockaddr_storage ls;
+ socklen_t ll = sizeof ls;
+ if (!getsockname(sock, (struct sockaddr *)&ls, &ll)) {
+ if (ls.ss_family == AF_INET) inet_ntop(AF_INET, &((struct sockaddr_in *)&ls)->sin_addr, ip, sizeof ip);
+ else if (ls.ss_family == AF_INET6) inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ls)->sin6_addr, ip, sizeof ip);
+ }
+ if (!strncmp(ip, "::ffff:", 7)) memmove(ip, ip + 7, strlen(ip + 7) + 1);
+ int v6 = !rw_addr_text(ip, &la) && la.family == 6;
+ o += (size_t)snprintf(ice + o, cap - o, "[\"stun:%s%s%s:%d\"", v6 ? "[" : "", ip, v6 ? "]" : "", rtc_port(rtc));
+ for (int i = 0; i < rd_conf.nstun && o < cap - 120; i++) o += (size_t)snprintf(ice + o, cap - o, ",\"stun:%s\"", rd_conf.stun[i]);
+ snprintf(ice + o, cap - o, "]");
}
-/* The WebRTC peer, before the hello: its UDP port is also the browser's STUN server. */
-static void rtc_start(void) {
+/* The WebRTC peer, before the hello and after each failed one: its UDP port is also the browser's
+ * STUN server. The TURN password is only there for the first one (rtc.c wipes it), so a later peer
+ * has no relay unless the bridge kept its key: it doesn't, by design. */
+static int rtc_start(void) {
RtcConfig c;
memset(&c, 0, sizeof c);
c.access = &rd_conf.access;
@@ -948,41 +924,55 @@ static void rtc_start(void) {
for (int i = 0; i < rd_conf.nstun; i++) snprintf(c.stun[i], sizeof c.stun[i], "%s", rd_conf.stun[i]);
snprintf(c.turn, sizeof c.turn, "%s", rd_conf.turn);
snprintf(c.turn_user, sizeof c.turn_user, "%s", rd_conf.turn_user);
+ c.turn_transport = rd_conf.turn_transport;
+ c.turn_has_fp = rd_conf.turn_has_fp;
+ memcpy(c.turn_fp, rd_conf.turn_fp, 32);
c.turn_pass = turn_pass; /* rtc.c owns it now and wipes it */
turn_pass = NULL;
c.max_kbps = 12000;
+ c.ice_lfd = ice_lfd;
+ memcpy(c.route, ice_route, sizeof c.route);
+ c.tcp_port = rd_conf.port;
RtcCallbacks cb = { rtc_signal, rtc_state, rtc_message, rtc_key, rtc_rate, NULL };
rtc = rtc_new(&c, &cb);
+ return rtc ? 0 : -1;
+}
+
+/* This peer is over (it failed, its offer was refused, or the browser closed it): the session stops
+ * sending to it, the browser is told why, and a new peer waits for the browser's next offer. The page
+ * decides whether to try again (docs/REMOTE.md, "Reconnecting"); there is nothing to fall back to. */
+static void video_reset(const char *code, const char *why) {
+ char st[160];
+ if (rtc) rtc_stats(rtc, st, sizeof st); else snprintf(st, sizeof st, "-");
+ sd_log("video: peer closed (%s; %s)", why, st);
+ if (vmode) { unsigned char v = 0; sess_send(RP_VMODE, &v, 1); vmode = 0; }
+ unsigned char v[8] = { 0 };
+ sess_send(RP_VIDEO, v, 8);
+ rtc_free(rtc);
+ rtc = NULL;
+ tiles_lost = 0;
+ /* a new peer at once, but not more often than every 2 s and at most 60 per connection */
+ double now = rd_now();
+ if (++resets > 60) { ws_text("{\"t\":\"rtc\",\"fail\":\"%s\"}", code); ws_text("{\"t\":\"bye\"}"); closing = 1; return; }
+ if (now - reset_at < 2) { reset_at = now + 2; ws_text("{\"t\":\"rtc\",\"fail\":\"%s\"}", code); return; }
+ reset_at = now;
+ char ice[600];
+ if (rtc_start()) { ws_text("{\"t\":\"rtc\",\"fail\":\"%s\",\"why2\":\"server\"}", code); return; }
+ ice_list(ice, sizeof ice);
+ ws_text("{\"t\":\"rtc\",\"fail\":\"%s\",\"ready\":1,\"ice\":%s}", code, ice);
}
-#endif
static void bridge(const struct passwd *pw) {
char hello[96];
snprintf(hello, sizeof hello, "%s %s", peer, pw->pw_name);
sess_send(RP_HELLO, hello, strlen(hello));
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
-#ifdef LORD_RTC
- if (want_rtc) rtc_start();
- if (rtc) {
- /* the built-in STUN server is this connection's UDP port, at the address the page used */
- char ice[600], ip[64] = "";
- size_t o = 0;
- RwAddr la;
- struct sockaddr_storage ls;
- socklen_t ll = sizeof ls;
- if (!getsockname(sock, (struct sockaddr *)&ls, &ll)) {
- if (ls.ss_family == AF_INET) inet_ntop(AF_INET, &((struct sockaddr_in *)&ls)->sin_addr, ip, sizeof ip);
- else if (ls.ss_family == AF_INET6) inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ls)->sin6_addr, ip, sizeof ip);
- }
- if (!strncmp(ip, "::ffff:", 7)) memmove(ip, ip + 7, strlen(ip + 7) + 1);
- int v6 = !rw_addr_text(ip, &la) && la.family == 6;
- o += (size_t)snprintf(ice + o, sizeof ice - o, "[\"stun:%s%s%s:%d\"", v6 ? "[" : "", ip, v6 ? "]" : "", rtc_port(rtc));
- for (int i = 0; i < rd_conf.nstun && o < sizeof ice - 120; i++) o += (size_t)snprintf(ice + o, sizeof ice - o, ",\"stun:%s\"", rd_conf.stun[i]);
- snprintf(ice + o, sizeof ice - o, "]");
+ /* the WebRTC peer is the only way the screen reaches the page: without it, the page says why */
+ if (rtc_start() == 0) {
+ char ice[600];
+ ice_list(ice, sizeof ice);
ws_text("{\"t\":\"hello\",\"user\":\"%s\",\"rtc\":1,\"ice\":%s}", pw->pw_name, ice);
- } else
-#endif
- ws_text("{\"t\":\"hello\",\"user\":\"%s\"}", pw->pw_name);
+ } else ws_text("{\"t\":\"hello\",\"user\":\"%s\",\"rtc\":0}", pw->pw_name);
rs_ws_init(&wsr, RS_WS_MAX_MSG);
unsigned char *sin = NULL;
size_t sin_len = 0, sin_cap = 0;
@@ -999,19 +989,17 @@ static void bridge(const struct passwd *pw) {
for (int i = 0; i < 20 && wout_len; i++) { struct pollfd q = { sock, POLLOUT, 0 }; poll(&q, 1, 100); if (ws_flush()) break; }
return;
}
- struct pollfd p[4] = { { sock, (short)(POLLIN | (wout_len ? POLLOUT : 0)), 0 }, { sess, POLLIN, 0 }, { -1, 0, 0 }, { -1, 0, 0 } };
+ struct pollfd p[2 + RTC_MAX_FDS] = { { sock, (short)(POLLIN | (wout_len ? POLLOUT : 0)), 0 }, { sess, POLLIN, 0 } };
int np = 2;
/* while there is something to send, nothing more is read from the session (flow control) */
if (wout_len > (4u << 20)) p[1].events = 0;
- int timeout = njpegs ? 200 : 15000;
-#ifdef LORD_RTC
+ int timeout = 15000;
if (rtc) {
- np += rtc_pollfds(rtc, p + 2, 2);
+ np += rtc_pollfds(rtc, p + 2, RTC_MAX_FDS);
int t = rtc_timeout(rtc);
if (t >= 0 && t < timeout) timeout = t;
- }
+ } else if (reset_at > rd_now()) timeout = 200;
if (tiles_lost && timeout > 500) timeout = 500;
-#endif
int r = SSL_pending(ssl) ? 1 : poll(p, (nfds_t)np, timeout);
if (r < 0 && errno != EINTR) return;
if (SSL_pending(ssl) || (p[0].revents & POLLIN)) {
@@ -1048,27 +1036,23 @@ static void bridge(const struct passwd *pw) {
memmove(sin, sin + off, sin_len - off);
sin_len -= off;
}
-#ifdef LORD_RTC
if (rtc) {
if (np > 2) rtc_dispatch(rtc, p + 2, np - 2);
if (rtc) rtc_tick(rtc);
- if (rtc_dead) { rtc_dead = 0; video_off("connection failed"); }
+ if (rtc_dead) { const char *why = rtc_dead; rtc_dead = NULL; video_reset(why, "the connection failed"); }
/* tiles that didn't fit: the whole lossless layer again, at most every 2 s */
- if (vmode && tiles_lost && rd_now() - tiles_lost_at > 2) {
+ if (rtc && vmode && tiles_lost && rd_now() - tiles_lost_at > 2) {
tiles_lost = 0;
unsigned char v = 1;
sess_send(RP_VMODE, &v, 1);
}
+ } else if (reset_at && rd_now() >= reset_at && !closing) { /* a reset that had to wait: the new peer */
+ reset_at = rd_now();
+ char ice[600];
+ if (rtc_start() == 0) { ice_list(ice, sizeof ice); ws_text("{\"t\":\"rtc\",\"ready\":1,\"ice\":%s}", ice); }
+ else { ws_text("{\"t\":\"rtc\",\"fail\":\"server\"}"); reset_at = 0; }
}
-#endif
if (locked) { ws_flush(); return; }
- /* screen is still: whatever was JPEG is replaced with PNG (lossless) */
- if (njpegs && !vmode && rd_now() - last_frame > 0.4 && wout_len < (1u << 20)) {
- int k = njpegs;
- njpegs = 0;
- for (int i = 0; i < k; i++) send_rect(jpegs[i].x, jpegs[i].y, jpegs[i].w, jpegs[i].h, 0);
- ws_frame(2, (unsigned char[]){ 4 }, 1, NULL, 0); /* replacement (no ack) */
- }
if (rd_now() - last_input > 60) return; /* the browser sends a ping every 2 s */
}
}
@@ -1100,8 +1084,8 @@ static void close_all_but(const int *keep, int nkeep) {
static int drop_root(const struct passwd *pw, pid_t parent) {
if (rd_shared) munmap(rd_shared, sizeof *rd_shared);
rd_shared = NULL;
- int keep[3] = { sock, sess, lock_fd };
- close_all_but(keep, 3);
+ int keep[4] = { sock, sess, lock_fd, ice_lfd };
+ close_all_but(keep, 4);
clearenv();
struct rlimit core = { 0, 0 };
setrlimit(RLIMIT_CORE, &core);
@@ -1121,6 +1105,86 @@ static int drop_root(const struct passwd *pw, pid_t parent) {
return 0;
}
+/* ---- ICE over TCP (docs/REMOTE.md, "ICE over TCP") ---- */
+
+/* As root, before the bridge forks: a listening socket in root's /run/lordos/rtc (0700) named after
+ * this connection's random route. Only root can connect to it: the connection children below. */
+static void ice_listen(void) {
+ struct stat st;
+ mkdir(SD_RUN_DIR, 0755);
+ mkdir(RD_RTC_DIR, 0700);
+ if (lstat(RD_RTC_DIR, &st) || !S_ISDIR(st.st_mode) || st.st_uid != 0 || (st.st_mode & 077)) return;
+ static const char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+ unsigned char b[8];
+ if (getrandom(b, 8, 0) != 8) return;
+ for (int i = 0; i < 8; i++) ice_route[i] = al[b[i] % (sizeof al - 1)];
+ ice_route[8] = 0;
+ snprintf(ice_path, sizeof ice_path, RD_RTC_DIR "/%s", ice_route);
+ int fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ struct sockaddr_un a = { .sun_family = AF_UNIX };
+ snprintf(a.sun_path, sizeof a.sun_path, "%s", ice_path);
+ unlink(ice_path);
+ mode_t old = umask(077);
+ int ok = fd >= 0 && !bind(fd, (struct sockaddr *)&a, sizeof a) && !listen(fd, 8);
+ umask(old);
+ if (!ok) { if (fd >= 0) close(fd); unlink(ice_path); ice_path[0] = 0; ice_route[0] = 0; return; }
+ ice_lfd = fd;
+}
+
+static void ice_unlisten(void) {
+ if (ice_lfd >= 0) close(ice_lfd);
+ ice_lfd = -1;
+ if (ice_path[0]) unlink(ice_path);
+ ice_path[0] = 0;
+}
+
+static int read_full(int fd, unsigned char *b, size_t n) {
+ size_t got = 0;
+ while (got < n) {
+ double left = deadline - mono();
+ struct pollfd p = { fd, POLLIN, 0 };
+ if (left <= 0 || poll(&p, 1, (int)(left * 1000) + 1) <= 0) return -1;
+ ssize_t k = recv(fd, b + got, n - got, MSG_DONTWAIT);
+ if (k < 0 && (errno == EINTR || errno == EAGAIN)) continue;
+ if (k <= 0) return -1;
+ got += (size_t)k;
+ }
+ return 0;
+}
+
+/* A connection to the remote desktop port that starts with an RFC 4571 frame instead of a TLS
+ * ClientHello: the browser's ICE-TCP check. This root child reads exactly that first frame, takes the
+ * route from its user name (rtcwire.c rw_ice_route, nothing else is trusted here) and hands the
+ * socket and the frame to that bridge, which checks the message's integrity with its ICE password.
+ * The address already passed the same rules as any other connection (sessiond.c accept_remote). */
+static void ice_tcp(int fd) {
+ unsigned char b[2 + RW_TCP_FRAME_MAX];
+ size_t len = 0;
+ if (read_full(fd, b, 2) || rw_tcp_frame(b, 2 + RW_TCP_FRAME_MAX, &len) < 0) return;
+ len = (size_t)(b[0] << 8 | b[1]);
+ if (len < 20 || len > 1500 || read_full(fd, b + 2, len)) return;
+ char route[9], path[64];
+ if (rw_ice_route(b + 2, len, route)) return;
+ snprintf(path, sizeof path, RD_RTC_DIR "/%s", route);
+ struct stat st;
+ if (lstat(path, &st) || !S_ISSOCK(st.st_mode) || st.st_uid != 0) return;
+ int u = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
+ struct sockaddr_un a = { .sun_family = AF_UNIX };
+ snprintf(a.sun_path, sizeof a.sun_path, "%s", path);
+ if (u < 0 || connect(u, (struct sockaddr *)&a, sizeof a)) { if (u >= 0) close(u); return; }
+ char cbuf[CMSG_SPACE(sizeof(int))];
+ memset(cbuf, 0, sizeof cbuf);
+ struct iovec iov = { b, 2 + len };
+ struct msghdr mh = { .msg_iov = &iov, .msg_iovlen = 1, .msg_control = cbuf, .msg_controllen = sizeof cbuf };
+ struct cmsghdr *cm = CMSG_FIRSTHDR(&mh);
+ cm->cmsg_level = SOL_SOCKET;
+ cm->cmsg_type = SCM_RIGHTS;
+ cm->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(cm), &fd, sizeof(int));
+ if (sendmsg(u, &mh, MSG_NOSIGNAL) < 0) sd_log("ICE-TCP: could not hand the connection over");
+ close(u);
+}
+
/* ---- one connection ---- */
static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {
@@ -1146,11 +1210,9 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {
if (h > 1600) h = 1600;
sess = sd_session_get(&me, w & ~1, h & ~1);
if (sess < 0) { respond(503, NULL); return; }
-#ifdef LORD_RTC
- want_rtc = rd_conf.video && query_num(r->query, "v", 0) == 1;
/* the TURN password, read here as root (RD_TURN_KEY is root 0600) and handed to the bridge's WebRTC
* peer in memory only; never logged, never sent to the browser */
- if (want_rtc && rd_conf.turn[0] && rd_conf.turn_user[0]) {
+ if (rd_conf.turn[0] && rd_conf.turn_user[0]) {
int f = open(RD_TURN_KEY, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
struct stat st;
char buf[160];
@@ -1165,15 +1227,15 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {
}
explicit_bzero(buf, sizeof buf);
}
-#endif
- if (ws_handshake(r)) return;
+ ice_listen();
+ if (ws_handshake(r)) { ice_unlisten(); return; }
conn_register(name);
int pp[2];
if (pipe2(pp, O_CLOEXEC)) return;
pid_t parent = getpid();
alarm(0);
pid_t b = fork();
- if (b < 0) return;
+ if (b < 0) { ice_unlisten(); return; }
if (b == 0) {
close(pp[0]);
lock_fd = pp[1];
@@ -1186,6 +1248,8 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {
}
/* the root side keeps nothing of the connection: it only listens for "the session was locked" */
if (turn_pass) { explicit_bzero(turn_pass, strlen(turn_pass)); free(turn_pass); turn_pass = NULL; }
+ if (ice_lfd >= 0) close(ice_lfd); /* the bridge has it; the path goes when the bridge does */
+ ice_lfd = -1;
close(pp[1]);
close(sess);
close(sock);
@@ -1213,6 +1277,7 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {
}
}
while (waitpid(b, NULL, 0) < 0 && errno == EINTR) {}
+ ice_unlisten();
}
void rd_serve(SSL_CTX *ctx, int fd, const char *ip, const char *key) {
@@ -1224,6 +1289,18 @@ void rd_serve(SSL_CTX *ctx, int fd, const char *ip, const char *key) {
sd_log_prefix(prefix);
alarm(ALARM_SECONDS); /* backstop: SIGALRM ends a stuck connection */
deadline = mono() + HEAD_SECONDS;
+ /* TLS starts with a handshake record (0x16); an ICE-TCP check with its 16-bit length (< 0x10) */
+ for (;;) {
+ unsigned char first = 0;
+ struct pollfd p = { fd, POLLIN, 0 };
+ double left = deadline - mono();
+ if (left <= 0 || poll(&p, 1, (int)(left * 1000) + 1) <= 0) return;
+ ssize_t k = recv(fd, &first, 1, MSG_PEEK | MSG_DONTWAIT);
+ if (k < 0 && (errno == EINTR || errno == EAGAIN)) continue;
+ if (k <= 0) return;
+ if (first < 0x10) { ice_tcp(fd); return; }
+ break;
+ }
ssl = SSL_new(ctx);
if (!ssl) return;
SSL_set_fd(ssl, fd);
diff --git a/server/src/remote/enc.c b/server/src/remote/enc.c
index 943b766..d1f1d6f 100644
--- a/server/src/remote/enc.c
+++ b/server/src/remote/enc.c
@@ -1,10 +1,6 @@
-/* enc.c — compression of screen rectangles for the browser (lord-sessiond).
- *
- * PNG (lossless, ideal for text and UI) or JPEG (photos, videos, large gradients),
- * with vendored stb_image_write. It picks on its own: if the PNG of a large rectangle comes out heavy
- * (more than 1 byte per pixel) or the screen is in motion (allow_jpeg 2: straight to JPEG,
- * without trying PNG), JPEG is sent and the bridge replaces it with PNG once the screen stays
- * still (conn.c). */
+/* enc.c — PNG for the remote desktop (lord-sessiond's bridge), with vendored stb_image_write: the
+ * lossless refresh of still areas that goes over the video's data channel, and the apps' cursor images.
+ * The screen itself only travels as WebRTC video (docs/REMOTE.md, "Video (WebRTC)"). */
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STBI_WRITE_NO_STDIO
#pragma GCC diagnostic push
@@ -17,23 +13,6 @@
#include "sessiond.h"
#include <stdlib.h>
-#include <string.h>
-
-typedef struct { unsigned char *p; int n, cap; } Buf;
-
-static void put(void *ctx, void *data, int size) {
- Buf *b = ctx;
- if (b->n + size > b->cap) {
- int cap = b->cap ? b->cap * 2 : 65536;
- while (cap < b->n + size) cap *= 2;
- unsigned char *nb = realloc(b->p, (size_t)cap);
- if (!nb) return;
- b->p = nb;
- b->cap = cap;
- }
- memcpy(b->p + b->n, data, (size_t)size);
- b->n += size;
-}
static unsigned char *to_rgb(const uint32_t *px, int stride, int w, int h) {
unsigned char *rgb = malloc((size_t)w * h * 3);
@@ -51,40 +30,15 @@ static unsigned char *to_rgb(const uint32_t *px, int stride, int w, int h) {
return rgb;
}
-unsigned char *rd_encode(const uint32_t *px, int stride, int w, int h, int allow_jpeg, int *len, int *jpeg) {
- *jpeg = 0;
+unsigned char *rd_encode_png(const uint32_t *px, int stride, int w, int h, int *len) {
*len = 0;
unsigned char *rgb = to_rgb(px, stride, w, h);
if (!rgb) return NULL;
- long area = (long)w * h;
- if (allow_jpeg == 2 && area >= 16384) {
- Buf b = { 0 };
- stbi_write_jpg_to_func(put, &b, w, h, 3, rgb, 70);
- free(rgb);
- if (!b.p) return NULL;
- *len = b.n;
- *jpeg = 1;
- return b.p;
- }
stbi_write_png_compression_level = 2;
int n = 0;
unsigned char *png = stbi_write_png_to_mem(rgb, w * 3, w, h, 3, &n);
- if (png && (!allow_jpeg || area < 16384 || n <= area)) {
- free(rgb);
- *len = n;
- return png;
- }
- Buf b = { 0 };
- stbi_write_jpg_to_func(put, &b, w, h, 3, rgb, 80);
free(rgb);
- if (b.p && (!png || b.n < n)) {
- free(png);
- *len = b.n;
- *jpeg = 1;
- return b.p;
- }
- free(b.p);
- *len = n;
+ *len = png ? n : 0;
return png;
}
diff --git a/server/src/remote/mirror.c b/server/src/remote/mirror.c
index 8458bfc..70bdf12 100644
--- a/server/src/remote/mirror.c
+++ b/server/src/remote/mirror.c
@@ -2,9 +2,12 @@
*
* lord-server (already as the session's user) listens on $XDG_RUNTIME_DIR/lordos/remote.sock;
* it only accepts root (lord-sessiond: the monitor and the network bridge) and the same user (SO_PEERCRED). Through it:
- * - it sends the screen in 64×64 tiles: on each frame it compares with the previous copy and
- * each connection accumulates the tiles that changed until it asks for the next frame
- * (RP_READY): that way a slow network gets fewer frames but always the current state;
+ * - the screen goes to the session's shared live encoder (media/mstream.c) for every viewer that
+ * subscribed to the video (RP_VIDEO); the encoded frames go back to each one (RP_VFRAME);
+ * - on each frame it compares with the previous copy in 64×64 tiles: what changed is announced at
+ * once (RP_DAMAGE) and, once the screen has been still for STILL_MS, sent as lossless rectangles
+ * (RP_RECT) to each viewer whose video plays (RP_VMODE), which the browser draws over the video;
+ * - nothing is captured or compared while no viewer takes the video (mirror_watchers());
* - it sends the cursor position and image separately, and the text clipboard;
* - it receives mouse, keyboard and clipboard, which main.c injects as if they were local.
* lord-sessiond (docs/SESSIONS.md) connects as root and marks itself with RP_SEAT: it doesn't get the
@@ -47,7 +50,7 @@ typedef struct {
/* the video path (docs/REMOTE.md, "Video (WebRTC)") */
int vcodec, vaudio, vneed_key; /* subscribed to the session's encoder (1 H.264, 2 VP8) */
unsigned vkbps;
- int vmode; /* tiles only when the screen is still, damage right away */
+ int vmode; /* the video plays: lossless tiles once still, damage right away */
} Client;
static int lfd = -1;
@@ -200,7 +203,8 @@ static void pump(Client *c) {
msg(c, RP_CLIP, clip_text, clip_len, NULL, 0);
c->clip_gen = clip_gen_seen;
}
- if (c->ready && c->dirty && fb && c->out_len - c->out_off < OUT_LIMIT && (!c->vmode || still())) {
+ /* the lossless refresh: only for a viewer whose video plays, only once the screen is still */
+ if (c->vmode && c->ready && c->dirty && fb && c->out_len - c->out_off < OUT_LIMIT && still()) {
int any = 0;
for (int ty = 0; ty < TH; ty++) {
for (int tx = 0; tx < TW;) {
@@ -295,6 +299,7 @@ static void handle(Client *c, int type, const unsigned char *d, uint32_t n) {
if (c->vcodec && c->vcodec != was) c->vneed_key = 1;
video_update();
if (c->vcodec && fb) mstream_frame(fb, W, W, H);
+ else if (c->vcodec && cb.refresh) cb.refresh(); /* the first viewer: a whole frame */
}
break;
case RP_KEYFRAME: if (c->vcodec) { mstream_keyframe(c->vcodec); c->vneed_key = 1; } break;
@@ -309,7 +314,7 @@ static void handle(Client *c, int type, const unsigned char *d, uint32_t n) {
case RP_VMODE:
if (n >= 1) {
c->vmode = d[0] == 1;
- all_dirty(c); /* the lossless layer starts (or the tiles start) whole */
+ all_dirty(c); /* the lossless layer starts whole */
c->ready = 1;
}
break;
@@ -472,6 +477,12 @@ int mirror_clients(void) {
return n;
}
+int mirror_watchers(void) {
+ int n = 0;
+ for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && !cl[i].seat && cl[i].vcodec) n++;
+ return n;
+}
+
int mirror_has_seat(void) {
for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].seat) return 1;
return 0;
@@ -548,7 +559,7 @@ void mirror_dispatch(const struct pollfd *p, int n) {
}
void mirror_frame(const uint32_t *px, int stride, int w, int h) {
- if (lfd < 0 || !mirror_clients()) { free(fb); fb = NULL; return; }
+ if (lfd < 0 || !mirror_watchers()) { free(fb); fb = NULL; return; }
int resized = !fb || w != W || h != H, any = 0;
int new_size = w != W || h != H;
if (resized) {
@@ -645,7 +656,7 @@ double mirror_timeout(void) {
}
void mirror_cursor(int x, int y, int mode, const uint32_t *argb, int w, int h, int hx, int hy, unsigned gen) {
- if (lfd < 0 || !mirror_clients()) return;
+ if (lfd < 0 || !mirror_watchers()) return;
cur_x = x;
cur_y = y;
cur_mode = mode;
diff --git a/server/src/remote/mirror.h b/server/src/remote/mirror.h
index c170558..8b01b1b 100644
--- a/server/src/remote/mirror.h
+++ b/server/src/remote/mirror.h
@@ -36,7 +36,10 @@ int mirror_start(int w, int h, const MirrorInput *in);
void mirror_stop(void);
int mirror_pollfds(struct pollfd *p, int max); /* adds its fds; returns how many */
void mirror_dispatch(const struct pollfd *p, int n); /* the same ones, with revents */
-int mirror_clients(void); /* connections watching the screen (not lord-sessiond) */
+int mirror_clients(void); /* remote desktop connections (not lord-sessiond) */
+/* Connections that take the video now (RP_VIDEO): only for them is the screen captured, compared and
+ * encoded. A browser that is still connecting, or reconnecting its peer, costs nothing. */
+int mirror_watchers(void);
int mirror_has_seat(void); /* is lord-sessiond (monitor) connected? */
/* Requests from the session to whoever shows it (short JSON): RP_SESSION to every connection
* ({"a":"lock"}: lock; {"a":"switch"}: switch user) and RP_GREETER to the monitor. */
@@ -45,12 +48,12 @@ void mirror_greeter(const char *json);
/* To lord-sessiond: the first frame after an RP_DISPLAY_KEEP is on the screen (RP_SHOWN). */
void mirror_shown(void);
-/* A new screen frame (without the cursor). Compares it tile by tile with the previous one and sends
- * only what changed to each connection that is ready. */
+/* A new screen frame (without the cursor), for the encoder; compares it tile by tile with the previous
+ * one: the damage goes out at once, the lossless tiles once the screen is still. */
void mirror_frame(const uint32_t *px, int stride, int w, int h);
/* Cursor: position and mode (0 arrow, 1 app image, 2 hidden); the image only if gen changed. */
void mirror_cursor(int x, int y, int mode, const uint32_t *argb, int w, int h, int hx, int hy, unsigned gen);
-/* The video path: lossless tiles for video-mode viewers once the screen is still, the encoder's
+/* Lossless tiles for viewers whose video plays once the screen is still, the encoder's
* pending frame. Called on every loop iteration; mirror_timeout says when it has work (ms, -1 none). */
void mirror_tick(void);
double mirror_timeout(void);
diff --git a/server/src/remote/remote_admin.c b/server/src/remote/remote_admin.c
index 5e0c1ae..33c9053 100644
--- a/server/src/remote/remote_admin.c
+++ b/server/src/remote/remote_admin.c
@@ -9,10 +9,11 @@
* remote-allow add|del <cidr> the "only these addresses" list (net=list)
* remote-member <u>\n1|0 adds or removes a user from the lordos-remote group
* remote-kick <pid> cuts a connection (only a pid from lord-sessiond's list)
- * remote-ice <lines> the video path (docs/REMOTE.md, "Video (WebRTC)"): video=on|off,
+ * remote-ice <lines> the WebRTC connection (docs/REMOTE.md, "Video (WebRTC)"):
* stun=<host:port>[,…] (up to 4), turn=<host:port> (empty: none),
- * turn_user=<name>, turn_pass=<password> (kept in turn.key, root 0600;
- * never written to the config, the logs or an answer)
+ * turn_transport=udp|tcp|tls, turn_fp=<sha-256> (TLS: a pinned certificate;
+ * empty: the system's CAs), turn_user=<name>, turn_pass=<password> (kept in
+ * turn.key, root 0600; never written to the config, the logs or an answer)
*
* The remote desktop is part of lord-sessiond (lordos.service, docs/SESSIONS.md): these
* actions only change /etc/lordos/remote/config and notify it with SIGHUP. */
@@ -51,22 +52,23 @@ static int write_file(const char *path, const char *text, mode_t mode) {
typedef struct {
int enabled, port, group_only, insecure_dev, net;
- int video, nstun, turn_key;
- char stun[4][96], turn[96], turn_user[129];
+ int nstun, turn_key, turn_transport;
+ char stun[4][96], turn[96], turn_user[129], turn_fp[96];
} Conf;
static void conf_read(Conf *c) {
c->port = 5673;
c->enabled = c->group_only = c->insecure_dev = 0;
c->net = RS_NET_LAN;
- c->video = 1;
c->nstun = c->turn_key = 0;
- c->turn[0] = c->turn_user[0] = 0;
+ c->turn_transport = RW_TURN_UDP;
+ c->turn[0] = c->turn_user[0] = c->turn_fp[0] = 0;
FILE *f = fopen(CONF_DIR "/config", "r");
char line[256], s[32], h[160];
int v;
while (f && fgets(line, sizeof line, f)) {
- if (sscanf(line, "video=%d", &v) == 1) { c->video = v != 0; continue; }
+ if (sscanf(line, "turn_transport=%31s", s) == 1) { if (rw_turn_transport(s) >= 0) c->turn_transport = rw_turn_transport(s); continue; }
+ if (sscanf(line, "turn_fp=%159s", h) == 1) { uint8_t fp[32]; if (!rw_fp_parse(h, fp)) snprintf(c->turn_fp, sizeof c->turn_fp, "%.95s", h); continue; }
if (sscanf(line, "turn_key=%d", &v) == 1) { c->turn_key = v == 1; continue; }
if (sscanf(line, "stun=%159s", h) == 1) { if (rw_ice_server_valid(h) && c->nstun < 4) snprintf(c->stun[c->nstun++], 96, "%.95s", h); continue; }
if (sscanf(line, "turn=%159s", h) == 1) { if (rw_ice_server_valid(h)) snprintf(c->turn, sizeof c->turn, "%.95s", h); continue; }
@@ -83,11 +85,15 @@ static void conf_read(Conf *c) {
static int conf_write(const Conf *c) {
mkdir("/etc/lordos", 0755);
mkdir(CONF_DIR, 0755);
- char text[1400];
- int n = snprintf(text, sizeof text, "# LordOS · remote desktop (Settings → Sharing). See docs/REMOTE.md\nenabled=%d\nport=%d\nallow=%s\nnet=%s\ninsecure_dev=%d\nvideo=%d\n",
- c->enabled, c->port, c->group_only ? "group" : "all", rs_net_name(c->net), c->insecure_dev, c->video);
+ char text[1600];
+ int n = snprintf(text, sizeof text, "# LordOS · remote desktop (Settings → Sharing). See docs/REMOTE.md\nenabled=%d\nport=%d\nallow=%s\nnet=%s\ninsecure_dev=%d\n",
+ c->enabled, c->port, c->group_only ? "group" : "all", rs_net_name(c->net), c->insecure_dev);
for (int i = 0; i < c->nstun; i++) n += snprintf(text + n, sizeof text - (size_t)n, "stun=%s\n", c->stun[i]);
- if (c->turn[0]) n += snprintf(text + n, sizeof text - (size_t)n, "turn=%s\nturn_user=%s\nturn_key=%d\n", c->turn, c->turn_user, c->turn_key);
+ if (c->turn[0]) {
+ n += snprintf(text + n, sizeof text - (size_t)n, "turn=%s\nturn_user=%s\nturn_key=%d\nturn_transport=%s\n", c->turn, c->turn_user, c->turn_key,
+ rw_turn_transport_name(c->turn_transport));
+ if (c->turn_fp[0] && c->turn_transport == RW_TURN_TLS) n += snprintf(text + n, sizeof text - (size_t)n, "turn_fp=%s\n", c->turn_fp);
+ }
return write_file(CONF_DIR "/config", text, 0644);
}
@@ -344,7 +350,7 @@ void remote_act_failures(const AdminReq *r, const char *arg, size_t len) {
admin_ok("remote.failures", "count", lerr_int(rows), NULL);
}
-/* remote-ice: the video path's settings (Settings → Sharing → Video). Every line is checked before
+/* remote-ice: the WebRTC connection's settings (Settings → Sharing → Connection). Every line is checked before
* anything is written; the TURN password goes only to turn.key (root 0600), which lord-sessiond reads
* for each video connection and hands to the bridge in memory. */
void remote_act_ice(const AdminReq *r, const char *arg, size_t len) {
@@ -356,8 +362,7 @@ void remote_act_ice(const AdminReq *r, const char *arg, size_t len) {
int set_turn = 0, set_pass = 0;
snprintf(buf, sizeof buf, "%.*s", (int)len, arg);
for (char *save = NULL, *line = strtok_r(buf, "\n", &save); line; line = strtok_r(NULL, "\n", &save)) {
- if (!strcmp(line, "video=on") || !strcmp(line, "video=off")) c.video = !strcmp(line, "video=on");
- else if (!strncmp(line, "stun=", 5)) {
+ if (!strncmp(line, "stun=", 5)) {
c.nstun = 0;
char list[600];
snprintf(list, sizeof list, "%s", line + 5);
@@ -369,6 +374,13 @@ void remote_act_ice(const AdminReq *r, const char *arg, size_t len) {
if (line[5] && !rw_ice_server_valid(line + 5)) { admin_err("remote.badIceServer", NULL); goto out; }
snprintf(c.turn, sizeof c.turn, "%.95s", line + 5);
set_turn = 1;
+ } else if (!strncmp(line, "turn_transport=", 15)) {
+ if (rw_turn_transport(line + 15) < 0) { admin_err("admin.invalidArgument", NULL); goto out; }
+ c.turn_transport = rw_turn_transport(line + 15);
+ } else if (!strncmp(line, "turn_fp=", 8)) {
+ uint8_t fp[32];
+ if (line[8] && rw_fp_parse(line + 8, fp)) { admin_err("remote.badTurnFingerprint", NULL); goto out; }
+ snprintf(c.turn_fp, sizeof c.turn_fp, "%.95s", line + 8);
} else if (!strncmp(line, "turn_user=", 10)) {
if (!rw_turn_user_valid(line + 10)) { admin_err("remote.badTurnUser", NULL); goto out; }
snprintf(c.turn_user, sizeof c.turn_user, "%.128s", line + 10);
@@ -378,7 +390,7 @@ void remote_act_ice(const AdminReq *r, const char *arg, size_t len) {
set_pass = 1;
} else { admin_err("admin.invalidArgument", NULL); goto out; }
}
- if (set_turn && !c.turn[0]) { c.turn_user[0] = 0; c.turn_key = 0; set_pass = 0; unlink(RD_TURN_KEY_PATH); }
+ if (set_turn && !c.turn[0]) { c.turn_user[0] = c.turn_fp[0] = 0; c.turn_key = 0; c.turn_transport = RW_TURN_UDP; set_pass = 0; unlink(RD_TURN_KEY_PATH); }
if (c.turn[0] && !c.turn_user[0]) { admin_err("remote.badTurnUser", NULL); goto out; }
if (set_pass) {
if (!c.turn[0]) { admin_err("admin.invalidArgument", NULL); goto out; }
diff --git a/server/src/remote/rsec.c b/server/src/remote/rsec.c
index e612a6a..4b27e6f 100644
--- a/server/src/remote/rsec.c
+++ b/server/src/remote/rsec.c
@@ -570,7 +570,6 @@ int rs_browser_input(const RsJson *j, int W, int H, RsInput *in) {
in->b = b != 0;
return in->type = RS_IN_KEY;
}
- if (!strcmp(t, "ack")) return in->type = RS_IN_ACK;
if (!strcmp(t, "resize")) {
if (rs_json_num(j, "w", &a) || rs_json_num(j, "h", &b) || a < 640 || b < 480 || a > 3840 || b > 2400) return RS_IN_NONE;
in->a = (int)(a & ~1LL);
diff --git a/server/src/remote/rsec.h b/server/src/remote/rsec.h
index cdec6e6..5c2c613 100644
--- a/server/src/remote/rsec.h
+++ b/server/src/remote/rsec.h
@@ -102,7 +102,7 @@ const char *rs_json_str(const RsJson *o, const char *key, size_t *len);
int rs_json_num(const RsJson *o, const char *key, long long *v);
/* A browser message, validated and clamped against a W×H screen. */
-enum { RS_IN_NONE = 0, RS_IN_PTR, RS_IN_BTN, RS_IN_WHEEL, RS_IN_SCROLL, RS_IN_PINCH, RS_IN_KEY, RS_IN_ACK, RS_IN_RESIZE,
+enum { RS_IN_NONE = 0, RS_IN_PTR, RS_IN_BTN, RS_IN_WHEEL, RS_IN_SCROLL, RS_IN_PINCH, RS_IN_KEY, RS_IN_RESIZE,
RS_IN_PING, RS_IN_CLIP, RS_IN_RTC, RS_IN_ICE, RS_IN_VMODE };
#define RS_SDP_MAX 32768 /* {"t":"rtc","sdp":…} (rtcwire.h RW_SDP_MAX) */
#define RS_CLIP_MAX (1u << 20)
diff --git a/server/src/remote/rtc.c b/server/src/remote/rtc.c
index f389c36..8107b10 100644
--- a/server/src/remote/rtc.c
+++ b/server/src/remote/rtc.c
@@ -8,13 +8,21 @@
* UDP socket is a plain STUN server for this connection only (the browser is told to use it), so a
* LAN works with no outside server. It has to be another port: browsers hand every STUN packet
* from their STUN server's address to the server's own request handler, ICE answers included.
+ * - ICE over TCP (RFC 6544): passive candidates on the remote desktop's own TCP port, for networks
+ * that drop UDP. lord-sessiond's root connection child reads the first framed check of such a
+ * connection, picks the bridge by the first 8 characters of its user name (our "route") and hands
+ * the socket over; everything after that, the check's integrity included, is ours.
+ * - ICE restarts: a later offer brings new credentials for both sides over the same DTLS association,
+ * so a changed or interrupted network path recovers without a new sign-in or a new peer.
* - DTLS 1.2 with OpenSSL (lord-sessiond already links it), we are the server; the browser's
* certificate must hash to the fingerprint in the offer that came over the authenticated
* WebSocket, or the connection ends.
* - SRTP with libsrtp2 (keys exported from DTLS), RTCP feedback (NACK, PLI, FIR, receiver reports).
* - The data channels over SCTP with usrsctp (no threads: its timers run from rtc_tick).
* - Optional external STUN (our public address) and TURN (a relayed candidate; the relay only ever
- * sees DTLS and SRTP). The TURN password never leaves this process and is never logged.
+ * sees DTLS and SRTP), reached over UDP, TCP or TLS (checked against the system's CAs and the
+ * server's name, or a pinned fingerprint). The TURN password never leaves this process and is never
+ * logged.
* Every packet goes through the strict parsers in rtcwire.c first. */
#include "rtc.h"
#include "sessiond.h"
@@ -30,7 +38,11 @@
#include <stdlib.h>
#include <string.h>
#include <sys/random.h>
+#include <netinet/tcp.h>
+#include <poll.h>
+#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <time.h>
#include <unistd.h>
@@ -46,8 +58,13 @@
#define HIST 1024 /* sent video packets kept for retransmission (NACK) */
#define MAX_PATHS 8
#define MAX_DC_MSG 262144 /* what our answer advertises (a=max-message-size) */
-#define CONSENT_MS 30000 /* no valid check for this long: the browser is gone */
+#define CONSENT_MS 45000 /* no valid check for this long: the browser is gone (restarts can't help) */
+#define CONNECT_MS 20000 /* from an offer (or a restart) until DTLS is up */
#define ANSWER_WAIT_MS 1200 /* for the external STUN/TURN answers before sending ours */
+#define ANSWER_WAIT_TCP_MS 3000 /* the same with a TURN server over TCP or TLS (connect, handshake) */
+#define MAX_TCP 4 /* ICE-TCP connections per peer */
+#define TCP_CHECK_MS 10000 /* a new ICE-TCP connection must bring a valid check by then */
+#define TCP_OUT_MAX (1u << 20) /* queued bytes per stream; past it a packet is dropped (media is lossy) */
typedef struct { uint16_t seq, len; uint8_t b[MTU + 64]; } Hist;
typedef struct {
@@ -57,7 +74,19 @@ typedef struct {
double last_ms;
Hist *hist;
} Track;
-typedef struct { RwAddr a; int relay, valid, nominated, proven; double last; } Path;
+/* tcp: 0 for UDP, else the ICE-TCP connection's slot + 1 */
+typedef struct { RwAddr a; int relay, tcp, valid, nominated, proven; double last; } Path;
+/* A byte stream we write packets to without blocking: an ICE-TCP connection or the TURN server's */
+typedef struct { uint8_t *b; size_t len, off, cap; } OutQ;
+typedef struct {
+ int fd;
+ RwAddr peer;
+ uint8_t in[RW_TCP_FRAME_MAX + 2];
+ size_t inl;
+ OutQ out;
+ double born;
+ int checked; /* a valid ICE check came over it */
+} TcpConn;
typedef struct {
int state; /* 0 off, 1 asked, 2 asked with credentials, 3 allocated, -1 failed */
RwAddr srv, relayed, mapped;
@@ -69,13 +98,25 @@ typedef struct {
int tries;
RwAddr perm[8];
int nperm, perm_dirty;
+ /* over TCP or TLS: the stream (fd), its TLS, whether it is connected and handshaken */
+ int transport, fd, up, connecting;
+ char host[96];
+ SSL_CTX *tctx;
+ SSL *tssl;
+ uint8_t in[RW_STUN_MAX];
+ size_t inl;
+ OutQ out;
+ double started;
} Turn;
struct Rtc {
RtcCallbacks cb;
RtcConfig cfg;
int fd, port, sfd, sport; /* media socket; the built-in STUN server's socket */
- char ufrag[9], pwd[25];
+ char ufrag[13], pwd[25]; /* ufrag: the route (8) + 4 random, new at each ICE restart */
+ TcpConn tcp[MAX_TCP];
+ unsigned restarts;
+ unsigned long checks; /* valid ICE checks since the last offer */
/* DTLS */
SSL_CTX *ctx;
SSL *ssl;
@@ -135,11 +176,12 @@ static void rnd_text(char *out, size_t n) {
out[n] = 0;
}
-static void fail(Rtc *r, const char *why) {
+/* code: RTC_WHY_* for the browser; why: the journal's line */
+static void fail(Rtc *r, const char *code, const char *why) {
if (r->failed) return;
r->failed = 1;
sd_log("video: %s", why);
- if (r->cb.state) r->cb.state(r->cb.ud, RTC_FAILED, why);
+ if (r->cb.state) r->cb.state(r->cb.ud, RTC_FAILED, code);
}
static void hmac_sha1(const uint8_t *key, size_t klen, const uint8_t *msg, size_t n, uint8_t out[20]) {
@@ -214,8 +256,90 @@ static void raw_send(Rtc *r, const RwAddr *to, const void *p, size_t n) {
if (sendto(r->fd, p, n, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&s, sizeof s) < 0 && errno != EAGAIN) {}
}
-/* To a peer, directly or wrapped in a TURN Send indication. */
-static void send_to(Rtc *r, const RwAddr *to, int relay, const void *p, size_t n) {
+/* ---- byte streams (ICE-TCP, TURN over TCP/TLS) ---- */
+
+/* Queues a whole packet (with an optional 2-byte length header) or drops it: never half of one. */
+static int q_put(OutQ *q, const void *hdr, size_t hl, const void *p, size_t n) {
+ if (q->len - q->off + hl + n > TCP_OUT_MAX) return -1;
+ if (q->off && q->off == q->len) q->off = q->len = 0;
+ if (q->len + hl + n > q->cap) {
+ if (q->off) { memmove(q->b, q->b + q->off, q->len - q->off); q->len -= q->off; q->off = 0; }
+ size_t cap = q->cap ? q->cap : 16384;
+ while (cap < q->len + hl + n) cap *= 2;
+ if (cap > q->cap) {
+ uint8_t *nb = realloc(q->b, cap);
+ if (!nb) return -1;
+ q->b = nb;
+ q->cap = cap;
+ }
+ }
+ if (hl) memcpy(q->b + q->len, hdr, hl);
+ memcpy(q->b + q->len + hl, p, n);
+ q->len += hl + n;
+ return 0;
+}
+static void q_free(OutQ *q) { free(q->b); memset(q, 0, sizeof *q); }
+
+static void tcp_close(Rtc *r, int i, const char *why);
+
+static void tcp_flush(Rtc *r, int i) {
+ TcpConn *c = &r->tcp[i];
+ while (c->fd >= 0 && c->out.off < c->out.len) {
+ ssize_t w = send(c->fd, c->out.b + c->out.off, c->out.len - c->out.off, MSG_DONTWAIT | MSG_NOSIGNAL);
+ if (w < 0 && errno == EINTR) continue;
+ if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return;
+ if (w <= 0) { tcp_close(r, i, "write failed"); return; }
+ c->out.off += (size_t)w;
+ }
+}
+
+/* One packet over an ICE-TCP connection, framed (RFC 4571). */
+static void tcp_send(Rtc *r, int slot, const void *p, size_t n) {
+ if (slot < 1 || slot > MAX_TCP || n > RW_TCP_FRAME_MAX) return;
+ TcpConn *c = &r->tcp[slot - 1];
+ if (c->fd < 0) return;
+ uint8_t h[2] = { (uint8_t)(n >> 8), (uint8_t)n };
+ if (q_put(&c->out, h, 2, p, n)) return;
+ tcp_flush(r, slot - 1);
+}
+
+static void turn_flush(Rtc *r) {
+ Turn *t = &r->turn;
+ while (t->up && t->out.off < t->out.len) {
+ size_t left = t->out.len - t->out.off;
+ if (t->tssl) {
+ int w = SSL_write(t->tssl, t->out.b + t->out.off, left > (1u << 20) ? 1 << 20 : (int)left);
+ if (w <= 0) {
+ int e = SSL_get_error(t->tssl, w);
+ if (e == SSL_ERROR_WANT_WRITE || e == SSL_ERROR_WANT_READ) return;
+ ERR_clear_error();
+ t->up = 0;
+ t->state = -1;
+ sd_log("video: the TURN server's TLS connection broke");
+ return;
+ }
+ t->out.off += (size_t)w;
+ } else {
+ ssize_t w = send(t->fd, t->out.b + t->out.off, left, MSG_DONTWAIT | MSG_NOSIGNAL);
+ if (w < 0 && errno == EINTR) continue;
+ if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return;
+ if (w <= 0) { t->up = 0; t->state = -1; sd_log("video: the TURN server's TCP connection broke"); return; }
+ t->out.off += (size_t)w;
+ }
+ }
+}
+
+/* A STUN message to the TURN server: a datagram, or on its stream (queued until it's connected). */
+static void turn_raw(Rtc *r, const void *p, size_t n) {
+ Turn *t = &r->turn;
+ if (t->transport == RW_TURN_UDP) { raw_send(r, &t->srv, p, n); return; }
+ if (t->fd < 0 || q_put(&t->out, NULL, 0, p, n)) return;
+ turn_flush(r);
+}
+
+/* To a peer: directly (UDP or an ICE-TCP connection) or wrapped in a TURN Send indication. */
+static void send_to(Rtc *r, const RwAddr *to, int relay, int tcp, const void *p, size_t n) {
+ if (tcp) { tcp_send(r, tcp, p, n); return; }
if (!relay) { raw_send(r, to, p, n); return; }
if (r->turn.state != 3 || n > 1400) return;
static uint8_t b[1500];
@@ -234,14 +358,14 @@ static void send_to(Rtc *r, const RwAddr *to, int relay, const void *p, size_t n
size_t total = o + 4 + padded;
b[2] = (uint8_t)((total - 20) >> 8);
b[3] = (uint8_t)(total - 20);
- raw_send(r, &r->turn.srv, b, total);
+ turn_raw(r, b, total);
}
static Path *sel_path(Rtc *r) { return r->sel >= 0 ? &r->path[r->sel] : NULL; }
static void send_sel(Rtc *r, const void *p, size_t n) {
Path *s = sel_path(r);
- if (s) send_to(r, &s->a, s->relay, p, n);
+ if (s) send_to(r, &s->a, s->relay, s->tcp, p, n);
}
/* ---- DTLS: our certificate, a write BIO that sends each record as a datagram ---- */
@@ -396,7 +520,7 @@ static int sctp_recv(struct socket *sock, union sctp_sockstore addr, void *data,
const union sctp_notification *nt = data;
if (len >= sizeof nt->sn_assoc_change && nt->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
int st = nt->sn_assoc_change.sac_state;
- if (st == SCTP_COMM_LOST || st == SCTP_SHUTDOWN_COMP) fail(r, "the browser closed the data channels");
+ if (st == SCTP_COMM_LOST || st == SCTP_SHUTDOWN_COMP) fail(r, RTC_WHY_CLOSED, "the browser closed the data channels");
}
free(data);
return 1;
@@ -405,9 +529,9 @@ static int sctp_recv(struct socket *sock, union sctp_sockstore addr, void *data,
/* a message larger than the receive buffer comes in parts: joined up to our limit */
if (r->rasm_len && (r->rasm_sid != sid || r->rasm_ppid != ppid)) { free(r->rasm); r->rasm = NULL; r->rasm_len = 0; }
if (!(flags & MSG_EOR) || r->rasm_len) {
- if (r->rasm_len + len > MAX_DC_MSG) { free(data); fail(r, "data channel message too large"); return 1; }
+ if (r->rasm_len + len > MAX_DC_MSG) { free(data); fail(r, RTC_WHY_CLOSED, "data channel message too large"); return 1; }
uint8_t *nb = realloc(r->rasm, r->rasm_len + len);
- if (!nb) { free(data); fail(r, "out of memory"); return 1; }
+ if (!nb) { free(data); fail(r, RTC_WHY_SERVER, "out of memory"); return 1; }
memcpy(nb + r->rasm_len, data, len);
r->rasm = nb;
r->rasm_len += len;
@@ -421,7 +545,7 @@ static int sctp_recv(struct socket *sock, union sctp_sockstore addr, void *data,
r->rasm_len = 0;
return 1;
}
- if (len > MAX_DC_MSG) { free(data); fail(r, "data channel message too large"); return 1; }
+ if (len > MAX_DC_MSG) { free(data); fail(r, RTC_WHY_CLOSED, "data channel message too large"); return 1; }
on_dc_message(r, sid, ppid, data, len);
free(data);
return 1;
@@ -436,7 +560,7 @@ static void sctp_start(Rtc *r) {
}
usrsctp_register_address(r);
r->sctp = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, sctp_recv, NULL, 0, r);
- if (!r->sctp) { fail(r, "no SCTP socket"); return; }
+ if (!r->sctp) { fail(r, RTC_WHY_SERVER, "no SCTP socket"); return; }
usrsctp_set_non_blocking(r->sctp, 1);
struct linger lg = { 1, 0 };
usrsctp_setsockopt(r->sctp, SOL_SOCKET, SO_LINGER, &lg, sizeof lg);
@@ -457,10 +581,10 @@ static void sctp_start(Rtc *r) {
sc.sconn_family = AF_CONN;
sc.sconn_port = htons(5000);
sc.sconn_addr = r;
- if (usrsctp_bind(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0) { fail(r, "SCTP bind failed"); return; }
+ if (usrsctp_bind(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0) { fail(r, RTC_WHY_SERVER, "SCTP bind failed"); return; }
int dm = r->of ? r->of->dm : -1;
sc.sconn_port = htons((uint16_t)(dm >= 0 ? r->of->m[dm].sctp_port : 5000));
- if (usrsctp_connect(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0 && errno != EINPROGRESS) { fail(r, "SCTP connect failed"); return; }
+ if (usrsctp_connect(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0 && errno != EINPROGRESS) { fail(r, RTC_WHY_SERVER, "SCTP connect failed"); return; }
r->sctp_on = 1;
r->sctp_t = now_ms();
}
@@ -473,13 +597,13 @@ static void dtls_up(Rtc *r) {
unsigned int l = 32;
int ok = peer && X509_digest(peer, EVP_sha256(), fp, &l) && l == 32 && !CRYPTO_memcmp(fp, r->of->fp, 32);
X509_free(peer);
- if (!ok) { fail(r, "the browser's DTLS certificate does not match the fingerprint of the signed-in offer"); return; }
+ if (!ok) { fail(r, RTC_WHY_DTLS, "the browser's DTLS certificate does not match the fingerprint of the signed-in offer"); return; }
const SRTP_PROTECTION_PROFILE *pr = SSL_get_selected_srtp_profile(r->ssl);
- if (!pr || (pr->id != SRTP_AES128_CM_SHA1_80 && pr->id != SRTP_AEAD_AES_128_GCM)) { fail(r, "no SRTP profile"); return; }
+ if (!pr || (pr->id != SRTP_AES128_CM_SHA1_80 && pr->id != SRTP_AEAD_AES_128_GCM)) { fail(r, RTC_WHY_DTLS, "no SRTP profile"); return; }
int gcm = pr->id == SRTP_AEAD_AES_128_GCM;
size_t kl = 16, sl = gcm ? 12 : 14;
uint8_t km[2 * (16 + 14)], txk[30], rxk[30];
- if (SSL_export_keying_material(r->ssl, km, 2 * (kl + sl), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0) != 1) { fail(r, "no SRTP keys"); return; }
+ if (SSL_export_keying_material(r->ssl, km, 2 * (kl + sl), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0) != 1) { fail(r, RTC_WHY_DTLS, "no SRTP keys"); return; }
/* client key, server key, client salt, server salt; we are the server */
memcpy(txk, km + kl, kl);
memcpy(txk + kl, km + 2 * kl + sl, sl);
@@ -500,14 +624,15 @@ static void dtls_up(Rtc *r) {
int e2 = srtp_create(&r->rx, &p);
explicit_bzero(txk, sizeof txk);
explicit_bzero(rxk, sizeof rxk);
- if (e1 || e2) { fail(r, "SRTP setup failed"); return; }
+ if (e1 || e2) { fail(r, RTC_WHY_SERVER, "SRTP setup failed"); return; }
r->srtp_up = 1;
r->dtls_up = 1;
Path *s = sel_path(r);
if (s) s->proven = 1;
char a[64] = "";
if (s) rw_addr_format(&s->a, a, sizeof a);
- sd_log("video: connected (%s, %s, %s%s)", r->codec == RW_CODEC_H264 ? "H.264" : "VP8", gcm ? "AES-GCM" : "AES-CM", a, s && s->relay ? " via TURN" : "");
+ sd_log("video: connected (%s, %s, %s%s)", r->codec == RW_CODEC_H264 ? "H.264" : "VP8", gcm ? "AES-GCM" : "AES-CM", a,
+ s && s->relay ? " via TURN" : s && s->tcp ? " over TCP" : "");
if (r->of->dm >= 0) sctp_start(r);
if (r->cb.state) r->cb.state(r->cb.ud, RTC_CONNECTED, NULL);
}
@@ -519,7 +644,7 @@ static void dtls_input(Rtc *r, const uint8_t *p, size_t n) {
if (k == 1) dtls_up(r);
else {
int e = SSL_get_error(r->ssl, k);
- if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, "DTLS handshake failed"); return; }
+ if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, RTC_WHY_DTLS, "DTLS handshake failed"); return; }
}
}
static uint8_t buf[65536];
@@ -527,8 +652,8 @@ static void dtls_input(Rtc *r, const uint8_t *p, size_t n) {
int k = SSL_read(r->ssl, buf, sizeof buf);
if (k > 0) { r->dtls_rx++; if (r->sctp_on) usrsctp_conninput(r, buf, (size_t)k, 0); continue; }
int e = SSL_get_error(r->ssl, k);
- if (e == SSL_ERROR_ZERO_RETURN) fail(r, "the browser closed DTLS");
- else if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, "DTLS error"); }
+ if (e == SSL_ERROR_ZERO_RETURN) fail(r, RTC_WHY_CLOSED, "the browser closed DTLS");
+ else if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, RTC_WHY_DTLS, "DTLS error"); }
break;
}
}
@@ -647,23 +772,40 @@ static void on_fb(void *ud, const RwFb *f) {
/* ---- ICE-lite, the built-in STUN server, external STUN and TURN ---- */
-static Path *path_for(Rtc *r, const RwAddr *a, int relay, int add) {
- for (int i = 0; i < r->npath; i++) if (r->path[i].relay == relay && rw_addr_equal(&r->path[i].a, a)) return &r->path[i];
- if (!add || r->npath >= MAX_PATHS) return NULL;
- Path *p = &r->path[r->npath++];
+static Path *path_for(Rtc *r, const RwAddr *a, int relay, int tcp, int add) {
+ for (int i = 0; i < r->npath; i++)
+ if (r->path[i].relay == relay && r->path[i].tcp == tcp && rw_addr_equal(&r->path[i].a, a)) return &r->path[i];
+ if (!add) return NULL;
+ Path *p = NULL;
+ for (int i = 0; i < r->npath && !p; i++) if (!r->path[i].a.family && i != r->sel) p = &r->path[i]; /* a dead one */
+ if (!p && r->npath < MAX_PATHS) p = &r->path[r->npath++];
+ if (!p) return NULL;
memset(p, 0, sizeof *p);
p->a = *a;
p->relay = relay;
+ p->tcp = tcp;
return p;
}
-static void stun_answer(Rtc *r, const RwStun *s, const RwAddr *from, int relay) {
+/* The media's path went away (its TCP connection closed): another proven path, if any, takes over. */
+static void reselect(Rtc *r) {
+ int best = -1;
+ for (int i = 0; i < r->npath; i++) {
+ const Path *p = &r->path[i];
+ if (!p->a.family || !p->valid || !p->proven) continue;
+ if (best < 0 || (p->nominated && !r->path[best].nominated) || (p->nominated == r->path[best].nominated && p->last > r->path[best].last)) best = i;
+ }
+ r->sel = best;
+ if (best >= 0 && r->cb.keyframe) r->cb.keyframe(r->cb.ud);
+}
+
+static void stun_answer(Rtc *r, const RwStun *s, const RwAddr *from, int relay, int tcp) {
RwStunOut o;
rw_so_init(&o, RW_STUN_BINDING_OK, s->tid);
rw_so_xaddr(&o, RW_ATTR_XOR_MAPPED, from);
rw_so_mi(&o, (const uint8_t *)r->pwd, strlen(r->pwd), hmac_sha1);
rw_so_fingerprint(&o);
- if (!o.bad) send_to(r, from, relay, o.b, o.n);
+ if (!o.bad) send_to(r, from, relay, tcp, o.b, o.n);
}
/* The built-in STUN server: a plain binding request (no credentials) gets the address it came from,
@@ -683,7 +825,7 @@ static void stun_server(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from)
if (!o.bad) sendto(r->sfd, o.b, o.n, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&sa, sizeof sa);
}
-static void ice_check(Rtc *r, const uint8_t *p, size_t n, const RwStun *s, const RwAddr *from, int relay) {
+static void ice_check(Rtc *r, const uint8_t *p, size_t n, const RwStun *s, const RwAddr *from, int relay, int tcp) {
if (!r->offered || !s->username || !s->mi_off || !s->has_fp) return;
char want[300];
int wl = snprintf(want, sizeof want, "%s:%s", r->ufrag, r->of->ufrag);
@@ -696,20 +838,26 @@ static void ice_check(Rtc *r, const uint8_t *p, size_t n, const RwStun *s, const
if (!said++) sd_log("video: refused ICE check from %s (Settings -> Sharing: who can connect)", a);
return;
}
- Path *pa = path_for(r, from, relay, 1);
+ Path *pa = path_for(r, from, relay, tcp, 1);
if (!pa) return;
+ if (tcp) r->tcp[tcp - 1].checked = 1;
pa->valid = 1;
pa->last = now_ms();
r->last_consent = pa->last;
+ r->checks++;
if (s->use_candidate && !pa->nominated) {
for (int i = 0; i < r->npath; i++) r->path[i].nominated = 0;
pa->nominated = 1;
/* once DTLS is up, media only moves to a path that proved to be the browser (an authenticated
* DTLS or SRTCP packet from it, see prove()): ICE credentials alone can't redirect the stream */
- if (!r->dtls_up || pa->proven) r->sel = (int)(pa - r->path);
+ if (!r->dtls_up || pa->proven) {
+ int was = r->sel;
+ r->sel = (int)(pa - r->path);
+ if (r->dtls_up && was != r->sel && r->cb.keyframe) r->cb.keyframe(r->cb.ud);
+ }
}
- if (r->sel < 0) r->sel = (int)(pa - r->path);
- stun_answer(r, s, from, relay);
+ if (r->sel < 0 && !r->dtls_up) r->sel = (int)(pa - r->path);
+ stun_answer(r, s, from, relay, tcp);
}
static void turn_send(Rtc *r, uint16_t type, int auth, const RwAddr *peers, int npeers, uint32_t lifetime) {
@@ -727,7 +875,7 @@ static void turn_send(Rtc *r, uint16_t type, int auth, const RwAddr *peers, int
rw_so_mi(&o, t->key, 16, hmac_sha1);
}
rw_so_fingerprint(&o);
- if (!o.bad) raw_send(r, &t->srv, o.b, o.n);
+ if (!o.bad) turn_raw(r, o.b, o.n);
t->sent_at = now_ms();
}
@@ -769,13 +917,13 @@ static void turn_peer(Rtc *r, const RwAddr *a) {
t->perm_dirty = 1;
}
-static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay);
+static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay, int tcp);
static void prove(Rtc *r, Path *pa);
static void turn_input(Rtc *r, const uint8_t *p, size_t n, const RwStun *s) {
Turn *t = &r->turn;
if (s->type == RW_TURN_DATA_IND) { /* a packet from a peer, relayed */
- if (t->state == 3 && s->data && s->xor_peer.family && s->dlen <= 1400) handle_packet(r, s->data, s->dlen, &s->xor_peer, 1);
+ if (t->state == 3 && s->data && s->xor_peer.family && s->dlen <= 1400) handle_packet(r, s->data, s->dlen, &s->xor_peer, 1, 0);
return;
}
if (memcmp(s->tid, t->tid, 12)) return;
@@ -815,20 +963,20 @@ static void turn_input(Rtc *r, const uint8_t *p, size_t n, const RwStun *s) {
}
}
-static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay) {
+static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay, int tcp) {
if (!n || r->failed) return;
if (rw_is_stun(p, n)) {
RwStun s;
if (rw_stun_parse(p, n, &s)) return;
- if (!relay && r->turn.state && rw_addr_equal(from, &r->turn.srv)) { turn_input(r, p, n, &s); return; }
- if (s.type == RW_STUN_BINDING_REQ) { ice_check(r, p, n, &s, from, relay); return; }
- if (s.type == RW_STUN_BINDING_OK && !relay)
+ if (!relay && !tcp && r->turn.state && r->turn.transport == RW_TURN_UDP && rw_addr_equal(from, &r->turn.srv)) { turn_input(r, p, n, &s); return; }
+ if (s.type == RW_STUN_BINDING_REQ) { ice_check(r, p, n, &s, from, relay, tcp); return; }
+ if (s.type == RW_STUN_BINDING_OK && !relay && !tcp)
for (int i = 0; i < r->nstun_srv; i++)
if (rw_addr_equal(from, &r->stun_srv[i]) && !memcmp(s.tid, r->stun_tid[i], 12) && s.xor_mapped.family && !r->srflx.family) r->srflx = s.xor_mapped;
return;
}
/* DTLS and SRTP only from an address that passed an ICE check */
- Path *pa = path_for(r, from, relay, 0);
+ Path *pa = path_for(r, from, relay, tcp, 0);
if (!pa || !pa->valid) return;
if (p[0] >= 20 && p[0] <= 63) {
if (!r->offered) return;
@@ -850,15 +998,226 @@ static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from
/* An authenticated packet (DTLS application data or SRTCP) came from this path: it is the browser. If
* the browser nominated it, the media goes there now. */
static void prove(Rtc *r, Path *pa) {
- if (!pa || pa->proven) { if (pa && pa->nominated && r->sel != (int)(pa - r->path)) r->sel = (int)(pa - r->path); return; }
+ if (!pa) return;
+ int was = r->sel;
pa->proven = 1;
- if (pa->nominated) r->sel = (int)(pa - r->path);
+ if (pa->nominated || r->sel < 0) r->sel = (int)(pa - r->path);
+ if (r->dtls_up && was != r->sel && r->cb.keyframe) r->cb.keyframe(r->cb.ud); /* a new path: a clean picture */
+}
+
+/* ---- ICE-TCP connections ---- */
+
+static void tcp_close(Rtc *r, int i, const char *why) {
+ TcpConn *c = &r->tcp[i];
+ if (c->fd < 0) return;
+ if (c->checked) {
+ char a[64];
+ rw_addr_format(&c->peer, a, sizeof a);
+ sd_log("video: ICE-TCP connection from %s closed (%s)", a, why);
+ }
+ close(c->fd);
+ c->fd = -1;
+ q_free(&c->out);
+ c->inl = 0;
+ c->checked = 0;
+ int lost_sel = 0;
+ for (int k = 0; k < r->npath; k++) {
+ Path *p = &r->path[k];
+ if (p->tcp != i + 1) continue;
+ if (k == r->sel) lost_sel = 1;
+ memset(p, 0, sizeof *p); /* dead: path_for reuses it */
+ }
+ if (lost_sel) reselect(r);
+}
+
+/* lord-sessiond's root connection child hands us an ICE-TCP connection: the socket and the first
+ * framed packet it read (whose check names our route). Only from root, only a TCP socket. */
+static void tcp_accept(Rtc *r) {
+ int u = accept4(r->cfg.ice_lfd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
+ if (u < 0) return;
+ struct ucred cr;
+ socklen_t cl = sizeof cr;
+ uint8_t first[RW_TCP_FRAME_MAX + 2];
+ char cbuf[CMSG_SPACE(sizeof(int))];
+ struct iovec iov = { first, sizeof first };
+ struct msghdr mh = { .msg_iov = &iov, .msg_iovlen = 1, .msg_control = cbuf, .msg_controllen = sizeof cbuf };
+ int fd = -1;
+ ssize_t got = -1;
+ if (!getsockopt(u, SOL_SOCKET, SO_PEERCRED, &cr, &cl) && cr.uid == 0) {
+ struct pollfd pw = { u, POLLIN, 0 };
+ if (poll(&pw, 1, 200) > 0) got = recvmsg(u, &mh, MSG_CMSG_CLOEXEC | MSG_DONTWAIT);
+ for (struct cmsghdr *cm = got > 0 ? CMSG_FIRSTHDR(&mh) : NULL; cm; cm = CMSG_NXTHDR(&mh, cm))
+ if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_RIGHTS && cm->cmsg_len == CMSG_LEN(sizeof(int))) memcpy(&fd, CMSG_DATA(cm), sizeof(int));
+ }
+ close(u);
+ if (fd < 0) return;
+ int type = 0;
+ socklen_t tl = sizeof type;
+ struct sockaddr_in6 sa;
+ socklen_t sl = sizeof sa;
+ if (got < 2 || (mh.msg_flags & MSG_TRUNC) || getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &tl) || type != SOCK_STREAM ||
+ getpeername(fd, (struct sockaddr *)&sa, &sl) || sa.sin6_family != AF_INET6) { close(fd); return; }
+ int slot = -1;
+ double oldest = 0;
+ for (int i = 0; i < MAX_TCP; i++) if (r->tcp[i].fd < 0) { slot = i; break; }
+ if (slot < 0) /* full: the oldest one that never brought a valid check goes */
+ for (int i = 0; i < MAX_TCP; i++) if (!r->tcp[i].checked && (slot < 0 || r->tcp[i].born < oldest)) { slot = i; oldest = r->tcp[i].born; }
+ if (slot < 0) { close(fd); return; }
+ tcp_close(r, slot, "replaced");
+ TcpConn *c = &r->tcp[slot];
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+ int on = 1;
+ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
+ c->fd = fd;
+ from_sockaddr(&sa, &c->peer);
+ c->born = now_ms();
+ memcpy(c->in, first, (size_t)got);
+ c->inl = (size_t)got;
+}
+
+static void tcp_input(Rtc *r, int i) {
+ TcpConn *c = &r->tcp[i];
+ for (int rounds = 0; c->fd >= 0 && rounds < 64; rounds++) {
+ if (c->inl < sizeof c->in) {
+ ssize_t got = recv(c->fd, c->in + c->inl, sizeof c->in - c->inl, MSG_DONTWAIT);
+ if (got == 0) { tcp_close(r, i, "closed by the browser"); return; }
+ if (got < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { tcp_close(r, i, "read failed"); return; }
+ if (got > 0) c->inl += (size_t)got;
+ }
+ size_t used = 0, len;
+ int k;
+ while (c->fd >= 0 && (k = rw_tcp_frame(c->in + used, c->inl - used, &len)) == 1) {
+ handle_packet(r, c->in + used + 2, len, &c->peer, 0, i + 1);
+ used += 2 + len;
+ if (r->failed) return;
+ }
+ if (c->fd < 0) return;
+ if (k < 0) { tcp_close(r, i, "bad framing"); return; }
+ memmove(c->in, c->in + used, c->inl - used);
+ c->inl -= used;
+ if (!used && c->inl < sizeof c->in) {
+ /* nothing complete: wait for more (unless the kernel has more right now) */
+ int pending = 0;
+ if (ioctl(c->fd, FIONREAD, &pending) || pending <= 0) return;
+ }
+ }
+}
+
+/* ---- TURN over TCP or TLS ---- */
+
+static void turn_stream_input(Rtc *r) {
+ Turn *t = &r->turn;
+ for (int rounds = 0; t->fd >= 0 && t->up && rounds < 64; rounds++) {
+ ssize_t got;
+ if (t->tssl) {
+ int k = SSL_read(t->tssl, t->in + t->inl, (int)(sizeof t->in - t->inl));
+ if (k <= 0) {
+ int e = SSL_get_error(t->tssl, k);
+ if (e == SSL_ERROR_WANT_READ || e == SSL_ERROR_WANT_WRITE) return;
+ ERR_clear_error();
+ got = 0;
+ } else got = k;
+ } else {
+ got = recv(t->fd, t->in + t->inl, sizeof t->in - t->inl, MSG_DONTWAIT);
+ if (got < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) return;
+ }
+ if (got <= 0) { t->up = 0; t->state = -1; sd_log("video: the TURN server closed the connection"); return; }
+ t->inl += (size_t)got;
+ size_t used = 0, len;
+ int k;
+ while ((k = rw_stun_stream(t->in + used, t->inl - used, &len)) == 1) {
+ RwStun s;
+ if (!rw_stun_parse(t->in + used, len, &s)) turn_input(r, t->in + used, len, &s);
+ used += len;
+ if (r->failed || !t->up) return;
+ }
+ if (k < 0) { t->up = 0; t->state = -1; sd_log("video: the TURN server sent something that isn't STUN"); return; }
+ memmove(t->in, t->in + used, t->inl - used);
+ t->inl -= used;
+ }
+}
+
+/* The TLS certificate of the TURN server: the pinned fingerprint if Settings has one, else the
+ * system's CAs and the server's name (or IP address), checked by OpenSSL during the handshake. */
+static int turn_cert_ok(Rtc *r) {
+ Turn *t = &r->turn;
+ X509 *peer = SSL_get1_peer_certificate(t->tssl);
+ if (!peer) return 0;
+ int ok;
+ if (r->cfg.turn_has_fp) {
+ uint8_t fp[32];
+ unsigned int l = 32;
+ ok = X509_digest(peer, EVP_sha256(), fp, &l) && l == 32 && !CRYPTO_memcmp(fp, r->cfg.turn_fp, 32);
+ } else ok = SSL_get_verify_result(t->tssl) == X509_V_OK;
+ X509_free(peer);
+ return ok;
+}
+
+static void turn_stream_ready(Rtc *r) {
+ Turn *t = &r->turn;
+ if (t->connecting == 1) {
+ int err = 0;
+ socklen_t el = sizeof err;
+ if (getsockopt(t->fd, SOL_SOCKET, SO_ERROR, &err, &el) || err) { t->state = -1; sd_log("video: could not connect to the TURN server (%s)", strerror(err)); return; }
+ if (t->transport == RW_TURN_TCP) { t->connecting = 0; t->up = 1; turn_flush(r); return; }
+ t->connecting = 2;
+ t->tctx = SSL_CTX_new(TLS_client_method());
+ if (!t->tctx) { t->state = -1; return; }
+ SSL_CTX_set_min_proto_version(t->tctx, TLS1_2_VERSION);
+ if (!r->cfg.turn_has_fp) {
+ SSL_CTX_set_default_verify_paths(t->tctx);
+ SSL_CTX_set_verify(t->tctx, SSL_VERIFY_PEER, NULL);
+ } else SSL_CTX_set_verify(t->tctx, SSL_VERIFY_NONE, NULL); /* the pin is checked after the handshake */
+ t->tssl = SSL_new(t->tctx);
+ if (!t->tssl) { t->state = -1; return; }
+ SSL_set_fd(t->tssl, t->fd);
+ SSL_set_mode(t->tssl, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
+ RwAddr ip;
+ if (rw_addr_text(t->host, &ip)) { /* a name: SNI and the name check */
+ SSL_set_tlsext_host_name(t->tssl, t->host);
+ if (!r->cfg.turn_has_fp) SSL_set1_host(t->tssl, t->host);
+ } else if (!r->cfg.turn_has_fp) X509_VERIFY_PARAM_set1_ip_asc(SSL_get0_param(t->tssl), t->host);
+ SSL_set_connect_state(t->tssl);
+ }
+ if (t->connecting == 2) {
+ int k = SSL_do_handshake(t->tssl);
+ if (k == 1) {
+ if (!turn_cert_ok(r)) { t->state = -1; sd_log("video: the TURN server's certificate was refused (%s)", r->cfg.turn_has_fp ? "it doesn't match the pinned fingerprint" : X509_verify_cert_error_string(SSL_get_verify_result(t->tssl))); return; }
+ t->connecting = 0;
+ t->up = 1;
+ turn_flush(r);
+ return;
+ }
+ int e = SSL_get_error(t->tssl, k);
+ if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) {
+ long v = SSL_get_verify_result(t->tssl);
+ ERR_clear_error();
+ t->state = -1;
+ sd_log("video: TLS with the TURN server failed%s%s", v != X509_V_OK ? ": " : "", v != X509_V_OK ? X509_verify_cert_error_string(v) : "");
+ }
+ }
+}
+
+static void turn_stream_close(Turn *t) {
+ if (t->tssl) { SSL_free(t->tssl); t->tssl = NULL; }
+ if (t->tctx) { SSL_CTX_free(t->tctx); t->tctx = NULL; }
+ if (t->fd >= 0) close(t->fd);
+ t->fd = -1;
+ t->up = t->connecting = 0;
+ q_free(&t->out);
}
/* ---- life cycle ---- */
int rtc_port(const Rtc *r) { return r ? r->sport : 0; }
+static void new_credentials(Rtc *r) {
+ char tail[5];
+ rnd_text(tail, 4);
+ snprintf(r->ufrag, sizeof r->ufrag, "%.8s%s", r->cfg.route, tail);
+ rnd_text(r->pwd, 24);
+}
+
Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb) {
Rtc *r = calloc(1, sizeof *r);
if (!r) return NULL;
@@ -866,10 +1225,12 @@ Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb) {
r->cb = *cb;
r->sel = -1;
r->fd = r->sfd = -1;
+ r->turn.fd = -1;
+ for (int i = 0; i < MAX_TCP; i++) r->tcp[i].fd = -1;
r->created = r->last_consent = now_ms();
r->kbps = cfg->max_kbps && cfg->max_kbps < 4000 ? cfg->max_kbps : 4000;
- rnd_text(r->ufrag, 8);
- rnd_text(r->pwd, 24);
+ if (strlen(r->cfg.route) != 8) rnd_text(r->cfg.route, 8);
+ new_credentials(r);
if (!srtp_ready) { if (srtp_init() != srtp_err_status_ok) goto bad; srtp_ready = 1; }
if (dtls_init(r)) goto bad;
r->fd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
@@ -911,8 +1272,37 @@ Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb) {
r->nstun_srv++;
}
if (cfg->turn[0] && cfg->turn_user[0] && cfg->turn_pass) {
- if (!resolve(cfg->turn, &r->turn.srv)) { r->turn.state = 1; turn_send(r, RW_TURN_ALLOCATE_REQ, 0, NULL, 0, 600); }
- else sd_log("video: the TURN server's name does not resolve");
+ Turn *t = &r->turn;
+ t->transport = cfg->turn_transport;
+ const char *colon = strrchr(cfg->turn, ':');
+ size_t hl = colon ? (size_t)(colon - cfg->turn) : 0;
+ if (hl && hl < sizeof t->host) {
+ memcpy(t->host, cfg->turn, hl);
+ t->host[hl] = 0;
+ if (t->host[0] == '[' && hl > 2) { memmove(t->host, t->host + 1, hl - 2); t->host[hl - 2] = 0; }
+ }
+ if (resolve(cfg->turn, &t->srv)) sd_log("video: the TURN server's name does not resolve");
+ else if (t->transport == RW_TURN_UDP) { t->state = 1; turn_send(r, RW_TURN_ALLOCATE_REQ, 0, NULL, 0, 600); }
+ else {
+ /* the stream is connected without blocking; the Allocate waits in its queue */
+ t->fd = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ struct sockaddr_in6 ts;
+ to_sockaddr(&t->srv, &ts);
+ if (t->fd >= 0) {
+ int v6only = 0, on = 1;
+ setsockopt(t->fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof v6only);
+ setsockopt(t->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
+ }
+ if (t->fd < 0 || (connect(t->fd, (struct sockaddr *)&ts, sizeof ts) && errno != EINPROGRESS)) {
+ sd_log("video: could not connect to the TURN server");
+ turn_stream_close(t);
+ } else {
+ t->connecting = 1;
+ t->started = now_ms();
+ t->state = 1;
+ turn_send(r, RW_TURN_ALLOCATE_REQ, 0, NULL, 0, 600);
+ }
+ }
}
if (r->turn.state != 1) wipe_pass(r);
return r;
@@ -934,6 +1324,8 @@ void rtc_free(Rtc *r) {
EVP_PKEY_free(r->key);
if (r->fd >= 0) close(r->fd);
if (r->sfd >= 0) close(r->sfd);
+ for (int i = 0; i < MAX_TCP; i++) if (r->tcp[i].fd >= 0) { close(r->tcp[i].fd); q_free(&r->tcp[i].out); }
+ turn_stream_close(&r->turn);
wipe_pass(r);
explicit_bzero(r->turn.key, sizeof r->turn.key);
explicit_bzero(r->pwd, sizeof r->pwd);
@@ -943,7 +1335,8 @@ void rtc_free(Rtc *r) {
free(r);
}
-/* host candidates: the address the WebSocket came in on first, then the other interfaces */
+/* host candidates: the address the WebSocket came in on first, then the other interfaces; each also
+ * as a passive TCP candidate on the remote desktop's port (lower priority: UDP wins when it works) */
static int local_cands(Rtc *r, RwLocalCand *c, int max) {
int n = 0;
RwAddr seen[16];
@@ -958,7 +1351,7 @@ static int local_cands(Rtc *r, RwLocalCand *c, int max) {
}
struct ifaddrs *ifs = NULL;
if (!getifaddrs(&ifs)) {
- for (struct ifaddrs *i = ifs; i && n < max - 2 && ns < 16; i = i->ifa_next) {
+ for (struct ifaddrs *i = ifs; i && n < max / 2 && ns < 16; i = i->ifa_next) {
if (!i->ifa_addr || (i->ifa_flags & IFF_LOOPBACK) || !(i->ifa_flags & IFF_UP)) continue;
RwAddr a = { 0 };
if (i->ifa_addr->sa_family == AF_INET) { a.family = 4; memcpy(a.a, &((struct sockaddr_in *)i->ifa_addr)->sin_addr, 4); }
@@ -980,6 +1373,15 @@ static int local_cands(Rtc *r, RwLocalCand *c, int max) {
}
freeifaddrs(ifs);
}
+ int nhost = n;
+ if (r->cfg.ice_lfd >= 0 && r->cfg.tcp_port > 0)
+ for (int i = 0; i < nhost && n < max - 2; i++) {
+ c[n] = c[i];
+ c[n].tcp = 1;
+ c[n].port = (uint16_t)r->cfg.tcp_port;
+ c[n].prio = (80u << 24) | ((unsigned)(60000 - i * 10) << 8) | 255u;
+ n++;
+ }
if (r->srflx.family && n < max) {
rw_addr_format(&r->srflx, c[n].ip, sizeof c[n].ip);
c[n].port = r->srflx.port;
@@ -1016,36 +1418,80 @@ static void json_escape(const char *s, char *out, size_t cap) {
static void send_answer(Rtc *r) {
if (r->answered) return;
r->answered = 1;
- RwLocalCand c[12];
- int nc = local_cands(r, c, 12);
- uint32_t sid;
- rnd(&sid, sizeof sid);
- RwAnswer an = { r->ufrag, r->pwd, r->fp, r->v.ssrc, r->a.ssrc, r->of->am >= 0, c, nc, sid & 0x7fffffff };
+ RwLocalCand c[24];
+ memset(c, 0, sizeof c);
+ int nc = local_cands(r, c, 24);
+ static uint32_t sid;
+ if (!sid) { rnd(&sid, sizeof sid); sid &= 0x7fffffff; if (!sid) sid = 1; }
+ RwAnswer an = { r->ufrag, r->pwd, r->fp, r->v.ssrc, r->a.ssrc, r->of->am >= 0, c, nc, sid, 2 + r->restarts };
static char sdp[16384], esc[20000], msg[20100];
int l = rw_sdp_answer(r->of, &an, sdp, sizeof sdp);
- if (l < 0) { fail(r, "the answer does not fit"); return; }
+ if (l < 0) { fail(r, RTC_WHY_SERVER, "the answer does not fit"); return; }
json_escape(sdp, esc, sizeof esc);
- snprintf(msg, sizeof msg, "{\"t\":\"rtc\",\"sdp\":\"%s\",\"codec\":\"%s\"}", esc, r->codec == RW_CODEC_H264 ? "h264" : "vp8");
+ snprintf(msg, sizeof msg, "{\"t\":\"rtc\",\"sdp\":\"%s\",\"codec\":\"%s\"%s}", esc, r->codec == RW_CODEC_H264 ? "h264" : "vp8",
+ r->restarts ? ",\"restart\":1" : "");
if (r->cb.signal) r->cb.signal(r->cb.ud, msg);
}
+static void add_remote_cands(Rtc *r) {
+ for (int i = 0; i < r->of->ncand && r->nrcand < 16; i++) {
+ const RwCand *c = &r->of->cand[i];
+ if (!c->mdns && !c->tcp && c->component == 1) { r->rcand[r->nrcand++] = c->addr; turn_peer(r, &c->addr); }
+ }
+ turn_perms(r);
+}
+
+/* An ICE restart (RFC 8445 §9): the same browser (the DTLS fingerprint of the signed-in offer), the same
+ * sections and codec, new credentials on both sides. Paths must be checked again with them; the media
+ * keeps its path until a new one is nominated and proven. */
+static int restart(Rtc *r, const char *sdp, size_t n, int allow_h264) {
+ RwOffer *of = malloc(sizeof *of);
+ if (!of) return -1;
+ int e = rw_sdp_parse_offer(sdp, n, allow_h264, of);
+ int same = !e && !CRYPTO_memcmp(of->fp, r->of->fp, 32) && of->vcodec == r->of->vcodec && of->vpt == r->of->vpt &&
+ of->apt == r->of->apt && of->nmedia == r->of->nmedia && (of->dm >= 0) == (r->of->dm >= 0);
+ for (int i = 0; same && i < of->nmedia; i++) same = of->m[i].kind == r->of->m[i].kind && !strcmp(of->m[i].mid, r->of->m[i].mid);
+ if (!same) {
+ sd_log("video: refused an ICE restart (%s)", e ? "the offer doesn't parse" : "it isn't the same peer, codec or sections");
+ free(of);
+ return e ? e : -1;
+ }
+ if (!strcmp(of->ufrag, r->of->ufrag) && !strcmp(of->pwd, r->of->pwd)) { /* not a restart: the same answer again */
+ free(of);
+ r->answered = 0;
+ send_answer(r);
+ return r->codec;
+ }
+ free(r->of);
+ r->of = of;
+ r->restarts++;
+ new_credentials(r);
+ for (int i = 0; i < r->npath; i++) { r->path[i].valid = 0; r->path[i].nominated = 0; }
+ r->checks = 0;
+ r->nrcand = 0;
+ r->last_consent = r->created = now_ms();
+ add_remote_cands(r);
+ r->answered = 0;
+ sd_log("video: ICE restart %u", r->restarts);
+ send_answer(r);
+ return r->codec;
+}
+
int rtc_offer(Rtc *r, const char *sdp, size_t n, int allow_h264) {
- if (!r || r->offered || r->failed) return -1;
+ if (!r || r->failed) return -1;
+ if (r->offered) return restart(r, sdp, n, allow_h264);
r->of = malloc(sizeof *r->of);
if (!r->of) return -1;
int e = rw_sdp_parse_offer(sdp, n, allow_h264, r->of);
if (e) { sd_log("video: refused the browser's offer (%d)", e); free(r->of); r->of = NULL; return e; }
r->offered = 1;
+ r->created = r->last_consent = now_ms();
r->codec = r->of->vcodec;
r->v.pt = r->of->vpt;
r->a.pt = r->of->apt;
- for (int i = 0; i < r->of->ncand && r->nrcand < 16; i++) {
- const RwCand *c = &r->of->cand[i];
- if (!c->mdns && !c->tcp && c->component == 1) { r->rcand[r->nrcand++] = c->addr; turn_peer(r, &c->addr); }
- }
- turn_perms(r);
+ add_remote_cands(r);
int waiting = (r->nstun_srv && !r->srflx.family) || (r->turn.state > 0 && r->turn.state < 3);
- if (waiting) r->answer_due = now_ms() + ANSWER_WAIT_MS;
+ if (waiting) r->answer_due = now_ms() + (r->turn.state > 0 && r->turn.transport != RW_TURN_UDP ? ANSWER_WAIT_TCP_MS : ANSWER_WAIT_MS);
else send_answer(r);
return r->codec;
}
@@ -1064,27 +1510,62 @@ int rtc_candidate(Rtc *r, const char *c, size_t n) {
}
int rtc_pollfds(Rtc *r, struct pollfd *p, int max) {
- if (!r || r->fd < 0 || max < 2 || r->failed) return 0;
- p[0] = (struct pollfd){ r->fd, POLLIN, 0 };
- p[1] = (struct pollfd){ r->sfd, POLLIN, 0 };
- return 2;
+ if (!r || r->fd < 0 || max < RTC_MAX_FDS || r->failed) return 0;
+ int n = 0;
+ p[n++] = (struct pollfd){ r->fd, POLLIN, 0 };
+ p[n++] = (struct pollfd){ r->sfd, POLLIN, 0 };
+ if (r->cfg.ice_lfd >= 0) p[n++] = (struct pollfd){ r->cfg.ice_lfd, POLLIN, 0 };
+ for (int i = 0; i < MAX_TCP; i++)
+ if (r->tcp[i].fd >= 0) p[n++] = (struct pollfd){ r->tcp[i].fd, (short)(POLLIN | (r->tcp[i].out.off < r->tcp[i].out.len ? POLLOUT : 0)), 0 };
+ Turn *t = &r->turn;
+ if (t->fd >= 0 && t->state >= 0) {
+ short ev = POLLIN;
+ if (t->connecting == 1 || t->out.off < t->out.len) ev |= POLLOUT;
+ if (t->connecting == 2 && t->tssl && SSL_want_write(t->tssl)) ev |= POLLOUT;
+ p[n++] = (struct pollfd){ t->fd, ev, 0 };
+ }
+ return n;
}
void rtc_dispatch(Rtc *r, const struct pollfd *p, int n) {
- if (!r || n < 2) return;
+ if (!r) return;
static uint8_t buf[2048];
- for (int f = 0; f < 2; f++) {
- if (!(p[f].revents & POLLIN)) continue;
- for (int k = 0; k < 256 && !r->failed; k++) {
- struct sockaddr_in6 s;
- socklen_t sl = sizeof s;
- ssize_t got = recvfrom(f ? r->sfd : r->fd, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&s, &sl);
- if (got < 0) break;
- if (got > 1500 || s.sin6_family != AF_INET6) continue;
- RwAddr from;
- from_sockaddr(&s, &from);
- if (f) stun_server(r, buf, (size_t)got, &from);
- else handle_packet(r, buf, (size_t)got, &from, 0);
+ for (int f = 0; f < n && !r->failed; f++) {
+ short ev = p[f].revents;
+ if (!ev) continue;
+ int fd = p[f].fd;
+ if (fd == r->fd || fd == r->sfd) {
+ if (!(ev & POLLIN)) continue;
+ for (int k = 0; k < 256 && !r->failed; k++) {
+ struct sockaddr_in6 s;
+ socklen_t sl = sizeof s;
+ ssize_t got = recvfrom(fd, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&s, &sl);
+ if (got < 0) break;
+ if (got > 1500 || s.sin6_family != AF_INET6) continue;
+ RwAddr from;
+ from_sockaddr(&s, &from);
+ if (fd == r->sfd) stun_server(r, buf, (size_t)got, &from);
+ else handle_packet(r, buf, (size_t)got, &from, 0, 0);
+ }
+ } else if (fd == r->cfg.ice_lfd) {
+ if (ev & POLLIN) for (int k = 0; k < 4; k++) { tcp_accept(r); struct pollfd q = { fd, POLLIN, 0 }; if (poll(&q, 1, 0) <= 0) break; }
+ /* the new connection's first packet is already in its buffer */
+ for (int i = 0; i < MAX_TCP; i++) if (r->tcp[i].fd >= 0 && r->tcp[i].inl && !r->tcp[i].checked) tcp_input(r, i);
+ } else if (fd == r->turn.fd) {
+ Turn *t = &r->turn;
+ if (t->connecting) turn_stream_ready(r);
+ else {
+ if (ev & (POLLERR | POLLHUP)) { t->up = 0; t->state = -1; sd_log("video: the TURN server's connection closed"); continue; }
+ if (ev & POLLOUT) turn_flush(r);
+ if (ev & POLLIN) turn_stream_input(r);
+ }
+ if (t->state < 0) turn_stream_close(t);
+ } else {
+ for (int i = 0; i < MAX_TCP; i++) {
+ if (r->tcp[i].fd != fd) continue;
+ if (ev & POLLOUT) tcp_flush(r, i);
+ if (r->tcp[i].fd >= 0 && (ev & (POLLIN | POLLHUP | POLLERR))) tcp_input(r, i);
+ }
}
}
}
@@ -1111,15 +1592,30 @@ void rtc_tick(Rtc *r) {
if (el >= 1) { r->sctp_t = t; usrsctp_handle_timers(el); }
}
if (r->srtp_up && t - r->last_sr > 1000) { send_sr(r, &r->v); send_sr(r, &r->a); r->last_sr = t; }
+ for (int i = 0; i < MAX_TCP; i++)
+ if (r->tcp[i].fd >= 0 && !r->tcp[i].checked && t - r->tcp[i].born > TCP_CHECK_MS) tcp_close(r, i, "no valid ICE check");
Turn *tu = &r->turn;
if (tu->state == 1 || tu->state == 2) {
- if (t - tu->sent_at > 500) { if (++tu->tries > 6) { tu->state = -1; wipe_pass(r); sd_log("video: no answer from the TURN server"); } else turn_send(r, RW_TURN_ALLOCATE_REQ, tu->state == 2, NULL, 0, 600); }
+ if (tu->transport == RW_TURN_UDP) {
+ if (t - tu->sent_at > 500) { if (++tu->tries > 6) { tu->state = -1; wipe_pass(r); sd_log("video: no answer from the TURN server"); } else turn_send(r, RW_TURN_ALLOCATE_REQ, tu->state == 2, NULL, 0, 600); }
+ } else if (t - tu->started > 8000) { /* a stream is reliable: no retransmissions, one deadline */
+ tu->state = -1;
+ wipe_pass(r);
+ sd_log("video: no answer from the TURN server over %s", rw_turn_transport_name(tu->transport));
+ turn_stream_close(tu);
+ }
} else if (tu->state == 3) {
if (t >= tu->refresh_at) { turn_send(r, RW_TURN_REFRESH_REQ, 1, NULL, 0, 600); tu->refresh_at = t + 60000; }
if (tu->perm_dirty || (tu->nperm && t >= tu->perm_at)) turn_perms(r);
- }
- if (r->offered && t - r->last_consent > CONSENT_MS) fail(r, "the browser stopped answering (no ICE checks)");
- if (r->offered && !r->dtls_up && t - r->created > 20000) fail(r, "no connection after 20 s");
+ } else if (tu->state < 0 && tu->fd >= 0) turn_stream_close(tu);
+ if (r->offered && r->dtls_up && t - r->last_consent > CONSENT_MS) fail(r, RTC_WHY_LOST, "the browser stopped answering (no ICE checks)");
+ if (r->offered && !r->dtls_up && t - r->created > CONNECT_MS)
+ fail(r, r->checks ? RTC_WHY_DTLS : RTC_WHY_NOROUTE, r->checks ? "no DTLS after 20 s (ICE checks arrived)" : "no connection after 20 s (no ICE check arrived over UDP or TCP)");
+}
+
+const char *rtc_path_kind(const Rtc *r) {
+ const Path *s = r && r->sel >= 0 ? &r->path[r->sel] : NULL;
+ return !s ? "" : s->relay ? "relay" : s->tcp ? "tcp" : "udp";
}
int rtc_send(Rtc *r, int chan, const void *p, size_t n, int binary) {
diff --git a/server/src/remote/rtc.h b/server/src/remote/rtc.h
index 260c9e0..1f0762e 100644
--- a/server/src/remote/rtc.h
+++ b/server/src/remote/rtc.h
@@ -1,7 +1,9 @@
/* rtc.h — the remote desktop's WebRTC peer, inside the bridge (see rtc.c and docs/REMOTE.md,
* "Video (WebRTC)"). One per browser connection, in the process that already dropped root: ICE-lite
- * with a built-in STUN responder, DTLS-SRTP (OpenSSL), SRTP (libsrtp2), the data channels over SCTP
- * (usrsctp), RTP for the session's encoded video and Opus audio, and an optional TURN relay. */
+ * over UDP and TCP (passive candidates on the remote desktop's own port) with a built-in STUN
+ * responder, ICE restarts, DTLS-SRTP (OpenSSL), SRTP (libsrtp2), the data channels over SCTP (usrsctp),
+ * RTP for the session's encoded video and Opus audio, and an optional TURN relay over UDP, TCP or TLS.
+ * The WebRTC peer is the only way the remote desktop shows the screen: there is no other path. */
#ifndef LORD_REMOTE_RTC_H
#define LORD_REMOTE_RTC_H
@@ -17,9 +19,17 @@ typedef struct Rtc Rtc;
enum { RTC_CONNECTED = 1, RTC_DC_OPEN, RTC_FAILED };
enum { RTC_CH_INPUT = 0, RTC_CH_TILES = 1 };
+/* Why a peer failed, as told to the browser (it explains it and says what to do; docs/REMOTE.md,
+ * "When it can't connect"): */
+#define RTC_WHY_NOROUTE "noroute" /* no ICE check reached us: UDP and TCP blocked, or no route */
+#define RTC_WHY_DTLS "dtls" /* checks arrived but DTLS never came up, or the certificate didn't match */
+#define RTC_WHY_LOST "lost" /* it was up, then the browser stopped answering */
+#define RTC_WHY_CLOSED "closed" /* the browser closed DTLS or the data channels */
+#define RTC_WHY_SERVER "server" /* something on this side (memory, sockets, SCTP) */
+
typedef struct {
void (*signal)(void *ud, const char *json); /* a message for the browser over the WebSocket */
- void (*state)(void *ud, int state, const char *why);
+ void (*state)(void *ud, int state, const char *why); /* RTC_FAILED: why is one of RTC_WHY_* */
void (*message)(void *ud, int chan, const uint8_t *p, size_t n, int binary); /* from a data channel */
void (*keyframe)(void *ud); /* the browser lost a frame (PLI/FIR) */
void (*rate)(void *ud, unsigned kbps); /* new target bit rate for this viewer */
@@ -34,8 +44,17 @@ typedef struct {
int nstun;
char stun[RTC_MAX_STUN][96]; /* host:port of external STUN servers (Settings → Sharing) */
char turn[96], turn_user[129]; /* host:port and user of a TURN server, or "" */
+ int turn_transport; /* RW_TURN_UDP, RW_TURN_TCP or RW_TURN_TLS */
+ int turn_has_fp; /* TLS: pin this certificate instead of the system's CAs */
+ uint8_t turn_fp[32];
char *turn_pass; /* in memory only; rtc_new takes it over and wipes it when done */
unsigned max_kbps;
+ /* ICE over TCP: lord-sessiond's root connection child hands us, over this listening socket (root's
+ * /run/lordos/rtc/<route>, created before the bridge dropped root), each TCP connection to the
+ * remote desktop port whose first ICE check names our route; -1: no ICE-TCP */
+ int ice_lfd;
+ char route[9]; /* 8 characters [A-Za-z0-9]: the start of every ufrag we use */
+ int tcp_port; /* the remote desktop port, for our passive TCP candidates */
} RtcConfig;
Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb);
@@ -43,11 +62,14 @@ void rtc_free(Rtc *r);
int rtc_port(const Rtc *r); /* the built-in STUN server's UDP port (for the browser) */
/* The browser's offer (from the authenticated WebSocket): parsed strictly (rtcwire.c); the answer goes
- * out through signal(). Returns the chosen RW_CODEC_* or < 0. Only one offer per connection. */
+ * out through signal(). Returns the chosen RW_CODEC_* or < 0 (RW_E*). A later offer is an ICE restart:
+ * new ICE credentials on both sides, the same DTLS association (its fingerprint must not change), the
+ * same codec and sections; anything else is refused (-1) and the browser starts a new peer. */
int rtc_offer(Rtc *r, const char *sdp, size_t n, int allow_h264);
/* A trickled candidate from the browser ("candidate:..."): only used for TURN permissions. */
int rtc_candidate(Rtc *r, const char *c, size_t n);
+#define RTC_MAX_FDS 9 /* UDP media, UDP STUN, the ICE-TCP handoff, 4 ICE-TCP, TURN */
int rtc_pollfds(Rtc *r, struct pollfd *p, int max);
void rtc_dispatch(Rtc *r, const struct pollfd *p, int n);
int rtc_timeout(Rtc *r); /* ms until rtc_tick is due */
@@ -62,6 +84,8 @@ void rtc_audio(Rtc *r, const uint8_t *p, size_t n, uint32_t samples);
/* Data channel message; -1 if the channel isn't open or its send buffer is full (try later). */
int rtc_send(Rtc *r, int chan, const void *p, size_t n, int binary);
int rtc_dc_open(const Rtc *r, int chan);
+/* "udp", "tcp" or "relay": how the media goes now ("" before it does) */
+const char *rtc_path_kind(const Rtc *r);
/* Line for the logs and the browser's status: "h264 2400 kbps 1.2 % lost" */
void rtc_stats(const Rtc *r, char *out, size_t n);
diff --git a/server/src/remote/rtcwire.c b/server/src/remote/rtcwire.c
index e9eb3bf..cb66154 100644
--- a/server/src/remote/rtcwire.c
+++ b/server/src/remote/rtcwire.c
@@ -86,6 +86,31 @@ int rw_turn_pass_valid(const char *s) {
return 1;
}
+int rw_turn_transport(const char *s) {
+ if (!strcmp(s, "udp")) return RW_TURN_UDP;
+ if (!strcmp(s, "tcp")) return RW_TURN_TCP;
+ if (!strcmp(s, "tls")) return RW_TURN_TLS;
+ return -1;
+}
+const char *rw_turn_transport_name(int t) { return t == RW_TURN_TLS ? "tls" : t == RW_TURN_TCP ? "tcp" : "udp"; }
+
+int rw_fp_parse(const char *s, uint8_t out[32]) {
+ size_t n = strlen(s), k = 0;
+ int colons = n == 95;
+ if (n != 64 && n != 95) return -1;
+ for (size_t i = 0; i < n;) {
+ if (colons && i % 3 == 2) { if (s[i] != ':') return -1; i++; continue; }
+ int hi = -1, lo = -1;
+ char c = s[i], d = i + 1 < n ? s[i + 1] : 0;
+ hi = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1;
+ lo = d >= '0' && d <= '9' ? d - '0' : d >= 'a' && d <= 'f' ? d - 'a' + 10 : d >= 'A' && d <= 'F' ? d - 'A' + 10 : -1;
+ if (hi < 0 || lo < 0 || k >= 32) return -1;
+ out[k++] = (uint8_t)(hi << 4 | lo);
+ i += 2;
+ }
+ return k == 32 ? 0 : -1;
+}
+
/* ---- small text helpers ---- */
static int ice_char(char c) {
@@ -493,7 +518,7 @@ int rw_sdp_answer(const RwOffer *of, const RwAnswer *a, char *buf, size_t cap) {
Out o = { buf, 0, cap, 0 };
if (!cap) return -1;
buf[0] = 0;
- outf(&o, "v=0\r\no=- %u 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=ice-lite\r\na=group:BUNDLE", a->sess_id);
+ outf(&o, "v=0\r\no=- %u %u IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=ice-lite\r\na=group:BUNDLE", a->sess_id, a->version ? a->version : 2);
for (int i = 0; i < of->nmedia; i++) if (!of->m[i].rejected) outf(&o, " %s", of->m[i].mid);
outf(&o, "\r\na=msid-semantic: WMS lordos\r\n");
int first = 1;
@@ -517,8 +542,9 @@ int rw_sdp_answer(const RwOffer *of, const RwAnswer *a, char *buf, size_t cap) {
for (int c = 0; c < a->ncand; c++) {
const RwLocalCand *lc = &a->cand[c];
const char *typ = lc->type == RW_CAND_RELAY ? "relay" : lc->type == RW_CAND_SRFLX ? "srflx" : "host";
- outf(&o, "a=candidate:%d 1 udp %u %s %u typ %s", lc->type * 10 + c, lc->prio, lc->ip, lc->port, typ);
- if (lc->type != RW_CAND_HOST) outf(&o, " raddr %s rport %u", lc->raddr[0] ? lc->raddr : "0.0.0.0", lc->rport);
+ outf(&o, "a=candidate:%d 1 %s %u %s %u typ %s", lc->type * 10 + c, lc->tcp ? "tcp" : "udp", lc->prio, lc->ip, lc->port, typ);
+ if (lc->tcp) outf(&o, " tcptype passive");
+ else if (lc->type != RW_CAND_HOST) outf(&o, " raddr %s rport %u", lc->raddr[0] ? lc->raddr : "0.0.0.0", lc->rport);
outf(&o, "\r\n");
}
outf(&o, "a=end-of-candidates\r\n");
@@ -554,6 +580,39 @@ int rw_sdp_answer(const RwOffer *of, const RwAnswer *a, char *buf, size_t cap) {
int rw_is_stun(const uint8_t *p, size_t n) { return n >= 20 && p[0] < 4 && be32(p + 4) == 0x2112A442u; }
+int rw_tcp_frame(const uint8_t *p, size_t n, size_t *len) {
+ if (n < 2) return 0;
+ size_t l = be16(p);
+ if (!l || l > RW_TCP_FRAME_MAX) return -1;
+ if (n < 2 + l) return 0;
+ *len = l;
+ return 1;
+}
+
+int rw_stun_stream(const uint8_t *p, size_t n, size_t *len) {
+ if (n < 1) return 0;
+ if (p[0] > 3) return -1; /* ChannelData or garbage */
+ if (n < 20) return 0;
+ size_t l = be16(p + 2);
+ if (l % 4 || 20 + l > RW_STUN_MAX || be32(p + 4) != 0x2112A442u) return -1;
+ if (n < 20 + l) return 0;
+ *len = 20 + l;
+ return 1;
+}
+
+int rw_ice_route(const uint8_t *p, size_t n, char route[9]) {
+ RwStun s;
+ if (!rw_is_stun(p, n) || rw_stun_parse(p, n, &s) || s.type != RW_STUN_BINDING_REQ || !s.username || s.ulen < 10 || !s.mi_off) return -1;
+ for (int i = 0; i < 8; i++) {
+ char c = (char)s.username[i];
+ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) return -1;
+ route[i] = c;
+ }
+ route[8] = 0;
+ if (!memchr(s.username, ':', s.ulen)) return -1;
+ return 0;
+}
+
uint32_t rw_crc32(const uint8_t *p, size_t n) {
uint32_t c = 0xffffffffu;
for (size_t i = 0; i < n; i++) {
diff --git a/server/src/remote/rtcwire.h b/server/src/remote/rtcwire.h
index 8cfd091..cd16da8 100644
--- a/server/src/remote/rtcwire.h
+++ b/server/src/remote/rtcwire.h
@@ -5,8 +5,8 @@
* (build/remote-sec-test, build/remote-fuzz).
*
* Every parser rejects instead of guessing. The browser's SDP is only accepted over the remote
- * desktop's own authenticated WebSocket; everything else here arrives on the bridge's UDP socket
- * and is parsed before any cryptography looks at it. */
+ * desktop's own authenticated WebSocket; everything else here arrives on the bridge's UDP socket, an
+ * ICE-TCP connection or the TURN server's stream, and is parsed before any cryptography looks at it. */
#ifndef LORD_REMOTE_RTCWIRE_H
#define LORD_REMOTE_RTCWIRE_H
@@ -26,6 +26,14 @@ int rw_ice_server_valid(const char *s);
/* A TURN user name: 1..128 printable ASCII, no space, quote or backslash. Password: 1..128 printable ASCII. */
int rw_turn_user_valid(const char *s);
int rw_turn_pass_valid(const char *s);
+/* How the bridge reaches its TURN server: "udp" (the default), "tcp" or "tls" (TURN over TLS, the
+ * turns: scheme; port 443 works). RW_TURN_* or -1. */
+enum { RW_TURN_UDP = 0, RW_TURN_TCP = 1, RW_TURN_TLS = 2 };
+int rw_turn_transport(const char *s);
+const char *rw_turn_transport_name(int t);
+/* A pinned SHA-256 certificate fingerprint for a TURN-over-TLS server with its own certificate:
+ * 64 hex digits, optionally in pairs separated by ':' (upper or lower case). 0 and out[32], or -1. */
+int rw_fp_parse(const char *s, uint8_t out[32]);
/* ---- SDP: the browser's offer ---- */
@@ -103,6 +111,7 @@ typedef struct {
uint32_t prio;
char raddr[64];
uint16_t rport;
+ int tcp; /* a passive ICE-TCP host candidate (RFC 6544) */
} RwLocalCand;
typedef struct {
@@ -113,6 +122,7 @@ typedef struct {
const RwLocalCand *cand;
int ncand;
uint32_t sess_id;
+ unsigned version; /* o= session version: 2, one more for each ICE restart */
} RwAnswer;
/* ICE-lite, setup:passive, each offered section answered in order (unusable ones rejected with port 0).
* Returns the length or -1 if it doesn't fit. */
@@ -146,6 +156,21 @@ typedef struct {
uint32_t lifetime;
} RwStun;
+/* ICE over TCP (RFC 6544): every packet framed by a 16-bit big-endian length (RFC 4571). Is there a
+ * whole frame at the start of p? 1 (*len = its payload length, the payload at p + 2), 0 not yet, -1
+ * never (a zero or oversized length: the connection is closed). */
+#define RW_TCP_FRAME_MAX 4096
+int rw_tcp_frame(const uint8_t *p, size_t n, size_t *len);
+/* A TURN server's TCP or TLS stream carries bare STUN messages, delimited by their own length (RFC
+ * 8656 §6.2; we never bind channels, so ChannelData is refused). 1 (*len = the whole message), 0 not
+ * yet, -1 never. */
+int rw_stun_stream(const uint8_t *p, size_t n, size_t *len);
+/* The first ICE check on a new ICE-TCP connection, read by lord-sessiond's root connection child
+ * (before any bridge is involved): a STUN binding request whose USERNAME starts with the 8 routing
+ * characters [A-Za-z0-9] of the bridge it is for (our ufrag is <route><4 random>, see rtc.c). 0 and
+ * route[9], or -1. Nothing else of the message is trusted here: the bridge checks its integrity. */
+int rw_ice_route(const uint8_t *p, size_t n, char route[9]);
+
/* First byte demultiplexing (RFC 7983): 0..3 STUN, 20..63 DTLS, 128..191 RTP/RTCP. */
int rw_is_stun(const uint8_t *p, size_t n);
/* Strict: header (magic cookie, length a multiple of 4 and exact), attributes with their padding,
diff --git a/server/src/remote/sessiond.c b/server/src/remote/sessiond.c
index fc224fa..3dfa79b 100644
--- a/server/src/remote/sessiond.c
+++ b/server/src/remote/sessiond.c
@@ -14,7 +14,8 @@
* one child per connection that drops root as soon as it knows whose session it is.
*
* /etc/lordos/session.conf AUTOLOGIN=user (empty or missing line: login screen)
- * /etc/lordos/remote/config enabled=0|1, port=5673, allow=all|group, net=lan|list|any, insecure_dev=0 (0644)
+ * /etc/lordos/remote/config enabled=0|1, port=5673, allow=all|group, net=lan|list|any, insecure_dev=0,
+ * stun=, turn=, turn_user=, turn_key=, turn_transport=udp|tcp|tls, turn_fp= (0644)
* /etc/lordos/remote/access manual "allow <cidr>" / "block <cidr>" lists (root 0600, admin actions)
* /var/lib/lordos/remote/blocklist sources blocked after failed logins (root 0600, survives restarts)
* /var/lib/lordos/remote/requests "unblock <cidr>" lines from the admin helper (read on SIGHUP)
@@ -133,15 +134,17 @@ static void load_config(void) {
rd_conf.port = RD_DEFAULT_PORT;
rd_conf.enabled = rd_conf.group_only = rd_conf.insecure_dev = 0;
rd_conf.net_mode = RS_NET_LAN;
- rd_conf.video = 1;
rd_conf.nstun = 0;
rd_conf.turn[0] = rd_conf.turn_user[0] = 0;
+ rd_conf.turn_transport = RW_TURN_UDP;
+ rd_conf.turn_has_fp = 0;
FILE *f = fopen(RD_CONF_DIR "/config", "r");
char line[256];
while (f && fgets(line, sizeof line, f)) {
int v;
char s[32], h[160];
- if (sscanf(line, "video=%d", &v) == 1) { rd_conf.video = v != 0; continue; }
+ if (sscanf(line, "turn_transport=%31s", s) == 1) { if (rw_turn_transport(s) >= 0) rd_conf.turn_transport = rw_turn_transport(s); continue; }
+ if (sscanf(line, "turn_fp=%159s", h) == 1) { rd_conf.turn_has_fp = !rw_fp_parse(h, rd_conf.turn_fp); continue; }
if (sscanf(line, "stun=%159s", h) == 1) {
if (rw_ice_server_valid(h) && rd_conf.nstun < 4) snprintf(rd_conf.stun[rd_conf.nstun++], 96, "%.95s", h);
continue;
@@ -805,7 +808,23 @@ static void stop_remote(void) {
tls = NULL;
}
+/* ICE-TCP handoff sockets left by connections that ended without cleaning up (killed): only
+ * sockets, only in root's own folder (conn.c ice_listen) */
+static void clean_rtc_dir(void) {
+ struct stat st;
+ if (lstat(RD_RTC_DIR, &st) || !S_ISDIR(st.st_mode) || st.st_uid != 0) return;
+ DIR *d = opendir(RD_RTC_DIR);
+ if (!d) return;
+ struct dirent *e;
+ while ((e = readdir(d))) {
+ if (e->d_name[0] == '.' || strlen(e->d_name) != 8) continue;
+ if (!fstatat(dirfd(d), e->d_name, &st, AT_SYMLINK_NOFOLLOW) && S_ISSOCK(st.st_mode)) unlinkat(dirfd(d), e->d_name, 0);
+ }
+ closedir(d);
+}
+
static void start_remote(void) {
+ clean_rtc_dir();
if (ensure_cert()) { sd_log("could not create the certificate"); return; }
write_fingerprint();
/* the private key: root's, 0600 (fixed if it isn't) */
@@ -1225,8 +1244,9 @@ int main(int argc, char **argv) {
int was = rd_conf.enabled, port = rd_conf.port;
load_config();
unblock_requests();
- sd_log("configuration: remote %s, port %d, from %s; video %s, %d STUN server(s), TURN %s", rd_conf.enabled ? "on" : "off", rd_conf.port,
- rs_net_name(rd_conf.net_mode), rd_conf.video ? "on" : "off", rd_conf.nstun, rd_conf.turn[0] ? "configured" : "none");
+ sd_log("configuration: remote %s, port %d, from %s; %d STUN server(s), TURN %s%s%s", rd_conf.enabled ? "on" : "off", rd_conf.port,
+ rs_net_name(rd_conf.net_mode), rd_conf.nstun, rd_conf.turn[0] ? "over " : "none", rd_conf.turn[0] ? rw_turn_transport_name(rd_conf.turn_transport) : "",
+ rd_conf.turn[0] && rd_conf.turn_has_fp ? " (pinned certificate)" : "");
if (listening && (!rd_conf.enabled || port != rd_conf.port)) stop_remote();
if (rd_conf.enabled && (!listening || !was)) start_remote();
bump();
diff --git a/server/src/remote/sessiond.h b/server/src/remote/sessiond.h
index 4b14bec..2f109c4 100644
--- a/server/src/remote/sessiond.h
+++ b/server/src/remote/sessiond.h
@@ -33,6 +33,7 @@
#define RD_ACCESS RD_CONF_DIR "/access" /* manual allow/block lists (admin actions, root 0600) */
#define RD_COOKIE "__Host-lordos_remote"
#define RD_TURN_KEY RD_CONF_DIR "/turn.key" /* the TURN password (admin action remote-ice, root 0600) */
+#define RD_RTC_DIR SD_RUN_DIR "/rtc" /* ICE-TCP handoff sockets, one per bridge (root 0700; conn.c) */
#define SD_MAX_SESSIONS 32
/* State shared between the main process and its children. */
@@ -73,10 +74,12 @@ typedef struct {
int insecure_dev; /* allows binaries not owned by root (development VMs only) */
int net_mode; /* RS_NET_LAN (default) | RS_NET_LIST | RS_NET_ANY: who may connect */
RsAccess access; /* RD_ACCESS */
- /* the video path (docs/REMOTE.md, "Video (WebRTC)"): video=1 (the default), up to 4 stun=host:port,
- * turn=host:port and turn_user=…; the TURN password is in RD_TURN_KEY (root 0600) */
- int video, nstun;
+ /* the WebRTC connection (docs/REMOTE.md, "Video (WebRTC)"): up to 4 stun=host:port, turn=host:port,
+ * turn_user=…, turn_transport=udp|tcp|tls and, for TLS, an optional pinned turn_fp=<sha-256>; the
+ * TURN password is in RD_TURN_KEY (root 0600) */
+ int nstun, turn_transport, turn_has_fp;
char stun[4][96], turn[96], turn_user[129];
+ uint8_t turn_fp[32];
char server[512], theme[512], greeter_theme[512], fonts[512], shell[512], workdir[512];
} RdConfig;
@@ -133,8 +136,8 @@ void rd_serve(SSL_CTX *ctx, int fd, const char *ip, const char *key);
void rd_tokens_drop_uid(uid_t uid); /* every cookie of that user (lock) */
void rd_tokens_clear(void); /* every cookie (remote turned off) */
-/* ---- enc.c: rectangle (0xAARRGGBB) → PNG or JPEG. Returns malloc'd memory; *jpeg = 1 if it chose JPEG. */
-unsigned char *rd_encode(const uint32_t *px, int stride, int w, int h, int allow_jpeg, int *len, int *jpeg);
+/* ---- enc.c: rectangle (0xAARRGGBB) → PNG (the lossless refresh of still areas). malloc'd memory. */
+unsigned char *rd_encode_png(const uint32_t *px, int stride, int w, int h, int *len);
unsigned char *rd_encode_rgba(const uint32_t *px, int w, int h, int *len); /* cursor, with alpha */
/* ---- monitor.c: lending the monitor to a session ---- */
diff --git a/server/tests/remote_fuzz.c b/server/tests/remote_fuzz.c
index 2c7b54a..2e13f2b 100644
--- a/server/tests/remote_fuzz.c
+++ b/server/tests/remote_fuzz.c
@@ -134,8 +134,8 @@ static void rtc_one(const uint8_t *b, size_t n) {
if (!rw_sdp_parse_offer((const char *)c, n, (int)(rnd() & 1), o)) {
static char ans[16384];
uint8_t fp[32] = { 0 };
- RwLocalCand lc = { "192.168.1.5", 4000, RW_CAND_HOST, 1, "", 0 };
- RwAnswer a = { "ufraguf", "passwordpasswordpassword", fp, 1, 2, 1, &lc, 1, 3 };
+ RwLocalCand lc[2] = { { "192.168.1.5", 4000, RW_CAND_HOST, 1, "", 0, 0 }, { "192.168.1.5", 5673, RW_CAND_HOST, 1, "", 0, 1 } };
+ RwAnswer a = { "ufraguf", "passwordpasswordpassword", fp, 1, 2, 1, lc, 2, 3, (unsigned)(rnd() % 5) };
rw_sdp_answer(o, &a, ans, (size_t)(1 + rnd() % sizeof ans));
}
free(o);
@@ -150,7 +150,14 @@ static void rtc_one(const uint8_t *b, size_t n) {
rw_h264_packetize(c, n, 64 + rnd() % 1500, pk, NULL);
rw_h264_is_idr(c, n);
rw_vp8_packetize(c, n, 64 + rnd() % 1500, pk, NULL);
+ /* ICE-TCP framing, the TURN stream, the root child's route (all on untrusted bytes) */
+ size_t fl = 0, off = 0;
+ while (off < n && rw_tcp_frame(c + off, n - off, &fl) == 1) { char route[9]; rw_ice_route(c + off + 2, fl, route); off += 2 + fl; }
+ for (off = 0; off < n && rw_stun_stream(c + off, n - off, &fl) == 1; off += fl) { RwStun s2; rw_stun_parse(c + off, fl, &s2); }
+ { char route[9]; rw_ice_route(c, n, route); }
c[n] = 0;
+ rw_turn_transport((const char *)c);
+ { uint8_t pin[32]; rw_fp_parse((const char *)c, pin); }
rw_ice_server_valid((const char *)c);
rw_turn_user_valid((const char *)c);
rw_turn_pass_valid((const char *)c);
@@ -183,13 +190,19 @@ static size_t rtc_seed(uint8_t *b, size_t cap) {
}
case 2: case 3:
rw_so_init(&o, (uint16_t)(rnd() % 2 ? RW_STUN_BINDING_REQ : RW_TURN_ALLOCATE_OK), tid);
- if (rnd() % 2) rw_so_attr(&o, RW_ATTR_USERNAME, "abcd:efgh", 9);
+ if (rnd() % 2) { const char *u = rnd() % 2 ? "abcd:efgh" : "Ab3dEf7hWxYz:efgh"; rw_so_attr(&o, RW_ATTR_USERNAME, u, strlen(u)); }
if (rnd() % 2) rw_so_u32(&o, RW_ATTR_PRIORITY, rnd());
if (rnd() % 2) rw_so_xaddr(&o, RW_ATTR_XOR_MAPPED, &a);
if (rnd() % 2) rw_so_xaddr(&o, RW_ATTR_XOR_RELAYED, &a);
if (rnd() % 3 == 0) rw_so_attr(&o, RW_ATTR_DATA, "\x16\xfe\xfd payload", 12);
if (rnd() % 2) rw_so_mi(&o, (const uint8_t *)"k", 1, fake_hmac);
if (rnd() % 2) rw_so_fingerprint(&o);
+ if (rnd() % 3 == 0 && o.n + 2 <= cap) { /* framed for ICE-TCP (RFC 4571) */
+ b[0] = (uint8_t)(o.n >> 8);
+ b[1] = (uint8_t)o.n;
+ memcpy(b + 2, o.b, o.n);
+ return rnd() % 2 ? mutate((char *)b, o.n + 2, 1500) : o.n + 2;
+ }
memcpy(b, o.b, o.n);
return rnd() % 2 ? mutate((char *)b, o.n, 1500) : o.n;
case 4: {
diff --git a/server/tests/remote_sec_test.c b/server/tests/remote_sec_test.c
index ed60dac..4b62c20 100644
--- a/server/tests/remote_sec_test.c
+++ b/server/tests/remote_sec_test.c
@@ -283,7 +283,7 @@ static void test_json(void) {
CHECK(jin("{\"t\":\"resize\",\"w\":100000,\"h\":1080}", 1280, 800, &in) == RS_IN_NONE);
CHECK(jin("{\"t\":\"resize\",\"w\":100,\"h\":100}", 1280, 800, &in) == RS_IN_NONE);
CHECK(jin("{\"t\":\"ping\",\"ts\":123}", 1280, 800, &in) == RS_IN_PING && in.ts == 123);
- CHECK(jin("{\"t\":\"ack\"}", 1280, 800, &in) == RS_IN_ACK);
+ CHECK(jin("{\"t\":\"ack\"}", 1280, 800, &in) == RS_IN_NONE); /* the image path's ack is gone (0.8.4) */
CHECK(jin("{\"t\":\"clip\",\"text\":\"h\\u00e9\\ud83d\\udc51\\n\"}", 1280, 800, &in) == RS_IN_CLIP && in.tlen == 8 &&
!memcmp(in.text, "h\xc3\xa9\xf0\x9f\x91\x91\n", 8));
CHECK(jin("{\"t\":\"clip\",\"text\":\"\\ud83d\"}", 1280, 800, &in) == -1); /* lone surrogate */
@@ -584,8 +584,8 @@ static void test_sdp(void) {
CHECK(rw_sdp_parse_offer(chrome_offer, n, 1, &o) == RW_OK);
uint8_t fp[32];
for (int i = 0; i < 32; i++) fp[i] = (uint8_t)i;
- RwLocalCand c[2] = { { "192.168.1.5", 40000, RW_CAND_HOST, 2130706431, "", 0 }, { "203.0.113.9", 3478, RW_CAND_RELAY, 33554431, "198.51.100.2", 5555 } };
- RwAnswer a = { "AbCdEfGh", "0123456789abcdefABCDEF01", fp, 111, 222, 1, c, 2, 42 };
+ RwLocalCand c[2] = { { "192.168.1.5", 40000, RW_CAND_HOST, 2130706431, "", 0, 0 }, { "203.0.113.9", 3478, RW_CAND_RELAY, 33554431, "198.51.100.2", 5555, 0 } };
+ RwAnswer a = { "AbCdEfGh", "0123456789abcdefABCDEF01", fp, 111, 222, 1, c, 2, 42, 0 };
char ans[16384];
int al = rw_sdp_answer(&o, &a, ans, sizeof ans);
CHECK(al > 0 && strstr(ans, "a=ice-lite\r\n") && strstr(ans, "a=setup:passive\r\n") && strstr(ans, "a=group:BUNDLE 0 1 2\r\n"));
@@ -598,6 +598,78 @@ static void test_sdp(void) {
CHECK(strstr(ans, "a=ssrc:111 cname:lordos") && strstr(ans, "a=ssrc:222 cname:lordos") && strstr(ans, "a=rtpmap:111 opus/48000/2"));
CHECK(!strstr(ans, "RifL1w")); /* never echoes the browser's secrets */
CHECK(rw_sdp_answer(&o, &a, ans, 200) == -1); /* doesn't fit */
+ CHECK(strstr(ans, "o=- 42 2 IN IP4") != NULL); /* first answer: version 2 */
+ /* ICE-TCP: a passive TCP host candidate on the remote desktop port; an ICE restart's answer has the
+ * next session version */
+ RwLocalCand t[2] = { { "192.168.1.5", 40000, RW_CAND_HOST, 2130706431, "", 0, 0 }, { "192.168.1.5", 5673, RW_CAND_HOST, 1358954751, "", 0, 1 } };
+ RwAnswer b = { "AbCdEfGhIjKl", "0123456789abcdefABCDEF01", fp, 111, 222, 1, t, 2, 42, 3 };
+ al = rw_sdp_answer(&o, &b, ans, sizeof ans);
+ CHECK(al > 0 && strstr(ans, "a=candidate:11 1 tcp 1358954751 192.168.1.5 5673 typ host tcptype passive\r\n"));
+ CHECK(strstr(ans, "a=candidate:10 1 udp 2130706431 192.168.1.5 40000 typ host\r\n") && strstr(ans, "o=- 42 3 IN IP4"));
+ CHECK(strstr(ans, "a=ice-ufrag:AbCdEfGhIjKl\r\n") != NULL);
+}
+
+/* ICE over TCP (RFC 6544/4571 framing), the TURN server's stream, the route a root child reads from the
+ * first check, the TURN transports and the pinned fingerprint */
+static void test_tcp(void) {
+ size_t len = 0;
+ uint8_t f[8] = { 0, 3, 'a', 'b', 'c', 0, 1 };
+ CHECK(rw_tcp_frame(f, 0, &len) == 0 && rw_tcp_frame(f, 1, &len) == 0 && rw_tcp_frame(f, 4, &len) == 0);
+ CHECK(rw_tcp_frame(f, 5, &len) == 1 && len == 3);
+ CHECK(rw_tcp_frame(f + 5, 2, &len) == 0); /* length 1, no byte yet */
+ uint8_t z[2] = { 0, 0 }, big[2] = { 0x10, 0x01 }, max[2] = { 0x10, 0x00 };
+ CHECK(rw_tcp_frame(z, 2, &len) == -1 && rw_tcp_frame(big, 2, &len) == -1 && rw_tcp_frame(max, 2, &len) == 0);
+ /* a STUN stream: a binding answer (from test_stun) whole, cut, and followed by ChannelData */
+ uint8_t p[200];
+ int n = hexbytes("0101002c2112a442496f307873764d3849326171002000080001df408d03a44700080014891a716ef20e7a305d8e94f44f899dba06a12704802800049abe8fcc", p);
+ CHECK(rw_stun_stream(p, (size_t)n, &len) == 1 && len == (size_t)n);
+ CHECK(rw_stun_stream(p, 19, &len) == 0 && rw_stun_stream(p, (size_t)n - 1, &len) == 0 && rw_stun_stream(p, 0, &len) == 0);
+ uint8_t cd[8] = { 0x40, 0x00, 0x00, 0x04, 1, 2, 3, 4 };
+ CHECK(rw_stun_stream(cd, 8, &len) == -1); /* ChannelData: we never bind channels */
+ uint8_t odd[20];
+ memcpy(odd, p, 20);
+ odd[3] = 0x2d; /* length not a multiple of 4 */
+ CHECK(rw_stun_stream(odd, 20, &len) == -1);
+ memcpy(odd, p, 20);
+ odd[4] = 0; /* no magic cookie */
+ CHECK(rw_stun_stream(odd, 20, &len) == -1);
+ /* the route: a binding request with MESSAGE-INTEGRITY and a user name "<8 alnum><more>:<theirs>" */
+ uint8_t tid[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
+ RwStunOut o;
+ rw_so_init(&o, RW_STUN_BINDING_REQ, tid);
+ rw_so_attr(&o, RW_ATTR_USERNAME, "Ab3dEf7hWxYz:brws", 17);
+ rw_so_mi(&o, (const uint8_t *)"pw", 2, hmac1);
+ rw_so_fingerprint(&o);
+ char route[9];
+ CHECK(rw_ice_route(o.b, o.n, route) == 0 && !strcmp(route, "Ab3dEf7h"));
+ const char *bad_users[] = { "Ab3dEf7:brws", "Ab3d+f7hWx:b", "Ab3dEf7hWxYz", "short:x" };
+ for (size_t i = 0; i < sizeof bad_users / sizeof bad_users[0]; i++) {
+ rw_so_init(&o, RW_STUN_BINDING_REQ, tid);
+ rw_so_attr(&o, RW_ATTR_USERNAME, bad_users[i], strlen(bad_users[i]));
+ rw_so_mi(&o, (const uint8_t *)"pw", 2, hmac1);
+ rw_so_fingerprint(&o);
+ CHECK(rw_ice_route(o.b, o.n, route) == -1);
+ }
+ rw_so_init(&o, RW_STUN_BINDING_REQ, tid); /* no integrity: never routed */
+ rw_so_attr(&o, RW_ATTR_USERNAME, "Ab3dEf7hWxYz:brws", 17);
+ rw_so_fingerprint(&o);
+ CHECK(rw_ice_route(o.b, o.n, route) == -1);
+ rw_so_init(&o, RW_STUN_BINDING_OK, tid); /* not a request */
+ rw_so_attr(&o, RW_ATTR_USERNAME, "Ab3dEf7hWxYz:brws", 17);
+ rw_so_mi(&o, (const uint8_t *)"pw", 2, hmac1);
+ CHECK(rw_ice_route(o.b, o.n, route) == -1);
+ CHECK(rw_ice_route((const uint8_t *)"\x16\x03\x01", 3, route) == -1); /* a TLS ClientHello */
+ /* TURN transports and the pin */
+ CHECK(rw_turn_transport("udp") == RW_TURN_UDP && rw_turn_transport("tcp") == RW_TURN_TCP && rw_turn_transport("tls") == RW_TURN_TLS);
+ CHECK(rw_turn_transport("TLS") < 0 && rw_turn_transport("") < 0 && rw_turn_transport("dtls") < 0 && rw_turn_transport("tls ") < 0);
+ CHECK(!strcmp(rw_turn_transport_name(RW_TURN_TLS), "tls") && !strcmp(rw_turn_transport_name(99), "udp"));
+ uint8_t fp[32];
+ CHECK(rw_fp_parse("000102030405060708090a0b0c0d0e0f101112131415161718191A1B1C1D1E1F", fp) == 0 && fp[0] == 0 && fp[31] == 0x1f && fp[26] == 0x1a);
+ CHECK(rw_fp_parse("00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F:10:11:12:13:14:15:16:17:18:19:1A:1B:1C:1D:1E:FF", fp) == 0 && fp[31] == 0xff);
+ CHECK(rw_fp_parse("00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F:10:11:12:13:14:15:16:17:18:19:1A:1B:1C:1D:1E", fp) == -1);
+ CHECK(rw_fp_parse("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1g", fp) == -1);
+ CHECK(rw_fp_parse("00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F-10-11-12-13-14-15-16-17-18-19-1A-1B-1C-1D-1E-FF", fp) == -1);
+ CHECK(rw_fp_parse("", fp) == -1 && rw_fp_parse("00", fp) == -1);
}
static void test_cand(void) {
@@ -863,6 +935,7 @@ int main(void) {
test_dcep();
test_rtp();
test_signal();
+ test_tcp();
printf("remote-sec-test: %d checks, %d failed\n", checks, fails);
return fails ? 1 : 0;
}
diff --git a/tools/iso/build.sh b/tools/iso/build.sh
index 9414068..57b66ba 100755
--- a/tools/iso/build.sh
+++ b/tools/iso/build.sh
@@ -3,7 +3,7 @@
# with BIOS and UEFI boot. See docs/ISO.md.
#
# Usage (as root, on Debian 13, from the root of the already built repository):
-# make -C server -j"$(nproc)" # lord-server and lord-sessiond (needs libssl-dev; libsrtp2-dev + libusrsctp-dev for the remote desktop's video; clang for lord-netperm's BPF)
+# make -C server -j"$(nproc)" # lord-server and lord-sessiond (needs libssl-dev, libsrtp2-dev and libusrsctp-dev: the remote desktop is WebRTC only; clang for lord-netperm's BPF)
# (the themes: npm run build on the development machine; the .bundle.js files travel with the tree)
# sudo tools/iso/build.sh [work folder] # default /var/tmp/lordos-iso
#
diff --git a/tools/iso/mkdeb.sh b/tools/iso/mkdeb.sh
index db79713..d53311a 100755
--- a/tools/iso/mkdeb.sh
+++ b/tools/iso/mkdeb.sh
@@ -42,7 +42,7 @@ VERSION=$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' package.json | head -1)
[ -x server/build/lord-netperm ] || { echo "missing server/build/lord-netperm (make -C server; clang for BPF)" >&2; exit 1; }
[ -x server/build/lord-netd ] || { echo "missing server/build/lord-netd (make -C server)" >&2; exit 1; }
[ -x server/build/lord-fand ] || { echo "missing server/build/lord-fand (make -C server)" >&2; exit 1; }
-[ -x server/build/lord-sessiond ] || { echo "missing server/build/lord-sessiond (make -C server, with libssl-dev)" >&2; exit 1; }
+[ -x server/build/lord-sessiond ] || { echo "missing server/build/lord-sessiond (make -C server, with libssl-dev, libsrtp2-dev and libusrsctp-dev)" >&2; exit 1; }
[ -f themes/greeter/theme.bundle.js ] || { echo "missing the login screen (npm run build)" >&2; exit 1; }
for t in themes/*/theme.bundle.js; do [ -f "$t" ] || { echo "built themes missing (npm run build)" >&2; exit 1; }; done
@@ -229,9 +229,9 @@ DOCDEPS=""
if [ -x "$L/bin/lord-doc" ]; then DOCDEPS=", libpoppler-glib8t64, libarchive13t64, libcairo2, libfreetype6, libfontconfig1, poppler-data"; fi
MEDIADEPS=""
if [ -x "$L/bin/lord-media" ]; then MEDIADEPS=", libavcodec61, libavformat61, libavutil59, libswscale8, libswresample5, libva2, libva-drm2"; fi
-# the remote desktop's video (docs/REMOTE.md, "Video (WebRTC)"): lord-sessiond built with libsrtp2 and usrsctp
-RTCDEPS=""
-if objdump -p "$L/bin/lord-sessiond" 2>/dev/null | grep -q "libsrtp2"; then RTCDEPS=", libsrtp2-1, libusrsctp2"; fi
+# the remote desktop is WebRTC only (docs/REMOTE.md, "Video (WebRTC)"): lord-sessiond always links libsrtp2 and usrsctp
+RTCDEPS=", libsrtp2-1, libusrsctp2"
+if ! objdump -p "$L/bin/lord-sessiond" 2>/dev/null | grep -q "libsrtp2"; then echo "lord-sessiond is not linked with libsrtp2: rebuild it (make -C server)" >&2; exit 1; fi
SIZE=$(du -sk "$PKG/usr" | awk '{s += $1} END {print s}')
cat > "$PKG/DEBIAN/control" <<C
Package: lordos