added session so multiple people can use it at once
This commit is contained in:
@@ -1,126 +1,104 @@
|
|||||||
let currentState = {
|
const fileSystem = require("../filesystem.json");
|
||||||
"username": "guest",
|
|
||||||
"directory": "/"
|
function getCurrentDir(sessionState) {
|
||||||
|
return sessionState.directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileSystem = require("../filesystem.json")
|
function customTextResponse(req, res, txt) {
|
||||||
|
respondToCommand(req, res, txt, req.session.state.directory, "");
|
||||||
function getCurrentDir(){
|
|
||||||
return currentState.directory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function customTextResponse(res, txt){
|
function ls(req, res) {
|
||||||
respondToCommand(res, txt, currentState.directory, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function ls(res) {
|
|
||||||
const command = "ls";
|
const command = "ls";
|
||||||
const contents = fileSystem[currentState.directory];
|
const contents = fileSystem[req.session.state.directory];
|
||||||
|
|
||||||
if (contents && typeof contents === 'object') {
|
if (contents && typeof contents === 'object') {
|
||||||
let keys = Object.keys(contents)
|
let keys = Object.keys(contents);
|
||||||
respondToCommand(res, keys.join(" "), currentState.directory, command);
|
respondToCommand(req, res, keys.join(" "), req.session.state.directory, command);
|
||||||
} else {
|
} 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){
|
function whoami(req, res) {
|
||||||
const command = "whoami"
|
const command = "whoami";
|
||||||
respondToCommand(res, currentState.username, currentState.directory, command);
|
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";
|
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
|
if (contents && typeof contents === "object") {
|
||||||
const contents = fileSystem[currentState.directory];
|
|
||||||
|
|
||||||
if (contents && typeof contents==="object"){
|
|
||||||
let keys = Object.keys(contents);
|
let keys = Object.keys(contents);
|
||||||
for (let i = 0; i < keys.length; i++){
|
for (let i = 0; i < keys.length; i++) {
|
||||||
let key = keys[i]
|
let key = keys[i];
|
||||||
|
|
||||||
if (key == arguement && fileSystem[currentState.directory][key] == "dir"){
|
if (key === argument && fileSystem[req.session.state.directory][key] === "dir") {
|
||||||
currentState.directory = currentState.directory + arguement;
|
req.session.state.directory += argument;
|
||||||
|
if (!req.session.state.directory.endsWith("/")) req.session.state.directory += "/";
|
||||||
if (!currentState.directory.endsWith("/")) currentState.directory += "/";
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||||
respondToCommand(res, "", currentState.directory, command);
|
|
||||||
return;
|
return;
|
||||||
}
|
} else if (key === argument) {
|
||||||
else if (key == arguement){
|
respondToCommand(req, res, "You cannot open a file as a directory", req.session.state.directory, command);
|
||||||
respondToCommand(res, "You cannot open a file as a directory", currentState.directory, command);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check a few things:
|
if (argument === "/" || argument === "~" || argument === "~/") {
|
||||||
if (arguement == "/" || arguement == "~" || arguement == "~/"){
|
req.session.state.directory = "/";
|
||||||
currentState.directory = "/";
|
respondToCommand(req, res, "", req.session.state.directory, command);
|
||||||
respondToCommand(res, "", currentState.directory, command);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguement == ".." || arguement == "../"){
|
if (argument === ".." || argument === "../") {
|
||||||
//move 1 folder back
|
const lastSlashIndex = req.session.state.directory.lastIndexOf('/');
|
||||||
const lastSlashIndex = currentState.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 pathWithoutTrailingSlash = lastSlashIndex === currentState.directory.length - 1 ? currentState.directory.substring(0, currentState.directory.length - 1) : currentState.directory;
|
|
||||||
const secondLastSlashIndex = pathWithoutTrailingSlash.lastIndexOf('/');
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
respondToCommand(res, "Unable to open directory", currentState.directory, command);
|
respondToCommand(req, res, "Unable to open directory", req.session.state.directory, command);
|
||||||
}
|
} else {
|
||||||
else
|
respondToCommand(req, res, "Internal Server Error, try again later.", req.session.state.directory, command);
|
||||||
{
|
|
||||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function pwd (res){
|
function pwd(req, res) {
|
||||||
const command = "pwd";
|
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";
|
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
|
if (contents && typeof contents === "object") {
|
||||||
const contents = fileSystem[currentState.directory];
|
|
||||||
|
|
||||||
if (contents && typeof contents==="object"){
|
|
||||||
let keys = Object.keys(contents);
|
let keys = Object.keys(contents);
|
||||||
for (let i = 0; i < keys.length; i++){
|
for (let i = 0; i < keys.length; i++) {
|
||||||
let key = keys[i]
|
let key = keys[i];
|
||||||
|
|
||||||
if (key == arguement && fileSystem[currentState.directory][key] != "dir"){
|
if (key === argument && fileSystem[req.session.state.directory][key] !== "dir") {
|
||||||
respondToCommand(res, fileSystem[currentState.directory][key], currentState.directory, command);
|
respondToCommand(req, res, fileSystem[req.session.state.directory][key], req.session.state.directory, command);
|
||||||
return;
|
return;
|
||||||
}
|
} else if (key === argument) {
|
||||||
else if (key == arguement){
|
respondToCommand(req, res, "You cannot open a directory as a file", req.session.state.directory, command);
|
||||||
respondToCommand(res, "You cannot open a directory as a file", currentState.directory, command);
|
|
||||||
return;
|
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(req, res) {
|
||||||
|
const command = "help";
|
||||||
function help(res){
|
|
||||||
const command = "help"
|
|
||||||
const text = `
|
const text = `
|
||||||
Welcome to the fancy help menu!!<br>
|
Welcome to the fancy help menu!!<br>
|
||||||
Here is a list of the available commands:<br><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>
|
"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>
|
"whoami" - shows you as what user you are logged in as<br>
|
||||||
"pwd" - shows you the current directory you are currently working in<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>
|
"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(res, text, currentState.directory, command);
|
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 = {
|
module.exports = {
|
||||||
getCurrentDir,
|
getCurrentDir,
|
||||||
customTextResponse,
|
customTextResponse,
|
||||||
|
|
||||||
ls,
|
ls,
|
||||||
whoami,
|
whoami,
|
||||||
cd,
|
cd,
|
||||||
pwd,
|
pwd,
|
||||||
cat,
|
cat,
|
||||||
help,
|
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": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2"
|
"express": "^4.18.2",
|
||||||
|
"express-session": "^1.17.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
@@ -206,6 +207,32 @@
|
|||||||
"node": ">= 0.10.0"
|
"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": {
|
"node_modules/finalhandler": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz",
|
||||||
@@ -438,6 +465,14 @@
|
|||||||
"node": ">= 0.8"
|
"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": {
|
"node_modules/parseurl": {
|
||||||
"version": "1.3.3",
|
"version": "1.3.3",
|
||||||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
||||||
@@ -477,6 +512,14 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"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": {
|
"node_modules/range-parser": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
|
||||||
@@ -626,6 +669,17 @@
|
|||||||
"node": ">= 0.6"
|
"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": {
|
"node_modules/unpipe": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
"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 express = require('express');
|
||||||
const app = express()
|
const session = require('express-session');
|
||||||
const port = 3000
|
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) => {
|
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)=> {
|
app.get("/execute", (req, res) => {
|
||||||
const raw_command = req.query.command;
|
const raw_command = req.query.command;
|
||||||
if (raw_command == ""){
|
if (raw_command == ""){
|
||||||
commands.customTextResponse(res, "");
|
commands.customTextResponse(req, res, "");
|
||||||
return;
|
return;
|
||||||
}a
|
}
|
||||||
|
|
||||||
const formatted_command = raw_command.trim();
|
const formatted_command = raw_command.trim();
|
||||||
const command = formatted_command.split(" ")[0]
|
const command = formatted_command.split(" ")[0];
|
||||||
|
|
||||||
switch(command){
|
switch(command){
|
||||||
default:
|
case "ls":
|
||||||
commands.customTextResponse(res, "Command '"+command+"' not found, please try again later, or type 'help' for the list of available commands");
|
commands.ls(req, res);
|
||||||
break;
|
break;
|
||||||
case "ls":
|
case "whoami":
|
||||||
commands.ls(res);
|
commands.whoami(req, res);
|
||||||
break;
|
break;
|
||||||
case "whoami":
|
case "cd":
|
||||||
commands.whoami(res);
|
commands.cd(req, res, formatted_command);
|
||||||
break;
|
break;
|
||||||
case "cd":
|
case "pwd":
|
||||||
commands.cd(res, formatted_command);
|
commands.pwd(req, res);
|
||||||
break;
|
break;
|
||||||
case "pwd":
|
case "cat":
|
||||||
commands.pwd(res);
|
commands.cat(req, res, formatted_command);
|
||||||
break;
|
break;
|
||||||
case "cat":
|
case "help":
|
||||||
commands.cat(res, formatted_command )
|
commands.help(req, res);
|
||||||
break;
|
break;
|
||||||
case "help":
|
default:
|
||||||
commands.help(res);
|
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)=>{
|
app.get("/currentDir", (req, res) => {
|
||||||
res.send(commands.getCurrentDir());
|
res.send(commands.getCurrentDir(req.session.state));
|
||||||
})
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
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