Xx / server.js
Reaperxxxx's picture
Update server.js
189c642 verified
Raw
History Blame
4.52 kB
const express = require("express");
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
const bcrypt = require("bcrypt");
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const usersFile = path.join(__dirname, "users.json");
const usersDir = path.join(__dirname, "users");
// Ensure `users.json` and `users/` exist
if (!fs.existsSync(usersFile)) fs.writeFileSync(usersFile, JSON.stringify([]));
if (!fs.existsSync(usersDir)) fs.mkdirSync(usersDir);
// Helper to write logs
function appendLog(username, log) {
const userLogsDir = path.join(usersDir, username, "logs.txt");
fs.appendFileSync(userLogsDir, log + "\n");
}
// Endpoint to handle user sign-up
app.post("/signup", async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).send("Username and password are required.");
}
try {
const usersData = JSON.parse(fs.readFileSync(usersFile));
if (usersData.some((user) => user.username === username)) {
return res.status(400).send("User already exists.");
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
// Add new user
usersData.push({ username, password: hashedPassword });
fs.writeFileSync(usersFile, JSON.stringify(usersData, null, 2));
// Create user directory and logs file
const userDir = path.join(usersDir, username);
if (!fs.existsSync(userDir)) {
fs.mkdirSync(userDir, { recursive: true });
}
fs.writeFileSync(path.join(userDir, "logs.txt"), "");
res.send("User signed up successfully.");
} catch (error) {
console.error("Error during signup:", error);
res.status(500).send("Internal server error.");
}
});
// Endpoint to handle login
app.post("/login", async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).send("Username and password are required.");
}
try {
const usersData = JSON.parse(fs.readFileSync(usersFile));
const user = usersData.find((u) => u.username === username);
if (!user) {
return res.status(400).send("Invalid username or password.");
}
// Compare the provided password with the hashed password
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
return res.status(400).send("Invalid username or password.");
}
res.send("Login successful.");
} catch (error) {
console.error("Error during login:", error);
res.status(500).send("Internal server error.");
}
});
// Endpoint to handle terminal commands
app.post("/execute", (req, res) => {
const { username, command } = req.body;
const userDir = path.join(usersDir, username);
if (!fs.existsSync(userDir)) {
return res.status(400).send("User not found.");
}
const child = exec(command, { cwd: userDir });
child.stdout.on("data", (data) => {
appendLog(username, data.trim());
});
child.stderr.on("data", (data) => {
appendLog(username, data.trim());
});
child.on("close", (code) => {
appendLog(username, `Process exited with code ${code}`);
});
res.send("Command executed.");
});
// Endpoint to get user logs
app.get("/logs", (req, res) => {
const { username } = req.query;
const userDir = path.join(usersDir, username);
if (!fs.existsSync(userDir)) {
return res.status(400).send("User not found.");
}
const logs = fs.readFileSync(path.join(userDir, "logs.txt"), "utf-8");
res.send(logs);
});
// Endpoint for "Deploy Kaizen"
app.post("/deploy", (req, res) => {
const { username, repoUrl } = req.body;
const userDir = path.join(usersDir, username);
if (!fs.existsSync(userDir)) {
return res.status(400).send("User not found.");
}
// Clear user directory
fs.rmdirSync(userDir, { recursive: true });
fs.mkdirSync(userDir);
const cloneCommand = `git clone ${repoUrl} . && yarn install && yarn start`;
const child = exec(cloneCommand, { cwd: userDir });
child.stdout.on("data", (data) => {
appendLog(username, data.trim());
});
child.stderr.on("data", (data) => {
appendLog(username, data.trim());
});
child.on("close", (code) => {
appendLog(username, `Deploy process exited with code ${code}`);
});
res.send("Deployment started.");
});
// Start the server
app.listen(7860, () => {
console.log("Server running on http://localhost:3000");
});