@seflless/ghosttown 1.0.1 → 1.1.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/LICENSE +21 -0
- package/README.md +9 -2
- package/bin/ghosttown.js +676 -0
- package/dist/__vite-browser-external-2447137e.js +4 -0
- package/dist/ghostty-vt.wasm +0 -0
- package/dist/ghostty-web.js +3216 -0
- package/dist/ghostty-web.umd.cjs +13 -0
- package/dist/index.d.ts +1944 -0
- package/ghostty-vt.wasm +0 -0
- package/package.json +80 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Coder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ghost-town
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ghost town let's you continue terminal sessions from other devices using a browser. The runtime is based on Ghostty with a recreated UI that should feel like using Ghostty for desktop.
|
|
4
|
+
|
|
5
|
+
We're building this to make it easy to build web apps using coding agents, while on the go.
|
|
6
|
+
|
|
7
|
+
# Credits
|
|
8
|
+
|
|
9
|
+
- [Ghostty](https://ghostty.org/)
|
|
10
|
+
- [ghostty-web](https://github.com/coder/ghostty-web)
|
package/bin/ghosttown.js
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ghosttown CLI - Web-based terminal emulator
|
|
5
|
+
*
|
|
6
|
+
* Starts a local HTTP server with WebSocket PTY support.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ghosttown [options]
|
|
10
|
+
*
|
|
11
|
+
* Options:
|
|
12
|
+
* -p, --port <port> Port to listen on (default: 8080, or PORT env var)
|
|
13
|
+
* -h, --help Show this help message
|
|
14
|
+
*
|
|
15
|
+
* Examples:
|
|
16
|
+
* ghosttown
|
|
17
|
+
* ghosttown -p 3000
|
|
18
|
+
* ghosttown --port 3000
|
|
19
|
+
* PORT=3000 ghosttown
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import fs from 'fs';
|
|
23
|
+
import http from 'http';
|
|
24
|
+
import { homedir, networkInterfaces } from 'os';
|
|
25
|
+
import path from 'path';
|
|
26
|
+
import { fileURLToPath } from 'url';
|
|
27
|
+
|
|
28
|
+
// Node-pty for cross-platform PTY support
|
|
29
|
+
import pty from '@lydell/node-pty';
|
|
30
|
+
// WebSocket server
|
|
31
|
+
import { WebSocketServer } from 'ws';
|
|
32
|
+
|
|
33
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
34
|
+
const __dirname = path.dirname(__filename);
|
|
35
|
+
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// Parse CLI arguments
|
|
38
|
+
// ============================================================================
|
|
39
|
+
|
|
40
|
+
function parseArgs() {
|
|
41
|
+
const args = process.argv.slice(2);
|
|
42
|
+
let port = null;
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < args.length; i++) {
|
|
45
|
+
const arg = args[i];
|
|
46
|
+
|
|
47
|
+
if (arg === '-h' || arg === '--help') {
|
|
48
|
+
console.log(`
|
|
49
|
+
Usage: ghosttown [options]
|
|
50
|
+
|
|
51
|
+
Options:
|
|
52
|
+
-p, --port <port> Port to listen on (default: 8080, or PORT env var)
|
|
53
|
+
-h, --help Show this help message
|
|
54
|
+
|
|
55
|
+
Examples:
|
|
56
|
+
ghosttown
|
|
57
|
+
ghosttown -p 3000
|
|
58
|
+
ghosttown --port 3000
|
|
59
|
+
PORT=3000 ghosttown
|
|
60
|
+
`);
|
|
61
|
+
process.exit(0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (arg === '-p' || arg === '--port') {
|
|
65
|
+
const nextArg = args[i + 1];
|
|
66
|
+
if (!nextArg || nextArg.startsWith('-')) {
|
|
67
|
+
console.error(`Error: ${arg} requires a port number`);
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
port = parseInt(nextArg, 10);
|
|
71
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
72
|
+
console.error(`Error: Invalid port number: ${nextArg}`);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
i++; // Skip the next argument since we consumed it
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { port };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const cliArgs = parseArgs();
|
|
83
|
+
const HTTP_PORT = cliArgs.port || process.env.PORT || 8080;
|
|
84
|
+
|
|
85
|
+
// ============================================================================
|
|
86
|
+
// Locate ghosttown assets
|
|
87
|
+
// ============================================================================
|
|
88
|
+
|
|
89
|
+
function findAssets() {
|
|
90
|
+
// Assets are in the package root (one level up from bin/)
|
|
91
|
+
const packageRoot = path.join(__dirname, '..');
|
|
92
|
+
const distPath = path.join(packageRoot, 'dist');
|
|
93
|
+
const wasmPath = path.join(packageRoot, 'ghostty-vt.wasm');
|
|
94
|
+
|
|
95
|
+
if (!fs.existsSync(path.join(distPath, 'ghostty-web.js'))) {
|
|
96
|
+
console.error('Error: dist/ghostty-web.js not found.');
|
|
97
|
+
console.error('The package may not have been built correctly.');
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!fs.existsSync(wasmPath)) {
|
|
102
|
+
console.error('Error: ghostty-vt.wasm not found.');
|
|
103
|
+
console.error('The package may not have been built correctly.');
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { distPath, wasmPath };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { distPath, wasmPath } = findAssets();
|
|
111
|
+
|
|
112
|
+
// ============================================================================
|
|
113
|
+
// HTML Template
|
|
114
|
+
// ============================================================================
|
|
115
|
+
|
|
116
|
+
const HTML_TEMPLATE = `<!doctype html>
|
|
117
|
+
<html lang="en">
|
|
118
|
+
<head>
|
|
119
|
+
<meta charset="UTF-8" />
|
|
120
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
|
|
121
|
+
<title>ghosttown</title>
|
|
122
|
+
<style>
|
|
123
|
+
:root {
|
|
124
|
+
--vvh: 100vh;
|
|
125
|
+
--vv-offset-top: 0px;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
* {
|
|
129
|
+
margin: 0;
|
|
130
|
+
padding: 0;
|
|
131
|
+
box-sizing: border-box;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
html, body {
|
|
135
|
+
margin: 0;
|
|
136
|
+
padding: 0;
|
|
137
|
+
height: var(--vvh);
|
|
138
|
+
overflow: hidden;
|
|
139
|
+
overscroll-behavior: none;
|
|
140
|
+
touch-action: none;
|
|
141
|
+
transition: none;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
body {
|
|
145
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
146
|
+
background: #292c34;
|
|
147
|
+
padding: 8px 8px 14px 8px;
|
|
148
|
+
box-sizing: border-box;
|
|
149
|
+
position: fixed;
|
|
150
|
+
inset: 0;
|
|
151
|
+
height: var(--vvh);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.terminal-window {
|
|
155
|
+
width: 100%;
|
|
156
|
+
height: 100%;
|
|
157
|
+
background: #ededed;
|
|
158
|
+
display: flex;
|
|
159
|
+
flex-direction: column;
|
|
160
|
+
overflow: hidden;
|
|
161
|
+
box-shadow:
|
|
162
|
+
0 0 0 0.5px #65686e,
|
|
163
|
+
0 0 0 1px #74777c,
|
|
164
|
+
0 0 0 1.5px #020203,
|
|
165
|
+
0px 4px 10px 0px rgba(0, 0, 0, 0.5);
|
|
166
|
+
border-radius: 8px;
|
|
167
|
+
box-sizing: border-box;
|
|
168
|
+
transform: translateY(calc(var(--vv-offset-top) * -1));
|
|
169
|
+
transition: none;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.title-bar {
|
|
173
|
+
background: #292c34;
|
|
174
|
+
padding: 8px 16px 6px 10px;
|
|
175
|
+
display: flex;
|
|
176
|
+
align-items: center;
|
|
177
|
+
gap: 12px;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.traffic-lights {
|
|
181
|
+
display: flex;
|
|
182
|
+
gap: 8px;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.light {
|
|
186
|
+
width: 12px;
|
|
187
|
+
height: 12px;
|
|
188
|
+
border-radius: 50%;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.light.red { background: #ff5f56; }
|
|
192
|
+
.light.yellow { background: #ffbd2e; }
|
|
193
|
+
.light.green { background: #27c93f; }
|
|
194
|
+
|
|
195
|
+
.title {
|
|
196
|
+
color: #e5e5e5;
|
|
197
|
+
font-size: 13px;
|
|
198
|
+
font-weight: 500;
|
|
199
|
+
letter-spacing: 0.3px;
|
|
200
|
+
display: flex;
|
|
201
|
+
align-items: center;
|
|
202
|
+
gap: 8px;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
.title-separator { color: #666; }
|
|
206
|
+
.current-directory {
|
|
207
|
+
color: #888;
|
|
208
|
+
font-size: 12px;
|
|
209
|
+
font-weight: 400;
|
|
210
|
+
max-width: 400px;
|
|
211
|
+
overflow: hidden;
|
|
212
|
+
text-overflow: ellipsis;
|
|
213
|
+
white-space: nowrap;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.connection-status {
|
|
217
|
+
margin-left: auto;
|
|
218
|
+
font-size: 11px;
|
|
219
|
+
color: #888;
|
|
220
|
+
display: flex;
|
|
221
|
+
align-items: center;
|
|
222
|
+
gap: 6px;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.connection-dot {
|
|
226
|
+
width: 6px;
|
|
227
|
+
height: 6px;
|
|
228
|
+
border-radius: 50%;
|
|
229
|
+
background: #666;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.connection-dot.connected { background: #27c93f; }
|
|
233
|
+
|
|
234
|
+
#terminal-container {
|
|
235
|
+
flex: 1;
|
|
236
|
+
padding: 2px 0 2px 2px;
|
|
237
|
+
background: #292c34;
|
|
238
|
+
position: relative;
|
|
239
|
+
overflow: hidden;
|
|
240
|
+
min-height: 0;
|
|
241
|
+
touch-action: none;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#terminal-container canvas {
|
|
245
|
+
display: block;
|
|
246
|
+
touch-action: none;
|
|
247
|
+
}
|
|
248
|
+
</style>
|
|
249
|
+
</head>
|
|
250
|
+
<body>
|
|
251
|
+
<div class="terminal-window">
|
|
252
|
+
<div class="title-bar">
|
|
253
|
+
<div class="traffic-lights">
|
|
254
|
+
<span class="light red"></span>
|
|
255
|
+
<span class="light yellow"></span>
|
|
256
|
+
<span class="light green"></span>
|
|
257
|
+
</div>
|
|
258
|
+
<div class="title">
|
|
259
|
+
<span>ghosttown</span>
|
|
260
|
+
<span class="title-separator" id="title-separator" style="display: none">•</span>
|
|
261
|
+
<span class="current-directory" id="current-directory"></span>
|
|
262
|
+
</div>
|
|
263
|
+
<div class="connection-status">
|
|
264
|
+
<span class="connection-dot" id="connection-dot"></span>
|
|
265
|
+
<span id="connection-text">Disconnected</span>
|
|
266
|
+
</div>
|
|
267
|
+
</div>
|
|
268
|
+
<div id="terminal-container"></div>
|
|
269
|
+
</div>
|
|
270
|
+
|
|
271
|
+
<script type="module">
|
|
272
|
+
import { init, Terminal, FitAddon } from '/dist/ghostty-web.js';
|
|
273
|
+
|
|
274
|
+
let term;
|
|
275
|
+
let ws;
|
|
276
|
+
let fitAddon;
|
|
277
|
+
|
|
278
|
+
async function initTerminal() {
|
|
279
|
+
await init();
|
|
280
|
+
|
|
281
|
+
term = new Terminal({
|
|
282
|
+
cursorBlink: true,
|
|
283
|
+
fontSize: 12,
|
|
284
|
+
fontFamily: 'Monaco, Menlo, "Courier New", monospace',
|
|
285
|
+
theme: {
|
|
286
|
+
background: '#292c34',
|
|
287
|
+
foreground: '#d4d4d4',
|
|
288
|
+
},
|
|
289
|
+
smoothScrollDuration: 0,
|
|
290
|
+
scrollback: 10000,
|
|
291
|
+
scrollbarVisible: false,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
fitAddon = new FitAddon();
|
|
295
|
+
term.loadAddon(fitAddon);
|
|
296
|
+
|
|
297
|
+
term.open(document.getElementById('terminal-container'));
|
|
298
|
+
fitAddon.fit();
|
|
299
|
+
fitAddon.observeResize();
|
|
300
|
+
|
|
301
|
+
// Desktop: auto-focus. Mobile: focus only on tap.
|
|
302
|
+
const isCoarsePointer = window.matchMedia && window.matchMedia('(pointer: coarse)').matches;
|
|
303
|
+
if (!isCoarsePointer) {
|
|
304
|
+
term.focus();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Prevent page scroll on iOS
|
|
308
|
+
document.addEventListener('touchmove', (e) => {
|
|
309
|
+
const container = document.getElementById('terminal-container');
|
|
310
|
+
if (container && !container.contains(e.target)) {
|
|
311
|
+
e.preventDefault();
|
|
312
|
+
}
|
|
313
|
+
}, { passive: false });
|
|
314
|
+
|
|
315
|
+
// Mobile keyboard handling via visualViewport
|
|
316
|
+
{
|
|
317
|
+
const root = document.documentElement;
|
|
318
|
+
let watchRafId = null;
|
|
319
|
+
let vvhCurrent = 0;
|
|
320
|
+
let vvhTarget = 0;
|
|
321
|
+
let offsetCurrent = 0;
|
|
322
|
+
let offsetTarget = 0;
|
|
323
|
+
let lastTick = 0;
|
|
324
|
+
|
|
325
|
+
const readViewport = () => ({
|
|
326
|
+
height: window.visualViewport?.height ?? window.innerHeight,
|
|
327
|
+
offsetTop: window.visualViewport?.offsetTop ?? 0,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const applyVars = () => {
|
|
331
|
+
root.style.setProperty('--vvh', vvhCurrent + 'px');
|
|
332
|
+
root.style.setProperty('--vv-offset-top', offsetCurrent + 'px');
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const startKeyboardWatch = () => {
|
|
336
|
+
if (watchRafId !== null) return;
|
|
337
|
+
lastTick = performance.now();
|
|
338
|
+
|
|
339
|
+
const tick = () => {
|
|
340
|
+
const now = performance.now();
|
|
341
|
+
const dtMs = Math.max(1, now - lastTick);
|
|
342
|
+
lastTick = now;
|
|
343
|
+
|
|
344
|
+
const { height, offsetTop } = readViewport();
|
|
345
|
+
vvhTarget = height;
|
|
346
|
+
offsetTarget = offsetTop;
|
|
347
|
+
|
|
348
|
+
if (vvhCurrent === 0) vvhCurrent = vvhTarget;
|
|
349
|
+
|
|
350
|
+
const tauMs = 100;
|
|
351
|
+
const alpha = 1 - Math.exp(-dtMs / tauMs);
|
|
352
|
+
const deltaH = vvhTarget - vvhCurrent;
|
|
353
|
+
const deltaO = offsetTarget - offsetCurrent;
|
|
354
|
+
|
|
355
|
+
if (Math.abs(deltaH) > 1) vvhCurrent += deltaH * alpha;
|
|
356
|
+
else vvhCurrent = vvhTarget;
|
|
357
|
+
|
|
358
|
+
if (Math.abs(deltaO) > 0.5) offsetCurrent += deltaO * alpha;
|
|
359
|
+
else offsetCurrent = offsetTarget;
|
|
360
|
+
|
|
361
|
+
applyVars();
|
|
362
|
+
fitAddon.fit();
|
|
363
|
+
|
|
364
|
+
const stillAnimating = Math.abs(vvhTarget - vvhCurrent) > 0.5 || Math.abs(offsetTarget - offsetCurrent) > 0.5;
|
|
365
|
+
if (stillAnimating) {
|
|
366
|
+
watchRafId = requestAnimationFrame(tick);
|
|
367
|
+
} else {
|
|
368
|
+
watchRafId = null;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
watchRafId = requestAnimationFrame(tick);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// Initial setup
|
|
376
|
+
const { height, offsetTop } = readViewport();
|
|
377
|
+
vvhCurrent = height;
|
|
378
|
+
vvhTarget = height;
|
|
379
|
+
offsetCurrent = offsetTop;
|
|
380
|
+
offsetTarget = offsetTop;
|
|
381
|
+
applyVars();
|
|
382
|
+
|
|
383
|
+
window.addEventListener('resize', startKeyboardWatch);
|
|
384
|
+
window.addEventListener('focusin', startKeyboardWatch);
|
|
385
|
+
window.addEventListener('focusout', startKeyboardWatch);
|
|
386
|
+
window.visualViewport?.addEventListener('resize', startKeyboardWatch);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Handle terminal resize
|
|
390
|
+
term.onResize((size) => {
|
|
391
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
392
|
+
ws.send(JSON.stringify({ type: 'resize', cols: size.cols, rows: size.rows }));
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// Handle user input
|
|
397
|
+
term.onData((data) => {
|
|
398
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
399
|
+
ws.send(data);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Handle directory changes
|
|
404
|
+
const directoryElement = document.getElementById('current-directory');
|
|
405
|
+
const separatorElement = document.getElementById('title-separator');
|
|
406
|
+
term.onDirectoryChange((directory) => {
|
|
407
|
+
if (directoryElement && separatorElement) {
|
|
408
|
+
if (directory) {
|
|
409
|
+
directoryElement.textContent = directory;
|
|
410
|
+
separatorElement.style.display = 'inline';
|
|
411
|
+
} else {
|
|
412
|
+
directoryElement.textContent = '';
|
|
413
|
+
separatorElement.style.display = 'none';
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
connectWebSocket();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function connectWebSocket() {
|
|
422
|
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
423
|
+
const wsUrl = protocol + '//' + window.location.host + '/ws?cols=' + term.cols + '&rows=' + term.rows;
|
|
424
|
+
|
|
425
|
+
ws = new WebSocket(wsUrl);
|
|
426
|
+
|
|
427
|
+
ws.onopen = () => {
|
|
428
|
+
updateConnectionStatus(true);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
ws.onmessage = (event) => {
|
|
432
|
+
term.write(event.data);
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
ws.onerror = () => {
|
|
436
|
+
updateConnectionStatus(false);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
ws.onclose = () => {
|
|
440
|
+
updateConnectionStatus(false);
|
|
441
|
+
setTimeout(() => {
|
|
442
|
+
if (!ws || ws.readyState === WebSocket.CLOSED) {
|
|
443
|
+
connectWebSocket();
|
|
444
|
+
}
|
|
445
|
+
}, 3000);
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function updateConnectionStatus(connected) {
|
|
450
|
+
const dot = document.getElementById('connection-dot');
|
|
451
|
+
const text = document.getElementById('connection-text');
|
|
452
|
+
if (connected) {
|
|
453
|
+
dot.classList.add('connected');
|
|
454
|
+
text.textContent = 'Connected';
|
|
455
|
+
} else {
|
|
456
|
+
dot.classList.remove('connected');
|
|
457
|
+
text.textContent = 'Disconnected';
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
initTerminal();
|
|
462
|
+
</script>
|
|
463
|
+
</body>
|
|
464
|
+
</html>`;
|
|
465
|
+
|
|
466
|
+
// ============================================================================
|
|
467
|
+
// MIME Types
|
|
468
|
+
// ============================================================================
|
|
469
|
+
|
|
470
|
+
const MIME_TYPES = {
|
|
471
|
+
'.html': 'text/html',
|
|
472
|
+
'.js': 'application/javascript',
|
|
473
|
+
'.mjs': 'application/javascript',
|
|
474
|
+
'.css': 'text/css',
|
|
475
|
+
'.json': 'application/json',
|
|
476
|
+
'.wasm': 'application/wasm',
|
|
477
|
+
'.png': 'image/png',
|
|
478
|
+
'.svg': 'image/svg+xml',
|
|
479
|
+
'.ico': 'image/x-icon',
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
// ============================================================================
|
|
483
|
+
// HTTP Server
|
|
484
|
+
// ============================================================================
|
|
485
|
+
|
|
486
|
+
const httpServer = http.createServer((req, res) => {
|
|
487
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
488
|
+
const pathname = url.pathname;
|
|
489
|
+
|
|
490
|
+
// Serve index page
|
|
491
|
+
if (pathname === '/' || pathname === '/index.html') {
|
|
492
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
493
|
+
res.end(HTML_TEMPLATE);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Serve dist files
|
|
498
|
+
if (pathname.startsWith('/dist/')) {
|
|
499
|
+
const filePath = path.join(distPath, pathname.slice(6));
|
|
500
|
+
serveFile(filePath, res);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// Serve WASM file
|
|
505
|
+
if (pathname === '/ghostty-vt.wasm') {
|
|
506
|
+
serveFile(wasmPath, res);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// 404
|
|
511
|
+
res.writeHead(404);
|
|
512
|
+
res.end('Not Found');
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
function serveFile(filePath, res) {
|
|
516
|
+
const ext = path.extname(filePath);
|
|
517
|
+
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
518
|
+
|
|
519
|
+
fs.readFile(filePath, (err, data) => {
|
|
520
|
+
if (err) {
|
|
521
|
+
res.writeHead(404);
|
|
522
|
+
res.end('Not Found');
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
526
|
+
res.end(data);
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// ============================================================================
|
|
531
|
+
// WebSocket Server
|
|
532
|
+
// ============================================================================
|
|
533
|
+
|
|
534
|
+
const sessions = new Map();
|
|
535
|
+
|
|
536
|
+
function getShell() {
|
|
537
|
+
if (process.platform === 'win32') {
|
|
538
|
+
return process.env.COMSPEC || 'cmd.exe';
|
|
539
|
+
}
|
|
540
|
+
return process.env.SHELL || '/bin/bash';
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function createPtySession(cols, rows) {
|
|
544
|
+
const shell = getShell();
|
|
545
|
+
|
|
546
|
+
const ptyProcess = pty.spawn(shell, [], {
|
|
547
|
+
name: 'xterm-256color',
|
|
548
|
+
cols: cols,
|
|
549
|
+
rows: rows,
|
|
550
|
+
cwd: homedir(),
|
|
551
|
+
env: {
|
|
552
|
+
...process.env,
|
|
553
|
+
TERM: 'xterm-256color',
|
|
554
|
+
COLORTERM: 'truecolor',
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
return ptyProcess;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
562
|
+
|
|
563
|
+
httpServer.on('upgrade', (req, socket, head) => {
|
|
564
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
565
|
+
|
|
566
|
+
if (url.pathname === '/ws') {
|
|
567
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
568
|
+
wss.emit('connection', ws, req);
|
|
569
|
+
});
|
|
570
|
+
} else {
|
|
571
|
+
socket.destroy();
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
wss.on('connection', (ws, req) => {
|
|
576
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
577
|
+
const cols = Number.parseInt(url.searchParams.get('cols') || '80');
|
|
578
|
+
const rows = Number.parseInt(url.searchParams.get('rows') || '24');
|
|
579
|
+
|
|
580
|
+
const ptyProcess = createPtySession(cols, rows);
|
|
581
|
+
sessions.set(ws, { pty: ptyProcess });
|
|
582
|
+
|
|
583
|
+
ptyProcess.onData((data) => {
|
|
584
|
+
if (ws.readyState === ws.OPEN) {
|
|
585
|
+
ws.send(data);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
ptyProcess.onExit(({ exitCode }) => {
|
|
590
|
+
if (ws.readyState === ws.OPEN) {
|
|
591
|
+
ws.send(`\r\n\x1b[33mShell exited (code: ${exitCode})\x1b[0m\r\n`);
|
|
592
|
+
ws.close();
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
ws.on('message', (data) => {
|
|
597
|
+
const message = data.toString('utf8');
|
|
598
|
+
|
|
599
|
+
if (message.startsWith('{')) {
|
|
600
|
+
try {
|
|
601
|
+
const msg = JSON.parse(message);
|
|
602
|
+
if (msg.type === 'resize') {
|
|
603
|
+
ptyProcess.resize(msg.cols, msg.rows);
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
} catch (e) {
|
|
607
|
+
// Not JSON, treat as input
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
ptyProcess.write(message);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
ws.on('close', () => {
|
|
615
|
+
const session = sessions.get(ws);
|
|
616
|
+
if (session) {
|
|
617
|
+
session.pty.kill();
|
|
618
|
+
sessions.delete(ws);
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
ws.on('error', () => {
|
|
623
|
+
// Ignore socket errors
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// ============================================================================
|
|
628
|
+
// Startup
|
|
629
|
+
// ============================================================================
|
|
630
|
+
|
|
631
|
+
function getLocalIPs() {
|
|
632
|
+
const interfaces = networkInterfaces();
|
|
633
|
+
const ips = [];
|
|
634
|
+
for (const name of Object.keys(interfaces)) {
|
|
635
|
+
for (const iface of interfaces[name] || []) {
|
|
636
|
+
if (iface.family === 'IPv4' && !iface.internal) {
|
|
637
|
+
ips.push(iface.address);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return ips;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function printBanner(url) {
|
|
645
|
+
const localIPs = getLocalIPs();
|
|
646
|
+
console.log('\n' + '═'.repeat(50));
|
|
647
|
+
console.log(' 👻 ghosttown');
|
|
648
|
+
console.log('═'.repeat(50));
|
|
649
|
+
console.log(`\n Open: ${url}`);
|
|
650
|
+
if (localIPs.length > 0) {
|
|
651
|
+
console.log(` Network:`);
|
|
652
|
+
for (const ip of localIPs) {
|
|
653
|
+
console.log(` http://${ip}:${HTTP_PORT}`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
console.log(`\n Shell: ${getShell()}`);
|
|
657
|
+
console.log(` Home: ${homedir()}`);
|
|
658
|
+
console.log('\n Warning: This server provides shell access.');
|
|
659
|
+
console.log(' Only use for local development.\n');
|
|
660
|
+
console.log('═'.repeat(50));
|
|
661
|
+
console.log(' Press Ctrl+C to stop.\n');
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
process.on('SIGINT', () => {
|
|
665
|
+
console.log('\n\nShutting down...');
|
|
666
|
+
for (const [ws, session] of sessions.entries()) {
|
|
667
|
+
session.pty.kill();
|
|
668
|
+
ws.close();
|
|
669
|
+
}
|
|
670
|
+
wss.close();
|
|
671
|
+
process.exit(0);
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
httpServer.listen(HTTP_PORT, '0.0.0.0', () => {
|
|
675
|
+
printBanner(`http://localhost:${HTTP_PORT}`);
|
|
676
|
+
});
|
|
Binary file
|