changing the commands to ostly run server-side (NOT DONE))

a
This commit is contained in:
valentijn
2024-01-19 19:16:30 +01:00
parent 33db47102c
commit d0fb604680
4 changed files with 59 additions and 89 deletions

View File

@@ -1,34 +1,48 @@
function ls (req, res, dir, fs) { let currentState = {
dir = dir.startsWith('/') ? dir : '/' + dir; "username": "guest",
dir = dir.endsWith('/') ? dir : dir + '/'; "directory": "/"
}
const fileSystem = require("../filesystem.json")
function getCurrentDir(){
return currentState.directory;
}
function customTextResponse(res, txt){
respondToCommand(res, txt, currentState.directory, "");
}
const contents = fs[dir]; function ls(res) {
const command = "ls";
const contents = fileSystem[currentState.directory];
if (contents && typeof contents === 'object') { if (contents && typeof contents === 'object') {
let keys = Object.keys(contents) let keys = Object.keys(contents)
res.json({"response": keys.join(" "), "command": "ls"}); respondToCommand(res, keys.join(" "), currentState.directory, command);
} else { } else {
res.json({"response": "No Files Found!", "command": "ls"}); respondToCommand(res, "No Files Found In Current Directory", currentState.directory, command);
} }
} }
function whoami (req, res){
res.json({"response": "guest", "command": "whoami"});
}
function pwd (req, res, dir){
res.json({"response": `/home/guest${dir}`})
}
function help (req, res){
res.json({"response": "Here is a list of commands:<br><br>help: show this menu.<br>cd: open a directory, for example 'cd myFolder'.<br>ls: shows you a list of the files in the current directory.<br>whoami: shows you who you are logged in as.<br>pwd: shows you the current directory you are located in.", "command":"help"});
}
module.exports = { module.exports = {
ls, getCurrentDir,
whoami, customTextResponse,
pwd,
help ls
}
function respondToCommand(res, text, directory, command){
res.json({
"username": "guest",
"directory": directory,
"response": text,
"command": command
})
} }

10
filesystem.json Normal file
View File

@@ -0,0 +1,10 @@
{
"/": {
"file1.txt": "this is the content of file1",
"projects": "dir"
},
"/projects/": {
"file2.txt": "this is file 2 :0",
"file3.txt": "and file 3 :D"
}
}

View File

@@ -1,54 +1,17 @@
const terminalContainer = document.getElementById("terminal"); const terminalContainer = document.getElementById("terminal");
const hostname = `192.168.${Math.floor(Math.random() * 100)}.${Math.floor(Math.random() * 255)}`; const hostname = `192.168.${Math.floor(Math.random() * 100)}.${Math.floor(Math.random() * 255)}`;
let currentFolder = "/";
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
document.querySelector(".pathText").innerHTML = `guest@${hostname}`; document.querySelector(".pathText").innerHTML = `guest@${hostname}`;
addInputEventListeners(); addInputEventListeners();
}); });
async function sendCommand(e){ async function sendCommand(e){
const command = e.target.value; const command = e.target.value;
deactivateAllStuff(); deactivateAllStuff();
if (command.startsWith("cd")){ if (command == "clear"){
let commandSplit = command.split(" ");
try {
if (commandSplit[1].startsWith("/")){
currentFolder = commandSplit[1];
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
}
else if (commandSplit[1].startsWith("~")){
currentFolder = "/";
}
else if (commandSplit[1].startsWith("./")) {
currentFolder += commandSplit[1].slice(2);
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
} else {
currentFolder += commandSplit[1];
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
}
createNewTerminalLine();
return;
} catch {
handleExecutionResponse({"response": "Invalid Argument"})
return;
}
}
if (command.startsWith("clear")){
lines = document.querySelectorAll(".terminalLine").forEach((line)=>{ lines = document.querySelectorAll(".terminalLine").forEach((line)=>{
line.remove(); line.remove();
}) })

View File

@@ -8,17 +8,6 @@ app.use(express.static("httpdocs/public"))
const documentRoot = `${__dirname}/httpdocs` const documentRoot = `${__dirname}/httpdocs`
let fileSystem = {
"/": {
"file1.txt": "this is the content of file1",
"projects": "dir"
},
"/projects/": {
"file2.txt": "this is file 2 :0",
"file3.txt": "and file 3 :D"
}
}
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.sendFile(documentRoot+"/index.html") res.sendFile(documentRoot+"/index.html")
@@ -27,30 +16,24 @@ app.get('/', (req, res) => {
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 == ""){
res.json({"response": ""}); commands.customTextResponse(res, "");
return; return;
} }
const formatted_command = String(raw_command).trim(); const formatted_command = raw_command.trim();
const command = formatted_command.split(" ")[0]
const currentDir = req.query.currentDir 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);
}
})
if (formatted_command.startsWith("ls")){ app.get("/currentDir", (req, res)=>{
commands.ls(req, res, currentDir, fileSystem); res.send(commands.getCurrentDir());
}
else if (formatted_command.startsWith("whoami")){
commands.whoami(req, res);
}
else if (formatted_command.startsWith("pwd")){
commands.pwd(req, res, currentDir);
}
else if (formatted_command.startsWith("help"))
{
commands.help(req, res);
}
else {
res.json({"response": "Unknown command, please try again later, or run 'help' for a list of commands"});
}
}) })
app.listen(port, () => { app.listen(port, () => {