2022-07-17 11:25:47 +00:00
|
|
|
{% extends "console-base.html" %}
|
|
|
|
{% load i18n %}
|
|
|
|
{% load static %}
|
|
|
|
|
|
|
|
{% block head %}
|
|
|
|
<title>WebVirtCloud - XTerm</title>
|
2022-07-21 10:31:37 +00:00
|
|
|
<link rel="stylesheet" href="{% static 'css/xterm.css' %}"/>
|
|
|
|
<script src="{% static 'js/xterm@3.6.0/xterm.js' %}"></script>
|
|
|
|
<script src="{% static 'js/xterm@3.6.0/addons/fit/fit.js' %}"></script>
|
|
|
|
<script src="{% static 'js/xterm@3.6.0/addons/fullscreen/fullscreen.js' %}"></script>
|
2022-07-17 11:25:47 +00:00
|
|
|
|
2022-07-21 10:31:37 +00:00
|
|
|
<script src="{% static 'js/socket.io.js' %}"></script>
|
2022-07-17 11:25:47 +00:00
|
|
|
|
|
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
|
|
<div style="background: white; padding-bottom: 5px;">
|
|
|
|
<span style="font-size: small;">Status: <span style="font-size: small;" id="status">connecting...</span></span>
|
2022-07-21 10:31:37 +00:00
|
|
|
<button id="button" type="button" onclick="myFunction();">Connect</button>
|
2022-07-17 11:25:47 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div style="width: 100%; height:100%;" id="terminal"></div>
|
|
|
|
<script crossorigin="anonymous">
|
2022-07-21 10:31:37 +00:00
|
|
|
Terminal.applyAddon(fit);
|
|
|
|
|
|
|
|
let host = readQueryVariable('host', '{{ socketio_host }}');
|
|
|
|
let port = readQueryVariable('port', '{{ socketio_port }}');
|
|
|
|
let path = readQueryVariable('path', '{{ socketio_path }}');
|
|
|
|
|
|
|
|
|
|
|
|
// Build the websocket URL used to connect
|
|
|
|
let url;
|
|
|
|
if (window.location.protocol === "https:") {
|
|
|
|
url = 'wss';
|
|
|
|
} else {
|
|
|
|
url = 'ws';
|
|
|
|
}
|
|
|
|
url += '://' + host;
|
|
|
|
if (port) {
|
|
|
|
url += ':' + port;
|
|
|
|
}
|
2022-07-17 11:25:47 +00:00
|
|
|
|
2022-07-21 10:31:37 +00:00
|
|
|
var socket = io.connect(url, {transports: ["websocket", "polling"]});
|
2022-07-17 11:25:47 +00:00
|
|
|
|
2022-07-21 10:31:37 +00:00
|
|
|
const status = document.getElementById("status");
|
|
|
|
const button = document.getElementById("button");
|
2022-07-17 11:25:47 +00:00
|
|
|
|
|
|
|
var term = new Terminal({
|
|
|
|
cursorBlink: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
term.open(document.getElementById('terminal'));
|
|
|
|
|
|
|
|
term.on('key', (key, ev) => {
|
|
|
|
console.log("pressed key", key)
|
|
|
|
socket.emit("pty_input", {"input": key})
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("pty_output", function(output){
|
|
|
|
console.log(output["output"])
|
|
|
|
term.write(output["output"])
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("connect", () => {
|
2022-07-21 10:31:37 +00:00
|
|
|
status.innerHTML = '<span style="background-color: lightgreen;">connected</span>'
|
|
|
|
button.innerHTML = 'Disconnect'
|
2022-07-17 11:25:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("disconnect", () => {
|
2022-07-21 10:31:37 +00:00
|
|
|
status.innerHTML = '<span style="background-color: #ff8383;">disconnected</span>'
|
|
|
|
button.innerHTML = 'Connect'
|
2022-07-17 11:25:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function myFunction(){
|
|
|
|
if (button.innerHTML =='Connect'){
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (button.innerHTML == "Disconnect"){
|
|
|
|
socket.emit("disconnect_request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resize(){
|
|
|
|
term.fit()
|
|
|
|
socket.emit("resize", {"cols": term.cols, "rows": term.rows})
|
|
|
|
}
|
|
|
|
|
2022-07-21 10:31:37 +00:00
|
|
|
// This function extracts the value of one variable from the
|
|
|
|
// query string. If the variable isn't defined in the URL
|
|
|
|
// it returns the default value instead.
|
|
|
|
function readQueryVariable(name, defaultValue) {
|
|
|
|
// A URL with a query parameter can look like this:
|
|
|
|
// https://www.example.com?myqueryparam=myvalue
|
|
|
|
//
|
|
|
|
// Note that we use location.href instead of location.search
|
|
|
|
// because Firefox < 53 has a bug w.r.t location.search
|
|
|
|
const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
|
|
|
|
match = document.location.href.match(re);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
// We have to decode the URL since want the cleartext value
|
|
|
|
return decodeURIComponent(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2022-07-17 11:25:47 +00:00
|
|
|
window.onresize = resize
|
|
|
|
window.onload = resize
|
|
|
|
</script>
|
|
|
|
{% endblock %}
|