-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.html
More file actions
454 lines (404 loc) · 16.6 KB
/
app.html
File metadata and controls
454 lines (404 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, interactive-widget=resizes-content">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="./manifest.json">
<link rel="stylesheet" href="./gun/kit/web.css"><script src="./gun/kit/web.js"></script>
<form id="sign" onsubmit="cop.sign()">
<input id="host" name="host" placeholder="host:" class="fog sap lip">
<input id="pass" name="pass" type="password" placeholder="pass:" class="fog sap lip">
<button type="submit"></button>
</form>
<dialog id="vmDialog" class="fog sap lip" style="background: var(--mood, #000); color: var(--pop, lime); border: 1px solid var(--pop, lime); border-radius: 1em; padding: 2em; z-index: 1000;">
<p style="margin-top: 0; font-family: monospace;">Host failed to connect.</p>
<p style="font-family: monospace;">Do you want to boot a Linux VM in the browser?</p>
<div style="display: flex; justify-content: flex-end; gap: 1em; margin-top: 2em; font-family: monospace;">
<button onclick="vmDialog.close('no')" style="background: transparent; color: inherit; border: 1px solid var(--pop, lime); padding: 0.5em 1em; border-radius: 0.5em; cursor: pointer;">No</button>
<button onclick="vmDialog.close('yes')" style="background: var(--pop, lime); color: var(--mood, #000); border: none; padding: 0.5em 1em; border-radius: 0.5em; cursor: pointer; font-weight: bold;">Yes</button>
</div>
</dialog>
<div id="vmBoot" style="display:none; position:fixed; bottom:4em; left:1em; right:1em; z-index:999; font-family:monospace; color:var(--pop, lime); font-size:0.8em; opacity:0.7; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"></div>
<style>
:root { --fill: var(--mood); }
body, html { margin: 0; padding: 0; height: 100%; overflow: hidden; back-ground-image: url('https://picsum.photos/1200/900'); }
body { display: flex; flex-direction: column; }
#sign { flex-shrink: 0; }
iframe { width: 100%; flex: 1; min-height: 0; height: 0 !important; border: none; display: block; }
input::placeholder { opacity: 1; }
</style>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').catch(console.error);
}
</script>
<script>;(function(cop){
host.focus();
console.off=1;
cop.sign = async ()=>{ if(cop.ws){ return }
var opened = false;
cop.ws = new WebSocket((localStorage.host=host.value).replace(/^http/,'ws')||'ws://localhost:8022');
cop.ws.onopen = ()=>{
opened = true;
console.log('ws open');
cop.ws.send(localStorage.pass=pass.value);
location.path = 'chat.html';
};
cop.ws.onmessage = (eve) => {
//console.log('app ws msg');//, eve.data);
kit.say(eve.data,'chat');
};
cop.ws.onerror = (error) => {
console.error('ws err:', error);
};
cop.ws.onclose = (eve) => { cop.ws=0;
console.log('ws close:', eve.code, 'Reason:', eve.reason);
if(!opened) {
vmDialog.showModal();
vmDialog.onclose = function() {
if(vmDialog.returnValue === 'yes') {
bootVM();
}
};
} else {
cop.wait = cop.wait || 1000;
kit.say('Reconnecting...\n', 'chat');
setTimeout(() => {
cop.wait = Math.min(cop.wait * 2, 30000);
cop.sign();
}, cop.wait);
}
};
cop.ws.onopen = ((fn) => () => { cop.wait = 1000; fn(); })(cop.ws.onopen);
}
function bootVM() {
var VER = 's1', CDN = "https://cdn.jsdelivr.net/gh/abenezermario/v86-alpine@main";
var db = {};
db.save = (buf, cb) => {
var r = indexedDB.open('cop', 1);
r.onupgradeneeded = (e) => { e.target.result.createObjectStore('vm'); };
r.onsuccess = (e) => {
var tx = e.target.result.transaction('vm', 'readwrite');
tx.objectStore('vm').put(buf, VER);
tx.oncomplete = () => { cb && cb(); };
};
r.onerror = () => { cb && cb(); };
};
db.load = (cb) => {
var r = indexedDB.open('cop', 1);
r.onupgradeneeded = (e) => { e.target.result.createObjectStore('vm'); };
r.onsuccess = (e) => {
var tx = e.target.result.transaction('vm', 'readonly');
var req = tx.objectStore('vm').get(VER);
req.onsuccess = () => { cb(req.result || null); };
req.onerror = () => { cb(null); };
};
r.onerror = () => { cb(null); };
};
vmBoot.style.display = 'block';
vmBoot.textContent = 'Loading VM...';
var s = document.createElement('script');
s.src = CDN + "/build/libv86.js";
s.onload = () => {
db.load((state) => {
vmBoot.textContent = state ? 'Restoring VM...' : 'Downloading VM...';
var cfg = {
wasm_path: CDN + "/build/v86.wasm",
memory_size: 256 * 1024 * 1024,
vga_memory_size: 8 * 1024 * 1024,
bios: { url: CDN + "/bios/seabios.bin" },
vga_bios: { url: CDN + "/bios/vgabios.bin" },
filesystem: {
baseurl: CDN + "/images/alpine-rootfs-flat",
basefs: CDN + "/images/alpine-fs.json",
},
bzimage_initrd_from_filesystem: true,
cmdline: "rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable mitigations=off random.trust_cpu=on console=ttyS0",
net_device: { type: "virtio", relay_url: "fetch" },
autostart: true,
disable_keyboard: true,
disable_mouse: true,
};
if (state) { cfg.initial_state = { buffer: state }; }
var emulator = window.emulator = new V86(cfg);
var emulatorReady = false;
var commandQueue = [];
var boot = state ? 3 : 0;
// Snapshot restore — set ready, wait for chat.html iframe to register
if (state) {
emulator.add_listener("emulator-ready", () => {
vmBoot.textContent = '';
vmBoot.style.display = 'none';
emulatorReady = true;
var flush = () => {
if (kit.views && kit.views.size > 0) {
while(commandQueue.length > 0) {
emulator.serial0_send(commandQueue.shift());
}
} else { requestAnimationFrame(flush); }
};
flush();
});
}
cop.ws = {
send: function(cmd) {
console.log("Sending to VM:", JSON.stringify(cmd));
// Ensure commands end with a newline so the shell processes them
if (cmd && !cmd.endsWith('\n') && !cmd.endsWith('\r')) {
cmd += '\n';
}
// Intercept commands that need network — route through browser fetch
var trimmed = (cmd || '').replace(/[\r\n]+$/, '').trim();
var m = trimmed.match(/^git\s+clone\s+(?:-b\s+(\S+)\s+)?(?:https?:\/\/)?github\.com\/([^\/\s]+)\/([^\s]+?)(?:\s+(\S+))?\s*$/);
if (m && emulatorReady) {
browserGitClone(m[2], m[3].replace(/\.git$/, ''), m[1] || '', m[4] || '');
return;
}
if (emulatorReady && pkg.run(trimmed, emulator)) return;
if (!emulatorReady) {
commandQueue.push(cmd);
} else {
emulator.serial0_send(cmd);
}
}
};
while(preBootQueue.length > 0) {
cop.ws.send(preBootQueue.shift());
}
var buf = "";
emulator.add_listener("serial0-output-byte", (byte) => {
var c = String.fromCharCode(byte);
buf += c;
if (boot < 3) {
// Show live boot logs in vmBoot div (outside chat — no parser impact)
var lines = buf.replace(/\x1b\[[^m]*m/g, '').split('\n');
var last = lines[lines.length - 1] || lines[lines.length - 2] || '';
if (last.trim()) vmBoot.textContent = last.trim().slice(0, 80);
}
if (boot === 0 && buf.includes("login:")) {
boot = 1;
emulator.serial0_send("root\n");
buf = "";
return;
}
if (boot === 1 && buf.includes("~#")) {
boot = 2;
emulator.serial0_send("export PS1='$ '\n");
buf = "";
return;
}
if (boot === 2 && /\n\$ /.test(buf)) {
vmBoot.textContent = '';
vmBoot.style.display = 'none';
boot = 3;
buf = "";
emulatorReady = true;
emulator.save_state().then((s) => { db.save(s); console.log('[snap] saved', s.byteLength, 'bytes'); });
while(commandQueue.length > 0) {
emulator.serial0_send(commandQueue.shift());
}
return;
}
if (window.serialTimeout) clearTimeout(window.serialTimeout);
window.serialTimeout = setTimeout(function() {
if (emulatorReady) {
// Strip terminal queries (DSR cursor-position) — meaningless for chat
var out = buf.replace(/\x1b\[6n/g, '');
if (out) {
console.log("VM output:", JSON.stringify(out));
kit.say(out, 'chat');
}
}
buf = "";
}, 50);
});
// Visual boot progress (vmBoot div — outside chat, no parser impact)
var dl = {};
emulator.add_listener("download-progress", (e) => {
if (!e.file_name || dl[e.file_name]) return;
dl[e.file_name] = 1;
var short = e.file_name.split('/').pop();
vmBoot.textContent = 'Loading ' + short;
});
emulator.add_listener("download-error", (e) => {
vmBoot.textContent = 'Error: ' + (e.file_name || e.message || e);
console.error("Download error:", e);
});
location.path = 'chat.html';
}); // db.load
};
document.head.appendChild(s);
}
// --- Browser-side git clone via GitHub REST API (CORS-safe) ---
function ensureDir(emu, path) {
var parts = path.split('/').filter(Boolean);
var current = '';
for (var i = 0; i < parts.length; i++) {
current += '/' + parts[i];
var info = emu.fs9p.SearchPath(current);
if (info.id === -1) {
var parentPath = '/' + parts.slice(0, i).join('/');
if (!parentPath) parentPath = '/';
var parentInfo = emu.fs9p.SearchPath(parentPath);
if (parentInfo.id !== -1) {
emu.fs9p.CreateDirectory(parts[i], parentInfo.id);
}
}
}
}
async function browserGitClone(owner, repo, branch, dest) {
var emu = window.emulator;
dest = dest || '/root/' + repo;
if (dest.charAt(0) !== '/') dest = '/root/' + dest;
var say = function(msg) { kit.say(msg, 'chat'); };
say("Cloning into '" + (dest.split('/').pop()) + "'...\n");
try {
// Try the requested branch, then main, then master
var branches = branch ? [branch, 'main', 'master'] : ['main', 'master'];
var tree, usedBranch;
for (var bi = 0; bi < branches.length; bi++) {
var res = await fetch('https://api.github.com/repos/' + owner + '/' + repo + '/git/trees/' + branches[bi] + '?recursive=1');
if (res.ok) {
tree = await res.json();
usedBranch = branches[bi];
break;
}
if (res.status === 403) throw new Error('API rate limit exceeded (60/hr unauthenticated)');
}
if (!tree) throw new Error('repository \'' + owner + '/' + repo + '\' not found');
if (tree.truncated) say("warning: tree truncated (large repo), some files may be missing\n");
var files = tree.tree.filter(function(t) { return t.type === 'blob'; });
var dirs = tree.tree.filter(function(t) { return t.type === 'tree'; });
var total = files.length;
say("remote: Enumerating objects: " + tree.tree.length + ", done.\n");
// Create directory structure in 9P
ensureDir(emu, dest);
dirs.forEach(function(d) { ensureDir(emu, dest + '/' + d.path); });
// Download files in parallel batches of 8
var done = 0;
for (var i = 0; i < files.length; i += 8) {
var batch = files.slice(i, i + 8);
await Promise.all(batch.map(async function(f) {
var url = 'https://raw.githubusercontent.com/' + owner + '/' + repo + '/' + usedBranch + '/' + f.path;
var r = await fetch(url);
if (!r.ok) { say("warning: skipped " + f.path + " (" + r.status + ")\n"); return; }
var data = new Uint8Array(await r.arrayBuffer());
await emu.create_file(dest + '/' + f.path, data);
done++;
if (done % 25 === 0 || done === total) {
say("\nReceiving objects: " + Math.round(done / total * 100) + "% (" + done + "/" + total + ")");
}
}));
}
say("\nReceiving objects: 100% (" + total + "/" + total + "), done.\n");
// Trigger a real ls in the VM so the shell prompt returns naturally
emu.serial0_send("ls " + dest + "\n");
} catch(e) {
say("fatal: " + e.message + "\n");
// Send empty command so shell prompt comes back
emu.serial0_send("\n");
}
}
// --- Browser-side package install via CORS-friendly CDN ---
;(function(){
var say = (msg) => { kit.say(msg, 'chat'); };
var pkg = window.pkg = {};
pkg.run = function(cmd, emu) {
var m;
if (m = cmd.match(/^npm\s+install\s+(.+)/)) { pkg.npm(m[1].trim().split(/\s+/), emu); return 1; }
if (m = cmd.match(/^apk\s+add\s+(.+)/)) { pkg.apk(m[1].trim().split(/\s+/), emu); return 1; }
};
// npm install — resolve via registry (CORS *), download files via jsdelivr
pkg.got = {};
pkg.one = async function(name, ver, emu, depth, base) {
if (pkg.got[name]) return;
pkg.got[name] = 1;
var pad = ' '.repeat(depth || 0);
// Resolve version via npm registry (supports 'latest' properly)
try {
var res = await fetch("https://registry.npmjs.org/" + name + "/latest");
if (!res.ok) res = await fetch("https://registry.npmjs.org/" + name);
if (!res.ok) { console.log('[pkg]', pad + 'skip', name); return; }
var meta = await res.json();
ver = meta.version || (meta['dist-tags'] || {}).latest;
if (!ver) { console.log('[pkg]', pad + 'skip', name, 'no ver'); return; }
} catch(e) { console.log('[pkg]', pad + 'err', name, e.message); return; }
console.log('[pkg]', pad + name + '@' + ver);
say(pad + "+ " + name + "@" + ver + "\n");
// Get file tree from jsdelivr (CORS *)
var tree = await fetch("https://data.jsdelivr.com/v1/packages/npm/" + name + "@" + ver);
if (!tree.ok) { console.log('[pkg]', pad + 'tree fail', name); return; }
var files = [];
var flat = (arr, pre) => {
(arr || []).forEach((f) => {
var p = pre + '/' + f.name;
if (f.type === 'directory') flat(f.files, p);
else files.push(p);
});
};
flat((await tree.json()).files, '');
var dest = base + '/node_modules/' + name;
ensureDir(emu, dest);
// Download files in batches of 12
for (var j = 0; j < files.length; j += 12) {
var batch = files.slice(j, j + 12);
await Promise.all(batch.map(async (f) => {
try {
var r = await fetch("https://cdn.jsdelivr.net/npm/" + name + "@" + ver + f);
if (!r.ok) return;
var data = new Uint8Array(await r.arrayBuffer());
var full = dest + f;
ensureDir(emu, full.slice(0, full.lastIndexOf('/')));
await emu.create_file(full, data);
} catch(e) {}
}));
}
// Recurse into deps
var deps = Object.keys(meta.dependencies || {});
for (var k = 0; k < deps.length; k++) {
await pkg.one(deps[k], 'latest', emu, (depth || 0) + 1, base);
}
};
pkg.npm = async function(names, emu) {
pkg.got = {};
var base = '/root';
var t = Date.now();
say("npm install " + names.join(' ') + "\n");
for (var i = 0; i < names.length; i++) {
var raw = names[i].replace(/^--.*/, '');
if (!raw) continue;
var at = raw.lastIndexOf('@');
var name = at > 0 ? raw.slice(0, at) : raw;
var ver = at > 0 ? raw.slice(at + 1) : 'latest';
try {
await pkg.one(name, ver, emu, 0, base);
} catch(e) {
say("ERR " + e.message + "\n");
}
}
var count = Object.keys(pkg.got).length;
var sec = ((Date.now() - t) / 1000).toFixed(1);
say("added " + count + " packages in " + sec + "s\n");
emu.serial0_send("echo done\n");
};
// apk add — needs a CORS mirror (future: abenezermario/apk repo)
pkg.apk = async function(names, emu) {
say("apk add " + names.join(' ') + "\n");
say("note: runtime apk is not yet wired to a CORS mirror\n");
say("to add packages, edit cop-vm/Dockerfile and rebuild\n");
emu.serial0_send("\n");
};
}());
(pass.value = localStorage.pass||'') && cop.sign();
var preBootQueue = [];
kit.ear('host',(eve)=>{
var cmd = eve.detail||eve.data;
if('string' != typeof cmd){ return }
if(!cop.ws){
preBootQueue.push(cmd);
return;
}
cop.ws.send(cmd);
});
}(window.cop = {}));
</script>