jsgotty 0.9.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +229 -0
- package/README.md +87 -0
- package/client.mjs +694 -0
- package/gotty.js +742 -57
- package/package.json +1 -1
- package/rz.js +24 -6
- package/single-exe/README.md +80 -0
- package/single-exe/assetsHelper.js +90 -0
- package/single-exe/assetsLoader.mjs +85 -0
- package/single-exe/compiled.js +138 -0
- package/single-exe/entry.mjs +9 -0
- package/single-exe/packAssets.sh +7 -0
- package/static/css/kitty.css +1 -1
- package/static/help.md +125 -0
- package/static/index.html +8 -1
- package/static/js/gotty.js +633 -89
- package/static/js/gotty.licenses.txt +23 -0
- package/static/js/kitty.js +83 -16
- package/static/js/xterm-addon-unicode11.js +2 -0
- package/static/js/zmodem.js +1 -1
- package/sz.js +77 -10
- package/viu.mjs +7 -8
- package/zmodem-node.js +157 -8
package/gotty.js
CHANGED
|
@@ -28,6 +28,11 @@ const MSG_SET_RECONNECT = "5";
|
|
|
28
28
|
const MSG_SET_BUFFER_SIZE = "6";
|
|
29
29
|
const MSG_KITTY_GRAPHICS = "7";
|
|
30
30
|
const MSG_WINDOWS_BRIDGE = "9";
|
|
31
|
+
const MSG_SET_ROLE = "a";
|
|
32
|
+
const MSG_CONTROL_RESULT = "b";
|
|
33
|
+
const MSG_ZMODEM_MODE = "z";
|
|
34
|
+
const MSG_ZMODEM_OUTPUT = "y";
|
|
35
|
+
const MSG_ZMODEM_ACK = "Y";
|
|
31
36
|
const KITTY_TRACE = process.env.KITTY_TRACE === "1";
|
|
32
37
|
const WINDOWS_BRIDGE_TRACE = process.env.WINDOWS_BRIDGE_TRACE === "1";
|
|
33
38
|
const WINDOWS_BRIDGE_PREFIX = Buffer.from("@@GOTTYCTL:", "ascii");
|
|
@@ -57,26 +62,60 @@ const DEFAULTS = {
|
|
|
57
62
|
height: 0,
|
|
58
63
|
wsOrigin: "",
|
|
59
64
|
wsQueryArgs: "",
|
|
65
|
+
webgl: false,
|
|
60
66
|
quiet: false,
|
|
61
67
|
closeSignal: "SIGINT",
|
|
62
68
|
closeTimeout: -1
|
|
63
69
|
};
|
|
64
70
|
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
71
|
+
const compiledHelperPromise = import("./single-exe/compiled.js").catch(() => null);
|
|
72
|
+
const assetsHelperPromise = import("./single-exe/assetsHelper.js").catch(() => null);
|
|
73
|
+
|
|
74
|
+
let RESOURCE_ROOT = __dirname;
|
|
75
|
+
let STATIC_ROOT = path.resolve(RESOURCE_ROOT, "./static");
|
|
76
|
+
let DEFAULT_INDEX = path.join(STATIC_ROOT, "index.html");
|
|
77
|
+
let DEFAULT_MANIFEST = path.join(STATIC_ROOT, "manifest.json");
|
|
78
|
+
let DEFAULT_HELP = path.join(STATIC_ROOT, "help.md");
|
|
79
|
+
let DEFAULT_README = path.resolve(RESOURCE_ROOT, "./README.md");
|
|
80
|
+
let DEFAULT_README_EN = path.resolve(RESOURCE_ROOT, "./README.en.md");
|
|
81
|
+
let helperAssetPath = null;
|
|
82
|
+
let helperReadInternalAssetText = null;
|
|
83
|
+
let helperReadInternalAssetBytes = null;
|
|
84
|
+
|
|
85
|
+
function configureResourceRoot(compiledHelper) {
|
|
86
|
+
RESOURCE_ROOT = compiledHelper?.REPO_ROOT
|
|
87
|
+
STATIC_ROOT = path.resolve(RESOURCE_ROOT, "./static");
|
|
88
|
+
DEFAULT_INDEX = path.join(STATIC_ROOT, "index.html");
|
|
89
|
+
DEFAULT_MANIFEST = path.join(STATIC_ROOT, "manifest.json");
|
|
90
|
+
DEFAULT_HELP = path.join(STATIC_ROOT, "help.md");
|
|
91
|
+
DEFAULT_README = path.resolve(RESOURCE_ROOT, "./README.md");
|
|
92
|
+
DEFAULT_README_EN = path.resolve(RESOURCE_ROOT, "./README.en.md");
|
|
93
|
+
}
|
|
68
94
|
|
|
69
|
-
|
|
70
|
-
{
|
|
71
|
-
|
|
72
|
-
{
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
95
|
+
let cliBootstrapPromise = null;
|
|
96
|
+
for (const i of ["rz.js", "sz.js", "viu.mjs", "client.mjs"]) {
|
|
97
|
+
let subcmd = i.split(".")[0] ;
|
|
98
|
+
if (process.argv[2] === `--${subcmd}`) {
|
|
99
|
+
process.argv.splice(2, 1);
|
|
100
|
+
process.argv[1] = process.argv[1].replace(/gotty\.js$/, i);
|
|
101
|
+
cliBootstrapPromise = (async () => {
|
|
102
|
+
|
|
103
|
+
if(subcmd=="rz")
|
|
104
|
+
await import("./rz.js");
|
|
105
|
+
else if(subcmd=="sz")
|
|
106
|
+
await import("./sz.js");
|
|
107
|
+
else if(subcmd=="viu")
|
|
108
|
+
await import("./viu.mjs");
|
|
109
|
+
else if(subcmd=="client")
|
|
110
|
+
await import("./client.mjs");
|
|
111
|
+
|
|
112
|
+
})().catch((error) => {
|
|
113
|
+
console.error(String(error && error.stack ? error.stack : error));
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
});
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
80
119
|
|
|
81
120
|
|
|
82
121
|
const pkg = require('./package.json');
|
|
@@ -90,6 +129,12 @@ Version: ${pkg.version}
|
|
|
90
129
|
|
|
91
130
|
Usage: bun gotty.js [options] <command> [<arguments...>]
|
|
92
131
|
|
|
132
|
+
Built-in tools:
|
|
133
|
+
--rz Start rz.js
|
|
134
|
+
--sz Start sz.js
|
|
135
|
+
--viu Start viu.mjs
|
|
136
|
+
--client Start client.mjs
|
|
137
|
+
|
|
93
138
|
Options:
|
|
94
139
|
-a, --address <value> IP address to listen
|
|
95
140
|
(default: 0.0.0.0)
|
|
@@ -154,6 +199,9 @@ Options:
|
|
|
154
199
|
Adds a fixed query string to the WebSocket URL
|
|
155
200
|
Format: keyA=valA&keyB=valB
|
|
156
201
|
(default: empty)
|
|
202
|
+
|
|
203
|
+
--webgl Enable xterm WebGL renderer
|
|
204
|
+
(default: disabled)
|
|
157
205
|
|
|
158
206
|
--quiet Disable logging
|
|
159
207
|
(default: log to stderr)
|
|
@@ -163,7 +211,30 @@ Options:
|
|
|
163
211
|
--close-timeout <value> Seconds before force kill after ${DEFAULTS.closeSignal}
|
|
164
212
|
(default: -1 (disabled) )
|
|
165
213
|
|
|
166
|
-
--help
|
|
214
|
+
--help-web Show help for WebUI frontend
|
|
215
|
+
--readme Show README.md and exit
|
|
216
|
+
--readme-en Show README.en.md and exit
|
|
217
|
+
--help, -h Show help for CLI
|
|
218
|
+
|
|
219
|
+
Experimental:
|
|
220
|
+
--build-exe Build a Bun single-file executable and exit
|
|
221
|
+
--build-for <target> Build a Bun single-file executable for target`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function printMarkdownFile(filePath) {
|
|
225
|
+
const text = readTextFile(filePath);
|
|
226
|
+
const markdownAnsi =
|
|
227
|
+
typeof Bun !== "undefined" &&
|
|
228
|
+
Bun &&
|
|
229
|
+
Bun.markdown &&
|
|
230
|
+
typeof Bun.markdown.ansi === "function"
|
|
231
|
+
? Bun.markdown.ansi
|
|
232
|
+
: null;
|
|
233
|
+
console.log(markdownAnsi ? markdownAnsi(text, { hyperlinks: true }) : text);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function printWebHelp() {
|
|
237
|
+
printMarkdownFile(DEFAULT_HELP);
|
|
167
238
|
}
|
|
168
239
|
|
|
169
240
|
function fatal(message, code = 1) {
|
|
@@ -412,6 +483,18 @@ function parseArgs(argv) {
|
|
|
412
483
|
printUsage();
|
|
413
484
|
process.exit(0);
|
|
414
485
|
break;
|
|
486
|
+
case "--help-web":
|
|
487
|
+
printWebHelp();
|
|
488
|
+
process.exit(0);
|
|
489
|
+
break;
|
|
490
|
+
case "--readme":
|
|
491
|
+
printMarkdownFile(DEFAULT_README);
|
|
492
|
+
process.exit(0);
|
|
493
|
+
break;
|
|
494
|
+
case "--readme-en":
|
|
495
|
+
printMarkdownFile(DEFAULT_README_EN);
|
|
496
|
+
process.exit(0);
|
|
497
|
+
break;
|
|
415
498
|
case "--address":
|
|
416
499
|
case "-a":
|
|
417
500
|
options.address = next();
|
|
@@ -488,6 +571,9 @@ function parseArgs(argv) {
|
|
|
488
571
|
case "--ws-query-args":
|
|
489
572
|
options.wsQueryArgs = next();
|
|
490
573
|
break;
|
|
574
|
+
case "--webgl":
|
|
575
|
+
options.webgl = true;
|
|
576
|
+
break;
|
|
491
577
|
case "--quiet":
|
|
492
578
|
options.quiet = true;
|
|
493
579
|
break;
|
|
@@ -552,7 +638,33 @@ function previewBuffer(buffer, limit = 96) {
|
|
|
552
638
|
}
|
|
553
639
|
|
|
554
640
|
function readTextFile(filePath) {
|
|
555
|
-
|
|
641
|
+
const bundledText = helperReadInternalAssetText?.(internalAssetPathFor(filePath));
|
|
642
|
+
if (bundledText == null) {
|
|
643
|
+
return fs.readFileSync(filePath, "utf8");
|
|
644
|
+
}
|
|
645
|
+
return bundledText;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function internalAssetPathFor(filePath) {
|
|
649
|
+
if (!helperAssetPath) {
|
|
650
|
+
return filePath;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const normalizedPath = path.normalize(filePath);
|
|
654
|
+
const staticRelativePath = path.relative(STATIC_ROOT, normalizedPath);
|
|
655
|
+
if (staticRelativePath && !staticRelativePath.startsWith("..") && !path.isAbsolute(staticRelativePath)) {
|
|
656
|
+
return helperAssetPath("static", staticRelativePath);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (normalizedPath === DEFAULT_README) {
|
|
660
|
+
return helperAssetPath("README.md");
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (normalizedPath === DEFAULT_README_EN) {
|
|
664
|
+
return helperAssetPath("README.en.md");
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
return filePath;
|
|
556
668
|
}
|
|
557
669
|
|
|
558
670
|
function contentType(filePath) {
|
|
@@ -637,7 +749,12 @@ function parseInitMessage(raw) {
|
|
|
637
749
|
return {
|
|
638
750
|
Arguments: typeof parsed.Arguments === "string" ? parsed.Arguments : "",
|
|
639
751
|
AuthToken: typeof parsed.AuthToken === "string" ? parsed.AuthToken : "",
|
|
640
|
-
ReconnectToken: typeof parsed.ReconnectToken === "string"
|
|
752
|
+
ReconnectToken: typeof parsed.ReconnectToken === "string"
|
|
753
|
+
? parsed.ReconnectToken.trim()
|
|
754
|
+
: "",
|
|
755
|
+
ReadOnly: parsed.ReadOnly === true,
|
|
756
|
+
DisconnectWriter: parsed.DisconnectWriter === true,
|
|
757
|
+
TerminateSession: parsed.TerminateSession === true
|
|
641
758
|
};
|
|
642
759
|
}
|
|
643
760
|
|
|
@@ -1202,7 +1319,8 @@ class NodePtyBackend {
|
|
|
1202
1319
|
constructor(options) {
|
|
1203
1320
|
let nodePty;
|
|
1204
1321
|
try {
|
|
1205
|
-
|
|
1322
|
+
let preventSingleExe="./lib/"
|
|
1323
|
+
nodePty = require(preventSingleExe+"node-pty");
|
|
1206
1324
|
} catch (error) {
|
|
1207
1325
|
throw new Error("node-pty is required on Windows but is not installed");
|
|
1208
1326
|
}
|
|
@@ -1302,6 +1420,11 @@ class NodePtyBackend {
|
|
|
1302
1420
|
this.closeTimer = null;
|
|
1303
1421
|
}
|
|
1304
1422
|
|
|
1423
|
+
if (process.platform === "win32") {
|
|
1424
|
+
this.kill();
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1305
1428
|
this.kill(this.options.closeSignal);
|
|
1306
1429
|
if (this.options.closeTimeout >= 0) {
|
|
1307
1430
|
this.closeTimer = setTimeout(() => {
|
|
@@ -1451,6 +1574,11 @@ class BunPtyBackend {
|
|
|
1451
1574
|
|
|
1452
1575
|
function createPtyBackend(options) {
|
|
1453
1576
|
if (process.platform === "win32") {
|
|
1577
|
+
if (globalThis.Bun?.semver?.satisfies?.(
|
|
1578
|
+
globalThis.Bun?.version, ">=1.3.14"
|
|
1579
|
+
)) {
|
|
1580
|
+
return new BunPtyBackend(options);
|
|
1581
|
+
}
|
|
1454
1582
|
return new NodePtyBackend(options);
|
|
1455
1583
|
}
|
|
1456
1584
|
|
|
@@ -1460,15 +1588,19 @@ function createPtyBackend(options) {
|
|
|
1460
1588
|
class PtySession {
|
|
1461
1589
|
constructor(options) {
|
|
1462
1590
|
this.options = options;
|
|
1463
|
-
this.
|
|
1591
|
+
this.clients = new Map();
|
|
1592
|
+
this.writerWs = null;
|
|
1464
1593
|
this.log = options.log;
|
|
1465
1594
|
this.permitWrite = options.permitWrite;
|
|
1466
1595
|
this.columns = options.width || 0;
|
|
1467
1596
|
this.rows = options.height || 0;
|
|
1468
|
-
this.bufferSize = 1024;
|
|
1469
|
-
this.encoding = "null";
|
|
1597
|
+
this.bufferSize = 64 * 1024;
|
|
1470
1598
|
this.closed = false;
|
|
1471
1599
|
this.backendExited = false;
|
|
1600
|
+
this.backendExitPromise = new Promise((resolve) => {
|
|
1601
|
+
this.resolveBackendExit = resolve;
|
|
1602
|
+
});
|
|
1603
|
+
this.backendTerminationPromise = null;
|
|
1472
1604
|
this.reconnectTimer = null;
|
|
1473
1605
|
this.reconnectToken = options.reconnect ? generateReconnectToken() : "";
|
|
1474
1606
|
this.backend = createPtyBackend(options);
|
|
@@ -1482,6 +1614,17 @@ class PtySession {
|
|
|
1482
1614
|
this.windowsBridgeSeen = new Set();
|
|
1483
1615
|
this.windowsBridgeSeenOrder = [];
|
|
1484
1616
|
this.pendingEchoBytes = Buffer.alloc(0);
|
|
1617
|
+
this.zmodemBinaryBypass = false;
|
|
1618
|
+
this.zmodemDetectTail = Buffer.alloc(0);
|
|
1619
|
+
this.zmodemBinaryBytesSent = 0;
|
|
1620
|
+
this.zmodemBinaryBytesLogged = 0;
|
|
1621
|
+
this.zmodemOutputQueue = [];
|
|
1622
|
+
this.zmodemOutputInFlight = new Map();
|
|
1623
|
+
this.zmodemOutputSeq = 0;
|
|
1624
|
+
this.zmodemOutputStreamId = 0;
|
|
1625
|
+
this.zmodemOutputAckedSeq = -1;
|
|
1626
|
+
this.zmodemOutputWindow = 8;
|
|
1627
|
+
this.zmodemRetransmitTimer = null;
|
|
1485
1628
|
|
|
1486
1629
|
this.slaveVars = {
|
|
1487
1630
|
command: options.command,
|
|
@@ -1495,6 +1638,10 @@ class PtySession {
|
|
|
1495
1638
|
if (this.closed) {
|
|
1496
1639
|
return;
|
|
1497
1640
|
}
|
|
1641
|
+
if (this.shouldBypassOutputParsers(raw)) {
|
|
1642
|
+
this.sendChunk(MSG_OUTPUT, raw);
|
|
1643
|
+
return;
|
|
1644
|
+
}
|
|
1498
1645
|
const cursorBefore = this.cursorTracker.snapshot();
|
|
1499
1646
|
const parsed = this.kittyParser.consume(raw, cursorBefore);
|
|
1500
1647
|
if (parsed.graphics.length > 0) {
|
|
@@ -1542,13 +1689,45 @@ class PtySession {
|
|
|
1542
1689
|
|
|
1543
1690
|
this.backend.onExit(() => {
|
|
1544
1691
|
this.backendExited = true;
|
|
1692
|
+
this.resolveBackendExit();
|
|
1545
1693
|
this.terminate(1000, "process exited", { skipBackendClose: true });
|
|
1546
1694
|
});
|
|
1547
1695
|
|
|
1548
|
-
this.attachWebSocket(
|
|
1696
|
+
this.attachWebSocket(
|
|
1697
|
+
this.options.ws,
|
|
1698
|
+
this.options.remoteAddr || "",
|
|
1699
|
+
this.options.readOnly === true
|
|
1700
|
+
);
|
|
1549
1701
|
}
|
|
1550
1702
|
|
|
1551
|
-
|
|
1703
|
+
shouldBypassOutputParsers(raw) {
|
|
1704
|
+
if (this.zmodemBinaryBypass) {
|
|
1705
|
+
return true;
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
const data = this.zmodemDetectTail.length > 0
|
|
1709
|
+
? Buffer.concat([this.zmodemDetectTail, raw])
|
|
1710
|
+
: raw;
|
|
1711
|
+
this.zmodemDetectTail = data.subarray(Math.max(0, data.length - 8));
|
|
1712
|
+
|
|
1713
|
+
const binaryHeaderA = Buffer.from([0x2a, 0x18, 0x41]);
|
|
1714
|
+
const binaryHeaderC = Buffer.from([0x2a, 0x18, 0x43]);
|
|
1715
|
+
const hexHeader = Buffer.from([0x2a, 0x2a, 0x18, 0x42]);
|
|
1716
|
+
if (
|
|
1717
|
+
data.indexOf(hexHeader) !== -1 ||
|
|
1718
|
+
data.indexOf(binaryHeaderA) !== -1 ||
|
|
1719
|
+
data.indexOf(binaryHeaderC) !== -1
|
|
1720
|
+
) {
|
|
1721
|
+
this.zmodemBinaryBypass = true;
|
|
1722
|
+
this.resetZmodemOutputFlow();
|
|
1723
|
+
this.log("ZMODEM detected: bypassing server-side output parsers for this session");
|
|
1724
|
+
return true;
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
return false;
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
attachWebSocket(ws, remoteAddr = "", readOnly = false) {
|
|
1552
1731
|
if (!ws || this.closed) {
|
|
1553
1732
|
return false;
|
|
1554
1733
|
}
|
|
@@ -1558,10 +1737,18 @@ class PtySession {
|
|
|
1558
1737
|
this.reconnectTimer = null;
|
|
1559
1738
|
}
|
|
1560
1739
|
|
|
1561
|
-
this.ws
|
|
1562
|
-
|
|
1563
|
-
|
|
1740
|
+
this.clients.set(ws, {
|
|
1741
|
+
remoteAddr,
|
|
1742
|
+
encoding: "null",
|
|
1743
|
+
readOnly,
|
|
1744
|
+
sendQueue: Promise.resolve()
|
|
1745
|
+
});
|
|
1746
|
+
if (this.permitWrite && !readOnly && !this.writerWs) {
|
|
1747
|
+
this.writerWs = ws;
|
|
1748
|
+
}
|
|
1749
|
+
this.sendSessionMetadata(ws, remoteAddr);
|
|
1564
1750
|
this.startWebSocketHandlers(ws);
|
|
1751
|
+
this.notifyRoles();
|
|
1565
1752
|
return true;
|
|
1566
1753
|
}
|
|
1567
1754
|
|
|
@@ -1575,26 +1762,47 @@ class PtySession {
|
|
|
1575
1762
|
);
|
|
1576
1763
|
}
|
|
1577
1764
|
|
|
1578
|
-
sendSessionMetadata() {
|
|
1765
|
+
sendSessionMetadata(ws, remoteAddr = "") {
|
|
1579
1766
|
const title = renderTemplate(
|
|
1580
1767
|
this.options.titleFormat,
|
|
1581
1768
|
buildTitleVariables(
|
|
1582
1769
|
this.options.command,
|
|
1583
1770
|
this.options.argv,
|
|
1584
1771
|
this.options.hostname,
|
|
1585
|
-
|
|
1772
|
+
remoteAddr,
|
|
1586
1773
|
this.slaveVars
|
|
1587
1774
|
)
|
|
1588
1775
|
);
|
|
1589
1776
|
|
|
1590
|
-
this.send(MSG_SET_WINDOW_TITLE, title);
|
|
1591
|
-
this.send(MSG_SET_BUFFER_SIZE, JSON.stringify(this.bufferSize));
|
|
1777
|
+
this.send(MSG_SET_WINDOW_TITLE, title, ws);
|
|
1778
|
+
this.send(MSG_SET_BUFFER_SIZE, JSON.stringify(this.bufferSize), ws);
|
|
1592
1779
|
|
|
1593
1780
|
if (this.options.reconnect) {
|
|
1594
1781
|
this.send(MSG_SET_RECONNECT, JSON.stringify({
|
|
1595
1782
|
time: this.options.reconnectTime,
|
|
1596
|
-
token: this.reconnectToken
|
|
1597
|
-
|
|
1783
|
+
token: this.reconnectToken,
|
|
1784
|
+
pid: this.backend.pid
|
|
1785
|
+
}), ws);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
isWriter(ws) {
|
|
1790
|
+
return this.writerSocket() === ws;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
writerSocket() {
|
|
1794
|
+
return this.writerWs && this.clients.has(this.writerWs)
|
|
1795
|
+
? this.writerWs
|
|
1796
|
+
: null;
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
notifyRoles() {
|
|
1800
|
+
for (const ws of this.clients.keys()) {
|
|
1801
|
+
const writable = this.isWriter(ws);
|
|
1802
|
+
this.send(MSG_SET_ROLE, JSON.stringify({
|
|
1803
|
+
role: writable ? "writer" : "viewer",
|
|
1804
|
+
writable
|
|
1805
|
+
}), ws);
|
|
1598
1806
|
}
|
|
1599
1807
|
}
|
|
1600
1808
|
|
|
@@ -1911,6 +2119,11 @@ class PtySession {
|
|
|
1911
2119
|
}
|
|
1912
2120
|
|
|
1913
2121
|
sendChunk(type, raw) {
|
|
2122
|
+
if (type === MSG_OUTPUT && this.zmodemBinaryBypass) {
|
|
2123
|
+
this.sendZmodemOutput(raw);
|
|
2124
|
+
return;
|
|
2125
|
+
}
|
|
2126
|
+
|
|
1914
2127
|
if (type === MSG_OUTPUT && this.supportsWindowsBridge) {
|
|
1915
2128
|
const bridged = this.outgoingWindowsBridgeParser.consume(raw);
|
|
1916
2129
|
for (const control of bridged.controls) {
|
|
@@ -1929,6 +2142,79 @@ class PtySession {
|
|
|
1929
2142
|
}
|
|
1930
2143
|
}
|
|
1931
2144
|
|
|
2145
|
+
sendZmodemOutput(raw) {
|
|
2146
|
+
this.zmodemBinaryBytesSent += raw.length;
|
|
2147
|
+
if (this.zmodemBinaryBytesSent - this.zmodemBinaryBytesLogged >= 16 * 1024 * 1024) {
|
|
2148
|
+
this.zmodemBinaryBytesLogged = this.zmodemBinaryBytesSent;
|
|
2149
|
+
this.log(`ZMODEM text websocket output ${this.zmodemBinaryBytesSent.toLocaleString()} bytes`);
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
const maxChunkSize = 3 * 1024;
|
|
2153
|
+
for (let offset = 0; offset < raw.length; offset += maxChunkSize) {
|
|
2154
|
+
const chunk = raw.subarray(offset, Math.min(offset + maxChunkSize, raw.length));
|
|
2155
|
+
this.zmodemOutputQueue.push({
|
|
2156
|
+
seq: this.zmodemOutputSeq,
|
|
2157
|
+
payload: chunk.toString("base64")
|
|
2158
|
+
});
|
|
2159
|
+
this.zmodemOutputSeq += 1;
|
|
2160
|
+
}
|
|
2161
|
+
this.flushZmodemOutput();
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
resetZmodemOutputFlow() {
|
|
2165
|
+
this.zmodemOutputQueue = [];
|
|
2166
|
+
this.zmodemOutputInFlight = new Map();
|
|
2167
|
+
this.zmodemOutputSeq = 0;
|
|
2168
|
+
this.zmodemOutputStreamId += 1;
|
|
2169
|
+
this.zmodemOutputAckedSeq = -1;
|
|
2170
|
+
if (this.zmodemRetransmitTimer) {
|
|
2171
|
+
clearTimeout(this.zmodemRetransmitTimer);
|
|
2172
|
+
this.zmodemRetransmitTimer = null;
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
flushZmodemOutput() {
|
|
2177
|
+
const targetWs = this.writerWs && this.writerWs.readyState === WebSocket.OPEN
|
|
2178
|
+
? this.writerWs
|
|
2179
|
+
: this.clients.keys().next().value;
|
|
2180
|
+
if (!targetWs || targetWs.readyState !== WebSocket.OPEN) {
|
|
2181
|
+
return;
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
while (this.zmodemOutputInFlight.size < this.zmodemOutputWindow && this.zmodemOutputQueue.length > 0) {
|
|
2185
|
+
const item = this.zmodemOutputQueue.shift();
|
|
2186
|
+
this.zmodemOutputInFlight.set(item.seq, item);
|
|
2187
|
+
this.sendZmodemOutputItem(item, targetWs);
|
|
2188
|
+
}
|
|
2189
|
+
this.scheduleZmodemRetransmit();
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
sendZmodemOutputItem(item, targetWs) {
|
|
2193
|
+
this.send(MSG_ZMODEM_OUTPUT, `${this.zmodemOutputStreamId}:${item.seq}:${item.payload}`, targetWs);
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
scheduleZmodemRetransmit() {
|
|
2197
|
+
if (this.zmodemRetransmitTimer || this.zmodemOutputInFlight.size === 0) {
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
this.zmodemRetransmitTimer = setTimeout(() => {
|
|
2201
|
+
this.zmodemRetransmitTimer = null;
|
|
2202
|
+
if (!this.zmodemBinaryBypass || this.zmodemOutputInFlight.size === 0) {
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
2205
|
+
const targetWs = this.writerWs && this.writerWs.readyState === WebSocket.OPEN
|
|
2206
|
+
? this.writerWs
|
|
2207
|
+
: this.clients.keys().next().value;
|
|
2208
|
+
if (!targetWs || targetWs.readyState !== WebSocket.OPEN) {
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
for (const item of this.zmodemOutputInFlight.values()) {
|
|
2212
|
+
this.sendZmodemOutputItem(item, targetWs);
|
|
2213
|
+
}
|
|
2214
|
+
this.scheduleZmodemRetransmit();
|
|
2215
|
+
}, 750);
|
|
2216
|
+
}
|
|
2217
|
+
|
|
1932
2218
|
filterEchoedTerminalReplies(raw) {
|
|
1933
2219
|
const needle = Buffer.from("^[[?1;2c", "ascii");
|
|
1934
2220
|
const combined = this.pendingEchoBytes.length > 0 ? Buffer.concat([this.pendingEchoBytes, raw]) : raw;
|
|
@@ -1997,22 +2283,33 @@ class PtySession {
|
|
|
1997
2283
|
|
|
1998
2284
|
startWebSocketHandlers(ws) {
|
|
1999
2285
|
ws.on("message", (message, isBinary) => {
|
|
2000
|
-
if (this.closed ||
|
|
2286
|
+
if (this.closed || !this.clients.has(ws) || isBinary) {
|
|
2001
2287
|
return;
|
|
2002
2288
|
}
|
|
2003
2289
|
try {
|
|
2004
|
-
this.handleClientMessage(message.toString("utf8"));
|
|
2290
|
+
this.handleClientMessage(ws, message.toString("utf8"));
|
|
2005
2291
|
} catch (error) {
|
|
2006
2292
|
this.log(`WS session error: ${error.message}`);
|
|
2007
|
-
|
|
2293
|
+
try {
|
|
2294
|
+
ws.close(1011, error.message);
|
|
2295
|
+
} catch (closeError) {
|
|
2296
|
+
// The client socket is already gone.
|
|
2297
|
+
}
|
|
2008
2298
|
}
|
|
2009
2299
|
});
|
|
2010
2300
|
|
|
2011
2301
|
const handleSocketGone = () => {
|
|
2012
|
-
if (
|
|
2302
|
+
if (!this.clients.has(ws) || this.closed) {
|
|
2303
|
+
return;
|
|
2304
|
+
}
|
|
2305
|
+
if (this.writerWs === ws) {
|
|
2306
|
+
this.writerWs = null;
|
|
2307
|
+
}
|
|
2308
|
+
this.clients.delete(ws);
|
|
2309
|
+
if (this.clients.size > 0) {
|
|
2310
|
+
this.notifyRoles();
|
|
2013
2311
|
return;
|
|
2014
2312
|
}
|
|
2015
|
-
this.ws = null;
|
|
2016
2313
|
if (this.options.reconnect && !this.backendExited) {
|
|
2017
2314
|
this.armReconnectTimeout();
|
|
2018
2315
|
return;
|
|
@@ -2024,36 +2321,40 @@ class PtySession {
|
|
|
2024
2321
|
ws.once("error", handleSocketGone);
|
|
2025
2322
|
}
|
|
2026
2323
|
|
|
2027
|
-
handleClientMessage(data) {
|
|
2324
|
+
handleClientMessage(ws, data) {
|
|
2028
2325
|
if (!data || data.length === 0) {
|
|
2029
2326
|
throw new Error("unexpected zero length read from master");
|
|
2030
2327
|
}
|
|
2031
2328
|
|
|
2032
2329
|
const type = data[0];
|
|
2033
2330
|
const payload = data.slice(1);
|
|
2331
|
+
const client = this.clients.get(ws);
|
|
2332
|
+
if (!client) {
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2034
2335
|
|
|
2035
2336
|
switch (type) {
|
|
2036
2337
|
case MSG_INPUT:
|
|
2037
|
-
if (!this.
|
|
2338
|
+
if (!this.isWriter(ws) || payload.length === 0) {
|
|
2038
2339
|
return;
|
|
2039
2340
|
}
|
|
2040
|
-
if (
|
|
2341
|
+
if (client.encoding === "base64") {
|
|
2041
2342
|
this.backend.write(Buffer.from(payload, "base64"));
|
|
2042
2343
|
} else {
|
|
2043
2344
|
this.backend.write(Buffer.from(payload, "utf8"));
|
|
2044
2345
|
}
|
|
2045
2346
|
break;
|
|
2046
2347
|
case MSG_INPUT_BINARY:
|
|
2047
|
-
if (!this.
|
|
2348
|
+
if (!this.isWriter(ws) || payload.length === 0) {
|
|
2048
2349
|
return;
|
|
2049
2350
|
}
|
|
2050
2351
|
this.backend.writeBinary(Buffer.from(payload, "base64"));
|
|
2051
2352
|
break;
|
|
2052
2353
|
case MSG_PING:
|
|
2053
|
-
this.send(MSG_PONG, "");
|
|
2354
|
+
this.send(MSG_PONG, "", ws);
|
|
2054
2355
|
break;
|
|
2055
2356
|
case MSG_WINDOWS_BRIDGE: {
|
|
2056
|
-
if (!this.supportsWindowsBridge || !this.
|
|
2357
|
+
if (!this.supportsWindowsBridge || !this.isWriter(ws) || payload.length === 0) {
|
|
2057
2358
|
return;
|
|
2058
2359
|
}
|
|
2059
2360
|
let message;
|
|
@@ -2070,11 +2371,46 @@ class PtySession {
|
|
|
2070
2371
|
}
|
|
2071
2372
|
case MSG_SET_ENCODING:
|
|
2072
2373
|
if (payload === "base64" || payload === "null") {
|
|
2073
|
-
|
|
2374
|
+
client.encoding = payload;
|
|
2375
|
+
}
|
|
2376
|
+
break;
|
|
2377
|
+
case MSG_ZMODEM_MODE:
|
|
2378
|
+
if (!this.isWriter(ws)) {
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2381
|
+
{
|
|
2382
|
+
const enabled = payload === "1";
|
|
2383
|
+
const wasEnabled = this.zmodemBinaryBypass;
|
|
2384
|
+
this.zmodemBinaryBypass = enabled;
|
|
2385
|
+
if (this.zmodemBinaryBypass && !wasEnabled) {
|
|
2386
|
+
this.zmodemBinaryBytesSent = 0;
|
|
2387
|
+
this.zmodemBinaryBytesLogged = 0;
|
|
2388
|
+
this.resetZmodemOutputFlow();
|
|
2389
|
+
} else if (!this.zmodemBinaryBypass) {
|
|
2390
|
+
this.resetZmodemOutputFlow();
|
|
2391
|
+
}
|
|
2392
|
+
this.log(`ZMODEM parser bypass ${this.zmodemBinaryBypass ? "enabled" : "disabled"} by browser`);
|
|
2393
|
+
}
|
|
2394
|
+
break;
|
|
2395
|
+
case MSG_ZMODEM_ACK: {
|
|
2396
|
+
if (!this.isWriter(ws)) {
|
|
2397
|
+
return;
|
|
2398
|
+
}
|
|
2399
|
+
const seq = Number.parseInt(payload, 10);
|
|
2400
|
+
if (!Number.isFinite(seq) || seq <= this.zmodemOutputAckedSeq) {
|
|
2401
|
+
return;
|
|
2402
|
+
}
|
|
2403
|
+
for (const key of this.zmodemOutputInFlight.keys()) {
|
|
2404
|
+
if (key <= seq) {
|
|
2405
|
+
this.zmodemOutputInFlight.delete(key);
|
|
2406
|
+
}
|
|
2074
2407
|
}
|
|
2408
|
+
this.zmodemOutputAckedSeq = seq;
|
|
2409
|
+
this.flushZmodemOutput();
|
|
2075
2410
|
break;
|
|
2411
|
+
}
|
|
2076
2412
|
case MSG_RESIZE_TERMINAL: {
|
|
2077
|
-
if (this.columns !== 0 && this.rows !== 0) {
|
|
2413
|
+
if (!this.isWriter(ws) || (this.columns !== 0 && this.rows !== 0)) {
|
|
2078
2414
|
return;
|
|
2079
2415
|
}
|
|
2080
2416
|
let args;
|
|
@@ -2096,10 +2432,100 @@ class PtySession {
|
|
|
2096
2432
|
}
|
|
2097
2433
|
}
|
|
2098
2434
|
|
|
2099
|
-
send(type, payload) {
|
|
2100
|
-
if (
|
|
2101
|
-
this.
|
|
2435
|
+
send(type, payload, targetWs = null) {
|
|
2436
|
+
if (targetWs) {
|
|
2437
|
+
this.queueSend(targetWs, type + payload);
|
|
2438
|
+
return;
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
for (const ws of this.clients.keys()) {
|
|
2442
|
+
this.queueSend(ws, type + payload);
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
queueSend(ws, payload, binary = false) {
|
|
2447
|
+
const client = this.clients.get(ws);
|
|
2448
|
+
if (!client || ws.readyState !== WebSocket.OPEN) {
|
|
2449
|
+
return;
|
|
2102
2450
|
}
|
|
2451
|
+
|
|
2452
|
+
const sendOne = async () => {
|
|
2453
|
+
await new Promise((resolve, reject) => {
|
|
2454
|
+
try {
|
|
2455
|
+
if (binary) {
|
|
2456
|
+
ws.send(payload, { binary: true }, (error) => {
|
|
2457
|
+
if (error) {
|
|
2458
|
+
reject(error);
|
|
2459
|
+
return;
|
|
2460
|
+
}
|
|
2461
|
+
resolve();
|
|
2462
|
+
});
|
|
2463
|
+
} else {
|
|
2464
|
+
ws.send(payload, (error) => {
|
|
2465
|
+
if (error) {
|
|
2466
|
+
reject(error);
|
|
2467
|
+
return;
|
|
2468
|
+
}
|
|
2469
|
+
resolve();
|
|
2470
|
+
});
|
|
2471
|
+
}
|
|
2472
|
+
} catch (error) {
|
|
2473
|
+
reject(error);
|
|
2474
|
+
}
|
|
2475
|
+
});
|
|
2476
|
+
};
|
|
2477
|
+
|
|
2478
|
+
client.sendQueue = client.sendQueue.then(sendOne, sendOne);
|
|
2479
|
+
client.sendQueue.catch((error) => {
|
|
2480
|
+
this.log(`WebSocket send failed: ${error && error.message ? error.message : error}`);
|
|
2481
|
+
});
|
|
2482
|
+
}
|
|
2483
|
+
|
|
2484
|
+
terminateBackend(gracePeriodMs = 5000) {
|
|
2485
|
+
if (this.backendTerminationPromise) {
|
|
2486
|
+
return this.backendTerminationPromise;
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
this.backendTerminationPromise = (async () => {
|
|
2490
|
+
if (this.backendExited) {
|
|
2491
|
+
return { forced: false };
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
const waitForExit = async (timeoutMs) => {
|
|
2495
|
+
let timer = null;
|
|
2496
|
+
const timedOut = new Promise((resolve) => {
|
|
2497
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
2498
|
+
});
|
|
2499
|
+
const exited = this.backendExitPromise.then(() => true);
|
|
2500
|
+
const result = await Promise.race([exited, timedOut]);
|
|
2501
|
+
if (timer) {
|
|
2502
|
+
clearTimeout(timer);
|
|
2503
|
+
}
|
|
2504
|
+
return result;
|
|
2505
|
+
};
|
|
2506
|
+
|
|
2507
|
+
if (process.platform === "win32") {
|
|
2508
|
+
this.backend.kill();
|
|
2509
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2510
|
+
return { forced: true };
|
|
2511
|
+
}
|
|
2512
|
+
throw new Error("process did not exit after PTY kill");
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
this.backend.kill("SIGTERM");
|
|
2516
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2517
|
+
return { forced: false };
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
this.backend.kill("SIGKILL");
|
|
2521
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2522
|
+
return { forced: true };
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
throw new Error("process did not exit after SIGKILL");
|
|
2526
|
+
})();
|
|
2527
|
+
|
|
2528
|
+
return this.backendTerminationPromise;
|
|
2103
2529
|
}
|
|
2104
2530
|
|
|
2105
2531
|
terminate(code, reason, { skipBackendClose = false } = {}) {
|
|
@@ -2121,9 +2547,10 @@ class PtySession {
|
|
|
2121
2547
|
this.backend.close();
|
|
2122
2548
|
}
|
|
2123
2549
|
|
|
2124
|
-
const
|
|
2125
|
-
this.
|
|
2126
|
-
|
|
2550
|
+
const clients = [...this.clients.keys()];
|
|
2551
|
+
this.clients.clear();
|
|
2552
|
+
this.writerWs = null;
|
|
2553
|
+
for (const ws of clients) {
|
|
2127
2554
|
try {
|
|
2128
2555
|
ws.close(code || 1000, reason);
|
|
2129
2556
|
} catch (error) {
|
|
@@ -2156,6 +2583,40 @@ function createServerRuntime(command, argv, options) {
|
|
|
2156
2583
|
let acceptedOnce = false;
|
|
2157
2584
|
let shuttingDown = false;
|
|
2158
2585
|
|
|
2586
|
+
const findSessionByIdentifier = (identifier) => {
|
|
2587
|
+
const value = String(identifier || "").trim();
|
|
2588
|
+
if (!value) {
|
|
2589
|
+
return null;
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2592
|
+
const byToken = reconnectRegistry.get(value);
|
|
2593
|
+
if (byToken && byToken.canResume(byToken.reconnectToken)) {
|
|
2594
|
+
return byToken;
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
for (const session of activeSessions) {
|
|
2598
|
+
if (!session || !session.backend) {
|
|
2599
|
+
continue;
|
|
2600
|
+
}
|
|
2601
|
+
if (String(session.backend.pid) === value) {
|
|
2602
|
+
return session;
|
|
2603
|
+
}
|
|
2604
|
+
if (session.reconnectToken && session.reconnectToken === value) {
|
|
2605
|
+
return session;
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
return null;
|
|
2610
|
+
};
|
|
2611
|
+
|
|
2612
|
+
const findReconnectSession = (identifier) => {
|
|
2613
|
+
const session = findSessionByIdentifier(identifier);
|
|
2614
|
+
if (session && session.canResume(session.reconnectToken)) {
|
|
2615
|
+
return session;
|
|
2616
|
+
}
|
|
2617
|
+
return null;
|
|
2618
|
+
};
|
|
2619
|
+
|
|
2159
2620
|
const counter = new Counter(options.timeout, () => {
|
|
2160
2621
|
log("Timeout reached without active connections, shutting down");
|
|
2161
2622
|
shutdown(false);
|
|
@@ -2228,6 +2689,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2228
2689
|
const lines = [
|
|
2229
2690
|
"var gotty_term = 'xterm';",
|
|
2230
2691
|
`var gotty_ws_query_args = ${JSON.stringify(options.wsQueryArgs)};`,
|
|
2692
|
+
`var gotty_enable_webgl = ${options.webgl ? "true" : "false"};`,
|
|
2231
2693
|
`var gotty_kitty_trace = ${KITTY_TRACE ? "true" : "false"};`
|
|
2232
2694
|
];
|
|
2233
2695
|
res.writeHead(200, {
|
|
@@ -2238,6 +2700,88 @@ function createServerRuntime(command, argv, options) {
|
|
|
2238
2700
|
return;
|
|
2239
2701
|
}
|
|
2240
2702
|
|
|
2703
|
+
if (relativePath === "help.md" || relativePath === "help") {
|
|
2704
|
+
const markdown = readTextFile(DEFAULT_HELP);
|
|
2705
|
+
const markdownToHtml = global.Bun?.markdown?.html;
|
|
2706
|
+
const renderHtml = relativePath === "help" && typeof markdownToHtml === "function";
|
|
2707
|
+
const body = renderHtml
|
|
2708
|
+
? markdownToHtml(markdown)
|
|
2709
|
+
: markdown;
|
|
2710
|
+
res.writeHead(200, {
|
|
2711
|
+
"Content-Type": renderHtml
|
|
2712
|
+
? "text/html; charset=utf-8"
|
|
2713
|
+
: "text/markdown; charset=utf-8",
|
|
2714
|
+
"Server": "GoTTY"
|
|
2715
|
+
});
|
|
2716
|
+
res.end(body);
|
|
2717
|
+
return;
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
if (relativePath === "css.md" || relativePath === "css") {
|
|
2721
|
+
const sessions = [];
|
|
2722
|
+
for (const session of activeSessions) {
|
|
2723
|
+
if (!session || !session.backend) {
|
|
2724
|
+
continue;
|
|
2725
|
+
}
|
|
2726
|
+
const pid = session.backend.pid
|
|
2727
|
+
? String(session.backend.pid)
|
|
2728
|
+
: "無";
|
|
2729
|
+
const command = session.options && session.options.command
|
|
2730
|
+
? path.basename(String(session.options.command))
|
|
2731
|
+
: "";
|
|
2732
|
+
const name = command.replace(/[\r\n\s]+/g, " ").trim() || "無";
|
|
2733
|
+
const reconnectToken = session.reconnectToken
|
|
2734
|
+
? String(session.reconnectToken).replace(/[\r\n\s]+/g, "")
|
|
2735
|
+
: "";
|
|
2736
|
+
sessions.push({ pid, name, token: reconnectToken });
|
|
2737
|
+
}
|
|
2738
|
+
sessions.sort((left, right) => Number(left.pid) - Number(right.pid));
|
|
2739
|
+
const scheme = options.tls ? "https" : "http";
|
|
2740
|
+
const requestHost = String(req.headers.host || "").trim();
|
|
2741
|
+
const fallbackAddress = server.address();
|
|
2742
|
+
const fallbackHost = fallbackAddress && typeof fallbackAddress !== "string"
|
|
2743
|
+
? `${net.isIPv6(fallbackAddress.address) ? `[${fallbackAddress.address}]` : fallbackAddress.address}:${fallbackAddress.port}`
|
|
2744
|
+
: "127.0.0.1";
|
|
2745
|
+
const publicOrigin = `${scheme}://${requestHost || fallbackHost}`;
|
|
2746
|
+
|
|
2747
|
+
const lines = [
|
|
2748
|
+
"# Current Session(s)",
|
|
2749
|
+
...(options.reconnect
|
|
2750
|
+
? [
|
|
2751
|
+
"- Click on the links directly or with Ctrl+Click to reconnect to session",
|
|
2752
|
+
"- Or use bun client.js -r pid"
|
|
2753
|
+
]
|
|
2754
|
+
: [
|
|
2755
|
+
"- Reconnect is disabled, so only PID and command name are shown"
|
|
2756
|
+
]),
|
|
2757
|
+
"## PID SESSION",
|
|
2758
|
+
...(sessions.length > 0
|
|
2759
|
+
? sessions.map((session) => {
|
|
2760
|
+
const name = session.name.replace(/([\\[\]])/g, "\\$1");
|
|
2761
|
+
if (session.token) {
|
|
2762
|
+
const reconnectUrl = `${publicOrigin}${basePath}?reconnect-token=${encodeURIComponent(session.token)}`;
|
|
2763
|
+
return `- ${session.pid} [${name}](${reconnectUrl})`;
|
|
2764
|
+
}
|
|
2765
|
+
return `- ${session.pid} ${name}`;
|
|
2766
|
+
})
|
|
2767
|
+
: ["- 無 無"])
|
|
2768
|
+
];
|
|
2769
|
+
const markdown = `${lines.join("\n")}\n`;
|
|
2770
|
+
const markdownToHtml = global.Bun?.markdown?.html;
|
|
2771
|
+
const renderHtml = relativePath === "css" && typeof markdownToHtml === "function";
|
|
2772
|
+
const body = renderHtml
|
|
2773
|
+
? markdownToHtml(markdown)
|
|
2774
|
+
: markdown;
|
|
2775
|
+
res.writeHead(200, {
|
|
2776
|
+
"Content-Type": renderHtml
|
|
2777
|
+
? "text/html; charset=utf-8"
|
|
2778
|
+
: "text/markdown; charset=utf-8",
|
|
2779
|
+
"Server": "GoTTY"
|
|
2780
|
+
});
|
|
2781
|
+
res.end(body);
|
|
2782
|
+
return;
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2241
2785
|
const assetPath = path.normalize(path.join(STATIC_ROOT, relativePath));
|
|
2242
2786
|
if (!assetPath.startsWith(STATIC_ROOT)) {
|
|
2243
2787
|
res.writeHead(403);
|
|
@@ -2245,6 +2789,18 @@ function createServerRuntime(command, argv, options) {
|
|
|
2245
2789
|
return;
|
|
2246
2790
|
}
|
|
2247
2791
|
|
|
2792
|
+
const bundledAsset = helperReadInternalAssetBytes?.(internalAssetPathFor(assetPath));
|
|
2793
|
+
if (bundledAsset != null) {
|
|
2794
|
+
const body = bundledAsset instanceof Uint8Array ? bundledAsset : new Uint8Array(bundledAsset);
|
|
2795
|
+
res.writeHead(200, {
|
|
2796
|
+
"Content-Type": contentType(assetPath),
|
|
2797
|
+
"Content-Length": body.length,
|
|
2798
|
+
"Server": "GoTTY"
|
|
2799
|
+
});
|
|
2800
|
+
res.end(body);
|
|
2801
|
+
return;
|
|
2802
|
+
}
|
|
2803
|
+
|
|
2248
2804
|
fs.stat(assetPath, (error, stats) => {
|
|
2249
2805
|
if (error || !stats.isFile()) {
|
|
2250
2806
|
res.writeHead(404, { "Server": "GoTTY" });
|
|
@@ -2293,6 +2849,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2293
2849
|
const wss = new WebSocketServer({
|
|
2294
2850
|
noServer: true,
|
|
2295
2851
|
clientTracking: false,
|
|
2852
|
+
perMessageDeflate: false,
|
|
2296
2853
|
handleProtocols(protocols) {
|
|
2297
2854
|
return protocols.has("webtty") ? "webtty" : false;
|
|
2298
2855
|
}
|
|
@@ -2368,7 +2925,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2368
2925
|
ws.once("close", () => finish("client"));
|
|
2369
2926
|
ws.once("error", (error) => finish(`an error: ${error.message}`));
|
|
2370
2927
|
|
|
2371
|
-
ws.once("message", (message, isBinary) => {
|
|
2928
|
+
ws.once("message", async (message, isBinary) => {
|
|
2372
2929
|
if (isBinary) {
|
|
2373
2930
|
ws.close(1008, "invalid message type");
|
|
2374
2931
|
return;
|
|
@@ -2387,14 +2944,87 @@ function createServerRuntime(command, argv, options) {
|
|
|
2387
2944
|
return;
|
|
2388
2945
|
}
|
|
2389
2946
|
|
|
2947
|
+
if (init.DisconnectWriter) {
|
|
2948
|
+
const existing = options.reconnect
|
|
2949
|
+
? findReconnectSession(init.ReconnectToken)
|
|
2950
|
+
: null;
|
|
2951
|
+
const writer = existing?.writerSocket();
|
|
2952
|
+
const result = existing && writer
|
|
2953
|
+
? {
|
|
2954
|
+
ok: true,
|
|
2955
|
+
message: "writer disconnected",
|
|
2956
|
+
token: existing.reconnectToken,
|
|
2957
|
+
pid: existing.backend.pid
|
|
2958
|
+
}
|
|
2959
|
+
: {
|
|
2960
|
+
ok: false,
|
|
2961
|
+
message: existing
|
|
2962
|
+
? "session has no active writer"
|
|
2963
|
+
: "reconnect session not found"
|
|
2964
|
+
};
|
|
2965
|
+
|
|
2966
|
+
if (writer) {
|
|
2967
|
+
try {
|
|
2968
|
+
writer.close(4001, "writer disconnected by remote request");
|
|
2969
|
+
} catch (error) {
|
|
2970
|
+
result.ok = false;
|
|
2971
|
+
result.message = `failed to disconnect writer: ${error.message}`;
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
|
|
2975
|
+
ws.send(MSG_CONTROL_RESULT + JSON.stringify(result), () => {
|
|
2976
|
+
ws.close(result.ok ? 1000 : 1008, result.message);
|
|
2977
|
+
});
|
|
2978
|
+
return;
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
if (init.TerminateSession) {
|
|
2982
|
+
const existing = findSessionByIdentifier(init.ReconnectToken);
|
|
2983
|
+
const token = existing?.reconnectToken || "";
|
|
2984
|
+
const pid = existing?.backend?.pid || "";
|
|
2985
|
+
const result = existing
|
|
2986
|
+
? {
|
|
2987
|
+
ok: true,
|
|
2988
|
+
message: "session terminated",
|
|
2989
|
+
token,
|
|
2990
|
+
pid
|
|
2991
|
+
}
|
|
2992
|
+
: {
|
|
2993
|
+
ok: false,
|
|
2994
|
+
message: "session not found"
|
|
2995
|
+
};
|
|
2996
|
+
|
|
2997
|
+
if (existing) {
|
|
2998
|
+
try {
|
|
2999
|
+
const termination = await existing.terminateBackend(5000);
|
|
3000
|
+
if (termination.forced) {
|
|
3001
|
+
result.message = "session force killed";
|
|
3002
|
+
}
|
|
3003
|
+
} catch (error) {
|
|
3004
|
+
result.ok = false;
|
|
3005
|
+
result.message = `failed to terminate session: ${error.message}`;
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
|
|
3009
|
+
ws.send(MSG_CONTROL_RESULT + JSON.stringify(result), () => {
|
|
3010
|
+
ws.close(result.ok ? 1000 : 1008, result.message);
|
|
3011
|
+
});
|
|
3012
|
+
return;
|
|
3013
|
+
}
|
|
3014
|
+
|
|
2390
3015
|
if (options.reconnect && init.ReconnectToken) {
|
|
2391
|
-
const existing =
|
|
2392
|
-
if (existing
|
|
3016
|
+
const existing = findReconnectSession(init.ReconnectToken);
|
|
3017
|
+
if (existing) {
|
|
3018
|
+
const reconnectToken = existing.reconnectToken;
|
|
2393
3019
|
log(` Reconnect token: (resume)
|
|
2394
|
-
${formatReconnectTokenForLog(
|
|
3020
|
+
${formatReconnectTokenForLog(reconnectToken)}
|
|
2395
3021
|
`);
|
|
2396
3022
|
session = existing;
|
|
2397
|
-
session.attachWebSocket(
|
|
3023
|
+
session.attachWebSocket(
|
|
3024
|
+
ws,
|
|
3025
|
+
req.socket.remoteAddress || "",
|
|
3026
|
+
init.ReadOnly
|
|
3027
|
+
);
|
|
2398
3028
|
return;
|
|
2399
3029
|
}
|
|
2400
3030
|
}
|
|
@@ -2420,6 +3050,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2420
3050
|
argv: [...argv, ...clientArgs],
|
|
2421
3051
|
headerEnv,
|
|
2422
3052
|
remoteAddr: req.socket.remoteAddress || "",
|
|
3053
|
+
readOnly: init.ReadOnly,
|
|
2423
3054
|
hostname,
|
|
2424
3055
|
titleFormat: options.titleFormat,
|
|
2425
3056
|
permitWrite: options.permitWrite,
|
|
@@ -2471,6 +3102,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2471
3102
|
|
|
2472
3103
|
function logUrls() {
|
|
2473
3104
|
const scheme = options.tls ? "https" : "http";
|
|
3105
|
+
const accessMode = options.permitWrite ? "writable" : "read-only";
|
|
2474
3106
|
const address = server.address();
|
|
2475
3107
|
if (!address || typeof address === "string") {
|
|
2476
3108
|
return;
|
|
@@ -2479,7 +3111,35 @@ function createServerRuntime(command, argv, options) {
|
|
|
2479
3111
|
const formattedHost = net.isIPv6(host) ? `[${host}]` : host;
|
|
2480
3112
|
log(`HTTP server is listening at:
|
|
2481
3113
|
${scheme}://${formattedHost}:${address.port}${basePath}
|
|
3114
|
+
Access mode: ${accessMode}
|
|
2482
3115
|
`);
|
|
3116
|
+
const hasCustomPath = normalizeBasePath(options.path) !== "/";
|
|
3117
|
+
const customPathValue = normalizeBasePath(options.path)
|
|
3118
|
+
.replace(/^\/+|\/+$/g, "");
|
|
3119
|
+
if (
|
|
3120
|
+
hasCustomPath &&
|
|
3121
|
+
!options.randomUrl &&
|
|
3122
|
+
customPathValue.length < 16
|
|
3123
|
+
) {
|
|
3124
|
+
log(`SECURITY NOTICE:
|
|
3125
|
+
The custom path is only ${customPathValue.length} characters long.
|
|
3126
|
+
Use at least 16 randomly generated characters to make URL guessing harder.
|
|
3127
|
+
A custom path is not a substitute for --credential and TLS.
|
|
3128
|
+
`);
|
|
3129
|
+
}
|
|
3130
|
+
if (
|
|
3131
|
+
options.permitWrite &&
|
|
3132
|
+
!options.credential &&
|
|
3133
|
+
!options.randomUrl &&
|
|
3134
|
+
!hasCustomPath
|
|
3135
|
+
) {
|
|
3136
|
+
log(`SECURITY WARNING:
|
|
3137
|
+
This writable terminal has no credential, random URL, or custom path.
|
|
3138
|
+
Anyone who can reach this server may execute arbitrary commands (ACE)
|
|
3139
|
+
with the same permissions as the GoTTY process.
|
|
3140
|
+
Enable --credential, --random-url, or at minimum use a non-default --path.
|
|
3141
|
+
`);
|
|
3142
|
+
}
|
|
2483
3143
|
if (options.address === "0.0.0.0") {
|
|
2484
3144
|
try {
|
|
2485
3145
|
const interfaces = os.networkInterfaces();
|
|
@@ -2544,4 +3204,29 @@ function main() {
|
|
|
2544
3204
|
runtime.start();
|
|
2545
3205
|
}
|
|
2546
3206
|
|
|
2547
|
-
|
|
3207
|
+
async function bootstrap() {
|
|
3208
|
+
try {
|
|
3209
|
+
const compiledHelper = await compiledHelperPromise;
|
|
3210
|
+
configureResourceRoot(compiledHelper);
|
|
3211
|
+
await compiledHelper?.buildEarlyExit?.(process.argv,'jsgt');
|
|
3212
|
+
|
|
3213
|
+
const assetsHelper = await assetsHelperPromise;
|
|
3214
|
+
if (assetsHelper) {
|
|
3215
|
+
helperAssetPath = assetsHelper.assetPath;
|
|
3216
|
+
helperReadInternalAssetText = assetsHelper.readInternalAssetText;
|
|
3217
|
+
helperReadInternalAssetBytes = assetsHelper.readInternalAssetBytes;
|
|
3218
|
+
}
|
|
3219
|
+
await globalThis.assetsLoaderPromise;
|
|
3220
|
+
} catch (error) {
|
|
3221
|
+
console.error(String(error && error.stack ? error.stack : error));
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
if (!cliBootstrapPromise) {
|
|
3225
|
+
main();
|
|
3226
|
+
}
|
|
3227
|
+
}
|
|
3228
|
+
|
|
3229
|
+
bootstrap().catch((error) => {
|
|
3230
|
+
console.error(String(error && error.stack ? error.stack : error));
|
|
3231
|
+
process.exitCode = 1;
|
|
3232
|
+
});
|