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.

9.9 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith, Erdoğan Yeşil. GPL-3.0, see LICENSE.
// Host role: pairing responder (numeric comparison),
// paired.json (the computers that connect to this PC), and `hello` auth. Port of mac/pairing.js.
//
// Events: 'change' — the pending request or the paired list changed;
//         'request' — a new code is ready to show (bring the window to the front).
'use strict';

const { EventEmitter } = require('events');
const P = require('../../shared/protocol.js');
const { PairFails } = require('../../shared/pairfail.js');

const HEX = (n) => new RegExp(`^[0-9a-f]{${n}}$`);

class Pairing extends EventEmitter {
  // identity: {id, name, fp}. storage: {read(name, fallback), write(name, obj)} (default: store.js).
  // No client is refused because another one is connected (mesh); one pairing at a time.
  constructor(server, identity, { storage } = {}) {
    super();
    this.store = storage || require('./store'); // the mesh's own files (store.js)
    this.server = server;
    this.id = identity;
    this.paired = this.store.read('paired.json', []);
    this.pending = null; // {ctx, id, name, commit, nm, nw, code, confirmed, allowed, timer}
    this.fails = new PairFails(); // failed pairings per address: a lockout after too many (#136)
    server.on('preauth', (ctx, msg) => this._onMessage(ctx, msg));
    server.on('closed', (ctx) => this._onClosed(ctx));
  }

  list() {
    return this.paired.map(({ id, name, os, pairedAt, lastSeen }) => ({ id, name, os: os || P.OS.WIN, pairedAt, lastSeen }));
  }

  // What the control window shows about an in-progress request.
  pendingView() {
    const p = this.pending;
    return p && { name: p.name, os: p.os, code: p.code, confirmed: p.confirmed, allowed: p.allowed, wired: p.wired, qr: p.qr };
  }

  touch(id) {
    const e = this.paired.find((x) => x.id === id);
    if (!e) return;
    e.lastSeen = Date.now();
    this._save();
  }

  forget(id) {
    // A pairing from it still going would add it straight back when it finishes: it ends here.
    const p = this.pending;
    if (p && p.id === id) { this._clear(); this.server.fail(p.ctx, P.E.PAIR_REFUSED, 'The pairing was forgotten'); }
    this.paired = this.paired.filter((x) => x.id !== id);
    this._save();
    this.server.closeDevice(id, P.E.BAD_TOKEN, 'This computer forgot the pairing');
    this.emit('change');
  }

  // Local Allow / Deny.
  respond(allow) {
    const p = this.pending;
    if (!p) return;
    if (!allow) return this._abort(P.E.PAIR_REFUSED, 'Pairing was denied on the other computer');
    if (!p.code) return; // nothing to compare yet
    p.allowed = true;
    this.emit('change');
    this._maybeFinish();
  }

  _save() {
    this.store.write('paired.json', this.paired);
  }

  _onMessage(ctx, msg) {
    if (msg.t === P.T.HELLO) return this._hello(ctx, msg);

    const p = this.pending;
    if (msg.t === P.T.PAIR_START) {
      if (this.fails.locked(ctx.address)) {
        return this.server.fail(ctx, P.E.PAIR_REFUSED, 'Too many failed pairing attempts from this address; wait a few minutes and try again');
      }
      if (p && p.ctx !== ctx) return this.server.fail(ctx, P.E.BUSY, 'Another pairing is in progress');
      if (p) return this.server.fail(ctx, P.E.BAD_MESSAGE, 'Pairing already started');
      if (msg.v !== P.PROTOCOL_VERSION) {
        return this.server.fail(ctx, P.E.VERSION_MISMATCH, `This computer speaks protocol v${P.PROTOCOL_VERSION}`);
      }
      if (typeof msg.id !== 'string' || !msg.id || typeof msg.commit !== 'string' || !HEX(64).test(msg.commit)) {
        this.fails.fail(ctx.address);
        return this.server.fail(ctx, P.E.BAD_MESSAGE, 'pair.start needs id and commit');
      }
      const nm = P.randomHex(16);
      this.pending = {
        ctx,
        id: msg.id,
        name: String(msg.name || 'PC').slice(0, 64),
        os: Object.values(P.OS).includes(msg.os) ? msg.os : P.OS.WIN,
        commit: msg.commit,
        nm,
        nw: null,
        code: null,
        confirmed: false,
        allowed: false,
        // A phone on the USB cable, with the nonce wired.js sent it through adb (wired.knows):
        // no code to compare, only Allow here.
        wired: !!(this.isWired && this.isWired(msg.wired)),
        qr: !!(this.isQr && this.isQr(msg.wired)), // the same, by Connection's QR code: on the network
        timer: setTimeout(() => this._abort(P.E.PAIR_TIMEOUT, 'Pairing timed out'), P.PAIR_TIMEOUT_MS),
      };
      this.server.send(ctx, { t: P.T.PAIR_NONCE, nonce: nm, os: P.OS.LINUX });
      return this.emit('change');
    }

    if (!p || p.ctx !== ctx) {
      if (msg.t === P.T.PAIR_CANCEL) return ctx.ws.close();
      return this.server.fail(ctx, P.E.BAD_MESSAGE, `"${msg.t}" without pair.start`);
    }

    switch (msg.t) {
      case P.T.PAIR_REVEAL:
        if (p.nw || typeof msg.nonce !== 'string' || !HEX(32).test(msg.nonce) || P.commitOf(msg.nonce) !== p.commit) {
          return this._abort(P.E.PAIR_REFUSED, 'Nonce does not match the commitment');
        }
        p.nw = msg.nonce;
        p.code = P.sasCode(this.id.fp, p.nw, p.nm);
        this.emit('change');
        this.emit('request');
        return;
      case P.T.PAIR_CONFIRM:
        if (!p.code) return this._abort(P.E.BAD_MESSAGE, 'pair.confirm before pair.reveal');
        p.confirmed = true;
        this.emit('change');
        return this._maybeFinish();
      case P.T.PAIR_CANCEL:
        this.fails.fail(p.ctx.address); // a cancel after an accepted start counts toward the lockout
        this._clear();
        ctx.ws.close();
        return;
      default:
        return this._abort(P.E.BAD_MESSAGE, `Unexpected "${msg.t}" during pairing`);
    }
  }

