All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.
JavaScript androidapple-ecosystemconnectelectronfile-sharingintel-unison-alternativeiosios-windowskde-connect-alternativekvmlinuxmacbookmacosmouse-sharingphone-link-alternativeproductivityscreen-sharingsidecar-alternativesynergy-alternativewindows
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Biometric unlock from this computer (docs/full-kvm.md): a Mac with Touch ID or a Windows PC with Windows
// Hello unlocks another computer running Full KVM, as a phone does, when that computer allows computers
// (and, in Multi-device, made this one a mesh manager allowed to unlock). One-to-one: the partner;
// Multi-device: every computer directly connected. Off on a new install (Unlock other computers).
// It speaks the phone's side of shared/fullkvm/unlock-host.js:
// → unlock.hi {key}, unlock.want {key}, unlock.sign {nonce, key, sig, k?}, unlock.no, unlock.key
// ← unlock.state, unlock.ask, unlock.done, unlock.end, unlock.enroll, unlock.saved, unlock.forget
// The prompt names the computer as this one knows it (its pairing), never as a message says. Three no's
// or unanswered asks in ten minutes and that computer's asks are refused for ten minutes.
//
// const c = unlockClient({ hub | link, signer, settings() (.unlocker), save(), push?(), name(id) })
// signer: { kind: 'mac' | 'hello', can() → Promise<bool>, create(key) → Promise<pub>,
// wrap(key, k) → Promise<wrapped>, sign(key, data: Buffer, reason, wrapped?) → Promise<{sig, k?}>, remove(key) }
// c.state() ; c.setOn(on) ; c.want(id) ; c.wantAll(ids) (a mesh manager's Unlock all: one after another)
'use strict';
const crypto = require('crypto');
const P = require('./protocol.js');
const PREFIX = 'twindesk-unlock-v1';
const KEY_ID = /^[A-Za-z0-9_-]{8,64}$/;
const BACK = ['unlock.state', 'unlock.ask', 'unlock.done', 'unlock.end', 'unlock.enroll', 'unlock.saved', 'unlock.forget'];
const QUIET_MS = 10 * 60 * 1000;
const str = (v, n = 200) => (typeof v === 'string' ? v.slice(0, n) : '');
function unlockClient(o) {
const signer = o.signer;
const cfg = () => {
const s = o.settings();
if (!s.unlocker || typeof s.unlocker !== 'object') s.unlocker = {};
const u = s.unlocker;
if (typeof u.on !== 'boolean') u.on = false;
if (!u.keys || typeof u.keys !== 'object') u.keys = {};
return u;
};
const targets = new Map(); // computer id → {on, set, locked, name, off, key}
const refusals = new Map(); // computer id → [times]
const quiet = new Map(); // computer id → until
let busy = ''; // a computer id while its prompt is open
let note = '';
let can = false;
const staged = new Map(); // computer id → key made for Add, until saved
let send;
let computers;
if (o.hub) {
const hub = o.hub;
computers = () => (hub.peers() || []).filter((d) => d.connected && !P.isPhoneOs(d.os)).map((d) => d.id);
send = (id, m) => hub.bus.send(id, m);
for (const t of BACK) hub.bus.on(t, (m, from) => heard(t, m || {}, from));
hub.bus.onPeer('welcome', (id) => setTimeout(() => hi(id), 300));
hub.bus.onPeer('closed', (id) => { targets.delete(id); push(); });
} else {
const link = o.link;
const cur = () => { const c = link.current; return c && c.deviceId && !P.isPhoneOs(c.os) ? c.deviceId : ''; };
let last = '';
computers = () => (cur() ? [cur()] : []);
send = (id, m) => { if (cur() !== id) return false; link.send(m); return true; };
for (const t of BACK) link.on(t, (m) => { const id = cur(); if (id) heard(t, m || {}, id); });
link.on('welcome', () => { last = cur(); if (last) setTimeout(() => hi(last), 300); });
link.on('closed', () => { if (last) targets.delete(last); last = ''; push(); });
}
function push() { if (o.push) try { o.push(); } catch (_) { /* no window */ } }
const nameOf = (id) => str(o.name(id), 64) || (targets.get(id) || {}).name || 'the other computer';
const keyFor = (id) => (KEY_ID.test(cfg().keys[id] || '') ? cfg().keys[id] : '');
function hi(id) {
send(id, { t: 'unlock.hi', key: cfg().on ? keyFor(id) : '' });
}
function refused(id) {
const now = Date.now();
const list = (refusals.get(id) || []).filter((t) => t > now - QUIET_MS);
list.push(now);
if (list.length >= 3) { quiet.set(id, now + QUIET_MS); refusals.delete(id); } else refusals.set(id, list);
}
async function heard(t, m, from) {
if (!computers().includes(from)) return;
switch (t) {
case 'unlock.state': {
const s = { on: !!m.on, set: !!m.set, locked: !!m.locked, name: str(m.name, 64), off: str(m.off, 120), key: str(m.key, 64) };
targets.set(from, s);
if (s.set && keyFor(from) && s.key && s.key !== keyFor(from)) note = 'Add this computer again on ' + nameOf(from);
push();
return;
}
case 'unlock.ask': {
const nonce = str(m.nonce, 80);
const key = keyFor(from);
if (!cfg().on || !key) return send(from, { t: 'unlock.no', nonce, why: 'off' });
if ((quiet.get(from) || 0) > Date.now() || busy) return send(from, { t: 'unlock.no', nonce, why: 'busy' });
busy = from;
push();
try {
const user = str(m.user, 64);
const r = await signer.sign(key, Buffer.from(PREFIX + '\n' + user + '\n' + nonce, 'utf8'), 'unlock ' + nameOf(from) + (user ? ' as ' + user : ''), str(m.wrapped, 1000) || undefined);
send(from, { t: 'unlock.sign', nonce, key, sig: r.sig, ...(r.k ? { k: r.k } : {}) });
} catch (e) {
if (e && e.message === 'gone') { note = 'Add this computer again on ' + nameOf(from) + ': its key is gone'; delete cfg().keys[from]; o.save(); }
else refused(from);
send(from, { t: 'unlock.no', nonce, why: e && e.message === 'cancelled' ? 'declined' : 'failed' });
} finally {
busy = '';
push();
}
return;
}
case 'unlock.done':
note = m.ok ? 'Unlocked ' + nameOf(from) : nameOf(from) + ' wasn’t unlocked: ' + str(m.why, 120);
if (queue.current === from) next();
push();
return;
case 'unlock.end':
return;
case 'unlock.enroll': {
const rid = str(m.rid, 40);
if (!cfg().on) return send(from, { t: 'unlock.no', rid, why: 'Turn on Unlock other computers on ' + str(o.name(null), 64) });
if (busy) return send(from, { t: 'unlock.no', rid, why: 'Busy' });
busy = from;
push();
const key = 'c' + crypto.randomBytes(12).toString('hex');
try {
const pub = await signer.create(key);
staged.set(from, key);
const wrapped = typeof m.wrap === 'string' && m.wrap ? await signer.wrap(key, m.wrap) : '';
const r = await signer.sign(key, Buffer.from(PREFIX + '\n' + str(m.user, 64) + '\n' + str(m.nonce, 80), 'utf8'), 'let ' + nameOf(from) + ' be unlocked from this computer');
send(from, { t: 'unlock.key', rid, key, pub, sig: r.sig, kind: signer.kind, name: str(o.name(null), 64), ...(wrapped ? { wrapped } : {}) });
} catch (e) {
staged.delete(from);
try { await signer.remove(key); } catch (_) { /* never made */ }
send(from, { t: 'unlock.no', rid, why: e && e.message === 'cancelled' ? 'Not confirmed on ' + str(o.name(null), 64) : 'This computer couldn’t make a key' });
} finally {
busy = '';
push();
}
return;
}
case 'unlock.saved': {
const key = str(m.key, 64);
if (staged.get(from) !== key) return;
staged.delete(from);
if (m.ok) {
const old = keyFor(from);
cfg().keys[from] = key;
o.save();
if (old && old !== key) signer.remove(old).catch(() => {});
note = nameOf(from) + ' can now be unlocked from this computer. Key ' + str(m.fp, 24);
} else signer.remove(key).catch(() => {});
push();
return;
}
case 'unlock.forget': {
const key = str(m.key, 64);
if (key && key === keyFor(from)) { delete cfg().keys[from]; o.save(); signer.remove(key).catch(() => {}); push(); }
return;
}
default:
}
}
async function refreshCan() {
try { can = !!(await signer.can()); } catch (_) { can = false; }
push();
}
refreshCan();
function state() {
return {
can,
kind: signer.kind,
on: cfg().on,
busy,
note,
targets: computers().map((id) => ({ id, name: nameOf(id), key: keyFor(id), ...(targets.get(id) || { on: false, set: false, locked: false, off: '' }) })),
};
}
function setOn(on) {
cfg().on = !!on;
o.save();
for (const id of computers()) hi(id);
push();
return state();
}
function want(id) {
const key = keyFor(id);
if (!cfg().on || !key || !computers().includes(id)) return state();
send(id, { t: 'unlock.want', key });
return state();
}
// Unlock all (a mesh manager): each locked computer that has this one's key, one after another (one
// Windows Hello prompt each); the next is asked once the last answered, or after 40 s without an answer.
const queue = { ids: [], current: '', timer: null };
function next() {
clearTimeout(queue.timer);
queue.current = '';
while (queue.ids.length) {
const id = queue.ids.shift();
const t = targets.get(id);
if (!keyFor(id) || !t || !t.on || !t.set || !t.locked || !computers().includes(id)) continue;
queue.current = id;
queue.timer = setTimeout(next, 40000);
want(id);
return;
}
}
function wantAll(ids) {
if (!cfg().on) return state();
queue.ids = [...new Set(ids)].filter((id) => typeof id === 'string').slice(0, 10);
if (!queue.current) next();
return state();
}
return { state, setOn, want, wantAll, refreshCan, _heard: heard };
}
// The platform's signer from its helper: the Mac's Touch ID (Unlock.swift) or Windows Hello (Hello.cs).
function macSigner(helper) {
return {
kind: 'mac',
can: async () => !!(await helper.request('touchCan', {})).can,
create: async (key) => (await helper.request('touchCreate', { key })).pub,
wrap: async (key, k) => (await helper.request('touchWrap', { key, k })).wrapped,
sign: async (key, data, reason, wrapped) => {
const r = await helper.request('touchSign', { key, data: data.toString('base64'), reason, ...(wrapped ? { wrapped } : {}) }, 70000);
return { sig: r.sig, ...(r.k ? { k: r.k } : {}) };
},
remove: async (key) => { await helper.request('touchDelete', { key }); },
};
}
// Windows Hello signs; K for a Windows partner is kept with DPAPI and given back only after it signed.
function helloSigner(helper) {
return {
kind: 'hello',
can: async () => !!(await helper.request('helloCan', {}, 8000)).can,
create: async (key) => (await helper.request('helloCreate', { key }, 70000)).pub,
wrap: async (_key, k) => (await helper.request('helloProtect', { data: k })).data,
sign: async (key, data, _reason, wrapped) => {
const r = await helper.request('helloSign', { key, data: data.toString('base64') }, 70000);
const out = { sig: r.sig };
if (wrapped) out.k = (await helper.request('helloUnprotect', { data: wrapped })).data;
return out;
},
remove: async (key) => { await helper.request('helloDelete', { key }); },
};
}
module.exports = { unlockClient, macSigner, helloSigner };