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.

3.9 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// shared/netlink.js, Network fallback (General, off by default, #345):
//   - off: losing the network asks for nothing (as before)
//   - on: a session that was on a network and now runs through Bluetooth asks the phone for a link
//     ('fallback'), once the phone said what it can do (net.caps); never with an iPhone, never when
//     the two were never on a network together
//   - net.lan (the phone's own Wi-Fi is back) lets the link go; no new ask for a while after that
//   - hosting the link here (Windows): it goes once this computer's own network was gone and is back
//   - ownLan() leaves out the links' ranges, link-local, Tailscale and virtual adapters
// Run with: node shared/test/netlink-fallback.js
'use strict';
const assert = require('node:assert/strict');
const { test } = require('node:test');
const { NetLink } = require('../netlink.js');
const P = require('../protocol.js');

function fixture(t, { fallback = true, os = P.OS.ANDROID, deviceId = 'phone-' + Math.random() } = {}) {
  const sent = [];
  let current = { deviceId, transport: 'ip', address: '192.168.1.20', os, role: 'guest' };
  const net = new NetLink({
    os: P.OS.LINUX, current: () => current, send: (m) => sent.push(m), fallback: () => fallback,
    host: { can: () => false, start: async () => {}, stop: async () => {} },
    join: { can: () => true, start: async () => {}, stop: async () => {} },
  });
  t.after(() => net.ended());
  const move = (o) => { current = { ...current, ...o }; };
  return { net, sent, move, needs: () => sent.filter((m) => m.t === P.T.NET_NEED).map((m) => m.why) };
}

// The session was on the network, the Wi-Fi dropped, it came back through Bluetooth.
function dropToBle(f) {
  f.net.started(); // on the network: remembered
  f.net.ended();
  f.move({ transport: 'ble', address: '' });
  f.net.started();
  f.net.onMessage({ t: P.T.NET_CAPS, host: true, join: true });
}

test('off: losing the network asks for nothing', (t) => {
  const f = fixture(t, { fallback: false });
  dropToBle(f);
  assert.deepEqual(f.needs(), []);
});

test('on: the phone is asked for a link once it said what it can do', (t) => {
  const f = fixture(t);
  dropToBle(f);
  assert.deepEqual(f.needs(), ['fallback']);
  f.net.transport(); // another transport event changes nothing
  assert.deepEqual(f.needs(), ['fallback']);
});

test('never on a network together: nothing', (t) => {
  const f = fixture(t);
  f.move({ transport: 'ble', address: '' });
  f.net.started();
  f.net.onMessage({ t: P.T.NET_CAPS, host: true, join: true });
  assert.deepEqual(f.needs(), []);
});

test('an iPhone: nothing', (t) => {
  const f = fixture(t, { os: P.OS.IOS });
  dropToBle(f);
  assert.deepEqual(f.needs(), []);
});

test('net.lan lets the link go and holds off a new ask', (t) => {
  const f = fixture(t);
  dropToBle(f);
  f.net.onMessage({ t: P.T.NET_LAN });
  assert.ok(f.sent.some((m) => m.t === P.T.NET_RELEASE && m.why === 'fallback'));
  assert.equal(f.net.needs.has('fallback'), false);
  f.net.transport(); // still on Bluetooth right after: moving back onto the network, no new ask
  assert.deepEqual(f.needs(), ['fallback']);
});

test('net.lan without a fallback link: ignored', (t) => {
  const f = fixture(t);
  f.net.started();
  f.net.onMessage({ t: P.T.NET_LAN });
  assert.equal(f.sent.filter((m) => m.t === P.T.NET_RELEASE).length, 0);
});

test('ownLan: the links, link-local, Tailscale and virtual adapters are not a network', () => {
  const v4 = (address) => [{ family: 'IPv4', address, internal: false }];
  assert.equal(NetLink.ownLan({ lo: [{ family: 'IPv4', address: '127.0.0.1', internal: true }] }), false);
  assert.equal(NetLink.ownLan({ 'Wi-Fi': v4('169.254.3.4'), 'Local Area Connection* 2': v4('192.168.137.1'), tailscale0: v4('100.101.1.2'), docker0: v4('172.17.0.1') }), false);
  assert.equal(NetLink.ownLan({ wlan0: v4('192.168.49.12') }), false);
  assert.equal(NetLink.ownLan({ 'Wi-Fi': v4('192.168.1.40') }), true);
});