  _maybeFinish() {
    const p = this.pending;
    if (!p || !p.confirmed || !p.allowed) return;
    const token = P.randomHex(32);
    const now = Date.now();
    this.paired = this.paired.filter((x) => x.id !== p.id);
    this.paired.push({ id: p.id, name: p.name, os: p.os, tokenHash: P.sha256Hex(token), pairedAt: now, lastSeen: now });
    this._save();
    this.fails.clear(p.ctx.address);
    this.server.send(p.ctx, { t: P.T.PAIR_OK, token, macId: this.id.id, macName: this.id.name, os: P.OS.LINUX });
    console.log(`[pairing] paired with ${p.name} (${p.id})`);
    if (p.qr && this.consumeQr) this.consumeQr(); // a QR pairing that went through: the link is single-use
    this._clear();
  }

  // Pairing without the 6-digit flow, for a device an already trusted device vouches for
  // (mesh.intro.ask, autojoin.js). Stores the entry like _maybeFinish and returns the credential
  // the device needs to dial this PC: {id, name, os, token}. The caller checks who may ask.
  grantFor({ id, name, os }) {
    if (typeof id !== 'string' || !id || id === this.id.id) return null;
    const token = P.randomHex(32);
    const now = Date.now();
    // The token it has may still be one of the last ones: a grant can be lost on its way (the introducer
    // gone, or the device already linked), and with only the new one valid it was 'not paired' at its
    // next connect and forgot this computer for good. The last two stay valid with the new one.
    const prev = this.paired.find((x) => x.id === id);
    const oldHashes = prev ? [prev.tokenHash].concat(prev.oldHashes || []).filter((h) => typeof h === 'string').slice(0, 2) : [];
    const entry = { id, name: String(name || 'Device').slice(0, 64), os: Object.values(P.OS).includes(os) ? os : P.OS.WIN, tokenHash: P.sha256Hex(token), pairedAt: now, lastSeen: now, ...(oldHashes.length ? { oldHashes } : {}) };
    this.paired = this.paired.filter((x) => x.id !== id);
    this.paired.push(entry);
    this._save();
    console.log(`[pairing] granted ${entry.name} (${id}) through an introduction`);
    this.emit('change');
    return { id, name: entry.name, os: entry.os, token };
  }

  _hello(ctx, msg) {
    if (msg.v !== P.PROTOCOL_VERSION) {
      return this.server.fail(ctx, P.E.VERSION_MISMATCH, `This computer speaks protocol v${P.PROTOCOL_VERSION}`);
    }
    const e = this.paired.find((x) => x.id === msg.id);
    if (!e || !(P.tokenMatches(msg.token, e.tokenHash) || (e.oldHashes || []).some((h) => P.tokenMatches(msg.token, h)))) {
      return this.server.fail(ctx, P.E.BAD_TOKEN, 'Not paired with this computer');
    }
    if (typeof msg.name === 'string' && msg.name && msg.name !== e.name) e.name = msg.name.slice(0, 64);
    if (Object.values(P.OS).includes(msg.os)) e.os = msg.os;
    e.lastSeen = Date.now();
    this._save();
    ctx.ilWanted = msg.il && typeof msg.il === 'object' ? msg.il : null; // the input lane (shared/inputlane.js)
    ctx.fx2Wanted = msg.fx2 === 1; // … and the files engine (shared/meshfx2.js)
    ctx.fxWanted = msg.fx === 1; // fast files (shared/meshfx.js): the bytes over HTTPS, a token of this session
    ctx.mesh = !!msg.mesh; // the device speaks the mesh (docs/mesh.md): it gets the roster
    this.server.authenticate(ctx, { deviceId: e.id, name: e.name, os: e.os || P.OS.WIN }, {
      t: P.T.WELCOME, v: P.PROTOCOL_VERSION, macId: this.id.id, macName: this.id.name, os: P.OS.LINUX, mesh: 1,
      tail: require('../../shared/tailnet.js').mine(), // its Tailscale addresses, for reconnecting away from this network (#161)
      lan: require('../../shared/lanaddrs.js').mine(), lanPort: this.server.port, // its LAN addresses: a phone on Bluetooth moves to the Wi-Fi without discovery (#287)
    });
    this.emit('change');
  }

  _abort(code, message) {
    const p = this.pending;
    if (!p) return;
    this.fails.fail(p.ctx.address); // a denial, a wrong code, a timeout: all count toward the lockout
    this._clear();
    this.server.fail(p.ctx, code, message);
  }

  _clear() {
    if (!this.pending) return;
    clearTimeout(this.pending.timer);
    this.pending = null;
    this.emit('change');
  }

  _onClosed(ctx) {
    if (this.pending && this.pending.ctx === ctx) {
      this.fails.fail(ctx.address); // a socket dropped mid-pairing counts like a cancel
      this._clear();
    }
    if (ctx.authed && ctx.deviceId) this.touch(ctx.deviceId);
    this.emit('change');
  }
}

module.exports = { Pairing };