added session so multiple people can use it at once
This commit is contained in:
@@ -1,126 +1,104 @@
|
||||
let currentState = {
|
||||
"username": "guest",
|
||||
"directory": "/"
|
||||
const fileSystem = require("../filesystem.json");
|
||||
|
||||
function getCurrentDir(sessionState) {
|
||||
return sessionState.directory;
|
||||
}
|
||||
|
||||
const fileSystem = require("../filesystem.json")
|
||||
|
||||
function getCurrentDir(){
|
||||
return currentState.directory;
|
||||
function customTextResponse(req, res, txt) {
|
||||
respondToCommand(req, res, txt, req.session.state.directory, "");
|
||||
}
|
||||
|
||||
function customTextResponse(res, txt){
|
||||
respondToCommand(res, txt, currentState.directory, "");
|
||||
}
|
||||
|
||||
|
||||
function ls(res) {
|
||||
function ls(req, res) {
|
||||
const command = "ls";
|
||||
const contents = fileSystem[currentState.directory];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
if (contents && typeof contents === 'object') {
|
||||
let keys = Object.keys(contents)
|
||||
respondToCommand(res, keys.join(" "), currentState.directory, command);
|
||||
let keys = Object.keys(contents);
|
||||
respondToCommand(req, res, keys.join(" "), req.session.state.directory, command);
|
||||
} else {
|
||||
respondToCommand(res, "No Files Found In Current Directory", currentState.directory, command);
|
||||
respondToCommand(req, res, "No Files Found In Current Directory", req.session.state.directory, command);
|
||||
}
|
||||
}
|
||||
|
||||
function whoami(res){
|
||||
const command = "whoami"
|
||||
respondToCommand(res, currentState.username, currentState.directory, command);
|
||||
function whoami(req, res) {
|
||||
const command = "whoami";
|
||||
respondToCommand(req, res, req.session.state.username, req.session.state.directory, command);
|
||||
}
|
||||
|
||||
function cd (res, raw_command) {
|
||||
function cd(req, res, raw_command) {
|
||||
const command = "cd";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
let argument = raw_command.split(" ")[1];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.directory];
|
||||
|
||||
if (contents && typeof contents==="object"){
|
||||
if (contents && typeof contents === "object") {
|
||||
let keys = Object.keys(contents);
|
||||
for (let i = 0; i < keys.length; i++){
|
||||
let key = keys[i]
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
|
||||
if (key == arguement && fileSystem[currentState.directory][key] == "dir"){
|
||||
currentState.directory = currentState.directory + arguement;
|
||||
|
||||
if (!currentState.directory.endsWith("/")) currentState.directory += "/";
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
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 == arguement){
|
||||
respondToCommand(res, "You cannot open a file as a directory", currentState.directory, command);
|
||||
} else if (key === argument) {
|
||||
respondToCommand(req, res, "You cannot open a file as a directory", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// check a few things:
|
||||
if (arguement == "/" || arguement == "~" || arguement == "~/"){
|
||||
currentState.directory = "/";
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
if (argument === "/" || argument === "~" || argument === "~/") {
|
||||
req.session.state.directory = "/";
|
||||
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguement == ".." || arguement == "../"){
|
||||
//move 1 folder back
|
||||
const lastSlashIndex = currentState.directory.lastIndexOf('/');
|
||||
const pathWithoutTrailingSlash = lastSlashIndex === currentState.directory.length - 1 ? currentState.directory.substring(0, currentState.directory.length - 1) : currentState.directory;
|
||||
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('/');
|
||||
currentState.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
||||
req.session.state.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
||||
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
|
||||
respondToCommand(res, "Unable to open directory", currentState.directory, command);
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
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 (res){
|
||||
function pwd(req, res) {
|
||||
const command = "pwd";
|
||||
respondToCommand(res, currentState.directory, currentState.directory, command);
|
||||
respondToCommand(req, res, req.session.state.directory, req.session.state.directory, command);
|
||||
}
|
||||
|
||||
function cat (res, raw_command){
|
||||
function cat(req, res, raw_command) {
|
||||
const command = "cat";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
let argument = raw_command.split(" ")[1];
|
||||
const contents = fileSystem[req.session.state.directory];
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.directory];
|
||||
|
||||
if (contents && typeof contents==="object"){
|
||||
if (contents && typeof contents === "object") {
|
||||
let keys = Object.keys(contents);
|
||||
for (let i = 0; i < keys.length; i++){
|
||||
let key = keys[i]
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
|
||||
if (key == arguement && fileSystem[currentState.directory][key] != "dir"){
|
||||
respondToCommand(res, fileSystem[currentState.directory][key], currentState.directory, command);
|
||||
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 == arguement){
|
||||
respondToCommand(res, "You cannot open a directory as a file", currentState.directory, command);
|
||||
} else if (key === argument) {
|
||||
respondToCommand(req, res, "You cannot open a directory as a file", req.session.state.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
respondToCommand(res, "Unable to open file", currentState.directory, command)
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function help(res){
|
||||
const command = "help"
|
||||
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>
|
||||
@@ -130,30 +108,27 @@ function help(res){
|
||||
"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, fox example cat file.txt shows you the content of file.txt if it is in the current directory<br>
|
||||
`
|
||||
respondToCommand(res, text, currentState.directory, command);
|
||||
"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 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,
|
||||
}
|
||||
|
||||
|
||||
|
||||
function respondToCommand(res, text, directory, command){
|
||||
res.json({
|
||||
"username": "guest",
|
||||
"directory": directory,
|
||||
"response": text,
|
||||
"command": command
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
56
package-lock.json
generated
56
package-lock.json
generated
@@ -5,7 +5,8 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3"
|
||||
}
|
||||
},
|
||||
"node_modules/accepts": {
|
||||
@@ -206,6 +207,32 @@
|
||||
"node": ">= 0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/express-session": {
|
||||
"version": "1.17.3",
|
||||
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz",
|
||||
"integrity": "sha512-4+otWXlShYlG1Ma+2Jnn+xgKUZTMJ5QD3YvfilX3AcocOAbIkVylSWEklzALe/+Pu4qV6TYBj5GwOBFfdKqLBw==",
|
||||
"dependencies": {
|
||||
"cookie": "0.4.2",
|
||||
"cookie-signature": "1.0.6",
|
||||
"debug": "2.6.9",
|
||||
"depd": "~2.0.0",
|
||||
"on-headers": "~1.0.2",
|
||||
"parseurl": "~1.3.3",
|
||||
"safe-buffer": "5.2.1",
|
||||
"uid-safe": "~2.1.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/express-session/node_modules/cookie": {
|
||||
"version": "0.4.2",
|
||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz",
|
||||
"integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==",
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/finalhandler": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz",
|
||||
@@ -438,6 +465,14 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/on-headers": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
|
||||
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/parseurl": {
|
||||
"version": "1.3.3",
|
||||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
||||
@@ -477,6 +512,14 @@
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/random-bytes": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
|
||||
"integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==",
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/range-parser": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
|
||||
@@ -626,6 +669,17 @@
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/uid-safe": {
|
||||
"version": "2.1.5",
|
||||
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
|
||||
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
|
||||
"dependencies": {
|
||||
"random-bytes": "~1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/unpipe": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3"
|
||||
}
|
||||
}
|
||||
|
||||
103
server.js
103
server.js
@@ -1,56 +1,71 @@
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
const port = 3000
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
const commands = require("./commands/commands")
|
||||
const commands = require("./commands/commands");
|
||||
|
||||
app.use(express.static("httpdocs/public"))
|
||||
app.use(express.static("httpdocs/public"));
|
||||
|
||||
const documentRoot = `${__dirname}/httpdocs`
|
||||
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) => {
|
||||
res.sendFile(documentRoot+"/index.html")
|
||||
})
|
||||
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(res, "");
|
||||
return;
|
||||
}a
|
||||
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]
|
||||
const formatted_command = raw_command.trim();
|
||||
const command = formatted_command.split(" ")[0];
|
||||
|
||||
switch(command){
|
||||
default:
|
||||
commands.customTextResponse(res, "Command '"+command+"' not found, please try again later, or type 'help' for the list of available commands");
|
||||
break;
|
||||
case "ls":
|
||||
commands.ls(res);
|
||||
break;
|
||||
case "whoami":
|
||||
commands.whoami(res);
|
||||
break;
|
||||
case "cd":
|
||||
commands.cd(res, formatted_command);
|
||||
break;
|
||||
case "pwd":
|
||||
commands.pwd(res);
|
||||
break;
|
||||
case "cat":
|
||||
commands.cat(res, formatted_command )
|
||||
break;
|
||||
case "help":
|
||||
commands.help(res);
|
||||
}
|
||||
})
|
||||
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());
|
||||
})
|
||||
app.get("/currentDir", (req, res) => {
|
||||
res.send(commands.getCurrentDir(req.session.state));
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Epic Terminal App listening on port ${port}`)
|
||||
})
|
||||
console.log(`Epic Terminal App listening on port ${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user