added support for download and upload through the gontrol server

This commit is contained in:
Stefan Etringer 2025-07-09 14:43:28 +00:00
parent b21dc2ea7a
commit 319ae00eb5
3 changed files with 28 additions and 16 deletions

View File

@ -475,7 +475,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Usage: download <filePath>", http.StatusBadRequest)
return
}
http.Redirect(w, r, "/download?filePath="+url.QueryEscape(args[0]), http.StatusFound)
http.Redirect(w, r, "download?filePath="+url.QueryEscape(args[0]), http.StatusFound)
return
} else {
cmdOutput := processCommand(input, currentDir)

View File

@ -39,7 +39,7 @@ function uploadFileFromBrowser(filePath, targetPath) {
const file = fileInput.files[0];
const formData = new FormData();
formData.append("file", file);
fetch("/upload", {
fetch(agentURL("upload"), {
method: "POST",
body: formData,
})
@ -50,3 +50,11 @@ function uploadFileFromBrowser(filePath, targetPath) {
})
.catch((error) => console.error("Upload failed:", error));
}
function agentURL(subPath) {
const parts = window.location.pathname.split("/");
if (parts.length >= 3 && parts[1] === "proxyAgent") {
return `/proxyAgent/${parts[2]}/${subPath}`;
}
return `/${subPath}`;
}

View File

@ -26,24 +26,28 @@ function getQueryParam(name) {
}
function makeWsUrl() {
const proxyIp = getQueryParam("ip"); // "10.0.0.42" if you came via /proxyAgent
const proxyPort = getQueryParam("port");
const usingProxy = proxyIp && proxyPort; // true only in that case
const loc = window.location; // shortcut
const proto = loc.protocol === "https:" ? "wss:" : "ws:";
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();
// Pathbased scheme: /proxyAgent/<key>/...
// Split pathname: ["", "proxyAgent", "<key>", ...]
const parts = loc.pathname.split("/");
if (parts.length >= 3 && parts[1] === "proxyAgent" && parts[2]) {
const agentKey = parts[2]; // e.g. "192.168.0.12:43211"
const ws = new URL(`/proxyAgent/${agentKey}/terminal`, loc);
ws.protocol = proto;
return ws.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();
// Direct fallback: same host, /terminal
const ws = new URL("/terminal", loc);
ws.protocol = proto;
return ws.toString();
}
function startInteractiveSession() {
interactiveMode = true;
// Hide the normal terminal and input.