In this demo, we're going to show how to use VanJS to develop a web-based Unix terminal that connects to your local computer, with some notable improvements, all under 300 lines of code. While there are numerous open source projects aiming for a better terminal or shell, this example demonstrates that, with VanJS, we can easily provide great extension to commandline utilities with fancy GUI by leveraging all available HTML elements.
The program is heavily tested in macOS, and should in theory works in Linux, or in any environment that has /bin/sh
.
You can preview the terminal app via CodeSandbox.
Compare to the ordinary Unix terminal that you're familar with, this web-based terminal has 2 notable improvements:
- Command
ps ...
will render an HTML table instead of text output. - Command
tree
(need the exact text match) will render an interactive tree-view of your current directory, like the one in the screenshot below:
server.ts
: A Deno-based server-side script which connects to the shell of your local computer.client.html
: An HTML file served byserver.ts
with client-side implementation for the GUI of the terminal.van-1.1.0.min.js
: Minified VanJS code, used byclient.html
.
Follow the steps below to run the terminal app in your local computer:
- For the terminal to work, you need Deno runtime to run the server-side script. If you don't have Deno in your environment, you can get it installed from https://deno.land/.
- Clone the
van
repo, if you haven't done already:
git clone https://github.com/vanjs-org/van.git
- Go to the root directory of the
van
repo:
cd van
- Run the command below under your working directory (You can specify the port via
--port
flag. By default, port 8000 will be chosen):
deno run --allow-net --allow-run --allow-read demo/terminal/server.ts
- You can visit the web-based shell with the URL printed in the console output of
deno run
. In your first visit, it will ask you to login, you need to paste the random key printed from the console to proceed. - After login, you will be able to see and use the web-based shell.
This demo program allows web access to your OS shell, which elevates the privilege to a level that you would not normally get with your browser. Here are the extra measures we're taking to ensure the security of your local computer while testing the demo:
- Only local connection to your server is allowed, unless flag
--allowRemote
is specified. - Before using the web-based terminal in your browser, you need to login with the key printed in the console of
server.ts
first (unless flag--skipLogin
is specified). The key is generated randomly every time the server restarts. You should never share the key to other people. - You're advised to shut down the server when you're not using the terminal to further reduce the risk of unauthorized access to your terminal with the leaked key. Next time, when the server restarts, any browser access needs the login with a new key generated randomly.
- Please be aware that any commands you run in the web-based shell are the real commands executed on your computer. Thus don't try dangerous stuff as they are IRREVERSIBLE.
If you're considering building a usable terminal beyond demo purposes, you should pack things up in a way that is both secure and convenient.