Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Termix connects to a [RustDesk](https://rustdesk.com) device the way the RustDes
- By default Termix finds the device through the public RustDesk server. To use your own, set the ID server, relay server and key in the admin settings, under Remote Desktop. The key is the contents of `id_ed25519.pub` on your server. A host can also set its own server.
- The Termix server talks to the ID server on TCP 21116 and the relay on TCP 21117. It always goes through the relay, since a server can't hole punch.
- Direct IP access connects to the host address on port 21118 instead, with no ID server. Turn on Direct IP access in RustDesk on the device first. The connection is encrypted, but with no ID server nothing proves which device answered. Jump hosts work in this mode.
- Older RustDesk versions answer direct IP access without encryption, and Termix refuses that by default. Turn on Allow unencrypted sessions on the host to connect anyway, and only over a network you trust, such as a VPN.
- Video stays in RustDesk's own format and your browser decodes it, so it needs a recent Chrome, Edge, Firefox or Safari. VP9, AV1 and VP8 are used, never H.264 or H.265.
- Clipboard text works both ways, and you hear the remote's audio. With more than one monitor, the toolbar has a button for each.
- Not supported yet: file transfer and recording.
Expand Down
5 changes: 5 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@
"rustdesk": {
"direct": "Direct IP access",
"directDesc": "Connect to the host address instead of looking up a RustDesk ID. Direct IP access must be on in RustDesk on that device.",
"allowPlain": "Allow unencrypted sessions",
"allowPlainDesc": "Older RustDesk versions answer direct IP access without encryption. Only turn this on over a network you trust, such as a VPN.",
"port": "Direct IP port",
"directNote": "The connection is encrypted, but with no ID server there is nothing to prove which device answered.",
"id": "RustDesk ID",
Expand Down Expand Up @@ -562,6 +564,9 @@
"rustdeskDirect": {
"label": "RustDesk direct IP access"
},
"rustdeskAllowPlain": {
"label": "RustDesk unencrypted direct IP sessions"
},
"rustdeskIdServer": {
"label": "RustDesk ID server"
},
Expand Down
14 changes: 12 additions & 2 deletions src/backend/engine/worker/rustdesk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,16 @@ export class RustDeskClient implements RemoteClient {
route.conn.close();
return;
}
await secure(route.conn, peerId, route, { key, direct, idServer });
this.host.log("debug", "RustDesk session secured", { direct });
const { plain } = await secure(route.conn, peerId, route, {
key,
direct,
idServer,
allowPlain: s.bool("allow-plain"),
});
this.host.log("debug", "RustDesk session secured", {
direct,
encrypted: !plain,
});
this.loginTimer = setTimeout(() => {
if (this.loggedIn || this.stopped) return;
this.abort(
Expand All @@ -200,6 +208,8 @@ export class RustDeskClient implements RemoteClient {
});
}
});
// An unencrypted peer already sent its password challenge.
if (plain) this.receive(plain, peerId);
}

