mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-13 08:46:09 +01:00
web terminal
This commit is contained in:
94
WebTerminal/static/WebTerminal/main.js
Executable file
94
WebTerminal/static/WebTerminal/main.js
Executable 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();
|
||||
5977
WebTerminal/static/WebTerminal/term.js
Executable file
5977
WebTerminal/static/WebTerminal/term.js
Executable file
File diff suppressed because it is too large
Load Diff
64
WebTerminal/static/WebTerminal/ws.js
Executable file
64
WebTerminal/static/WebTerminal/ws.js
Executable 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();
|
||||
Reference in New Issue
Block a user