153 lines
5.7 KiB
JavaScript
153 lines
5.7 KiB
JavaScript
const fileSystem = require("../filesystem.json");
|
|
|
|
function getCurrentDir(sessionState) {
|
|
return sessionState.directory;
|
|
}
|
|
|
|
function customTextResponse(req, res, txt) {
|
|
respondToCommand(req, res, txt, req.session.state.directory, "");
|
|
}
|
|
|
|
function ls(req, res) {
|
|
const command = "ls";
|
|
const contents = fileSystem[req.session.state.directory];
|
|
|
|
if (contents && typeof contents === 'object') {
|
|
let keys = Object.keys(contents);
|
|
respondToCommand(req, res, keys.join(" "), req.session.state.directory, command);
|
|
} else {
|
|
respondToCommand(req, res, "No Files Found In Current Directory", req.session.state.directory, command);
|
|
}
|
|
}
|
|
|
|
function whoami(req, res) {
|
|
const command = "whoami";
|
|
respondToCommand(req, res, req.session.state.username, req.session.state.directory, command);
|
|
}
|
|
|
|
function cd(req, res, raw_command) {
|
|
const command = "cd";
|
|
let argument = raw_command.split(" ")[1];
|
|
const contents = fileSystem[req.session.state.directory];
|
|
|
|
if (contents && typeof contents === "object") {
|
|
let keys = Object.keys(contents);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
let key = keys[i];
|
|
|
|
if (key === argument && fileSystem[req.session.state.directory][key] === "dir") {
|
|
req.session.state.directory += argument;
|
|
if (!req.session.state.directory.endsWith("/")) req.session.state.directory += "/";
|
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
else if (key === argument && fileSystem[req.session.state.directory][key] === "noperms") {
|
|
respondToCommand(req, res, "You don't have permission to open this directory", req.session.state.directory, command);
|
|
return;
|
|
} else if (key === argument) {
|
|
respondToCommand(req, res, "You cannot open a file as a directory", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (argument === "/") {
|
|
req.session.state.directory = "/";
|
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
|
|
if (argument === "~" || argument === "~/"){
|
|
req.session.state.directory = `/home/${req.session.state.username}`;
|
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
|
|
if (argument === ".." || argument === "../") {
|
|
const lastSlashIndex = req.session.state.directory.lastIndexOf('/');
|
|
const pathWithoutTrailingSlash = lastSlashIndex === req.session.state.directory.length - 1 ? req.session.state.directory.substring(0, req.session.state.directory.length - 1) : req.session.state.directory;
|
|
const secondLastSlashIndex = pathWithoutTrailingSlash.lastIndexOf('/');
|
|
req.session.state.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
|
|
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
|
|
respondToCommand(req, res, "Unable to open directory", req.session.state.directory, command);
|
|
} else {
|
|
respondToCommand(req, res, "Internal Server Error, try again later.", req.session.state.directory, command);
|
|
}
|
|
}
|
|
|
|
function pwd(req, res) {
|
|
const command = "pwd";
|
|
respondToCommand(req, res, req.session.state.directory, req.session.state.directory, command);
|
|
}
|
|
|
|
function cat(req, res, raw_command) {
|
|
const command = "cat";
|
|
let argument = raw_command.split(" ")[1];
|
|
const contents = fileSystem[req.session.state.directory];
|
|
|
|
if (contents && typeof contents === "object") {
|
|
let keys = Object.keys(contents);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
let key = keys[i];
|
|
|
|
if (key === argument && fileSystem[req.session.state.directory][key] !== "dir") {
|
|
respondToCommand(req, res, fileSystem[req.session.state.directory][key], req.session.state.directory, command);
|
|
return;
|
|
} else if (key === argument) {
|
|
respondToCommand(req, res, "You cannot open a directory as a file", req.session.state.directory, command);
|
|
return;
|
|
}
|
|
}
|
|
respondToCommand(req, res, "Unable to open file", req.session.state.directory, command);
|
|
} else {
|
|
respondToCommand(req, res, "Internal Server Error, try again later.", req.session.state.directory, command);
|
|
}
|
|
}
|
|
|
|
function help(req, res) {
|
|
const command = "help";
|
|
const text = `
|
|
Welcome to the fancy help menu!!<br>
|
|
Here is a list of the available commands:<br><br>
|
|
|
|
"help" - shows you this menu<br>
|
|
"clear" - clears the screen from previous ran commands<br>
|
|
"ls" - shows you the files and directories in your current directory<br>
|
|
"cd" - move into a folder you specify, you can use the name of the folder, or you can also use things like ../ to move back one folder<br>
|
|
"whoami" - shows you as what user you are logged in as<br>
|
|
"pwd" - shows you the current directory you are currently working in<br>
|
|
"cat" - shows you the content of files, for example, cat file.txt shows you the content of file.txt if it is in the current directory<br>
|
|
`;
|
|
respondToCommand(req, res, text, req.session.state.directory, command);
|
|
}
|
|
|
|
|
|
function info(req, res){
|
|
|
|
}
|
|
|
|
function respondToCommand(req, res, text, directory, command) {
|
|
res.json({
|
|
"username": req.session.state.username,
|
|
"directory": directory,
|
|
"response": text,
|
|
"command": command
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
getCurrentDir,
|
|
customTextResponse,
|
|
|
|
ls,
|
|
whoami,
|
|
cd,
|
|
pwd,
|
|
cat,
|
|
help,
|
|
info,
|
|
};
|