From 319ae00eb5345f60c86663184b1db52aba996564 Mon Sep 17 00:00:00 2001 From: Stefan Etringer Date: Wed, 9 Jul 2025 14:43:28 +0000 Subject: [PATCH] added support for download and upload through the gontrol server --- main.go | 2 +- static/download-command.js | 10 +++++++++- static/start-interactive.js | 32 ++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 8448b48..cf99b52 100644 --- a/main.go +++ b/main.go @@ -475,7 +475,7 @@ func handler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Usage: download ", 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) diff --git a/static/download-command.js b/static/download-command.js index 25ad655..98b70a6 100644 --- a/static/download-command.js +++ b/static/download-command.js @@ -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}`; +} diff --git a/static/start-interactive.js b/static/start-interactive.js index d212418..7d6ae38 100644 --- a/static/start-interactive.js +++ b/static/start-interactive.js @@ -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):///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(); + // Path‑based scheme: /proxyAgent//... + // Split pathname: ["", "proxyAgent", "", ...] + 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 we’re 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.