72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
const express = require('express');
|
|
const session = require('express-session');
|
|
const app = express();
|
|
const port = 3000;
|
|
|
|
const commands = require("./commands/commands");
|
|
|
|
app.use(express.static("httpdocs/public"));
|
|
|
|
const documentRoot = `${__dirname}/httpdocs`;
|
|
|
|
// Session configuration
|
|
app.use(session({
|
|
secret: 'your-secret', // Change this to a secret phrase
|
|
resave: false,
|
|
saveUninitialized: true,
|
|
cookie: { secure: false } // Set to true if using https
|
|
}));
|
|
|
|
app.get('/', (req, res) => {
|
|
if (!req.session.state) {
|
|
req.session.state = {
|
|
"username": "guest",
|
|
"directory": "/"
|
|
};
|
|
}
|
|
res.sendFile(documentRoot + "/index.html");
|
|
});
|
|
|
|
app.get("/execute", (req, res) => {
|
|
const raw_command = req.query.command;
|
|
if (raw_command == ""){
|
|
commands.customTextResponse(req, res, "");
|
|
return;
|
|
}
|
|
|
|
const formatted_command = raw_command.trim();
|
|
const command = formatted_command.split(" ")[0];
|
|
|
|
switch(command){
|
|
case "ls":
|
|
commands.ls(req, res);
|
|
break;
|
|
case "whoami":
|
|
commands.whoami(req, res);
|
|
break;
|
|
case "cd":
|
|
commands.cd(req, res, formatted_command);
|
|
break;
|
|
case "pwd":
|
|
commands.pwd(req, res);
|
|
break;
|
|
case "cat":
|
|
commands.cat(req, res, formatted_command);
|
|
break;
|
|
case "help":
|
|
commands.help(req, res);
|
|
break;
|
|
default:
|
|
commands.customTextResponse(req, res, "Command '" + command + "' not found, please try again later, or type 'help' for the list of available commands");
|
|
break;
|
|
}
|
|
});
|
|
|
|
app.get("/currentDir", (req, res) => {
|
|
res.send(commands.getCurrentDir(req.session.state));
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Epic Terminal App listening on port ${port}`);
|
|
});
|