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.

2.4 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Driver downloads (display driver, ViGEmBus) from the app itself, before the administrator prompt.
// Electron's net goes through Windows' proxy settings (and PAC), which PowerShell's
// Invoke-WebRequest in an elevated script does not, so a network that reaches GitHub only through
// a proxy still works (#348). The elevated script checks the hash and the signature again.
const crypto = require('crypto');
const fs = require('fs');

// A short reason for a failed download, in English (the error text Windows gives may be in the
// system's code page and unreadable once it comes back).
function reason(e, host) {
  const m = String((e && e.message) || e || '');
  if (/ERR_NAME_NOT_RESOLVED|ENOTFOUND|EAI_AGAIN/i.test(m)) return `couldn’t reach ${host} (its name doesn’t resolve). Check the internet connection, or a proxy, and try again`;
  if (/ERR_CONNECTION|ECONNRE|ETIMEDOUT|ERR_TIMED_OUT|TIMEOUT|ERR_PROXY|ERR_TUNNEL|ERR_SSL|abort/i.test(m)) return `couldn’t download from ${host} (${m.replace(/^net::/, '').slice(0, 80)}). Check the internet connection, or a proxy, and try again`;
  return m;
}

// Download url to dest and check its SHA-256 (hex, any case). Resolves dest.
async function fetchFile(url, sha256, dest, { timeoutMs = 240000, net } = {}) {
  const host = new URL(url).host;
  const n = net || require('electron').net;
  const ac = new AbortController();
  const t = setTimeout(() => ac.abort(), timeoutMs);
  try {
    let res;
    try { res = await n.fetch(url, { signal: ac.signal, redirect: 'follow' }); } catch (e) { throw new Error(reason(e, host)); }
    if (!res.ok) throw new Error(`couldn’t download from ${host} (HTTP ${res.status})`);
    let buf;
    try { buf = Buffer.from(await res.arrayBuffer()); } catch (e) { throw new Error(reason(e, host)); }
    const got = crypto.createHash('sha256').update(buf).digest('hex');
    if (got.toLowerCase() !== String(sha256).toLowerCase()) throw new Error('the download has the wrong checksum');
    fs.writeFileSync(dest, buf);
    return dest;
  } finally {
    clearTimeout(t);
  }
}

// A PowerShell result file: Set-Content -Encoding UTF8 writes a BOM in Windows PowerShell 5.
function readResult(file, missing) {
  try { return fs.readFileSync(file, 'utf8').replace(/^/, '').trim(); } catch { return missing; }
}

module.exports = { fetchFile, readResult, reason };