2017-08-28 12:27:45 -04:00
|
|
|
/**
|
|
|
|
* @description Remote Desktop
|
|
|
|
* @author Ylian Saint-Hilaire
|
|
|
|
* @version v0.0.2
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Construct a Intel AMT IDER object
|
|
|
|
var CreateAmtRemoteIder = function (serverurl) {
|
|
|
|
var obj = {};
|
|
|
|
obj.protocol = 3; // IDER
|
|
|
|
obj.state = 0;
|
|
|
|
obj.socket = null;
|
|
|
|
obj.serverurl = serverurl;
|
|
|
|
obj.bytesToAmt = 0;
|
|
|
|
obj.bytesFromAmt = 0;
|
|
|
|
|
|
|
|
// Private method
|
|
|
|
obj.Debug = function (msg) { console.log(msg); }
|
|
|
|
|
|
|
|
// Private method, called by parent when it change state
|
|
|
|
obj.xxStateChange = function (newstate) {
|
|
|
|
//console.log("STATE: " + newstate);
|
|
|
|
if (newstate == 0) { obj.Stop(); } // Tell Storage Server to stop IDER
|
|
|
|
if (newstate == 3) { obj.StateChange(3); _Send('D'); } // Tell Storage Server to start IDER
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private method
|
|
|
|
obj.StateChange = function (newstate) { obj.state = newstate; }
|
|
|
|
|
|
|
|
// Private method
|
|
|
|
obj.ProcessData = function (data) { _Send('F' + data); obj.bytesFromAmt += data.length; }
|
|
|
|
|
|
|
|
obj.Start = function (host, port, user, pass, tls) {
|
|
|
|
//obj.Debug("IDER-Start");
|
|
|
|
obj.host = host;
|
|
|
|
obj.port = port;
|
|
|
|
obj.user = user;
|
|
|
|
obj.pass = pass;
|
|
|
|
obj.tls = tls;
|
|
|
|
obj.bytesToAmt = 0;
|
|
|
|
obj.bytesFromAmt = 0;
|
|
|
|
obj.socket = new WebSocket(serverurl);
|
|
|
|
obj.socket.onopen = _OnSocketConnected;
|
|
|
|
obj.socket.onmessage = _OnMessage;
|
|
|
|
obj.socket.onclose = _OnSocketClosed;
|
|
|
|
obj.StateChange(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.Stop = function () {
|
|
|
|
if (obj.socket != null) { _Send('G'); obj.socket.close(); obj.socket = null; }
|
|
|
|
obj.StateChange(0);
|
|
|
|
obj.parent.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _OnSocketConnected() {
|
|
|
|
obj.Debug("Socket Connected");
|
|
|
|
obj.StateChange(2);
|
|
|
|
_Send('C');
|
|
|
|
}
|
|
|
|
|
2019-02-06 21:09:40 -05:00
|
|
|
// Setup the file reader
|
|
|
|
var fileReader = new FileReader();
|
|
|
|
var fileReaderInuse = false, fileReaderAcc = [];
|
|
|
|
if (fileReader.readAsBinaryString) {
|
|
|
|
// Chrome & Firefox (Draft)
|
|
|
|
fileReader.onload = function (e) { _OnSocketData(e.target.result); if (fileReaderAcc.length == 0) { fileReaderInuse = false; } else { fileReader.readAsBinaryString(new Blob([fileReaderAcc.shift()])); } }
|
|
|
|
} else if (fileReader.readAsArrayBuffer) {
|
|
|
|
// Chrome & Firefox (Spec)
|
|
|
|
fileReader.onloadend = function (e) { _OnSocketData(e.target.result); if (fileReaderAcc.length == 0) { fileReaderInuse = false; } else { fileReader.readAsArrayBuffer(fileReaderAcc.shift()); } }
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
function _OnMessage(e) {
|
|
|
|
if (typeof e.data == 'object') {
|
2019-02-06 21:09:40 -05:00
|
|
|
if (fileReaderInuse == true) { fileReaderAcc.push(e.data); return; }
|
|
|
|
if (fileReader.readAsBinaryString) {
|
2017-08-28 12:27:45 -04:00
|
|
|
// Chrome & Firefox (Draft)
|
2019-02-07 18:00:10 -05:00
|
|
|
fileReaderInuse = true;
|
2019-02-06 21:09:40 -05:00
|
|
|
fileReader.readAsBinaryString(new Blob([e.data]));
|
|
|
|
} else if (fileReader.readAsArrayBuffer) {
|
2017-08-28 12:27:45 -04:00
|
|
|
// Chrome & Firefox (Spec)
|
2019-02-07 18:00:10 -05:00
|
|
|
fileReaderInuse = true;
|
2019-02-06 21:09:40 -05:00
|
|
|
fileReader.readAsArrayBuffer(e.data);
|
2017-08-28 12:27:45 -04:00
|
|
|
} else {
|
|
|
|
// IE10, readAsBinaryString does not exist, use an alternative.
|
2019-02-06 21:09:40 -05:00
|
|
|
var binary = "", bytes = new Uint8Array(e.data), length = bytes.byteLength;
|
2017-08-28 12:27:45 -04:00
|
|
|
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
|
|
|
|
_OnSocketData(binary);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_OnSocketData(e.data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function _OnSocketData(data) {
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
// This is an ArrayBuffer, convert it to a string array (used in IE)
|
|
|
|
var binary = "";
|
|
|
|
var bytes = new Uint8Array(data);
|
|
|
|
var length = bytes.byteLength;
|
|
|
|
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
|
|
|
|
data = binary;
|
|
|
|
}
|
|
|
|
else if (typeof data !== 'string') { return; }
|
|
|
|
|
|
|
|
// console.log("CMD: " + data.substring(0, 1));
|
|
|
|
|
|
|
|
// Handle commands
|
|
|
|
switch (data.substring(0, 1)) {
|
|
|
|
case 'A': { data[0] = 'B'; _Send(data); break; } // Echo
|
|
|
|
case 'C': { obj.parent.Start(obj.host, obj.port, obj.user, obj.pass, obj.tls); break; } // Session Start
|
|
|
|
case 'E': { obj.Stop(); break; } // Stop IDER
|
|
|
|
case 'F': { obj.parent.xxSend(data.substring(1)); obj.bytesToAmt += (data.length - 1); break; } // IDER Data
|
2019-01-04 20:59:13 -05:00
|
|
|
case 'H': { if (obj.onDialogPrompt) obj.onDialogPrompt(obj, JSON.parse(data.substring(1))); break; } // IDER Dialog Prompt
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _OnSocketClosed() {
|
|
|
|
// obj.Debug("Socket Closed");
|
|
|
|
obj.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.dialogPrompt = function(x) { _Send('H' + JSON.stringify(x)); }
|
|
|
|
|
|
|
|
function _Send(x) {
|
|
|
|
// obj.Debug("Send(" + x.length + "): " + rstr2hex(x));
|
|
|
|
if (obj.socket != null && obj.socket.readyState == WebSocket.OPEN) {
|
|
|
|
var b = new Uint8Array(x.length);
|
|
|
|
for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); }
|
|
|
|
obj.socket.send(b.buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|