index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <!DOCTYPE html>
  2. <html lang="en" style="height:100%;">
  3. <meta property="og:image" content="https://webvm.io/assets/reddit.png"/>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=1100, initial-scale=1.0">
  7. <title>WebVM - Linux virtualization in WebAssembly</title>
  8. <meta name="description" content="Server-less virtual machine, networking included, running browser-side in HTML5/WebAssembly. Code in any programming language inside this Linux terminal.">
  9. <meta name="keywords" content="WebVM, Virtual Machine, CheerpX, x86 virtualization, WebAssembly, Tailscale, JIT">
  10. <meta property="og:title" content="WebVM - Linux virtualization in WebAssembly" />
  11. <meta property="og:type" content="website" />
  12. <meta property="og:site_name" content="WebVM"/>
  13. <meta property="og:image" content="https://webvm.io/assets/social.png" />
  14. <meta name="twitter:card" content="summary_large_image" />
  15. <meta name="twitter:site" content="@leaningtech" />
  16. <meta name="twitter:title" content="WebVM - Linux virtualization in WebAssembly" />
  17. <meta name="twitter:description" content="Server-less virtual machine, networking included, running browser-side in HTML5/WebAssembly. Code in any programming language inside this Linux terminal.">
  18. <meta name="twitter:image" content="https://webvm.io/assets/social.png" />
  19. <!-- Apple iOS web clip compatibility tags -->
  20. <meta name="application-name" content="WebVM" />
  21. <meta name="apple-mobile-web-app-title" content="WebVM" />
  22. <meta name="apple-mobile-web-app-capable" content="yes" />
  23. <meta name="mobile-web-app-capable" content="yes" />
  24. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  25. <link rel="shortcut icon" href="./tower.ico">
  26. <link rel="preconnect" href="https://fonts.googleapis.com">
  27. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  28. <link rel="stylesheet" id="us-fonts-css" href="https://fonts.googleapis.com/css?family=Montserrat%3A300%2C400%2C500%2C600%2C700&amp;display=swap&amp;ver=6.0.2" media="all">
  29. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />
  30. <link rel="stylesheet" href="./xterm/xterm.css" />
  31. <link rel="stylesheet" href="./scrollbar.css" />
  32. <style>.github-fork-ribbon:before { background-color: #ea1e69; }</style>
  33. <!-- Serviceworker script that adds the COI and CORS headers to the response headers in cases where the server does not support it. -->
  34. <script src="serviceWorker.js"></script>
  35. <script src="./xterm/xterm.js"></script>
  36. <script src="./xterm/xterm-addon-fit.js"></script>
  37. <script src="./xterm/xterm-addon-web-links.js"></script>
  38. <script src="network.js"></script>
  39. <script defer data-domain="webvm.io" src="https://plausible.leaningtech.com/js/script.js"></script>
  40. </head>
  41. <body style="margin:0;height:100%;background:black;color:white;overflow:hidden; display:flex; flex-direction: column; justify-content: space-between; height: 100%;">
  42. <a class="github-fork-ribbon right-bottom" href="https://github.com/leaningtech/webvm/" target="_blank" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
  43. <div>
  44. <div style="padding-top: 0.7em;padding-bottom: 0.7em;font-size: 0.3em; font-weight: 200;vertical-align:center;height: 120px;">
  45. <div style="margin-left: 20px; height: 100%; display: flex; align-items: center; justify-content: space-between;">
  46. <pre style="font-family: monospace; font-weight: 600; font-size: large; color: #ad7fa8;">
  47. __ __ _ __ ____ __
  48. \ \ / /__| |_\ \ / / \/ |
  49. \ \/\/ / -_) '_ \ V /| |\/| |
  50. \_/\_/\___|_.__/\_/ |_| |_|
  51. </pre>
  52. <div style="height:100%;display: flex; flex-direction: column;justify-content: space-between;">
  53. <div style="padding-top: 0.7em;font-size: 0.3em; font-weight: 200;vertical-align:center;height:50px;">
  54. <div style="margin-right: 10px; margin-left: 20px; height: 100%; display: flex; align-items: center; justify-content: flex-end;gap: 50px;">
  55. <div style="padding-top: 0.7em;font-size: 0.3em; font-weight: 200;vertical-align:center;">
  56. <a href="https://leaningtech.com" style="text-decoration: none; height: 100%;" target="_blank">
  57. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  58. <span>Made with &#10084;&#65039; by </span>
  59. <img src="assets/leaningtech.png" height="40px" style="margin-left: 5px;">
  60. </div>
  61. </a>
  62. </div>
  63. </div>
  64. </div>
  65. <div style="padding-top: 0.7em;font-size: 0.3em; font-weight: 200;vertical-align:center;height:50px;">
  66. <div style="margin-right: 10px; margin-left: 20px; height: 100%; display: flex; align-items: center; justify-content: flex-end;gap: 35px;">
  67. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: small; height: 100%; display: flex; align-items: center;">
  68. <a href="https://cheerpx.io/hackathon" style="color: white"><span>Join WebVM: The hackathon!</span></a>
  69. <span id="hackathon_link" style="margin-left: 7px;"></span>
  70. </div>
  71. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  72. <span>CPU </span>
  73. <span id="cpuactivity" style="margin-left: 7px;">&#x1F7E2;</span>
  74. </div>
  75. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  76. <span>HDD </span>
  77. <span id="hddactivity" style="margin-left: 7px;">&#x1F7E2;</span>
  78. </div>
  79. <a id="loginLink" style="user-select: text ;text-decoration: none; height: 100%;">
  80. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  81. <div style="position: relative;">
  82. <span style="cursor: pointer" id="networkStatus">Connect via Tailscale </span>
  83. <span style="cursor: pointer; position: absolute; right: 0px; visibility: hidden;" id="ipCopied">Copied! </span>
  84. </div>
  85. <img src="assets/tailscale.svg" height="35px" style="margin-left: 7px;">
  86. </div>
  87. </a>
  88. <a href="https://discord.gg/yTNZgySKGa" style="text-decoration: none; height: 100%;" target="_blank">
  89. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  90. <span>Join Discord </span>
  91. <img src="assets/discord-mark-blue.svg" height="35px" style="margin-left: 7px;">
  92. </div>
  93. </a>
  94. <a href="https://github.com/leaningtech/webvm/issues" style="text-decoration: none; height: 100%;" target="_blank">
  95. <div style="color: white; font-family: montserrat; font-weight: 400; font-size: large; height: 100%; display: flex; align-items: center;">
  96. <span>GitHub Issues </span>
  97. <img src="assets/github-mark-white.svg" height="35px" style="margin-left: 5px;">
  98. </div>
  99. </a>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. <div style="flex-grow:0; flex-shrink: 0; height:1px; width: 100%; background-color: white;">
  107. </div>
  108. <main style="display: flex; flex-direction: row; justify-content: space-between; margin: 5px; height: 100%;">
  109. <div style="flex-grow:1; height:100%;display:inline-block;margin:0;" class="scrollbar" id="console">
  110. </div>
  111. </main>
  112. <script>
  113. //Utility namespace to group all functionality related to printing (both error and non error) messages
  114. const color= "\x1b[1;35m";
  115. const bold= "\x1b[1;37m";
  116. const underline= "\x1b[94;4m";
  117. const normal= "\x1b[0m";
  118. var printOnTerm = {
  119. getAsciiText: function ()
  120. {
  121. var text = [
  122. "+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+",
  123. "| |",
  124. "| WebVM is a server-less virtual Linux environment running fully client-side |",
  125. "| in HTML5/WebAssembly. |",
  126. "| |",
  127. "| WebVM is powered by the CheerpX virtualization engine, which enables safe, |",
  128. "| sandboxed client-side execution of x86 binaries on any browser. |",
  129. "| |",
  130. "| CheerpX includes an x86-to-WebAssembly JIT compiler, a virtual block-based |",
  131. "| file system, and a Linux syscall emulator. |",
  132. "| |",
  133. "| [NEW!] Your own WebVM with custom images via Dockerfile: |",
  134. "| |",
  135. "| " + underline + "https://leaningtech.com/mini-webvm-your-linux-box-from-dockerfile-via-wasm" + normal +" |",
  136. "| |",
  137. "| |",
  138. "| Join our Discord server for updates: |",
  139. "| |",
  140. "| " + underline + "https://discord.leaningtech.com" + normal + " |",
  141. "| |",
  142. "+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+",
  143. "",
  144. " Welcome to WebVM. If unsure, try these examples:",
  145. "",
  146. " python3 examples/python3/fibonacci.py ",
  147. " gcc -o helloworld examples/c/helloworld.c && ./helloworld",
  148. " objdump -d ./helloworld | less -M",
  149. " vim examples/c/helloworld.c",
  150. " curl --max-time 15 parrot.live # requires networking",
  151. "",
  152. ];
  153. return text;
  154. },
  155. getSharedArrayBufferMissingMessage: function ()
  156. {
  157. const isCustom = window.location.hostname !== "webvm.io";
  158. const isSecureContext = window.isSecureContext;
  159. const text = [
  160. "",
  161. "",
  162. color + "CheerpX could not start" + normal,
  163. "",
  164. "CheerpX uses SharedArrayBuffer, which is not available right now.",
  165. "",
  166. !isSecureContext && " - This page is not in a secure context. Serve over HTTPS or WSS.",
  167. !isSecureContext && " " + underline + "https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts" + normal,
  168. isCustom && " - The document is not cross-origin isolated.",
  169. isCustom && " " + underline + "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements" + normal,
  170. " - Your browser might not support SharedArrayBuffer.",
  171. " Since 2022, all major browsers support this feature.",
  172. " " + underline + "https://caniuse.com/sharedarraybuffer" + normal,
  173. ].filter(Boolean);
  174. return text;
  175. },
  176. getErrorMessage: function (error_message)
  177. {
  178. const text = [
  179. "",
  180. "",
  181. color + "CheerpX could not start" + normal,
  182. "",
  183. "CheerpX internal error message is:",
  184. error_message,
  185. "",
  186. "",
  187. "CheerpX is expected to work with recent desktop versions of Chrome, Edge, Firefox and Safari",
  188. "",
  189. "",
  190. "Give it a try from a desktop version / another browser!",
  191. ]
  192. return text;
  193. },
  194. printMessage: function (text) {
  195. for (var i=0; i<text.length; i++)
  196. {
  197. term.write(text[i]);
  198. term.write('\n');
  199. }
  200. },
  201. printError: function (message)
  202. {
  203. this.printMessage(message);
  204. term.write("\n\n");
  205. function writeCustom(something)
  206. {
  207. term.write(something);
  208. }
  209. },
  210. };
  211. var consoleDiv = document.getElementById("console");
  212. //xterm.js related logic
  213. var term = new Terminal({cursorBlink:true,convertEol:true, fontFamily:"monospace", fontWeight: 400, fontWeightBold: 700});
  214. var fitAddon = new FitAddon.FitAddon();
  215. term.loadAddon(fitAddon);
  216. var linkAddon = new WebLinksAddon.WebLinksAddon();
  217. term.loadAddon(linkAddon);
  218. term.open(consoleDiv);
  219. term.scrollToTop();
  220. fitAddon.fit();
  221. window.addEventListener("resize", function(ev){fitAddon.fit();}, false);
  222. term.focus();
  223. var cxReadFunc = null;
  224. function writeData(buf)
  225. {
  226. term.write(new Uint8Array(buf));
  227. }
  228. function readData(str)
  229. {
  230. if(cxReadFunc == null)
  231. return;
  232. for(var i=0;i<str.length;i++)
  233. cxReadFunc(str.charCodeAt(i));
  234. }
  235. term.onData(readData);
  236. function hddCallback(state)
  237. {
  238. var h = document.getElementById("hddactivity");
  239. if(state == "ready")
  240. h.textContent = "\u{1F7E2}";
  241. else
  242. h.textContent = "\u{1F7E0}";
  243. }
  244. function cpuCallback(state)
  245. {
  246. var h = document.getElementById("cpuactivity");
  247. if(state == "ready")
  248. h.textContent = "\u{1F7E2}";
  249. else
  250. h.textContent = "\u{1F7E0}";
  251. }
  252. //Actual CheerpX and bash specific logic
  253. async function runBash()
  254. {
  255. // cmd, cwd, args and env are replaced by the Github actions workflow.
  256. var cmd = CMD;
  257. var args = ARGS;
  258. var env = ENV;
  259. var cwd = CWD;
  260. var device_type = DEVICE_TYPE;
  261. var image_url = IMAGE_URL;
  262. // Reasonable defaults for local deployments
  263. // var cmd = "/bin/bash";
  264. // var args = ["--login"];
  265. // var env = ["HOME=/home/user", "TERM=xterm", "USER=user", "SHELL=/bin/bash", "EDITOR=vim", "LANG=en_US.UTF-8", "LC_ALL=C"];
  266. // var cwd = "/home/user";
  267. // var device_type = "bytes";
  268. // var image_url = "/your_local_image.ext2";
  269. const structure = {
  270. cmd: cmd,
  271. args: args,
  272. env: env,
  273. cwd: cwd
  274. }
  275. if (typeof SharedArrayBuffer === "undefined")
  276. {
  277. printOnTerm.printError(printOnTerm.getSharedArrayBufferMissingMessage());
  278. return;
  279. }
  280. let networkInterface = setupNetworkInterface();
  281. async function runTest(cx)
  282. {
  283. var processCount = 0;
  284. function handleProcessCreated()
  285. {
  286. processCount++;
  287. plausible(`Process started: ${processCount}`);
  288. if(processCount == 5)
  289. {
  290. // Make sure no further event is reported
  291. cx.unregisterCallback("processCreated", handleProcessCreated);
  292. }
  293. }
  294. cx.registerCallback("processCreated", handleProcessCreated);
  295. cx.registerCallback("cpuActivity", cpuCallback);
  296. cx.registerCallback("diskActivity", hddCallback);
  297. registerNetworkLogin(cx, networkInterface);
  298. term.scrollToBottom();
  299. async function cxLogAndRun(cheerpx, cmd, args, env)
  300. {
  301. await cheerpx.run(cmd, args, env);
  302. printOnTerm.printMessage(" ");
  303. }
  304. cxReadFunc = cx.setCustomConsole(writeData, term.cols, term.rows);
  305. function preventDefaults (e) {
  306. e.preventDefault()
  307. e.stopPropagation()
  308. }
  309. consoleDiv.addEventListener("dragover", preventDefaults, false);
  310. consoleDiv.addEventListener("dragenter", preventDefaults, false);
  311. consoleDiv.addEventListener("dragleave", preventDefaults, false);
  312. consoleDiv.addEventListener("drop", preventDefaults, false);
  313. var opts = {env:structure.env, cwd:structure.cwd, uid: 1000, gid: 1000};
  314. while (true)
  315. {
  316. await cxLogAndRun(cx, structure.cmd, structure.args, opts);
  317. }
  318. }
  319. function failCallback(err)
  320. {
  321. printOnTerm.printError(printOnTerm.getErrorMessage(err));
  322. }
  323. // The device url and type are replaced by Github Actions.
  324. var blockDevice;
  325. switch (device_type)
  326. {
  327. case "cloud":
  328. try
  329. {
  330. blockDevice = await CheerpX.CloudDevice.create(image_url);
  331. }
  332. catch(e)
  333. {
  334. // Report the failure and try again with plain HTTP
  335. var wssProtocol = "wss:";
  336. if(image_url.startsWith(wssProtocol))
  337. {
  338. // WebSocket protocol failed, try agin using plain HTTP
  339. plausible("WS Disk failure");
  340. image_url = "https:" + image_url.substr(wssProtocol.length);
  341. blockDevice = await CheerpX.CloudDevice.create(image_url);
  342. }
  343. else
  344. {
  345. // No other recovery option
  346. throw e;
  347. }
  348. }
  349. break;
  350. case "bytes":
  351. try
  352. {
  353. blockDevice = await CheerpX.HttpBytesDevice.create(image_url);
  354. }
  355. catch(e)
  356. {
  357. printOnTerm.printError([e]);
  358. throw e;
  359. }
  360. break;
  361. case "github":
  362. blockDevice = await CheerpX.GitHubDevice.create(image_url);
  363. break;
  364. default:
  365. console.log("Unrecognized device type");
  366. return;
  367. }
  368. var overlayDevice = await CheerpX.OverlayDevice.create(blockDevice, await CheerpX.IDBDevice.create("block1"));
  369. var webDevice = await CheerpX.WebDevice.create("");
  370. var dataDevice = await CheerpX.DataDevice.create();
  371. CheerpX.Linux.create({mounts:[{type:"ext2",dev:overlayDevice,path:"/"},{type:"dir",dev:webDevice,path:"/app"},{type:"dir",dev:dataDevice,path:"/data"},{type:"devs",path:"/dev"},{type:"proc",path:"/proc"}], networkInterface: networkInterface}).then(runTest, failCallback);
  372. }
  373. function initialMessage()
  374. {
  375. printOnTerm.printMessage(printOnTerm.getAsciiText());
  376. console.log("Welcome. We appreciate curiosity, but be warned that keeping the DevTools open causes significant performance degradation and crashes.");
  377. }
  378. initialMessage();
  379. async function loadCX()
  380. {
  381. // Find the latest build
  382. var r = await fetch("https://cxrtnc.leaningtech.com/LATEST.txt");
  383. var url = await r.text();
  384. url = url.trim();
  385. var script = document.createElement('script');
  386. script.type = 'text/javascript';
  387. script.src = url;
  388. script.addEventListener("load", runBash, false);
  389. document.head.appendChild(script);
  390. }
  391. loadCX();
  392. </script>
  393. </body>
  394. </html>