2017-11-08 14:33:05 +00:00
|
|
|
jQuery(function($){
|
2017-12-08 09:00:14 +00:00
|
|
|
|
|
|
|
var status = $('#status'),
|
2018-02-26 03:15:59 +00:00
|
|
|
btn = $('.btn-primary'),
|
|
|
|
style = {};
|
2017-12-08 09:00:14 +00:00
|
|
|
|
2017-11-08 14:33:05 +00:00
|
|
|
$('form#connect').submit(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var form = $(this),
|
|
|
|
url = form.attr('action'),
|
|
|
|
type = form.attr('type'),
|
|
|
|
data = new FormData(this);
|
|
|
|
|
2018-02-06 23:40:17 +00:00
|
|
|
if (!data.get('hostname') || !data.get('port') || !data.get('username')) {
|
|
|
|
status.text('Hostname, port and username are required.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 13:48:18 +00:00
|
|
|
var pk = data.get('privatekey');
|
|
|
|
if (pk && pk.size > 16384) {
|
|
|
|
status.text('Key size exceeds maximum value.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status.text('');
|
|
|
|
btn.prop('disabled', true);
|
|
|
|
|
2017-11-08 14:33:05 +00:00
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: type,
|
|
|
|
data: data,
|
|
|
|
success: callback,
|
|
|
|
cache: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-26 03:15:59 +00:00
|
|
|
function parse_xterm_style() {
|
|
|
|
var text = $('.xterm-helpers style').text();
|
|
|
|
var arr = text.split('xterm-normal-char{width:');
|
|
|
|
style.width = parseInt(arr[1]) + 1;
|
|
|
|
arr = text.split('div{height:');
|
|
|
|
style.height = parseInt(arr[1]);
|
|
|
|
}
|
2018-02-01 07:06:30 +00:00
|
|
|
|
|
|
|
function current_geometry() {
|
2018-02-26 03:15:59 +00:00
|
|
|
if (!style.width || !style.height) {
|
|
|
|
parse_xterm_style();
|
|
|
|
}
|
|
|
|
cols = parseInt(window.innerWidth / style.width);
|
|
|
|
rows = parseInt(window.innerHeight / style.height);
|
2018-02-01 07:06:30 +00:00
|
|
|
return [cols, rows];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-08 14:33:05 +00:00
|
|
|
function callback(msg) {
|
|
|
|
// console.log(msg);
|
|
|
|
if (msg.status) {
|
2017-12-08 09:00:14 +00:00
|
|
|
status.text(msg.status);
|
|
|
|
setTimeout(function(){
|
|
|
|
btn.prop('disabled', false);
|
|
|
|
}, 3000);
|
2017-11-08 14:33:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ws_url = window.location.href.replace('http', 'ws'),
|
|
|
|
join = (ws_url[ws_url.length-1] == '/' ? '' : '/'),
|
2017-12-07 11:23:21 +00:00
|
|
|
url = ws_url + join + 'ws?id=' + msg.id,
|
2017-11-08 14:33:05 +00:00
|
|
|
socket = new WebSocket(url),
|
|
|
|
terminal = document.getElementById('#terminal'),
|
2018-02-01 07:06:30 +00:00
|
|
|
geometry = current_geometry();
|
|
|
|
term = new Terminal({
|
|
|
|
cursorBlink: true,
|
|
|
|
cols: geometry[0],
|
|
|
|
rows: geometry[1]
|
|
|
|
});
|
2017-11-08 14:33:05 +00:00
|
|
|
|
|
|
|
console.log(url);
|
|
|
|
term.on('data', function(data) {
|
|
|
|
// console.log(data);
|
|
|
|
socket.send(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.onopen = function(e) {
|
|
|
|
$('.container').hide();
|
|
|
|
term.open(terminal, true);
|
2017-12-07 11:25:02 +00:00
|
|
|
term.toggleFullscreen(true);
|
2017-11-08 14:33:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function(msg) {
|
|
|
|
// console.log(msg);
|
|
|
|
term.write(msg.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onerror = function(e) {
|
|
|
|
console.log(e);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onclose = function(e) {
|
|
|
|
console.log(e);
|
|
|
|
term.destroy();
|
|
|
|
$('.container').show();
|
2017-12-08 13:15:56 +00:00
|
|
|
status.text(e.reason);
|
|
|
|
btn.prop('disabled', false);
|
2017-11-08 14:33:05 +00:00
|
|
|
};
|
|
|
|
}
|
2018-02-01 07:06:30 +00:00
|
|
|
|
|
|
|
$(window).resize(function(){
|
2018-04-09 01:40:07 +00:00
|
|
|
if (typeof term != 'undefined') {
|
2018-02-01 07:06:30 +00:00
|
|
|
geometry = current_geometry();
|
|
|
|
term.geometry = geometry;
|
|
|
|
term.resize(geometry[0], geometry[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-08 14:33:05 +00:00
|
|
|
});
|