added support for interactive terminal session via proxy agent

This commit is contained in:
Stefan Etringer 2025-06-27 13:05:18 +00:00
parent bb1b013299
commit 4f0e5cdd79
5 changed files with 30 additions and 4 deletions

View File

@ -10,6 +10,7 @@ document.addEventListener("DOMContentLoaded", function () {
if (!interactiveMode && event.key === "Enter") { if (!interactiveMode && event.key === "Enter") {
const command = input.value.trim(); const command = input.value.trim();
if (command === "start-interactive") { if (command === "start-interactive") {
startInteractiveSession(); startInteractiveSession();
input.value = ""; input.value = "";
event.preventDefault(); event.preventDefault();
@ -19,6 +20,30 @@ document.addEventListener("DOMContentLoaded", function () {
} }
}); });
// Helper: get ?ip=…&port=… from the current location
function getQueryParam(name) {
return new URLSearchParams(window.location.search).get(name);
}
function makeWsUrl() {
const proxyIp = getQueryParam("ip"); // "10.0.0.42" if you came via /proxyAgent
const proxyPort = getQueryParam("port"); // "8080"
const usingProxy = proxyIp && proxyPort; // truthy only in that case
if (usingProxy) {
// Build ws(s)://<main-server>/proxyAgent/terminal?ip=…&port=…
const u = new URL("/proxyAgent/terminal", window.location);
u.searchParams.set("ip", proxyIp);
u.searchParams.set("port", proxyPort);
u.protocol = u.protocol === "https:" ? "wss:" : "ws:";
return u.toString();
}
// Fallback: open directly on the agent were already on
const u = new URL("/terminal", window.location);
u.protocol = u.protocol === "https:" ? "wss:" : "ws:";
return u.toString();
}
function startInteractiveSession() { function startInteractiveSession() {
interactiveMode = true; interactiveMode = true;
// Hide the normal terminal and input. // Hide the normal terminal and input.
@ -55,7 +80,8 @@ document.addEventListener("DOMContentLoaded", function () {
console.log("Initial fit: container width =", xtermContainer.offsetWidth, "cols =", term.cols); console.log("Initial fit: container width =", xtermContainer.offsetWidth, "cols =", term.cols);
}, 100); }, 100);
interactiveWS = new WebSocket("ws://" + location.host + "/terminal"); interactiveWS = new WebSocket(makeWsUrl());
// interactiveWS = new WebSocket("ws://" + location.host + "/terminal");
interactiveWS.binaryType = "arraybuffer"; interactiveWS.binaryType = "arraybuffer";
interactiveWS.onopen = function () { interactiveWS.onopen = function () {

View File

@ -11,9 +11,9 @@
<script type="text/javascript" src="static/start-interactive.js"></script> <script type="text/javascript" src="static/start-interactive.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ansi_up@5.0.0/ansi_up.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/ansi_up@5.0.0/ansi_up.min.js"></script>
<link rel="stylesheet" href="static/xterm.css" /> <link rel="stylesheet" href="static/xterm/xterm.css" />
<script rel="text/javascript" src="static/xterm.js"></script> <script rel="text/javascript" src="static/xterm/xterm.js"></script>
<script rel="text/javascript" src="static/xterm-addon-fit.js"></script> <script rel="text/javascript" src="static/xterm/xterm-addon-fit.js"></script>
<link rel="stylesheet" type="text/css" href="static/stylesheet.css"> <link rel="stylesheet" type="text/css" href="static/stylesheet.css">
</head> </head>