web terminal

This commit is contained in:
Usman Nasir
2019-11-02 19:29:02 +05:00
parent dd21a17b41
commit 087322f2b4
58 changed files with 16489 additions and 3141 deletions

View File

@@ -0,0 +1,94 @@
var charWidth = 6.2;
var charHeight = 15.2;
/**
* for full screen
* @returns {{w: number, h: number}}
*/
function getTerminalSize() {
var width = window.innerWidth;
var height = window.innerHeight;
return {
w: Math.floor(width / charWidth),
h: Math.floor(height / charHeight)
};
}
function openTerminal(options) {
if (!$.isEmptyObject($('.terminal')[0])) {
alert("Please refresh this page.");
return
}
var client = new WSSHClient();
var term = new Terminal({cols: 120, rows: 30, screenKeys: true, useStyle: true});
term.on('data', function (data) {
client.sendClientData(data);
});
term.open();
$('.terminal').detach().appendTo('#term');
$("#term").show();
term.write('Connecting...' + '\r\n');
client.connect({
onError: function (error) {
term.write('Error connecting to backend.\r\n');
//term.destroy();
},
onConnect: function () {
client.sendInitData(options);
term.write('connection established..\r\n');
},
onClose: function (e) {
term.write("\r\nconnection closed.")
//term.destroy();
},
onData: function (data) {
term.write(data);
}
})
}
function store(options) {
window.localStorage.host = options.host;
window.localStorage.port = options.port;
window.localStorage.username = options.username;
window.localStorage.ispwd = options.ispwd;
window.localStorage.secret = options.secret
}
function check() {
return validResult["host"] && validResult["port"] && validResult["username"];
}
function connect() {
var remember = $("#remember").is(":checked");
var options = {
host: $("#host").val(),
port: $("#port").val(),
username: $("#username").val(),
ispwd: $("input[name=ispwd]:checked").val(),
secret: $("#secret").val(),
verifyPath: $("#verifyPath").text()
}
console.debug(options);
if (remember) {
store(options)
}
// if (check()) {
// openTerminal(options)
// } else {
// for (var key in validResult) {
// if (!validResult[key]) {
// alert(errorMsg[key]);
// break;
// }
// }
// }
openTerminal(options)
}
connect();

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,64 @@
function WSSHClient() {
};
WSSHClient.prototype._generateEndpoint = function () {
var protocol = 'wss://';
var host = window.location.host.split(':')[0];
var endpoint = protocol + host + ':5678';
return endpoint;
};
WSSHClient.prototype.connect = function (options) {
var endpoint = this._generateEndpoint();
if (window.WebSocket) {
this._connection = new WebSocket(endpoint);
}
else if (window.MozWebSocket) {
this._connection = MozWebSocket(endpoint);
}
else {
options.onError('WebSocket Not Supported');
return;
}
this._connection.onerror = function (evt) {
options.onError(evt);
};
this._connection.onopen = function () {
options.onConnect();
};
this._connection.onmessage = function (evt) {
var data = evt.data.toString()
options.onData(data);
};
this._connection.onclose = function (evt) {
options.onClose(evt);
};
};
WSSHClient.prototype.send = function (data) {
this._connection.send(JSON.stringify(data));
};
WSSHClient.prototype.sendInitData = function (options) {
var data = {
hostname: options.host,
port: options.port,
username: options.username,
ispwd: options.ispwd,
secret: options.secret
};
this._connection.send(JSON.stringify({"tp": "init", "data": options}))
}
WSSHClient.prototype.sendClientData = function (data) {
this._connection.send(JSON.stringify({"tp": "client", "data": data, 'verifyPath': $("#verifyPath").text()}))
}
var client = new WSSHClient();