Files
container-console-agent/index.html
T
2024-05-20 10:31:21 +08:00

124 lines
3.0 KiB
HTML

<html lang="en">
<head>
<link rel="stylesheet" href="xterm/xterm.css"/>
<script src="xterm/xterm.js"></script>
<style>
body {
padding: 0;
margin: 5px;
background-color: #000;
height: 100%;
}
#container-terminal {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
<title>Docker Exec Web Console</title>
</head>
<body>
<div id="container-terminal"></div>
<script type="text/javascript">
function resizeTerminal(term) {
const container = document.getElementById('container-terminal');
if (!container) {
console.error('Container element not found.');
return;
}
// 现在使用更精确的测量值,这些值应该基于你的终端设置
const style = window.getComputedStyle(container);
const charWidth = parseInt(style.getPropertyValue('--xterm-char-width'), 10) || 10;
const charHeight = parseInt(style.getPropertyValue('--xterm-char-height'), 10) || 20;
const width = container.offsetWidth;
const height = container.offsetHeight;
const cols = Math.floor(width / charWidth);
console.log(cols);
const rows = Math.floor(height / charHeight);
term.resize(cols, rows);
console.log(term);
}
function getQueryVar(variable) {
const query = window.location.search.substring(1);
const vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
if (pair[0] === variable) {
return pair[1];
}
}
return false;
}
let token = getQueryVar('token');
if (!token) {
throw new Error('token not found');
}
let command = getQueryVar('cmd');
if (!command) {
command = '/bin/bash';
}
let websocket = new WebSocket(
"ws://"
+ window.location.hostname + ":"
+ window.location.port
+ window.location.pathname
+ "exec/"
+ token + ',' + window.btoa(command)
);
websocket.onopen = function () {
let term = new Terminal({
//cols: 300,
//rows: 30,
screenKeys: true,
useStyle: true,
cursorBlink: true,
});
term.on('data', function (data) {
websocket.send(data);
});
term.on('title', function (title) {
document.title = title;
});
term.open(document.getElementById('container-terminal'));
resizeTerminal(term);
window.addEventListener('resize', () => {
resizeTerminal(term);
});
websocket.onmessage = function (evt) {
term.write(evt.data);
}
websocket.onclose = function () {
term.write("Session terminated");
term.destroy();
}
websocket.onerror = function (evt) {
if (typeof console.log == "function") {
console.log(evt)
}
}
}
</script>
</body>
</html>