-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1591 lines (1510 loc) · 74.7 KB
/
Copy pathserver.js
File metadata and controls
1591 lines (1510 loc) · 74.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// phone-terminal-web: a small custom web terminal for shared tmux sessions.
//
// Replaces ttyd while keeping the existing tmux session model intact:
// - GET /api/sessions -> JSON list of live tmux sessions + metadata
// - WS /ws/session/<name> -> attach an existing session under a PTY
// - WS /ws/new[?name=<base>] -> create + attach a new session
//
// Wire protocol on each WebSocket (symmetric, frame-type discriminated):
// - text frame = JSON control message ({type:"session"|"resize"|"error"|"exit", ...})
// - binary frame = raw terminal bytes (stdin client->server, stdout server->client)
//
// Detach == kill the PTY's `tmux attach`/`tmux new` client process. tmux keeps
// the session alive as long as another client (e.g. local Ghostty) is attached;
// `destroy-unattached on` only reaps it once the last client leaves.
import http from 'node:http';
import https from 'node:https';
import os from 'node:os';
import path from 'node:path';
import { readFile, writeFile, mkdir, readdir, statfs } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { execFile, spawn } from 'node:child_process';
import { promisify } from 'node:util';
import { WebSocketServer, WebSocket as WsClient } from 'ws';
import pty from 'node-pty';
const execFileP = promisify(execFile);
// Optional: target a specific tmux server socket (tmux -L <name>). Defaults to
// the user's normal server. `-L` ignores the $TMUX env var, so it also gives
// real isolation when running a second instance.
const TMUX_SOCKET = process.env.TMUX_SOCKET || '';
const tmuxArgv = (args) => (TMUX_SOCKET ? ['-L', TMUX_SOCKET, ...args] : args);
// Absolute path or name of the tmux binary. Defaults to PATH lookup, but a
// launchd/systemd service often has a minimal PATH that misses Homebrew, so the
// installer bakes the resolved path in here.
const TMUX_BIN = process.env.TMUX_BIN || 'tmux';
// Optional explicit terminal emulator for "open on PC" (must accept `-e <cmd…>`,
// e.g. ghostty/kitty/alacritty/xterm). If unset, a per-OS default is used.
const DESKTOP_TERMINAL = process.env.DESKTOP_TERMINAL || '';
// The published installer one-liner (see README). The "Update" button runs this
// inside a visible tmux session so the user can watch the box self-update and the
// service restart. Overridable for forks/testing.
const INSTALL_URL = process.env.WEBMUX_INSTALL_URL || 'https://raw.githubusercontent.com/samoylenkodmitry/webmux/main/install.sh';
const UPDATE_SESSION = 'webmux-update';
// Tailscale UI integration is opt-in via the installer; the env var also acts
// as a hard kill-switch when users don't want webmux invoking `tailscale` at all.
const TAILSCALE_ENABLED = process.env.WEBMUX_TAILSCALE === '1';
// Set by the Android WebMux Host (Userland.startWebmux). On a phone there's no
// systemd, so self-update can't `systemctl restart`; instead we pull and let the
// host's runWebmuxForever supervisor restart the process. Also drives battery
// back-off (slower polling when no client is attached).
const IS_ANDROID = process.env.WEBMUX_ANDROID === '1';
// Path to the tailscale CLI, used for /api/tailnet. Resolved lazily so a launchd
// service with a minimal PATH still finds it in common install locations.
// Memoize the lookup as a single shared promise so parallel callers (status +
// serve) don't race past one another and see a half-initialized state.
let _tailscalePromise = null;
function resolveTailscale() {
if (_tailscalePromise) return _tailscalePromise;
_tailscalePromise = (async () => {
const candidates = [
process.env.TAILSCALE_BIN,
'tailscale',
process.env.HOME ? `${process.env.HOME}/.local/bin/tailscale` : '',
'/opt/homebrew/bin/tailscale',
'/usr/local/bin/tailscale',
'/Applications/Tailscale.app/Contents/MacOS/Tailscale',
].filter(Boolean);
for (const c of candidates) {
try { await execFileP(c, ['version'], { encoding: 'utf8' }); return c; }
catch { /* try next */ }
}
return null;
})();
return _tailscalePromise;
}
async function runTailscale(args) {
const bin = await resolveTailscale();
if (!bin) throw new Error('tailscale binary not found');
return (await execFileP(bin, args, { encoding: 'utf8' })).stdout;
}
async function tailscaleStatus() {
try {
const status = JSON.parse(await runTailscale(['status', '--json', '--self=true']));
const self = status.Self || {};
const dns = (self.DNSName || '').replace(/\.$/, '');
if (dns) _selfDns = dns; // so our probes can self-identify to phone peers
const keyExpiry = self.KeyExpiry || '';
const expiryMs = keyExpiry ? Date.parse(keyExpiry) : NaN;
const keyExpired = Boolean(self.Expired) || (Number.isFinite(expiryMs) && expiryMs <= Date.now());
const backendState = status.BackendState || '';
return {
present: true,
backendState,
running: backendState === 'Running',
needsLogin: backendState === 'NeedsLogin' || keyExpired,
health: Array.isArray(status.Health) ? status.Health : [],
dns,
keyExpiry,
keyExpired,
magicDNSSuffix: status.MagicDNSSuffix || (status.CurrentTailnet && status.CurrentTailnet.MagicDNSSuffix) || '',
};
} catch (e) {
return { present: false, error: e.message || String(e) };
}
}
// A peer connection descriptor. PC peers sit behind `tailscale serve` (HTTPS on
// :443, reached by dialing the Tailscale IP with the MagicDNS name as SNI+Host so
// the serve cert validates even when this node's MagicDNS can't resolve .ts.net).
// Phones can't run `tailscale serve`, so they run webmux as plain HTTP on their
// Tailscale IP — fine, because WireGuard already encrypts the tailnet. `conn`
// captures whichever applies so every peer call works the same way.
function httpsConn(ip, dns) { return { tls: true, host: ip, port: 443, servername: dns, hostHeader: dns, urlBase: `https://${dns}/` }; }
function httpConn(ip, port) { return { tls: false, host: ip, port, hostHeader: `${ip}:${port}`, urlBase: `http://${ip}:${port}/` }; }
// Zero-config discovery for nodes that can't run `tailscale status` (a phone whose
// webmux lives in a proot box): every webmux prober tags its requests with its own
// tailnet name (X-Webmux-Self), so such a node learns its peers from whoever probes
// it — the fleet is already knocking on its door every discovery cycle. No login,
// no coordinator. `_selfDns` is our own name (set once we read tailscale status).
let _selfDns = '';
const seenPeers = new Map(); // tailnet IP -> { dns, stamp }
const isTailnetIp = (ip) => {
const m = /^(\d+)\.(\d+)\./.exec(ip || '');
return Boolean(m) && +m[1] === 100 && +m[2] >= 64 && +m[2] <= 127;
};
const SELF_TAILNET_IP = isTailnetIp(process.env.HOST || '') ? process.env.HOST : null;
function clientTailnetIp(req) {
let ip = (req.socket && req.socket.remoteAddress) || '';
if (ip.startsWith('::ffff:')) ip = ip.slice(7);
return isTailnetIp(ip) ? ip : null;
}
// Defense-in-depth, zero user friction: even though the listener is bound to the tun
// interface, also reject any connection whose SOURCE isn't the tailnet (CGNAT 100.64/10
// or Tailscale's IPv6 ULA fd7a:115c:a1e0::/48) or on-device loopback. So if a bind ever
// lands on a hostile network (Tailscale off + a carrier-grade-NAT WiFi, a future bug),
// webmux — which has no auth — still serves nobody. Enforced on phones, where access is
// strictly tailnet/loopback; a PC may be reached over its LAN, so opt out with
// WEBMUX_TRUST_LAN=1. This can't isolate other devices already on your tailnet (that
// needs a secret you'd have to hold) — it closes the off-tailnet-exposure hole only.
function remoteOnTailnet(req) {
let ip = (req.socket && req.socket.remoteAddress) || '';
if (ip.startsWith('::ffff:')) ip = ip.slice(7);
if (ip === '127.0.0.1' || ip === '::1') return true; // loopback (on-device, tailscale serve)
if (isTailnetIp(ip)) return true; // 100.64.0.0/10
if (ip.toLowerCase().startsWith('fd7a:115c:a1e0')) return true; // Tailscale IPv6 ULA
return false;
}
const ENFORCE_TAILNET = IS_ANDROID && process.env.WEBMUX_TRUST_LAN !== '1';
// One request to a peer over its conn. Resolves { status, body } or null on a
// connection error / timeout. Body is capped.
function peerHttp(conn, { method = 'GET', path = '/', body = null, timeoutMs = 4000, maxBytes = 1_000_000 } = {}) {
return new Promise((resolve) => {
let done = false;
const finish = (v) => { if (!done) { done = true; resolve(v); } };
const headers = { Host: conn.hostHeader, 'X-Webmux-Self': _selfDns };
let payload = null;
if (body != null) {
payload = Buffer.from(typeof body === 'string' ? body : JSON.stringify(body));
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = payload.length;
} else if (method === 'POST') {
headers['Content-Length'] = 0;
}
const opts = { host: conn.host, port: conn.port, path, method, headers, timeout: timeoutMs };
if (conn.tls && conn.servername) opts.servername = conn.servername;
const req = (conn.tls ? https : http).request(opts, (res) => {
let b = ''; res.setEncoding('utf8');
res.on('data', (c) => { b += c; if (b.length > maxBytes) req.destroy(); });
res.on('end', () => finish({ status: res.statusCode, body: b }));
});
req.on('error', () => finish(null));
req.on('timeout', () => { req.destroy(); finish(null); });
if (payload) req.write(payload);
req.end();
});
}
// Is there a webmux at this conn? Returns its /api/health JSON if so, else null.
async function probeWebmuxHealth(conn, timeoutMs) {
const r = await peerHttp(conn, { path: '/api/health', timeoutMs, maxBytes: 65536 });
if (!r || r.status !== 200) return null;
try { const j = JSON.parse(r.body); return j && typeof j === 'object' && 'tmuxBin' in j ? j : null; }
catch { return null; }
}
// GET <path> from a peer over its conn, parsed JSON or null. Used to pull a
// peer's session list / stats so the picker can show every machine.
async function fetchPeerJson(conn, reqPath, timeoutMs) {
const r = await peerHttp(conn, { path: reqPath, timeoutMs });
if (!r || r.status !== 200) return null;
try { return JSON.parse(r.body); } catch { return null; }
}
// POST https://<dns>/api/update on a peer to ask it to self-update. Best-effort:
// resolves true if the peer accepted. Same SNI + Host dialing as the GET helper.
async function postPeerUpdate(conn, timeoutMs) {
const r = await peerHttp(conn, { method: 'POST', path: '/api/update', timeoutMs });
return Boolean(r && r.status === 200);
}
// Bootstrap a peer that's too OLD to have /api/update: drive its `/ws/new` the
// same way the UI does — it opens a tmux session with a shell — and "type" the
// installer one-liner into it. The install is wrapped in `nohup … & disown` so it
// detaches from the throwaway session and survives both the session closing and
// the service restart it triggers. Resolves true once the command was dispatched.
function bootstrapPeerViaShell(conn, connectTimeoutMs) {
return new Promise((resolve) => {
let settled = false, sent = false;
const settle = (v) => { if (!settled) { settled = true; resolve(v); } };
let ws;
try {
const wsOpts = { headers: { Host: conn.hostHeader }, handshakeTimeout: connectTimeoutMs };
if (conn.tls && conn.servername) wsOpts.servername = conn.servername;
ws = new WsClient(`${conn.tls ? 'wss' : 'ws'}://${conn.host}:${conn.port}/ws/new?cols=80&rows=24`, wsOpts);
} catch { return resolve(false); }
const giveUp = setTimeout(() => { try { ws.close(); } catch { /* ignore */ } settle(false); }, connectTimeoutMs + 8000);
ws.on('message', (data, isBinary) => {
if (isBinary || sent) return;
let msg; try { msg = JSON.parse(data.toString()); } catch { return; }
if (msg.type === 'session') {
sent = true;
// Let the shell's rc settle, then send a detached self-update + exit.
setTimeout(() => {
// Env var must sit on `bash` (the installer), not `curl`. cd to a dir
// without a server.js so install.sh takes its git-update path.
const inner = `cd /tmp && curl -fsSL ${INSTALL_URL} | WEBMUX_NONINTERACTIVE=1 bash`;
const cmd = `nohup sh -c '${inner}' >/tmp/webmux-update.log 2>&1 </dev/null & disown 2>/dev/null; exit\n`;
try { ws.send(Buffer.from(cmd, 'utf8')); }
catch { clearTimeout(giveUp); return settle(false); }
// Command dispatched — that's success. The `exit` in it makes the peer
// close the socket almost immediately; resolve now so that close (which
// would otherwise settle false) is a no-op. nohup keeps the install alive.
clearTimeout(giveUp);
settle(true);
setTimeout(() => { try { ws.close(); } catch { /* ignore */ } }, 1500);
}, 1000);
} else if (msg.type === 'error') {
clearTimeout(giveUp); settle(false);
}
});
ws.on('error', () => { clearTimeout(giveUp); settle(false); });
ws.on('close', () => { clearTimeout(giveUp); settle(false); });
});
}
// Update one peer: prefer the native endpoint (clean, watchable session on newer
// builds); fall back to driving its shell over /ws/new for builds that predate it.
async function updatePeer(p) {
if (await postPeerUpdate(p.conn, 8000)) return { name: p.name, dns: p.dns, ip: p.ip, ok: true, via: 'api' };
const ok = await bootstrapPeerViaShell(p.conn, 6000);
return { name: p.name, dns: p.dns, ip: p.ip, ok, via: ok ? 'shell' : 'fail' };
}
// Cached peer probe — discovering peers means a round-trip to every online tailnet
// node, so we cache the result for a few seconds rather than hammering on every
// picker refresh. Ports a phone might run webmux on (plain HTTP); overridable.
const PEER_CACHE_MS = 10_000;
const PEER_HTTP_PORTS = (process.env.WEBMUX_PEER_PORTS || '8083').split(',').map((s) => parseInt(s.trim(), 10)).filter(Boolean);
let _peersCachePromise = null;
let _peersCacheStamp = 0;
// Peers that have probed us recently (the learned-discovery fallback). Pruned to a
// 2-minute window so a node that goes offline drops out of the picker.
function learnedNodes() {
const cutoff = Date.now() - 120_000;
const out = [];
for (const [ip, v] of seenPeers) {
if (v.stamp < cutoff) { seenPeers.delete(ip); continue; }
out.push({ name: v.dns || ip, dns: v.dns || '', ip });
}
return out;
}
// Confirm each candidate is a webmux and capture how to reach it. PCs answer over
// tailscale-serve HTTPS (dial the IP, present the name as SNI+Host); phones over
// plain HTTP on a port. A learned phone peer may have no name — HTTP-only then.
async function probeNodes(nodes) {
const probes = nodes.map(async (n) => {
if (n.dns) {
const hc = httpsConn(n.ip, n.dns);
const h = await probeWebmuxHealth(hc, 2500);
if (h) { const node = { name: n.name, dns: n.dns, ip: n.ip, url: hc.urlBase, conn: hc, power: h.power || null }; recordWakeEndpoint(node, h.wakeEndpoint); return node; }
}
for (const port of PEER_HTTP_PORTS) {
const pc = httpConn(n.ip, port);
const h = await probeWebmuxHealth(pc, 1500);
if (h) { const node = { name: n.name, dns: n.dns || '', ip: n.ip, url: pc.urlBase, conn: pc, power: h.power || null }; recordWakeEndpoint(node, h.wakeEndpoint); return node; }
}
return null;
});
return (await Promise.all(probes)).filter(Boolean);
}
async function tailscalePeers() {
const now = Date.now();
if (_peersCachePromise && now - _peersCacheStamp < PEER_CACHE_MS) return _peersCachePromise;
_peersCacheStamp = now;
_peersCachePromise = (async () => {
let nodes = null;
try {
const status = JSON.parse(await runTailscale(['status', '--json']));
if (status.BackendState && status.BackendState !== 'Running') return [];
nodes = Object.values(status.Peer || {})
.filter((p) => p.Online && !p.Expired && p.InNetworkMap !== false && p.DNSName && Array.isArray(p.TailscaleIPs) && p.TailscaleIPs.length)
.map((p) => {
const dns = p.DNSName.replace(/\.$/, '');
const ip = p.TailscaleIPs.find((a) => a.includes('.')) || p.TailscaleIPs[0];
return { name: p.HostName || dns, dns, ip };
});
} catch {
nodes = null; // no tailscale CLI here (a phone's proot box) — learn instead
}
if (!nodes) nodes = learnedNodes();
return probeNodes(nodes);
})();
return _peersCachePromise;
}
// Best-effort lookup of this node's own tailnet HTTPS URL from `tailscale serve
// status`. Returns { url, dns } when serve is configured for our port; null
// otherwise (tailscale missing, not logged in, no serve config, etc.).
async function tailscaleSelf(status = null) {
try {
const ts = status || await tailscaleStatus();
const serveOut = await runTailscale(['serve', 'status', '--json']);
const serve = JSON.parse(serveOut);
const dns = ts.dns || '';
if (!dns || !serve.Web) return null;
const localPort = String(PORT);
for (const [hostPort, cfg] of Object.entries(serve.Web)) {
for (const [routePath, h] of Object.entries(cfg.Handlers || {})) {
const proxy = h.Proxy || '';
const m = proxy.match(/:(\d+)(?:\/|$)/);
if (!m || m[1] !== localPort) continue;
const [host, port = '443'] = hostPort.split(':');
const proto = port === '80' ? 'http' : 'https';
const portSuffix = (port === '443' || port === '80') ? '' : `:${port}`;
return { dns, url: `${proto}://${host}${portSuffix}${routePath}` };
}
}
return null;
} catch {
return null;
}
}
// How to spawn a terminal window that runs `tmux new-session -A -s <name>`.
// DESKTOP_TERMINAL is a token the installer picks from the installed terminals.
function desktopLaunchSpec(name) {
const run = [TMUX_BIN, ...(TMUX_SOCKET ? ['-L', TMUX_SOCKET] : []), 'new-session', '-A', '-s', name];
const cmdline = run.join(' '); // name (NAME_RE) + tmux path have no spaces/quotes to escape
const t = (DESKTOP_TERMINAL || '').toLowerCase();
if (process.platform === 'darwin') {
// macOS GUI apps can't be driven by `-e` from the CLI; use open / AppleScript.
if (t === 'iterm' || t === 'iterm2')
return ['osascript', ['-e', `tell application "iTerm" to create window with default profile command "${cmdline}"`]];
if (t === 'ghostty')
return ['open', ['-na', 'Ghostty', '--args', '-e', ...run]];
if (t && t !== 'terminal' && t !== 'terminal.app')
return [DESKTOP_TERMINAL, ['-e', ...run]]; // some other CLI emulator
return ['osascript', ['-e', `tell application "Terminal" to do script "${cmdline}"`]]; // default
}
// Linux / other
if (!t) return ['ghostty', ['-e', ...run]];
if (t === 'gnome-terminal') return ['gnome-terminal', ['--', ...run]];
if (t === 'wezterm') return ['wezterm', ['start', '--', ...run]];
return [DESKTOP_TERMINAL, ['-e', ...run]]; // ghostty/kitty/alacritty/foot/konsole/xterm
}
// A background user service (systemd/launchd) starts without the graphical
// session's env — so it has no DISPLAY/WAYLAND_DISPLAY/DBUS address and any GUI
// terminal it spawns can't reach the display and dies silently. The desktop
// imports those vars into the systemd *user manager* on login, so read them back
// from there at spawn time (always current, never hardcoded) and pass them to
// the child. Linux-only: macOS launches GUI apps via open/osascript without these.
const GRAPHICAL_VARS = [
'DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'DBUS_SESSION_BUS_ADDRESS',
'XDG_RUNTIME_DIR', 'XDG_SESSION_TYPE', 'XDG_CURRENT_DESKTOP', 'XDG_SESSION_DESKTOP',
];
const GRAPHICAL_ENV_TTL = 5000;
let _graphicalEnvPromise = null;
let _graphicalEnvStamp = 0;
function graphicalEnv() {
if (process.platform !== 'linux') return Promise.resolve({});
const now = Date.now();
if (_graphicalEnvPromise && now - _graphicalEnvStamp < GRAPHICAL_ENV_TTL) return _graphicalEnvPromise;
_graphicalEnvStamp = now;
_graphicalEnvPromise = (async () => {
try {
// `systemctl --user` reaches the manager over $XDG_RUNTIME_DIR (no DISPLAY
// needed), so this works even though our own process lacks the GUI env.
const { stdout } = await execFileP('systemctl', ['--user', 'show-environment'], { encoding: 'utf8' });
const env = {};
for (const line of stdout.split('\n')) {
const eq = line.indexOf('=');
if (eq <= 0) continue;
const key = line.slice(0, eq);
if (!GRAPHICAL_VARS.includes(key)) continue;
let val = line.slice(eq + 1);
if (val.length >= 2 && val.startsWith('"') && val.endsWith('"')) {
val = val.slice(1, -1).replace(/\\(["\\])/g, '$1'); // systemd quotes only when needed
}
if (val) env[key] = val;
}
return env;
} catch {
return {}; // no user manager / systemctl: fall back to process.env
}
})();
return _graphicalEnvPromise;
}
// Open a real terminal window on the PC attached to the tmux session, so it is
// visible locally and (being a second client) outlives the browser. Best-effort:
// if there's no display or the emulator is missing, the web session still works.
async function launchDesktop(name) {
const [cmd, args] = desktopLaunchSpec(name);
const env = { ...process.env, ...(await graphicalEnv()) };
try {
const child = spawn(cmd, args, { detached: true, stdio: 'ignore', env });
child.on('error', () => {}); // emulator missing / no display: ignore
child.unref();
} catch { /* ignore */ }
}
// Run the published installer one-liner inside a fresh `webmux-update` tmux
// session so the user can attach and watch the box self-update + restart. The
// session lives on webmux's own tmux server (shows up in the picker), runs
// non-interactively so it never blocks on a prompt, and drops to a shell after so
// the result stays visible. The tmux process is independent of this Node server,
// so the install's `systemctl restart` doesn't kill the update mid-run.
async function startUpdateSession() {
try { await tmux(['kill-session', '-t', `=${UPDATE_SESSION}`]); } catch { /* none running yet */ }
// cd /tmp so install.sh doesn't see this dir's server.js and skip the git pull;
// WEBMUX_NONINTERACTIVE must sit on `bash`, not `curl`, to take effect.
const script =
"clear 2>/dev/null; printf '\\033[1m== webmux update ==\\033[0m\\n'; cd /tmp; " +
'curl -fsSL ' + INSTALL_URL + ' | WEBMUX_NONINTERACTIVE=1 bash; ' +
'code=$?; printf \'\\n== update finished (exit %s) -- this shell stays open ==\\n\' "$code"; ' +
'exec "${SHELL:-/bin/sh}"';
await tmux(['new-session', '-d', '-s', UPDATE_SESSION, script]);
// Survive having no client attached (e.g. while the service restarts).
try { await tmux(['set-option', '-t', UPDATE_SESSION, 'destroy-unattached', 'off']); } catch { /* best effort */ }
}
// Self-update on Android: there's no systemd, so the installer's `systemctl restart`
// is a no-op and the box would keep serving the old code forever ("stuck updating").
// Instead, pull origin/main in an independent tmux session (the box's tmux server
// outlives this Node process), then `kill` ourselves — the WebMux Host's
// runWebmuxForever supervisor immediately re-execs `node server.js` on the fresh
// checkout. Same PID namespace under proot, so the kill reaches us.
async function startAndroidSelfUpdate() {
try { await tmux(['kill-session', '-t', `=${UPDATE_SESSION}`]); } catch { /* none running yet */ }
const script =
"clear 2>/dev/null; printf '\\033[1m== webmux update (android) ==\\033[0m\\n'; " +
`cd ${__dirname} && ` +
'git fetch --quiet origin main && git reset --hard origin/main && ' +
'(npm install --no-audit --no-fund || true); ' +
"printf '\\n== pulled — restarting webmux ==\\n'; " +
`kill ${process.pid}; ` +
'exec "${SHELL:-/bin/sh}"';
await tmux(['new-session', '-d', '-s', UPDATE_SESSION, script]);
try { await tmux(['set-option', '-t', UPDATE_SESSION, 'destroy-unattached', 'off']); } catch { /* best effort */ }
}
// Short git hash of the running checkout — the fleet-update progress UI uses it to
// tell when a machine has actually restarted onto the new code. Cached for the
// life of the process (the install restarts us, so a fresh process re-reads it).
let _versionPromise = null;
function webmuxVersion() {
if (_versionPromise) return _versionPromise;
_versionPromise = (async () => {
try {
const { stdout } = await execFileP('git', ['-C', __dirname, 'rev-parse', '--short', 'HEAD'], { encoding: 'utf8' });
return stdout.trim();
} catch { return ''; }
})();
return _versionPromise;
}
// The version the fleet will CONVERGE TO on update: the installer does
// `git reset --hard origin/main`, so the target is the *remote* main HEAD — not this
// coordinator's local version (which may be behind). Polling the coordinator's own
// version as the target leaves every peer stuck "updating…" forever when the
// coordinator isn't on HEAD. Falls back to local if the fetch fails.
async function remoteTargetVersion() {
try {
await execFileP('git', ['-C', __dirname, 'fetch', '--quiet', 'origin', 'main'], { encoding: 'utf8', timeout: 15000 });
const { stdout } = await execFileP('git', ['-C', __dirname, 'rev-parse', '--short', 'origin/main'], { encoding: 'utf8' });
return stdout.trim();
} catch {
return webmuxVersion();
}
}
// --- machine stats (CPU / GPU / disk / RAM) for the picker's machine row ----
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
function cpuTimes() {
let idle = 0, total = 0;
for (const c of os.cpus()) { for (const k in c.times) total += c.times[k]; idle += c.times.idle; }
return { idle, total };
}
async function cpuPercent() {
const a = cpuTimes();
await sleep(120);
const b = cpuTimes();
const dt = b.total - a.total, di = b.idle - a.idle;
return dt > 0 ? Math.max(0, Math.min(100, Math.round((1 - di / dt) * 100))) : null;
}
// Best-effort GPU utilisation %: NVIDIA via nvidia-smi, else AMD/Intel via the
// DRM sysfs `gpu_busy_percent`. Returns null when nothing reports it. We stop
// trying nvidia-smi once it's clearly absent, so we don't spawn it every poll.
let _noNvidia = false;
async function gpuPercent() {
if (!_noNvidia) {
try {
const { stdout } = await execFileP('nvidia-smi', ['--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'], { encoding: 'utf8', timeout: 1500 });
const v = parseInt(stdout.trim().split('\n')[0], 10);
if (Number.isFinite(v)) return v;
} catch (e) { if (e && e.code === 'ENOENT') _noNvidia = true; /* nvidia-smi not installed */ }
}
try {
for (const d of await readdir('/sys/class/drm')) {
if (!/^card\d+$/.test(d)) continue;
try {
const v = parseInt(await readFile(`/sys/class/drm/${d}/device/gpu_busy_percent`, 'utf8'), 10);
if (Number.isFinite(v)) return v;
} catch { /* try next card */ }
}
} catch { /* no drm sysfs */ }
return null;
}
async function diskFree() {
try {
const s = await statfs('/');
const total = s.blocks * s.bsize;
const free = s.bavail * s.bsize;
return { free, total, usedPct: total ? Math.round((1 - free / total) * 100) : null };
} catch { return null; }
}
function memPercent() {
const total = os.totalmem();
return total ? Math.round((1 - os.freemem() / total) * 100) : null;
}
async function cpuTemp() {
if (process.platform !== 'linux') return null;
try {
let cpu = null, fallback = null;
for (const d of await readdir('/sys/class/thermal')) {
if (!/^thermal_zone\d+$/.test(d)) continue;
let type = '', v = NaN;
try { type = (await readFile(`/sys/class/thermal/${d}/type`, 'utf8')).trim().toLowerCase(); } catch { /* no type */ }
try { v = parseInt(await readFile(`/sys/class/thermal/${d}/temp`, 'utf8'), 10); } catch { /* no temp */ }
if (!Number.isFinite(v) || v <= 0 || v >= 200000) continue;
const c = Math.round(v / 1000);
// Prefer a CPU/package zone; otherwise fall back to the hottest sensor.
if (/cpu|coretemp|x86_pkg|package|k10temp|zenpower|tctl|tdie/.test(type)) cpu = cpu == null ? c : Math.max(cpu, c);
fallback = Math.max(fallback ?? 0, c);
}
return cpu != null ? cpu : fallback;
} catch { return null; }
}
// Top processes by CPU (name + %). Linux/macOS ps differ on the all-processes flag.
async function topProcs() {
try {
const args = process.platform === 'darwin' ? ['-A', '-o', 'pcpu=,comm=', '-r'] : ['-eo', 'pcpu=,comm=', '--sort=-pcpu'];
const { stdout } = await execFileP('ps', args, { encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 });
const out = [];
for (const line of stdout.split('\n')) {
const m = line.trim().match(/^([\d.]+)\s+(.+)$/);
if (!m) continue;
out.push({ cpu: Math.round(parseFloat(m[1]) * 10) / 10, cmd: m[2].split('/').pop() });
if (out.length >= 5) break;
}
return out;
} catch { return []; }
}
// Cached so rapid polls (and several browser tabs) don't each pay the CPU sample.
// NOTE: deliberately excludes top processes — `ps -e` over all of /proc is too
// heavy for the every-5s poll on slow boxes (it pinned an Orange Pi). Top procs
// are fetched on demand via /api/procs only when the detail sheet is opened.
let _statsCache = null, _statsStamp = 0;
async function machineStats() {
const now = Date.now();
if (_statsCache && now - _statsStamp < 2000) return _statsCache;
const [cpu, gpu, disk, temp] = await Promise.all([cpuPercent(), gpuPercent(), diskFree(), cpuTemp()]);
_statsStamp = Date.now();
_statsCache = {
host: os.hostname(), cpu, gpu, mem: memPercent(), disk, temp,
uptime: Math.round(os.uptime()),
load: os.loadavg().map((x) => Math.round(x * 100) / 100),
};
return _statsCache;
}
// Read a JSON request body (small, capped). Used by POST endpoints with payloads.
function readBody(req, max = 65536) {
return new Promise((resolve) => {
let b = '';
req.setEncoding('utf8');
req.on('data', (c) => { b += c; if (b.length > max) req.destroy(); });
req.on('end', () => { try { resolve(JSON.parse(b || '{}')); } catch { resolve({}); } });
req.on('error', () => resolve({}));
});
}
// Run a shell command (broadcast), capturing combined output (bounded + timed).
async function runCommand(cmd) {
const host = os.hostname();
if (!cmd || typeof cmd !== 'string') return { host, ok: false, output: 'no command' };
try {
const { stdout, stderr } = await execFileP('sh', ['-c', cmd], { encoding: 'utf8', timeout: 20000, maxBuffer: 256 * 1024, cwd: HOME || process.cwd(), env: process.env });
return { host, ok: true, code: 0, output: (stdout + (stderr ? (stdout ? '\n' : '') + stderr : '')).slice(0, 8000) };
} catch (e) {
const out = (String(e.stdout || '') + String(e.stderr || '')).slice(0, 8000) || (e.killed ? 'timed out' : (e.message || 'failed'));
return { host, ok: false, code: e.code ?? 1, output: out };
}
}
// POST JSON to a peer (used for broadcast fan-out). Returns parsed JSON or null.
async function postPeerJson(conn, reqPath, bodyObj, timeoutMs) {
const r = await peerHttp(conn, { method: 'POST', path: reqPath, body: bodyObj || {}, timeoutMs });
if (!r || r.status !== 200) return null;
try { return JSON.parse(r.body); } catch { return null; }
}
const HOST = process.env.HOST || '127.0.0.1';
const PORT = parseInt(process.env.PORT || '8083', 10);
const TERM_NAME = process.env.TERM_NAME || 'xterm-256color';
const HOME = process.env.HOME || '';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PUBLIC = path.join(__dirname, 'public');
// Validation for session names. Real tmux names can contain spaces and unicode
// (and tmux maps ':' / '.' to '_' itself), so the old strict regex wrongly
// rejected sessions that the picker had listed ("session not found"). Names are
// always passed to tmux as an argv element (never a shell), so the only genuine
// hazard is control characters; existence is checked separately via has-session.
const NAME_RE = /^[^\x00-\x1f\x7f]{1,128}$/;
// --- tmux helpers ----------------------------------------------------------
async function tmux(args) {
const { stdout } = await execFileP(TMUX_BIN, tmuxArgv(args), { encoding: 'utf8' });
return stdout;
}
async function ensureTmuxMouse() {
try {
await tmux(['set-option', '-g', 'mouse', 'on']);
} catch {
// Attach/create paths below will report real tmux failures to the client.
}
}
function prettyPath(p) {
if (!p) return '';
if (p === HOME) return '~';
if (HOME && p.startsWith(HOME + '/')) return '~' + p.slice(HOME.length);
return p;
}
// tmux 3.6 replaces non-printable bytes (TAB, \x1f, etc.) with '_' in -F output,
// so we use a printable multi-char delimiter that won't appear in real paths/commands.
const FS = '<<>>';
// tmux's `pane_current_command` is just the comm of the pane's foreground process
// — so `sudo btop` shows as "sudo". Resolve the pane's controlling-terminal
// foreground process group (via /proc tpgid) and read its full argv for a much
// more informative label. Linux-only; elsewhere we keep the comm.
function cleanCommand(argv) {
if (!argv || !argv.length) return '';
let prog = String(argv[0]).replace(/^-/, ''); // login shells appear as "-zsh"
prog = prog.split('/').pop(); // basename of the program
const s = [prog, ...argv.slice(1)].join(' ').trim();
return s.length > 160 ? s.slice(0, 159) + '…' : s;
}
async function foregroundCommand(panePid, fallback) {
const pid = Number(panePid);
if (process.platform !== 'linux' || !Number.isInteger(pid) || pid <= 0) return fallback;
try {
// /proc/<pid>/stat: after the "(comm)" field come state ppid pgrp session
// tty_nr tpgid … — index 5 past the last ')' is tpgid (foreground pgid).
const stat = await readFile(`/proc/${pid}/stat`, 'utf8');
const after = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
const tpgid = parseInt(after[5], 10);
const target = Number.isInteger(tpgid) && tpgid > 0 ? tpgid : pid;
const argv = (await readFile(`/proc/${target}/cmdline`, 'utf8')).split('\0').filter(Boolean);
return argv.length ? cleanCommand(argv) : fallback;
} catch {
return fallback;
}
}
async function listSessions() {
let out;
try {
// Pane vars resolve against each session's active pane, so one call is enough.
out = await tmux([
'list-sessions', '-F',
`#{session_name}${FS}#{session_attached}${FS}#{session_windows}${FS}#{pane_current_command}${FS}#{pane_current_path}${FS}#{pane_pid}${FS}#{session_activity}`,
]);
} catch {
return []; // no server running -> no sessions
}
const rows = out.split('\n').filter(Boolean).map((line) => {
const [name, attached, windows, command, cwd, panePid, activity] = line.split(FS);
return {
name,
attached: Number(attached) || 0,
windows: Number(windows) || 0,
command: command || '',
path: prettyPath(cwd || ''),
activity: Number(activity) || 0, // unix seconds of last activity (for "new" badges)
panePid,
};
});
// Upgrade each comm to the full foreground command line (best-effort, parallel).
await Promise.all(rows.map(async (r) => { r.command = await foregroundCommand(r.panePid, r.command); delete r.panePid; }));
return rows;
}
async function uniqueName(base) {
const want = NAME_RE.test(base || '') ? base : 'web';
const taken = new Set((await listSessions()).map((s) => s.name));
if (!taken.has(want)) return want;
for (let i = 2; ; i++) {
const candidate = `${want}-${i}`;
if (!taken.has(candidate)) return candidate;
}
}
async function listWindows(name) {
const out = await tmux([
'list-windows', '-t', `=${name}`, '-F',
`#{window_index}${FS}#{window_name}${FS}#{window_active}${FS}#{pane_current_command}`,
]);
return out.split('\n').filter(Boolean).map((line) => {
const [index, wname, active, command] = line.split(FS);
return {
index: Number(index),
name: wname || '',
active: active === '1',
command: command || '',
};
});
}
// One session's live command + directory (for the dynamic browser title). Also
// returns session_activity so the client can mark this session "seen".
async function sessionInfo(name) {
const out = await tmux(['display-message', '-p', '-t', `=${name}:`, `#{pane_current_command}${FS}#{pane_current_path}${FS}#{session_activity}`]);
const [command, dir, activity] = out.trim().split(FS);
return { name, command: command || '', dir: dir || '', path: prettyPath(dir || ''), activity: Number(activity) || 0 };
}
// --- recent directories (history of closed sessions) -----------------------
// We sample live sessions; when one disappears, its last directory + command is
// pushed to a small persisted list so it can be reopened from the picker.
const STATE_DIR = process.env.WEBMUX_STATE || path.join(os.homedir(), '.local', 'state', 'webmux');
const HISTORY_FILE = path.join(STATE_DIR, 'recents.json');
const RECENTS_MAX = 30;
let recents = [];
let seen = new Map(); // session name -> { dir, command }
async function loadRecents() {
try {
const data = JSON.parse(await readFile(HISTORY_FILE, 'utf8'));
if (Array.isArray(data)) recents = data.filter((r) => r && r.dir).slice(0, RECENTS_MAX);
} catch { /* no history yet */ }
}
let saveTimer = null;
function saveRecents() {
clearTimeout(saveTimer);
saveTimer = setTimeout(async () => {
try {
await mkdir(STATE_DIR, { recursive: true });
await writeFile(HISTORY_FILE, JSON.stringify(recents, null, 2));
} catch { /* best effort */ }
}, 500);
}
function recordRecent(dir, command) {
if (!dir) return;
recents = recents.filter((r) => r.dir !== dir);
recents.unshift({ dir, command: command || '', ts: Date.now() });
if (recents.length > RECENTS_MAX) recents.length = RECENTS_MAX;
saveRecents();
}
// --- quick-reply snippets (shared across the fleet) ------------------------
// Stored server-side and persisted, so editing them on one webmux can be pushed
// to every tailnet peer (each box's clients read from their own server).
const SNIPPETS_FILE = path.join(STATE_DIR, 'snippets.json');
const DEFAULT_SNIPPETS = ['y', 'n', 'continue', 'approve', '/clear', 'exit'];
let snippets = DEFAULT_SNIPPETS.slice();
async function loadSnippetsFile() {
try {
const a = JSON.parse(await readFile(SNIPPETS_FILE, 'utf8'));
if (Array.isArray(a)) snippets = a.filter((s) => typeof s === 'string');
} catch { /* keep defaults */ }
}
function saveSnippetsFile() {
mkdir(STATE_DIR, { recursive: true }).then(() => writeFile(SNIPPETS_FILE, JSON.stringify(snippets))).catch(() => {});
}
// --- Web Push (activity alerts) --------------------------------------------
// Notify the phone when a session that was producing output goes quiet while no
// client is attached — i.e. "your agent/build finished and is waiting." Best
// effort: if web-push isn't installed, the endpoints just report disabled.
const VAPID_FILE = path.join(STATE_DIR, 'vapid.json');
const SUBS_FILE = path.join(STATE_DIR, 'push-subs.json');
let webpush = null;
let vapid = null;
let pushSubs = [];
async function initPush() {
try { webpush = (await import('web-push')).default; }
catch { return; } // dependency missing → push disabled, server still runs
try { vapid = JSON.parse(await readFile(VAPID_FILE, 'utf8')); } catch { vapid = null; }
if (!vapid || !vapid.publicKey || !vapid.privateKey) {
vapid = webpush.generateVAPIDKeys();
try { await mkdir(STATE_DIR, { recursive: true }); await writeFile(VAPID_FILE, JSON.stringify(vapid)); } catch { /* best effort */ }
}
webpush.setVapidDetails('mailto:webmux@localhost', vapid.publicKey, vapid.privateKey);
try { const d = JSON.parse(await readFile(SUBS_FILE, 'utf8')); if (Array.isArray(d)) pushSubs = d; } catch { /* none yet */ }
}
function savePushSubs() {
mkdir(STATE_DIR, { recursive: true }).then(() => writeFile(SUBS_FILE, JSON.stringify(pushSubs))).catch(() => {});
}
async function sendPush(payload) {
if (!webpush || !pushSubs.length) return;
const body = JSON.stringify(payload);
const dead = [];
await Promise.all(pushSubs.map(async (s) => {
try { await webpush.sendNotification(s, body); }
catch (e) { if (e && (e.statusCode === 404 || e.statusCode === 410)) dead.push(s.endpoint); }
}));
if (dead.length) { pushSubs = pushSubs.filter((s) => !dead.includes(s.endpoint)); savePushSubs(); }
}
// --- wake-on-demand registry ----------------------------------------------
// A phone in battery-saver sleeps when idle; to reach it we POST to its UnifiedPush
// "wake endpoint" (served by a distributor like ntfy), which wakes the app. We learn
// each phone's endpoint from its /api/health while it's awake and persist it, so any
// box can wake a phone that's currently offline (and keep it listed in the picker).
const WAKE_REGISTRY_FILE = path.join(STATE_DIR, 'wake-registry.json');
const wakeRegistry = new Map(); // dns||ip -> { name, dns, ip, endpoint, stamp }
let _wakeEndpoint = ''; // this box's own endpoint (Android only)
let _powerState = null; // last power snapshot from the host control API (Android only)
let _powerStateAt = 0;
const wakeKey = (n) => String(n.dns || n.ip || n.name || '').toLowerCase();
async function loadWakeRegistry() {
try {
const a = JSON.parse(await readFile(WAKE_REGISTRY_FILE, 'utf8'));
if (Array.isArray(a)) for (const e of a) if (e && e.endpoint) wakeRegistry.set(wakeKey(e), e);
} catch { /* none yet */ }
}
function saveWakeRegistry() {
mkdir(STATE_DIR, { recursive: true })
.then(() => writeFile(WAKE_REGISTRY_FILE, JSON.stringify([...wakeRegistry.values()], null, 2)))
.catch(() => {});
}
// Record/refresh a peer's wake endpoint learned from its health probe.
function recordWakeEndpoint(node, endpoint) {
if (!endpoint) return;
const key = wakeKey(node);
if (!key) return;
const prev = wakeRegistry.get(key);
wakeRegistry.set(key, {
name: node.name || (prev && prev.name) || key, dns: node.dns || '', ip: node.ip || '',
endpoint, stamp: Date.now(),
});
if (!prev || prev.endpoint !== endpoint) saveWakeRegistry();
}
// On Android, read our own endpoint from the WebMux Host control API (loopback :8084).
function refreshWakeEndpoint() {
if (!IS_ANDROID) return;
const r = http.request({ host: '127.0.0.1', port: 8084, path: '/wake-endpoint', method: 'GET', timeout: 2000 }, (res) => {
let b = ''; res.on('data', (c) => (b += c));
res.on('end', () => { try { _wakeEndpoint = JSON.parse(b).endpoint || _wakeEndpoint; } catch { /* ignore */ } });
});
r.on('error', () => {});
r.end();
}
// On Android, read the host's live power/battery snapshot (loopback :8084). Cached and
// refreshed lazily from /api/health so the fleet UI can show each phone's battery + sleep
// state without the health call ever blocking on the loopback hop.
function refreshPowerState() {
if (!IS_ANDROID) return;
const r = http.request({ host: '127.0.0.1', port: 8084, path: '/power', method: 'GET', timeout: 2000 }, (res) => {
let b = ''; res.on('data', (c) => (b += c));
res.on('end', () => {
try { const j = JSON.parse(b); if (j && typeof j.battery !== 'undefined') { _powerState = j; _powerStateAt = Date.now(); } }
catch { /* ignore */ }
});
});
r.on('error', () => {});
r.end();
}
// On Android, ask the WebMux Host (loopback :8084) to release the wake-lock and sleep
// now (it respects on-demand: tears the box fully down if on-demand is enabled). The
// terminals are already closed by the caller. Best-effort; resolves bool.
function sleepHost() {
return new Promise((resolve) => {
if (!IS_ANDROID) return resolve(false);
const r = http.request({ host: '127.0.0.1', port: 8084, path: '/sleep', method: 'POST', timeout: 2000 }, (res) => {
res.resume(); resolve(res.statusCode >= 200 && res.statusCode < 300);
});
r.on('error', () => resolve(false));
r.on('timeout', () => { r.destroy(); resolve(false); });
r.end();
});
}
// POST to a UnifiedPush endpoint URL to wake the phone. Best-effort; resolves bool.
function sendWake(endpoint) {
return new Promise((resolve) => {
let u; try { u = new URL(endpoint); } catch { return resolve(false); }
const mod = u.protocol === 'https:' ? https : http;
const r = mod.request({
host: u.hostname, port: u.port || (u.protocol === 'https:' ? 443 : 80),
path: u.pathname + u.search, method: 'POST', timeout: 6000,
}, (res) => { res.resume(); resolve(res.statusCode >= 200 && res.statusCode < 300); });
r.on('error', () => resolve(false));
r.on('timeout', () => { r.destroy(); resolve(false); });
r.end('webmux wake');
});
}
// Per-session activity state machine: notify once when a session goes idle for
// IDLE_NOTIFY_MS after having produced output, while unattached.
const IDLE_NOTIFY_MS = 12000;
const activityState = new Map(); // name -> { activity, changedAt, notified }
function checkActivity(name, activity, attached, nowMs) {
let st = activityState.get(name);
if (!st) { activityState.set(name, { activity, changedAt: nowMs, notified: true }); return; } // first sight: don't alert
if (attached > 0) st.notified = true; // you're watching it; consider it acknowledged
if (activity > st.activity) { // new output → it's busy again
st.activity = activity; st.changedAt = nowMs;
if (attached === 0) st.notified = false;
return;
}
if (!st.notified && attached === 0 && nowMs - st.changedAt >= IDLE_NOTIFY_MS) {
st.notified = true;
sendPush({ title: 'webmux', body: `“${name}” is idle — your turn`, tag: `idle-${name}`, name });
}
}
async function sampleSessions() {
let out = '';
try {
out = await tmux(['list-sessions', '-F', `#{session_name}${FS}#{pane_current_path}${FS}#{pane_current_command}${FS}#{session_activity}${FS}#{session_attached}`]);
} catch { /* no server -> every tracked session has closed */ }
const current = new Map();
const nowMs = Date.now();
const live = new Set();
for (const line of out.split('\n').filter(Boolean)) {
const [name, dir, command, activity, attached] = line.split(FS);
current.set(name, { dir, command });
live.add(name);
checkActivity(name, Number(activity) || 0, Number(attached) || 0, nowMs);
}
for (const [name, info] of seen) {
if (!current.has(name)) recordRecent(info.dir, info.command);
}
for (const name of activityState.keys()) { if (!live.has(name)) activityState.delete(name); } // prune closed
seen = current;
}
// --- tiny HTTP layer -------------------------------------------------------
const TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.webmanifest': 'application/manifest+json; charset=utf-8',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.map': 'application/json; charset=utf-8',
};
function sendJson(res, code, obj) {
const body = JSON.stringify(obj);
res.writeHead(code, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(body);
}