/** Ends the session with an error, once. */
Expand Down
22 changes: 19 additions & 3 deletions src/backend/engine/worker/rustdesk/handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,15 @@ export async function secure(
conn: Connection,
peerId: string,
route: Route,
options: { key: string; direct: boolean; idServer: string },
options: {
key: string;
direct: boolean;
idServer: string;
/** Carry on when a direct peer skips the key exchange. */
allowPlain?: boolean;
},
timeoutMs = 10_000,
): Promise<{ verified: boolean }> {
): Promise<{ verified: boolean; plain?: Uint8Array }> {
let peerSignPk: Uint8Array | null = null;
if (!options.direct) {
const rsKey = serverKey(
Expand All @@ -311,9 +317,19 @@ export async function secure(
peerSignPk = idPk.pk;
}

const first = PbMessage.decode(await conn.next(timeoutMs));
const frame = await conn.next(timeoutMs);
const first = PbMessage.decode(frame);
const signedId = first.message(MSG.signedId);
if (!signedId) {
// Older RustDesk answers direct IP access with the password challenge
// straight away and never encrypts the session.
if (options.direct && first.has(MSG.hash)) {
if (options.allowPlain) return { verified: false, plain: frame };
throw new RustDeskError(
"The remote device answered without encryption. Allow unencrypted sessions on this host to connect anyway",
UPSTREAM_ERROR,
);
}
throw new RustDeskError(
"The remote device did not start a secure session",
UPSTREAM_ERROR,
Expand Down
4 changes: 4 additions & 0 deletions src/backend/host-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface RemoteDesktopHostSettings {
rustdeskRelayServer: string;
rustdeskKey: string;
rustdeskDirect: boolean;
rustdeskAllowPlain: boolean;
moonlightPort: number;
moonlightHostId: number;
moonlightAppId: number;
Expand Down Expand Up @@ -140,6 +141,7 @@ export async function readHostSettings(
rustdeskRelayServer: asText(values.rustdeskRelayServer),
rustdeskKey: asText(values.rustdeskKey),
rustdeskDirect: values.rustdeskDirect === true,
rustdeskAllowPlain: values.rustdeskAllowPlain === true,
moonlightPort: asPort(values.moonlightPort) ?? MOONLIGHT_DEFAULT_PORT,
moonlightHostId: asCount(values.moonlightHostId),
moonlightAppId: asCount(values.moonlightAppId),
Expand Down Expand Up @@ -218,6 +220,8 @@ export function normalizeImportedHost(
}
const direct = asBool(pick("rustdeskDirect"));
if (direct !== undefined) out.rustdeskDirect = direct;
const allowPlain = asBool(pick("rustdeskAllowPlain"));
if (allowPlain !== undefined) out.rustdeskAllowPlain = allowPlain;
const moonlight = asBool(pick("enableMoonlight"));
if (moonlight !== undefined) out.enableMoonlight = moonlight;
const moonlightPort = asPort(pick("moonlightPort"));
Expand Down
1 change: 1 addition & 0 deletions src/backend/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export async function rustdeskSettings(
password: input.password,
"rustdesk-id": settings.rustdeskId,
direct: settings.rustdeskDirect,
"allow-plain": settings.rustdeskDirect && settings.rustdeskAllowPlain,
"id-server": ownServer
? settings.rustdeskIdServer
: await admin("rustdeskIdServer"),
Expand Down
11 changes: 11 additions & 0 deletions src/frontend/HostEditorRustdeskTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ export function HostEditorRustdeskTab({
onChange={(v) => setField("rustdeskDirect", v)}
/>
</SettingRow>
{form.rustdeskDirect && (
<SettingRow
label={t("hosts.rustdesk.allowPlain")}
description={t("hosts.rustdesk.allowPlainDesc")}
>
<FakeSwitch
checked={form.rustdeskAllowPlain}
onChange={(v) => setField("rustdeskAllowPlain", v)}
/>
</SettingRow>
)}
{form.rustdeskDirect ? (
<div className="flex flex-col gap-1.5">
<label className={LABEL}>{t("hosts.rustdesk.port")}</label>
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/host-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RemoteHostOptions {
rustdeskPort: number;
rustdeskId: string;
rustdeskDirect: boolean;
rustdeskAllowPlain: boolean;
rustdeskIdServer: string;
rustdeskRelayServer: string;
rustdeskKey: string;
Expand Down Expand Up @@ -146,6 +147,7 @@ export function remoteOptions(
rustdeskPort: port(values.rustdeskPort, DEFAULT_PORT.rustdesk),
rustdeskId: text(values.rustdeskId),
rustdeskDirect: values.rustdeskDirect === true,
rustdeskAllowPlain: values.rustdeskAllowPlain === true,
rustdeskIdServer: text(values.rustdeskIdServer),
rustdeskRelayServer: text(values.rustdeskRelayServer),
rustdeskKey: text(values.rustdeskKey),
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/remote-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface RemoteDesktopForm {
rustdeskPort: number;
rustdeskId: string;
rustdeskDirect: boolean;
rustdeskAllowPlain: boolean;
rustdeskIdServer: string;
rustdeskRelayServer: string;
rustdeskKey: string;
Expand Down Expand Up @@ -81,6 +82,7 @@ const SETTING_FOR_FIELD: Partial<Record<keyof RemoteDesktopForm, string>> = {
rustdeskPort: "rustdeskPort",
rustdeskId: "rustdeskId",
rustdeskDirect: "rustdeskDirect",
rustdeskAllowPlain: "rustdeskAllowPlain",
rustdeskIdServer: "rustdeskIdServer",
rustdeskRelayServer: "rustdeskRelayServer",
rustdeskKey: "rustdeskKey",
Expand Down Expand Up @@ -195,6 +197,7 @@ export function remoteDesktopForm(props: HostEditorSectionProps): {
rustdeskPort: options.rustdeskPort,
rustdeskId: options.rustdeskId,
rustdeskDirect: options.rustdeskDirect,
rustdeskAllowPlain: options.rustdeskAllowPlain,
rustdeskIdServer: options.rustdeskIdServer,
rustdeskRelayServer: options.rustdeskRelayServer,
rustdeskKey: options.rustdeskKey,
Expand Down
57 changes: 56 additions & 1 deletion tests/backend/engine/worker/rustdesk/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@ async function rustdeskWorld(options: PeerOptions) {
};
}

/**
* A device on loopback that answers direct IP access the way older RustDesk
* does: no key exchange, the password challenge comes first.
*/
async function plainPeer(login: PeerOptions["login"]) {
const peerMessages: PbMessage[] = [];
const port = await listen((conn) => {
conn.send(
msg(MSG.hash, new PbWriter().string(1, "salt").string(2, "chal")),
);
conn.onFrame((frame) => {
const m = PbMessage.decode(frame);
peerMessages.push(m);
const lr = m.message(MSG.loginRequest);
if (lr) login(lr, conn);
});
});
return {
peerMessages,
settings: {
direct: "true",
hostname: "127.0.0.1",
port: String(port),
password: PASSWORD,
} as Record<string, string>,
};
}

const peerInfo = () =>
new PbWriter().message(
2,
Expand All @@ -238,7 +266,7 @@ function acceptLogin(lr: PbMessage, peer: Connection) {
);
}

function start(world: Awaited<ReturnType<typeof rustdeskWorld>>, extra = {}) {
function start(world: { settings: Record<string, string> }, extra = {}) {
const fake = fakeHost({ ...world.settings, ...extra });
const client = new RustDeskClient(fake.host);
clients.push(client);
Expand Down Expand Up @@ -410,6 +438,33 @@ describe("RustDeskClient", () => {
expect(events.failed!.message).toBe("The remote device is offline");
});

it("logs in to a direct peer without encryption when the host allows it", async () => {
const world = await plainPeer((lr, peer) => {
const ok = Buffer.from(lr.bytes(2)).equals(
Buffer.from(passwordHash(PASSWORD, "salt", "chal")),
);
peer.send(
msg(
MSG.loginResponse,
ok ? peerInfo() : new PbWriter().string(1, "Wrong Password"),
),
);
});
const { events } = start(world, { "allow-plain": "true" });
await until(() => events.ready || !!events.failed);
expect(events.failed).toBeNull();
expect(events.ready).toBe(true);
});

it("refuses a direct peer without encryption unless the host allows it", async () => {
const world = await plainPeer(() => {});
const { events } = start(world);
await until(() => !!events.failed);
expect(events.failed!.message).toMatch(/without encryption/);
expect(events.ready).toBe(false);
expect(world.peerMessages).toEqual([]);
});

it("needs an ID unless direct", async () => {
const { events } = start({ settings: {} } as never, {});
await until(() => !!events.failed);
Expand Down
34 changes: 34 additions & 0 deletions tests/backend/rustdesk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ describe("RustDesk hosts", () => {
expect(settings.rustdeskId).toBe("123 456 789");
expect(settings.rustdeskPort).toBe(21118);
expect(settings.rustdeskIdServer).toBe("");
expect(settings.rustdeskAllowPlain).toBe(false);
});

it("reads the switch for unencrypted direct sessions", async () => {
const settings = await readHostSettings(
ctxWith({}, { rustdeskDirect: true, rustdeskAllowPlain: true }),
1,
);
expect(settings.rustdeskAllowPlain).toBe(true);
});

it("imports the switch for unencrypted direct sessions", () => {
expect(
normalizeImportedHost({
pluginSettings: {
"remote-desktop": { rustdeskDirect: true, rustdeskAllowPlain: true },
},
}),
).toEqual({ rustdeskDirect: true, rustdeskAllowPlain: true });
});

it("imports its settings", () => {
Expand Down Expand Up @@ -125,6 +144,21 @@ describe("rustdeskSettings", () => {
expect(out["relay-server"]).toBe("");
});

it("allows an unencrypted session only for direct IP", async () => {
const direct = await rustdeskSettings(
ctxWith({}),
{ ...base, rustdeskDirect: true, rustdeskAllowPlain: true } as never,
input,
);
expect(direct["allow-plain"]).toBe(true);
const byId = await rustdeskSettings(
ctxWith({}),
{ ...base, rustdeskAllowPlain: true } as never,
input,
);
expect(byId["allow-plain"]).toBe(false);
});

it("drops codec lists it does not trust", async () => {
const out = await rustdeskSettings(ctxWith({}), base as never, {
...input,
Expand Down
10 changes: 10 additions & 0 deletions tests/frontend/HostEditorRustdeskTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ describe("RustDesk host editor", () => {
expect(screen.queryByText("RustDesk ID")).toBeNull();
expect(screen.queryByText("ID server")).toBeNull();
});

it("offers unencrypted sessions only for direct IP", async () => {
await renderSection({ enableRustdesk: true, rustdeskDirect: true });
expect(await screen.findByText("Allow unencrypted sessions")).toBeTruthy();
cleanup();
await rendered?.deactivate();
await renderSection({ enableRustdesk: true, rustdeskId: "123456789" });
expect(await screen.findByText("RustDesk ID")).toBeTruthy();
expect(screen.queryByText("Allow unencrypted sessions")).toBeNull();
});
});