added support for download and upload through the gontrol server
This commit is contained in:
		
							parent
							
								
									b21dc2ea7a
								
							
						
					
					
						commit
						319ae00eb5
					
				
							
								
								
									
										2
									
								
								main.go
								
								
								
								
							
							
						
						
									
										2
									
								
								main.go
								
								
								
								
							| 
						 | 
					@ -475,7 +475,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
					http.Error(w, "Usage: download <filePath>", http.StatusBadRequest)
 | 
										http.Error(w, "Usage: download <filePath>", http.StatusBadRequest)
 | 
				
			||||||
					return
 | 
										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
 | 
									return
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				cmdOutput := processCommand(input, currentDir)
 | 
									cmdOutput := processCommand(input, currentDir)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,7 @@ function uploadFileFromBrowser(filePath, targetPath) {
 | 
				
			||||||
	const file = fileInput.files[0];
 | 
						const file = fileInput.files[0];
 | 
				
			||||||
	const formData = new FormData();
 | 
						const formData = new FormData();
 | 
				
			||||||
	formData.append("file", file);
 | 
						formData.append("file", file);
 | 
				
			||||||
	fetch("/upload", {
 | 
						fetch(agentURL("upload"), {
 | 
				
			||||||
		method: "POST",
 | 
							method: "POST",
 | 
				
			||||||
		body:  formData,
 | 
							body:  formData,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
| 
						 | 
					@ -50,3 +50,11 @@ function uploadFileFromBrowser(filePath, targetPath) {
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		.catch((error) => console.error("Upload failed:", error));
 | 
							.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}`;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,24 +26,28 @@ function getQueryParam(name) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function makeWsUrl() {
 | 
					function makeWsUrl() {
 | 
				
			||||||
  const proxyIp   = getQueryParam("ip");     // "10.0.0.42" if you came via /proxyAgent
 | 
					  const loc    = window.location;                 // shortcut
 | 
				
			||||||
  const proxyPort = getQueryParam("port");
 | 
					  const proto  = loc.protocol === "https:" ? "wss:" : "ws:";
 | 
				
			||||||
  const usingProxy = proxyIp && proxyPort;   // true only in that case
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (usingProxy) {
 | 
					  // Path‑based scheme: /proxyAgent/<key>/...
 | 
				
			||||||
    // Build ws(s)://<main-server>/proxyAgent/terminal?ip=…&port=…
 | 
					  // Split pathname: ["", "proxyAgent", "<key>", ...]
 | 
				
			||||||
    const u = new URL("/proxyAgent/terminal", window.location);
 | 
					  const parts = loc.pathname.split("/");
 | 
				
			||||||
    u.searchParams.set("ip",   proxyIp);
 | 
					  if (parts.length >= 3 && parts[1] === "proxyAgent" && parts[2]) {
 | 
				
			||||||
    u.searchParams.set("port", proxyPort);
 | 
					    const agentKey = parts[2];                    // e.g. "192.168.0.12:43211"
 | 
				
			||||||
    u.protocol = u.protocol === "https:" ? "wss:" : "ws:";
 | 
					
 | 
				
			||||||
    return u.toString();
 | 
					    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);
 | 
					  // Direct fallback: same host, /terminal
 | 
				
			||||||
  u.protocol = u.protocol === "https:" ? "wss:" : "ws:";
 | 
					  const ws = new URL("/terminal", loc);
 | 
				
			||||||
  return u.toString();
 | 
					  ws.protocol = proto;
 | 
				
			||||||
 | 
					  return ws.toString();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function startInteractiveSession() {
 | 
					  function startInteractiveSession() {
 | 
				
			||||||
  interactiveMode = true;
 | 
					  interactiveMode = true;
 | 
				
			||||||
  // Hide the normal terminal and input.
 | 
					  // Hide the normal terminal and input.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue