kvu787 / httpVirt

Create VMs and use them with HTTP or WebSockets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

httpVirt

httpVirt is a tool for creating virtual machines and using them through a HTTP and WebSocket interface.

You can

  • Deploy httpVirt to a physical machine
  • Create resource-constrained VMs using physical machine resources
  • Send shell commands to these VMs via HTTP and WebSocket requests

Installation

  1. Install VirtualBox.
  2. Install Vagrant 1.8.6 or 1.8.5.
  3. Download the repo and run the httpVirt server:
$ git clone https://github.com/kvu787/httpVirt.git

Start the httpVirt server

$ cd httpVirt
$ vagrant up

The latter command starts a server on http://127.0.0.1:10411 . It takes a long time to run.

Create a VM and run a command

With an HTTP GET

Use cURL with the HTTP API:

$ id=`curl -w '\n' 'http://127.0.0.1:10411/create'`
8ebc3265a715ab0854fcb74c8305911e74c740471fa50f3a1b99949de43a9c0a
$ curl "http://127.0.0.1:10411/command/$id?command=ls%20-a"
.  ..  .bashrc  .profile

With a WebSocket

Use client-side Javascript with the WebSocket API:

  1. Visit http://127.0.0.1:10411 in your browser.
  2. Open the JavaScript console.
  3. Copy/paste and run the following code:
// Vanilla JS GET: http://stackoverflow.com/questions/247483/http-get-request-in-javascript
function httpGetAsync(theUrl, callback) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open('GET', theUrl, true); // true for asynchronous
    xmlHttp.send(null);
}

// create a VM
httpGetAsync('http://127.0.0.1:10411/create', function (containerID) {
  // open a WebSocket to the VM
  var webSocket = new WebSocket(`ws://127.0.0.1:10411/session/${containerID}`);

  // handler called for each incoming message
  webSocket.onmessage = function(e){
     var message = e.data;
     console.log('Message received: ' + message);
  }

  webSocket.onopen = function () {
    // when websocket is ready, send command as outgoing message
    webSocket.send('ls -a');
  }

  // onmessage handler will print output
});
  1. You should see Message received: <command output> in the console.

Shutdown httpVirt and clean up associated resources

$ cd httpVirt
$ vagrant destroy -f

Run the xterm demo

Make sure httpVirt is running and open demo/index.html in your web browser.

Updating httpVirt

When you make or pull new changes, destroy the currently running httpVirt VM with vagrant destroy -f and restart it with vagrant up.

About

Create VMs and use them with HTTP or WebSockets.


Languages

Language:JavaScript 74.9%Language:CSS 18.5%Language:Go 5.6%Language:Shell 0.7%Language:HTML 0.3%