Spaces:
Paused
Paused
File size: 3,878 Bytes
0267589 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | const express = require("express");
const { exec } = require("child_process");
const os = require("os");
const path = require("path");
const multer = require("multer");
const fs = require("fs");
const axios = require("axios");
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname)));
const MAX_MEM = 90;
const TIMEOUT = 60000;
const BLOCKED_COMMANDS = [
"ollama", "docker build", "docker run", "python train",
"cloudflared tunnel", "cloudflared tunnel --url",
"cloudflared --url", "pip install torch", "yes", ":(){ :|:& }:"
];
let commandHistory = [];
// ===== MEMORY CHECK =====
function getMemoryUsage() {
const total = os.totalmem();
const free = os.freemem();
return Math.round(((total - free) / total) * 100);
}
// ===== HOME PAGE =====
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// ===== COMMAND EXECUTION =====
app.post("/run", (req, res) => {
const cmd = req.body?.cmd || req.body?.command;
if (!cmd) return res.json({ error: "command not provided" });
console.log(`[RECEIVED]: ${cmd}`);
if (cmd.trim().toLowerCase() === "log") {
const historyText = commandHistory.length > 0
? commandHistory.map((item, i) => `${i + 1}. [${item.time}] ${item.command}`).join(" | ")
: "History is empty.";
return res.json({ output: historyText.replace(/\n/g, " "), error: "" });
}
if (cmd.trim().toLowerCase() === "clear") {
commandHistory = [];
return res.json({ output: "History cleared.", error: "" });
}
commandHistory.push({ time: new Date().toLocaleTimeString(), command: cmd });
for (const blocked of BLOCKED_COMMANDS) {
if (cmd.includes(blocked)) {
return res.json({ error: "❌ Extremely heavy task blocked." });
}
}
let finalCmd = cmd;
if (cmd.startsWith("apt") && cmd.includes("install") && !cmd.includes("-y")) {
finalCmd += " -y";
}
const memUsage = getMemoryUsage();
if (memUsage > MAX_MEM) console.log(`⚠ High memory usage: ${memUsage}%`);
exec(`timeout ${TIMEOUT / 1000} ${finalCmd}`, { maxBuffer: 5 * 1024 * 1024 }, (error, stdout, stderr) => {
const cleanOutput = (stdout || "").replace(/\n/g, " ").trim();
const cleanError = (stderr || "").replace(/\n/g, " ").trim();
if (error && !stdout && !stderr) {
return res.json({ error: error.message.replace(/\n/g, " ") });
}
res.json({ output: cleanOutput, error: cleanError });
});
});
// ===== UPLOAD TO /app (ROOT) =====
// temp upload
const upload = multer({ dest: "temp/" });
app.post("/upload", upload.single("file"), (req, res) => {
if (!req.file) {
return res.json({ error: "No file uploaded" });
}
const targetPath = path.join(__dirname, req.file.originalname);
fs.rename(req.file.path, targetPath, (err) => {
if (err) {
return res.json({ error: "Upload failed: " + err.message });
}
res.json({ output: `✅ File saved in /app as ${req.file.originalname}` });
});
});
// ===== DOWNLOAD FROM /app =====
app.get("/download", (req, res) => {
const filename = req.query.file;
if (!filename) {
return res.status(400).send("File name required");
}
const filePath = path.join(__dirname, filename);
if (!fs.existsSync(filePath)) {
return res.status(404).send("❌ File not found in /app");
}
res.download(filePath, filename, (err) => {
if (err) {
res.status(500).send("Download failed");
}
});
});
// ===== START SERVER =====
app.listen(7860, () => {
console.log("Server running on port 7860");
});
// ===== AUTO LOG =====
const HF_URL = "http://localhost:7860/run";
setInterval(async () => {
try {
const res = await axios.post(HF_URL, { cmd: "log" });
console.log("🔥 Auto log:", res.data.output || res.data);
} catch (err) {
console.error("⚠ Auto log error:", err.message);
}
}, 120 * 60 * 1000); |