jsgotty 0.9.1 → 1.0.0
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 +209 -0
- package/README.md +68 -0
- package/client.mjs +696 -0
- package/gotty.js +384 -50
- package/package.json +1 -1
- package/static/js/gotty.js +6 -2
- package/viu.mjs +1 -0
package/gotty.js
CHANGED
|
@@ -28,6 +28,8 @@ 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";
|
|
31
33
|
const KITTY_TRACE = process.env.KITTY_TRACE === "1";
|
|
32
34
|
const WINDOWS_BRIDGE_TRACE = process.env.WINDOWS_BRIDGE_TRACE === "1";
|
|
33
35
|
const WINDOWS_BRIDGE_PREFIX = Buffer.from("@@GOTTYCTL:", "ascii");
|
|
@@ -66,17 +68,20 @@ const STATIC_ROOT = path.resolve(__dirname, "./static");
|
|
|
66
68
|
const DEFAULT_INDEX = path.join(STATIC_ROOT, "index.html");
|
|
67
69
|
const DEFAULT_MANIFEST = path.join(STATIC_ROOT, "manifest.json");
|
|
68
70
|
|
|
69
|
-
|
|
70
|
-
{
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
71
|
+
let cliBootstrapPromise = null;
|
|
72
|
+
for (const i of ["rz.js", "sz.js", "viu.mjs", "client.mjs"]) {
|
|
73
|
+
if (process.argv[2] === `--${i.split(".")[0]}`) {
|
|
74
|
+
process.argv.splice(2, 1);
|
|
75
|
+
process.argv[1] = process.argv[1].replace(/gotty\.js$/, i);
|
|
76
|
+
cliBootstrapPromise = (async () => {
|
|
77
|
+
await import(`./${i}`);
|
|
78
|
+
})().catch((error) => {
|
|
79
|
+
console.error(String(error && error.stack ? error.stack : error));
|
|
80
|
+
process.exitCode = 1;
|
|
81
|
+
});
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
80
85
|
|
|
81
86
|
|
|
82
87
|
const pkg = require('./package.json');
|
|
@@ -90,6 +95,12 @@ Version: ${pkg.version}
|
|
|
90
95
|
|
|
91
96
|
Usage: bun gotty.js [options] <command> [<arguments...>]
|
|
92
97
|
|
|
98
|
+
Built-in tools:
|
|
99
|
+
--rz Start rz.js
|
|
100
|
+
--sz Start sz.js
|
|
101
|
+
--viu Start viu.mjs
|
|
102
|
+
--client Start client.mjs
|
|
103
|
+
|
|
93
104
|
Options:
|
|
94
105
|
-a, --address <value> IP address to listen
|
|
95
106
|
(default: 0.0.0.0)
|
|
@@ -637,7 +648,12 @@ function parseInitMessage(raw) {
|
|
|
637
648
|
return {
|
|
638
649
|
Arguments: typeof parsed.Arguments === "string" ? parsed.Arguments : "",
|
|
639
650
|
AuthToken: typeof parsed.AuthToken === "string" ? parsed.AuthToken : "",
|
|
640
|
-
ReconnectToken: typeof parsed.ReconnectToken === "string"
|
|
651
|
+
ReconnectToken: typeof parsed.ReconnectToken === "string"
|
|
652
|
+
? parsed.ReconnectToken.trim()
|
|
653
|
+
: "",
|
|
654
|
+
ReadOnly: parsed.ReadOnly === true,
|
|
655
|
+
DisconnectWriter: parsed.DisconnectWriter === true,
|
|
656
|
+
TerminateSession: parsed.TerminateSession === true
|
|
641
657
|
};
|
|
642
658
|
}
|
|
643
659
|
|
|
@@ -1302,6 +1318,11 @@ class NodePtyBackend {
|
|
|
1302
1318
|
this.closeTimer = null;
|
|
1303
1319
|
}
|
|
1304
1320
|
|
|
1321
|
+
if (process.platform === "win32") {
|
|
1322
|
+
this.kill();
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1305
1326
|
this.kill(this.options.closeSignal);
|
|
1306
1327
|
if (this.options.closeTimeout >= 0) {
|
|
1307
1328
|
this.closeTimer = setTimeout(() => {
|
|
@@ -1460,15 +1481,19 @@ function createPtyBackend(options) {
|
|
|
1460
1481
|
class PtySession {
|
|
1461
1482
|
constructor(options) {
|
|
1462
1483
|
this.options = options;
|
|
1463
|
-
this.
|
|
1484
|
+
this.clients = new Map();
|
|
1485
|
+
this.writerWs = null;
|
|
1464
1486
|
this.log = options.log;
|
|
1465
1487
|
this.permitWrite = options.permitWrite;
|
|
1466
1488
|
this.columns = options.width || 0;
|
|
1467
1489
|
this.rows = options.height || 0;
|
|
1468
1490
|
this.bufferSize = 1024;
|
|
1469
|
-
this.encoding = "null";
|
|
1470
1491
|
this.closed = false;
|
|
1471
1492
|
this.backendExited = false;
|
|
1493
|
+
this.backendExitPromise = new Promise((resolve) => {
|
|
1494
|
+
this.resolveBackendExit = resolve;
|
|
1495
|
+
});
|
|
1496
|
+
this.backendTerminationPromise = null;
|
|
1472
1497
|
this.reconnectTimer = null;
|
|
1473
1498
|
this.reconnectToken = options.reconnect ? generateReconnectToken() : "";
|
|
1474
1499
|
this.backend = createPtyBackend(options);
|
|
@@ -1542,13 +1567,18 @@ class PtySession {
|
|
|
1542
1567
|
|
|
1543
1568
|
this.backend.onExit(() => {
|
|
1544
1569
|
this.backendExited = true;
|
|
1570
|
+
this.resolveBackendExit();
|
|
1545
1571
|
this.terminate(1000, "process exited", { skipBackendClose: true });
|
|
1546
1572
|
});
|
|
1547
1573
|
|
|
1548
|
-
this.attachWebSocket(
|
|
1574
|
+
this.attachWebSocket(
|
|
1575
|
+
this.options.ws,
|
|
1576
|
+
this.options.remoteAddr || "",
|
|
1577
|
+
this.options.readOnly === true
|
|
1578
|
+
);
|
|
1549
1579
|
}
|
|
1550
1580
|
|
|
1551
|
-
attachWebSocket(ws, remoteAddr = "") {
|
|
1581
|
+
attachWebSocket(ws, remoteAddr = "", readOnly = false) {
|
|
1552
1582
|
if (!ws || this.closed) {
|
|
1553
1583
|
return false;
|
|
1554
1584
|
}
|
|
@@ -1558,10 +1588,17 @@ class PtySession {
|
|
|
1558
1588
|
this.reconnectTimer = null;
|
|
1559
1589
|
}
|
|
1560
1590
|
|
|
1561
|
-
this.ws
|
|
1562
|
-
|
|
1563
|
-
|
|
1591
|
+
this.clients.set(ws, {
|
|
1592
|
+
remoteAddr,
|
|
1593
|
+
encoding: "null",
|
|
1594
|
+
readOnly
|
|
1595
|
+
});
|
|
1596
|
+
if (this.permitWrite && !readOnly && !this.writerWs) {
|
|
1597
|
+
this.writerWs = ws;
|
|
1598
|
+
}
|
|
1599
|
+
this.sendSessionMetadata(ws, remoteAddr);
|
|
1564
1600
|
this.startWebSocketHandlers(ws);
|
|
1601
|
+
this.notifyRoles();
|
|
1565
1602
|
return true;
|
|
1566
1603
|
}
|
|
1567
1604
|
|
|
@@ -1575,26 +1612,47 @@ class PtySession {
|
|
|
1575
1612
|
);
|
|
1576
1613
|
}
|
|
1577
1614
|
|
|
1578
|
-
sendSessionMetadata() {
|
|
1615
|
+
sendSessionMetadata(ws, remoteAddr = "") {
|
|
1579
1616
|
const title = renderTemplate(
|
|
1580
1617
|
this.options.titleFormat,
|
|
1581
1618
|
buildTitleVariables(
|
|
1582
1619
|
this.options.command,
|
|
1583
1620
|
this.options.argv,
|
|
1584
1621
|
this.options.hostname,
|
|
1585
|
-
|
|
1622
|
+
remoteAddr,
|
|
1586
1623
|
this.slaveVars
|
|
1587
1624
|
)
|
|
1588
1625
|
);
|
|
1589
1626
|
|
|
1590
|
-
this.send(MSG_SET_WINDOW_TITLE, title);
|
|
1591
|
-
this.send(MSG_SET_BUFFER_SIZE, JSON.stringify(this.bufferSize));
|
|
1627
|
+
this.send(MSG_SET_WINDOW_TITLE, title, ws);
|
|
1628
|
+
this.send(MSG_SET_BUFFER_SIZE, JSON.stringify(this.bufferSize), ws);
|
|
1592
1629
|
|
|
1593
1630
|
if (this.options.reconnect) {
|
|
1594
1631
|
this.send(MSG_SET_RECONNECT, JSON.stringify({
|
|
1595
1632
|
time: this.options.reconnectTime,
|
|
1596
|
-
token: this.reconnectToken
|
|
1597
|
-
|
|
1633
|
+
token: this.reconnectToken,
|
|
1634
|
+
pid: this.backend.pid
|
|
1635
|
+
}), ws);
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
isWriter(ws) {
|
|
1640
|
+
return this.writerSocket() === ws;
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
writerSocket() {
|
|
1644
|
+
return this.writerWs && this.clients.has(this.writerWs)
|
|
1645
|
+
? this.writerWs
|
|
1646
|
+
: null;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
notifyRoles() {
|
|
1650
|
+
for (const ws of this.clients.keys()) {
|
|
1651
|
+
const writable = this.isWriter(ws);
|
|
1652
|
+
this.send(MSG_SET_ROLE, JSON.stringify({
|
|
1653
|
+
role: writable ? "writer" : "viewer",
|
|
1654
|
+
writable
|
|
1655
|
+
}), ws);
|
|
1598
1656
|
}
|
|
1599
1657
|
}
|
|
1600
1658
|
|
|
@@ -1997,22 +2055,33 @@ class PtySession {
|
|
|
1997
2055
|
|
|
1998
2056
|
startWebSocketHandlers(ws) {
|
|
1999
2057
|
ws.on("message", (message, isBinary) => {
|
|
2000
|
-
if (this.closed ||
|
|
2058
|
+
if (this.closed || !this.clients.has(ws) || isBinary) {
|
|
2001
2059
|
return;
|
|
2002
2060
|
}
|
|
2003
2061
|
try {
|
|
2004
|
-
this.handleClientMessage(message.toString("utf8"));
|
|
2062
|
+
this.handleClientMessage(ws, message.toString("utf8"));
|
|
2005
2063
|
} catch (error) {
|
|
2006
2064
|
this.log(`WS session error: ${error.message}`);
|
|
2007
|
-
|
|
2065
|
+
try {
|
|
2066
|
+
ws.close(1011, error.message);
|
|
2067
|
+
} catch (closeError) {
|
|
2068
|
+
// The client socket is already gone.
|
|
2069
|
+
}
|
|
2008
2070
|
}
|
|
2009
2071
|
});
|
|
2010
2072
|
|
|
2011
2073
|
const handleSocketGone = () => {
|
|
2012
|
-
if (
|
|
2074
|
+
if (!this.clients.has(ws) || this.closed) {
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
if (this.writerWs === ws) {
|
|
2078
|
+
this.writerWs = null;
|
|
2079
|
+
}
|
|
2080
|
+
this.clients.delete(ws);
|
|
2081
|
+
if (this.clients.size > 0) {
|
|
2082
|
+
this.notifyRoles();
|
|
2013
2083
|
return;
|
|
2014
2084
|
}
|
|
2015
|
-
this.ws = null;
|
|
2016
2085
|
if (this.options.reconnect && !this.backendExited) {
|
|
2017
2086
|
this.armReconnectTimeout();
|
|
2018
2087
|
return;
|
|
@@ -2024,36 +2093,40 @@ class PtySession {
|
|
|
2024
2093
|
ws.once("error", handleSocketGone);
|
|
2025
2094
|
}
|
|
2026
2095
|
|
|
2027
|
-
handleClientMessage(data) {
|
|
2096
|
+
handleClientMessage(ws, data) {
|
|
2028
2097
|
if (!data || data.length === 0) {
|
|
2029
2098
|
throw new Error("unexpected zero length read from master");
|
|
2030
2099
|
}
|
|
2031
2100
|
|
|
2032
2101
|
const type = data[0];
|
|
2033
2102
|
const payload = data.slice(1);
|
|
2103
|
+
const client = this.clients.get(ws);
|
|
2104
|
+
if (!client) {
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2034
2107
|
|
|
2035
2108
|
switch (type) {
|
|
2036
2109
|
case MSG_INPUT:
|
|
2037
|
-
if (!this.
|
|
2110
|
+
if (!this.isWriter(ws) || payload.length === 0) {
|
|
2038
2111
|
return;
|
|
2039
2112
|
}
|
|
2040
|
-
if (
|
|
2113
|
+
if (client.encoding === "base64") {
|
|
2041
2114
|
this.backend.write(Buffer.from(payload, "base64"));
|
|
2042
2115
|
} else {
|
|
2043
2116
|
this.backend.write(Buffer.from(payload, "utf8"));
|
|
2044
2117
|
}
|
|
2045
2118
|
break;
|
|
2046
2119
|
case MSG_INPUT_BINARY:
|
|
2047
|
-
if (!this.
|
|
2120
|
+
if (!this.isWriter(ws) || payload.length === 0) {
|
|
2048
2121
|
return;
|
|
2049
2122
|
}
|
|
2050
2123
|
this.backend.writeBinary(Buffer.from(payload, "base64"));
|
|
2051
2124
|
break;
|
|
2052
2125
|
case MSG_PING:
|
|
2053
|
-
this.send(MSG_PONG, "");
|
|
2126
|
+
this.send(MSG_PONG, "", ws);
|
|
2054
2127
|
break;
|
|
2055
2128
|
case MSG_WINDOWS_BRIDGE: {
|
|
2056
|
-
if (!this.supportsWindowsBridge || !this.
|
|
2129
|
+
if (!this.supportsWindowsBridge || !this.isWriter(ws) || payload.length === 0) {
|
|
2057
2130
|
return;
|
|
2058
2131
|
}
|
|
2059
2132
|
let message;
|
|
@@ -2070,11 +2143,11 @@ class PtySession {
|
|
|
2070
2143
|
}
|
|
2071
2144
|
case MSG_SET_ENCODING:
|
|
2072
2145
|
if (payload === "base64" || payload === "null") {
|
|
2073
|
-
|
|
2146
|
+
client.encoding = payload;
|
|
2074
2147
|
}
|
|
2075
2148
|
break;
|
|
2076
2149
|
case MSG_RESIZE_TERMINAL: {
|
|
2077
|
-
if (this.columns !== 0 && this.rows !== 0) {
|
|
2150
|
+
if (!this.isWriter(ws) || (this.columns !== 0 && this.rows !== 0)) {
|
|
2078
2151
|
return;
|
|
2079
2152
|
}
|
|
2080
2153
|
let args;
|
|
@@ -2096,12 +2169,68 @@ class PtySession {
|
|
|
2096
2169
|
}
|
|
2097
2170
|
}
|
|
2098
2171
|
|
|
2099
|
-
send(type, payload) {
|
|
2100
|
-
if (
|
|
2101
|
-
this.
|
|
2172
|
+
send(type, payload, targetWs = null) {
|
|
2173
|
+
if (targetWs) {
|
|
2174
|
+
if (this.clients.has(targetWs) && targetWs.readyState === WebSocket.OPEN) {
|
|
2175
|
+
targetWs.send(type + payload);
|
|
2176
|
+
}
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
for (const ws of this.clients.keys()) {
|
|
2181
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
2182
|
+
ws.send(type + payload);
|
|
2183
|
+
}
|
|
2102
2184
|
}
|
|
2103
2185
|
}
|
|
2104
2186
|
|
|
2187
|
+
terminateBackend(gracePeriodMs = 5000) {
|
|
2188
|
+
if (this.backendTerminationPromise) {
|
|
2189
|
+
return this.backendTerminationPromise;
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
this.backendTerminationPromise = (async () => {
|
|
2193
|
+
if (this.backendExited) {
|
|
2194
|
+
return { forced: false };
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
const waitForExit = async (timeoutMs) => {
|
|
2198
|
+
let timer = null;
|
|
2199
|
+
const timedOut = new Promise((resolve) => {
|
|
2200
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
2201
|
+
});
|
|
2202
|
+
const exited = this.backendExitPromise.then(() => true);
|
|
2203
|
+
const result = await Promise.race([exited, timedOut]);
|
|
2204
|
+
if (timer) {
|
|
2205
|
+
clearTimeout(timer);
|
|
2206
|
+
}
|
|
2207
|
+
return result;
|
|
2208
|
+
};
|
|
2209
|
+
|
|
2210
|
+
if (process.platform === "win32") {
|
|
2211
|
+
this.backend.kill();
|
|
2212
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2213
|
+
return { forced: true };
|
|
2214
|
+
}
|
|
2215
|
+
throw new Error("process did not exit after PTY kill");
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
this.backend.kill("SIGTERM");
|
|
2219
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2220
|
+
return { forced: false };
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
this.backend.kill("SIGKILL");
|
|
2224
|
+
if (await waitForExit(gracePeriodMs)) {
|
|
2225
|
+
return { forced: true };
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
throw new Error("process did not exit after SIGKILL");
|
|
2229
|
+
})();
|
|
2230
|
+
|
|
2231
|
+
return this.backendTerminationPromise;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2105
2234
|
terminate(code, reason, { skipBackendClose = false } = {}) {
|
|
2106
2235
|
if (this.closed) {
|
|
2107
2236
|
return;
|
|
@@ -2121,9 +2250,10 @@ class PtySession {
|
|
|
2121
2250
|
this.backend.close();
|
|
2122
2251
|
}
|
|
2123
2252
|
|
|
2124
|
-
const
|
|
2125
|
-
this.
|
|
2126
|
-
|
|
2253
|
+
const clients = [...this.clients.keys()];
|
|
2254
|
+
this.clients.clear();
|
|
2255
|
+
this.writerWs = null;
|
|
2256
|
+
for (const ws of clients) {
|
|
2127
2257
|
try {
|
|
2128
2258
|
ws.close(code || 1000, reason);
|
|
2129
2259
|
} catch (error) {
|
|
@@ -2156,6 +2286,40 @@ function createServerRuntime(command, argv, options) {
|
|
|
2156
2286
|
let acceptedOnce = false;
|
|
2157
2287
|
let shuttingDown = false;
|
|
2158
2288
|
|
|
2289
|
+
const findSessionByIdentifier = (identifier) => {
|
|
2290
|
+
const value = String(identifier || "").trim();
|
|
2291
|
+
if (!value) {
|
|
2292
|
+
return null;
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
const byToken = reconnectRegistry.get(value);
|
|
2296
|
+
if (byToken && byToken.canResume(byToken.reconnectToken)) {
|
|
2297
|
+
return byToken;
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
for (const session of activeSessions) {
|
|
2301
|
+
if (!session || !session.backend) {
|
|
2302
|
+
continue;
|
|
2303
|
+
}
|
|
2304
|
+
if (String(session.backend.pid) === value) {
|
|
2305
|
+
return session;
|
|
2306
|
+
}
|
|
2307
|
+
if (session.reconnectToken && session.reconnectToken === value) {
|
|
2308
|
+
return session;
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
return null;
|
|
2313
|
+
};
|
|
2314
|
+
|
|
2315
|
+
const findReconnectSession = (identifier) => {
|
|
2316
|
+
const session = findSessionByIdentifier(identifier);
|
|
2317
|
+
if (session && session.canResume(session.reconnectToken)) {
|
|
2318
|
+
return session;
|
|
2319
|
+
}
|
|
2320
|
+
return null;
|
|
2321
|
+
};
|
|
2322
|
+
|
|
2159
2323
|
const counter = new Counter(options.timeout, () => {
|
|
2160
2324
|
log("Timeout reached without active connections, shutting down");
|
|
2161
2325
|
shutdown(false);
|
|
@@ -2238,6 +2402,71 @@ function createServerRuntime(command, argv, options) {
|
|
|
2238
2402
|
return;
|
|
2239
2403
|
}
|
|
2240
2404
|
|
|
2405
|
+
if (relativePath === "css.md" || relativePath === "css") {
|
|
2406
|
+
const sessions = [];
|
|
2407
|
+
for (const session of activeSessions) {
|
|
2408
|
+
if (!session || !session.backend) {
|
|
2409
|
+
continue;
|
|
2410
|
+
}
|
|
2411
|
+
const pid = session.backend.pid
|
|
2412
|
+
? String(session.backend.pid)
|
|
2413
|
+
: "無";
|
|
2414
|
+
const command = session.options && session.options.command
|
|
2415
|
+
? path.basename(String(session.options.command))
|
|
2416
|
+
: "";
|
|
2417
|
+
const name = command.replace(/[\r\n\s]+/g, " ").trim() || "無";
|
|
2418
|
+
const reconnectToken = session.reconnectToken
|
|
2419
|
+
? String(session.reconnectToken).replace(/[\r\n\s]+/g, "")
|
|
2420
|
+
: "";
|
|
2421
|
+
sessions.push({ pid, name, token: reconnectToken });
|
|
2422
|
+
}
|
|
2423
|
+
sessions.sort((left, right) => Number(left.pid) - Number(right.pid));
|
|
2424
|
+
const scheme = options.tls ? "https" : "http";
|
|
2425
|
+
const requestHost = String(req.headers.host || "").trim();
|
|
2426
|
+
const fallbackAddress = server.address();
|
|
2427
|
+
const fallbackHost = fallbackAddress && typeof fallbackAddress !== "string"
|
|
2428
|
+
? `${net.isIPv6(fallbackAddress.address) ? `[${fallbackAddress.address}]` : fallbackAddress.address}:${fallbackAddress.port}`
|
|
2429
|
+
: "127.0.0.1";
|
|
2430
|
+
const publicOrigin = `${scheme}://${requestHost || fallbackHost}`;
|
|
2431
|
+
|
|
2432
|
+
const lines = [
|
|
2433
|
+
"# Current Session(s)",
|
|
2434
|
+
...(options.reconnect
|
|
2435
|
+
? [
|
|
2436
|
+
"- Click on the links directly or with Ctrl+Click to reconnect to session",
|
|
2437
|
+
"- Or use bun client.js -r pid"
|
|
2438
|
+
]
|
|
2439
|
+
: [
|
|
2440
|
+
"- Reconnect is disabled, so only PID and command name are shown"
|
|
2441
|
+
]),
|
|
2442
|
+
"## PID SESSION",
|
|
2443
|
+
...(sessions.length > 0
|
|
2444
|
+
? sessions.map((session) => {
|
|
2445
|
+
const name = session.name.replace(/([\\[\]])/g, "\\$1");
|
|
2446
|
+
if (session.token) {
|
|
2447
|
+
const reconnectUrl = `${publicOrigin}${basePath}?reconnect-token=${encodeURIComponent(session.token)}`;
|
|
2448
|
+
return `- ${session.pid} [${name}](${reconnectUrl})`;
|
|
2449
|
+
}
|
|
2450
|
+
return `- ${session.pid} ${name}`;
|
|
2451
|
+
})
|
|
2452
|
+
: ["- 無 無"])
|
|
2453
|
+
];
|
|
2454
|
+
const markdown = `${lines.join("\n")}\n`;
|
|
2455
|
+
const markdownToHtml = global.Bun?.markdown?.html;
|
|
2456
|
+
const renderHtml = relativePath === "css" && typeof markdownToHtml === "function";
|
|
2457
|
+
const body = renderHtml
|
|
2458
|
+
? markdownToHtml(markdown)
|
|
2459
|
+
: markdown;
|
|
2460
|
+
res.writeHead(200, {
|
|
2461
|
+
"Content-Type": renderHtml
|
|
2462
|
+
? "text/html; charset=utf-8"
|
|
2463
|
+
: "text/markdown; charset=utf-8",
|
|
2464
|
+
"Server": "GoTTY"
|
|
2465
|
+
});
|
|
2466
|
+
res.end(body);
|
|
2467
|
+
return;
|
|
2468
|
+
}
|
|
2469
|
+
|
|
2241
2470
|
const assetPath = path.normalize(path.join(STATIC_ROOT, relativePath));
|
|
2242
2471
|
if (!assetPath.startsWith(STATIC_ROOT)) {
|
|
2243
2472
|
res.writeHead(403);
|
|
@@ -2368,7 +2597,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2368
2597
|
ws.once("close", () => finish("client"));
|
|
2369
2598
|
ws.once("error", (error) => finish(`an error: ${error.message}`));
|
|
2370
2599
|
|
|
2371
|
-
ws.once("message", (message, isBinary) => {
|
|
2600
|
+
ws.once("message", async (message, isBinary) => {
|
|
2372
2601
|
if (isBinary) {
|
|
2373
2602
|
ws.close(1008, "invalid message type");
|
|
2374
2603
|
return;
|
|
@@ -2387,14 +2616,87 @@ function createServerRuntime(command, argv, options) {
|
|
|
2387
2616
|
return;
|
|
2388
2617
|
}
|
|
2389
2618
|
|
|
2619
|
+
if (init.DisconnectWriter) {
|
|
2620
|
+
const existing = options.reconnect
|
|
2621
|
+
? findReconnectSession(init.ReconnectToken)
|
|
2622
|
+
: null;
|
|
2623
|
+
const writer = existing?.writerSocket();
|
|
2624
|
+
const result = existing && writer
|
|
2625
|
+
? {
|
|
2626
|
+
ok: true,
|
|
2627
|
+
message: "writer disconnected",
|
|
2628
|
+
token: existing.reconnectToken,
|
|
2629
|
+
pid: existing.backend.pid
|
|
2630
|
+
}
|
|
2631
|
+
: {
|
|
2632
|
+
ok: false,
|
|
2633
|
+
message: existing
|
|
2634
|
+
? "session has no active writer"
|
|
2635
|
+
: "reconnect session not found"
|
|
2636
|
+
};
|
|
2637
|
+
|
|
2638
|
+
if (writer) {
|
|
2639
|
+
try {
|
|
2640
|
+
writer.close(4001, "writer disconnected by remote request");
|
|
2641
|
+
} catch (error) {
|
|
2642
|
+
result.ok = false;
|
|
2643
|
+
result.message = `failed to disconnect writer: ${error.message}`;
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
ws.send(MSG_CONTROL_RESULT + JSON.stringify(result), () => {
|
|
2648
|
+
ws.close(result.ok ? 1000 : 1008, result.message);
|
|
2649
|
+
});
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
if (init.TerminateSession) {
|
|
2654
|
+
const existing = findSessionByIdentifier(init.ReconnectToken);
|
|
2655
|
+
const token = existing?.reconnectToken || "";
|
|
2656
|
+
const pid = existing?.backend?.pid || "";
|
|
2657
|
+
const result = existing
|
|
2658
|
+
? {
|
|
2659
|
+
ok: true,
|
|
2660
|
+
message: "session terminated",
|
|
2661
|
+
token,
|
|
2662
|
+
pid
|
|
2663
|
+
}
|
|
2664
|
+
: {
|
|
2665
|
+
ok: false,
|
|
2666
|
+
message: "session not found"
|
|
2667
|
+
};
|
|
2668
|
+
|
|
2669
|
+
if (existing) {
|
|
2670
|
+
try {
|
|
2671
|
+
const termination = await existing.terminateBackend(5000);
|
|
2672
|
+
if (termination.forced) {
|
|
2673
|
+
result.message = "session force killed";
|
|
2674
|
+
}
|
|
2675
|
+
} catch (error) {
|
|
2676
|
+
result.ok = false;
|
|
2677
|
+
result.message = `failed to terminate session: ${error.message}`;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
ws.send(MSG_CONTROL_RESULT + JSON.stringify(result), () => {
|
|
2682
|
+
ws.close(result.ok ? 1000 : 1008, result.message);
|
|
2683
|
+
});
|
|
2684
|
+
return;
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2390
2687
|
if (options.reconnect && init.ReconnectToken) {
|
|
2391
|
-
const existing =
|
|
2392
|
-
if (existing
|
|
2688
|
+
const existing = findReconnectSession(init.ReconnectToken);
|
|
2689
|
+
if (existing) {
|
|
2690
|
+
const reconnectToken = existing.reconnectToken;
|
|
2393
2691
|
log(` Reconnect token: (resume)
|
|
2394
|
-
${formatReconnectTokenForLog(
|
|
2692
|
+
${formatReconnectTokenForLog(reconnectToken)}
|
|
2395
2693
|
`);
|
|
2396
2694
|
session = existing;
|
|
2397
|
-
session.attachWebSocket(
|
|
2695
|
+
session.attachWebSocket(
|
|
2696
|
+
ws,
|
|
2697
|
+
req.socket.remoteAddress || "",
|
|
2698
|
+
init.ReadOnly
|
|
2699
|
+
);
|
|
2398
2700
|
return;
|
|
2399
2701
|
}
|
|
2400
2702
|
}
|
|
@@ -2420,6 +2722,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2420
2722
|
argv: [...argv, ...clientArgs],
|
|
2421
2723
|
headerEnv,
|
|
2422
2724
|
remoteAddr: req.socket.remoteAddress || "",
|
|
2725
|
+
readOnly: init.ReadOnly,
|
|
2423
2726
|
hostname,
|
|
2424
2727
|
titleFormat: options.titleFormat,
|
|
2425
2728
|
permitWrite: options.permitWrite,
|
|
@@ -2471,6 +2774,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2471
2774
|
|
|
2472
2775
|
function logUrls() {
|
|
2473
2776
|
const scheme = options.tls ? "https" : "http";
|
|
2777
|
+
const accessMode = options.permitWrite ? "writable" : "read-only";
|
|
2474
2778
|
const address = server.address();
|
|
2475
2779
|
if (!address || typeof address === "string") {
|
|
2476
2780
|
return;
|
|
@@ -2479,7 +2783,35 @@ function createServerRuntime(command, argv, options) {
|
|
|
2479
2783
|
const formattedHost = net.isIPv6(host) ? `[${host}]` : host;
|
|
2480
2784
|
log(`HTTP server is listening at:
|
|
2481
2785
|
${scheme}://${formattedHost}:${address.port}${basePath}
|
|
2786
|
+
Access mode: ${accessMode}
|
|
2787
|
+
`);
|
|
2788
|
+
const hasCustomPath = normalizeBasePath(options.path) !== "/";
|
|
2789
|
+
const customPathValue = normalizeBasePath(options.path)
|
|
2790
|
+
.replace(/^\/+|\/+$/g, "");
|
|
2791
|
+
if (
|
|
2792
|
+
hasCustomPath &&
|
|
2793
|
+
!options.randomUrl &&
|
|
2794
|
+
customPathValue.length < 16
|
|
2795
|
+
) {
|
|
2796
|
+
log(`SECURITY NOTICE:
|
|
2797
|
+
The custom path is only ${customPathValue.length} characters long.
|
|
2798
|
+
Use at least 16 randomly generated characters to make URL guessing harder.
|
|
2799
|
+
A custom path is not a substitute for --credential and TLS.
|
|
2800
|
+
`);
|
|
2801
|
+
}
|
|
2802
|
+
if (
|
|
2803
|
+
options.permitWrite &&
|
|
2804
|
+
!options.credential &&
|
|
2805
|
+
!options.randomUrl &&
|
|
2806
|
+
!hasCustomPath
|
|
2807
|
+
) {
|
|
2808
|
+
log(`SECURITY WARNING:
|
|
2809
|
+
This writable terminal has no credential, random URL, or custom path.
|
|
2810
|
+
Anyone who can reach this server may execute arbitrary commands (ACE)
|
|
2811
|
+
with the same permissions as the GoTTY process.
|
|
2812
|
+
Enable --credential, --random-url, or at minimum use a non-default --path.
|
|
2482
2813
|
`);
|
|
2814
|
+
}
|
|
2483
2815
|
if (options.address === "0.0.0.0") {
|
|
2484
2816
|
try {
|
|
2485
2817
|
const interfaces = os.networkInterfaces();
|
|
@@ -2544,4 +2876,6 @@ function main() {
|
|
|
2544
2876
|
runtime.start();
|
|
2545
2877
|
}
|
|
2546
2878
|
|
|
2547
|
-
|
|
2879
|
+
if (!cliBootstrapPromise) {
|
|
2880
|
+
main();
|
|
2881
|
+
}
|
package/package.json
CHANGED
package/static/js/gotty.js
CHANGED
|
@@ -30976,9 +30976,13 @@
|
|
|
30976
30976
|
window.location.pathname +
|
|
30977
30977
|
"ws" +
|
|
30978
30978
|
o,
|
|
30979
|
-
l = window.location.search,
|
|
30979
|
+
l = new URLSearchParams(window.location.search),
|
|
30980
|
+
u = l.get("reconnect-token") || "";
|
|
30981
|
+
l.delete("reconnect-token");
|
|
30982
|
+
const g = l.toString(),
|
|
30980
30983
|
c = new e.ConnectionFactory(a, t.protocols),
|
|
30981
|
-
d = new t.WebTTY(n, c,
|
|
30984
|
+
d = new t.WebTTY(n, c, "" === g ? "" : "?" + g, gotty_auth_token);
|
|
30985
|
+
d.reconnectToken = u;
|
|
30982
30986
|
if ("undefined" != typeof window) {
|
|
30983
30987
|
window.__gottyXterm = s.__gottyXterm || null;
|
|
30984
30988
|
window.__gottyWebTTY = d;
|
package/viu.mjs
CHANGED