Skip to content

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.

7.7 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Full KVM on Windows, the app's side (docs/full-kvm.md), the Full KVM build only (main.js attaches
// it when shared/edition.js says so): the Full KVM tab's state and its Off / Signed in / Always,
// which twindesk-fullkvm.exe carries out (--setup asks for an administrator: one UAC prompt per
// change). No PowerShell, no scripts: the service's own exe does it all (docs/full-kvm.md).
// Biometric unlock (shared/fullkvm/unlock-host.js): Add phone stages the phone's key, the key K it wrapped
// and the password typed on the tab for --setup unlock-add (one UAC prompt), which checks the password
// with Windows and keeps it only under K (windows/fullkvm/service/Unlock.cs); the stage is deleted after.
//   require('./fullkvm/manager.js').attach({ helper, link | hub, name(), settings(), save(), may?(id, cls) })
'use strict';

const fs = require('fs');
const os = require('os');
const path = require('path');
const { execFile } = require('child_process');
const { app, ipcMain, session, screen } = require('electron');

const EXE = path.join(__dirname, 'bin', 'twindesk-fullkvm.exe');
const MODES = ['off', 'simple', 'system'];
// What System-wide copies for the machine node (shared/fullkvm/headless.js): this computer as the
// others know it.
const FILES = ['device.json', 'paired.json', 'paired-mesh.json', 'pairing.json', 'pairings.json', 'multi.json', 'settings.json'];

function run(args, ms = 10000) {
  return new Promise((resolve) => execFile(EXE, args, { timeout: ms, windowsHide: true, maxBuffer: 1 << 20 }, (err, out, errOut) =>
    resolve({ code: err ? (typeof err.code === 'number' ? err.code : -1) : 0, out: String(out || ''), err: String(errOut || '').trim() })));
}

let helper = null;
let lastError = '';

async function state() {
  let s = { mode: 'off', active: false };
  if (fs.existsSync(EXE)) {
    const r = await run(['--state']);
    try { s = JSON.parse(r.out); } catch (_) { /* not installed right */ }
  } else lastError = lastError || 'twindesk-fullkvm.exe is missing';
  return { os: 'win32', mode: MODES.includes(s.mode) ? s.mode : 'off', active: !!s.active, connected: !!(helper && helper.viaService), error: lastError };
}

// System-wide: what the machine node needs, in a folder of our own (the user's temp, only theirs)
// for --apply to copy to %ProgramData%\TwinDesk\machine. The certificate's passphrase goes unsealed:
// DPAPI (safeStorage) is this user's, LOCAL SERVICE couldn't open it; the machine folder is the
// service's and the administrators' only.
function stage() {
  const from = app.getPath('userData');
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'twindesk-fullkvm-'));
  for (const f of FILES) if (fs.existsSync(path.join(from, f))) fs.copyFileSync(path.join(from, f), path.join(dir, f));
  const id = path.join(from, 'identity');
  if (fs.existsSync(path.join(id, 'identity.pfx'))) {
    fs.mkdirSync(path.join(dir, 'identity'));
    fs.copyFileSync(path.join(id, 'identity.pfx'), path.join(dir, 'identity', 'identity.pfx'));
    const pass = require('../identity.js').readPass(path.join(id, 'identity.pass'));
    fs.writeFileSync(path.join(dir, 'identity', 'identity.pass'), pass);
  }
  const displays = screen.getAllDisplays().map((d, i) => ({ id: i + 1, label: d.label || 'screen', bounds: d.bounds, scaleFactor: d.scaleFactor }));
  fs.writeFileSync(path.join(dir, 'fullkvm-displays.json'), JSON.stringify(displays));
  return dir;
}

