Advanced HLL and web integration - Part 2

In first part we showed basic integration that can be used from other web apps. The question is can we integrate web terminal with other software as it is possible with native client and standard HLL API?

What if we want to use STRRMTCMD to start remote program from web terminal? Is it possible?

Well, the answer is...

YES IT IS!!!!!!


Let me explain....

Browsers are running in sand boxed environment which is very secure and where it is not possible to run or access external programs or OS directories. Still, browsers have WebSocket which is full duplex network protocol that can be used to connect to locally running service and exchange some information, instructions or A-HLL commands if you like :)

Having possibility to exchange A-HLL commands from browser to locally running WebSocket service we can easily add standard A-HLL integration similar to integration possible with native terminal and HLL API. Here, local WebSocket service is actually our command Proxy.

To create such a service, the easiest way is to use Node.JS which is slick, fast, simple and very powerful. So, this time we will show how to create basic WebSocket Windows Service based on Node.JS and how we can connect to it from any web page.

Point here is to show what is possible to achieve and to give you to think about. :)


Before we start

There are some requirements before we start.

For security reasons, we have to add filter inside of our local WebSocket service to accept connections only from specific clients like localhost is or to add filter for specific domain.

Also, WebSocket is asynchronous so we have to cache requests with their RID's so that when response is received from other side we can match them for data forwards.

WebSockets also have some rules. For example, if we are on https web page, WebSocket have to use SSL also in order to be able to connect. This will require two WebSocket services running on two ports. One for http and one for https. Additionally, we will need SSL certificate for secured version.

Here I will not describe how to create SSL certificate as this post will be too long. There are a lot of texts available online.

For this first introduction we will create simple Node.JS based Windows Service and basic WebSocket server to which we will be able to connect from any non https web page. All other mentioned rules we will implement next time.

Please note: Client machine have to have Node.JS installed. You can download it from here


Let's start with project preparation

If you have installed Node.JS and node -v is working on command line then we can continue...

  • Let's create empty folder and prepare empty project

  • Open console and create new directory

  • Position to newly created directory and create three empty files

    • package.json
    • index.js
    • install.js
  • Open package.json in text editor and enter minimum required the save it

      {}
    
  • Install two required libs for WebSocket and Windows Service

      npm install ws --save
      npm install node-windows --save
    

Let's create Windows Service

Windows service is standard service that can be started manually or automatically when Windows starts. Services can be accessed by starting services.msc from run command box in Windows.

To create service installer we have created install.js file in which we will add this simple code...

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Green Screens A-HLL',
  description: 'Advanced HLL service to enable web browser Terminal integration.',
  script: require('path').join(__dirname, 'service.js')
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

// start installation
svc.install();

Now, let' start it with node install.js from project command line. After successful execution, we will have active service under 'Green Screens A-HLL' name.

Start services.msc to find this service. Stop it for now so that we can add code to our empty index.js script which will contain WebSocket service app.

Note: To install service we need Administrative privileges.


Let's create WebSocket Service

Now we will create base WebSocket service to which we can connect from our web browser or any application that have WebSocket client implementation.

After entering demo code shown below, we could start service manually for testing and when everything is working ok, we can start / restart and monitor our service through Windows Services.

const WebSocketServer = require('ws').Server;

// Send response with error handling
function respond(message) {

    let ws = this;
    let data = message;

    if (typeof message !== 'string') {
        message.ts = Date.now();
        data = JSON.stringify(message);
    }

    ws.send(data, function ack(error) {
      // if error is not defined, send operation has been completed,
      // otherwise the error object will indicate what failed.
      if (error) {
          console.log(error);
      }
    });

}

//Handle received message
function onMessage(message) {

    let ws = this;
    let obj = null;
    let rid = null;

    try {
        obj = JSON.parse(message);
        obj.rid = obj.rid || -1;
        rid = obj.rid;
        ws.respond(obj);
    } catch (e) {
        ws.respond({success:false, msg : e.message, rid : rid});
    }

}

//Event when client connect
// Here we add event listener methods to handle client requests
function onConnection(ws) {
      ws.respond = respond.bind(ws);
      ws.on('message', onMessage.bind(ws));
}

//Start WebSocket service
function onStart() {

 let port = 5250;

 console.log(`AEHLL API service starting at port : ${port}`);

 // create websocket server and attach connection listener
 var wss = new WebSocketServer({ port: port});
 wss.on('connection', onConnection);

}

// detect application start and start WebSocket
onStart();


Finally, let's play

If you did not install already as noted on previous blogs, install Smart WebSocket Client from Chrome Web Store.

Then, enter location address ws://localhost:5250, click Connect and then Send. You should see some response.

There is also manual approach from browser console to test access from any non https web page...

var ws = new WebSocket('ws://localhost:5250/');
ws.onmessage=function(e){console.log(e.data)};
ws.onopen=function(){ws.send(JSON.stringify({cmd:'echo'}));};


Full working Green Screens SandBox Proxy Service with client filtering and http/https support can be found on our GitHub repository. It is still in development and testing stage and not yet ready for production. When it will be ready for production, we will add it to the main site products page.

NOTE:

When running as a Windows Service, child process started as a GUI program will not be visible because program will be started under different system user. For programs requiring GUI interactions, service must be started as a logged in user or Native Terminal Client should be used.