async function set(mode) {
  if (!MODES.includes(mode)) throw new Error('bad mode');
  lastError = '';
  const now = (await state()).mode;
  if (mode === now) return state();
  let dir = '';
  try {
    if (mode === 'system') dir = stage();
    // The client the service lets in: this exe (TwinDesk.exe; electron.exe in a checkout).
    const r = await run(['--setup', mode, process.execPath, dir || app.getPath('userData'), process.execPath], 180000);
    if (r.code === 5) throw new Error('The administrator’s approval was not given');
    if (r.code !== 0) throw new Error(r.err.split('\n').pop() || 'Full KVM could not be changed (' + r.code + ')');
  } catch (e) {
    lastError = e.message;
  } finally {
    if (dir) fs.rmSync(dir, { recursive: true, force: true });
  }
  return state();
}

let unlock = null;

async function unlockAdd({ user, key, pub, kind, name, secret, k, wrapped }) {
  if ((await state()).mode === 'off') throw new Error('Turn on Full KVM first');
  if (!fs.existsSync(path.join(__dirname, 'bin', 'TwinDeskUnlock.dll'))) throw new Error('This build has no TwinDeskUnlock.dll');
  if (!secret) throw new Error('Type your Windows password: Windows needs it to sign you in');
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'twindesk-unlock-'));
  const stage = path.join(dir, 'add.json');
  try {
    fs.writeFileSync(stage, JSON.stringify({ user, domain: process.env.USERDOMAIN || '', password: secret, key, pub, kind, name, k, wrapped }), { mode: 0o600 });
    const r = await run(['--setup', 'unlock-add', stage], 180000);
    if (r.code === 5) throw new Error('The administrator’s approval was not given');
    if (r.code === 4) throw new Error('Windows didn’t accept that password');
    if (r.code !== 0) throw new Error(r.err.split('\n').pop() || 'The phone could not be added (' + r.code + ')');
  } finally {
    try { fs.writeFileSync(stage, '0'.repeat(1024)); } catch (_) { /* gone */ }
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

async function unlockRemove(key) {
  const r = await run(['--setup', 'unlock-remove', key], 180000);
  if (r.code === 5) throw new Error('The administrator’s approval was not given');
  if (r.code !== 0) throw new Error(r.err.split('\n').pop() || 'The phone could not be removed');
}

function attach(ctx) {
  helper = ctx.helper;
  unlock = require('../../shared/fullkvm/unlock-host.js').unlockHost({
    helper, hub: ctx.hub, link: ctx.link, name: ctx.name || (() => os.hostname()), wrap: true,
    on: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlock),
    asks: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockAsk === true),
    phones: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockPhones), // Allow phones (off: turned on after a warning)
    computers: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockComputers), // Allow computers (off)
    may: ctx.may, // Multi-device: a computer only as a mesh manager this one accepted for Unlock (shared/mesh-mgr.js)
    enroll: unlockAdd,
    remove: unlockRemove,
  });
  ipcMain.handle('fullkvm', async (_e, op, arg, arg2) => {
    if (op === 'set') return set(String(arg));
    if (op === 'unlock') { await unlock.refresh(); return unlock.state(); }
    if (op === 'unlockAdd') return unlock.add(String(arg), typeof arg2 === 'string' ? arg2 : '');
    if (op === 'unlockDrop') return unlock.drop(String(arg));
    const KEY = { unlockOn: 'unlock', unlockAsk: 'unlockAsk', unlockPhones: 'unlockPhones', unlockComputers: 'unlockComputers' };
    if (KEY[op] && ctx.settings) {
      ctx.settings().fullkvm = { ...(ctx.settings().fullkvm || {}), [KEY[op]]: arg === true };
      ctx.save();
      await unlock.changed();
      return unlock.state();
    }
    return state();
  });
  const preload = path.join(__dirname, 'preload.js');
  const ses = session.defaultSession;
  if (typeof ses.registerPreloadScript === 'function') ses.registerPreloadScript({ type: 'frame', id: 'twindesk-fullkvm', filePath: preload });
  else ses.setPreloads([...ses.getPreloads(), preload]);
}

// What may unlock here changed outside the Full KVM tab (a mesh manager accepted or revoked): every
// connected device hears it now (shared/mesh-mgr.js).
function unlockChanged() {
  if (unlock) unlock.changed().catch(() => {});
}

module.exports = { attach, state, set, unlockChanged };