added some more commands
This commit is contained in:
@@ -2,6 +2,7 @@ let currentState = {
|
||||
"username": "guest",
|
||||
"directory": "/"
|
||||
}
|
||||
|
||||
const fileSystem = require("../filesystem.json")
|
||||
|
||||
function getCurrentDir(){
|
||||
@@ -15,8 +16,6 @@ function customTextResponse(res, txt){
|
||||
|
||||
function ls(res) {
|
||||
const command = "ls";
|
||||
|
||||
|
||||
const contents = fileSystem[currentState.directory];
|
||||
|
||||
if (contents && typeof contents === 'object') {
|
||||
@@ -27,13 +26,125 @@ function ls(res) {
|
||||
}
|
||||
}
|
||||
|
||||
function whoami(res){
|
||||
const command = "whoami"
|
||||
respondToCommand(res, currentState.username, currentState.directory, command);
|
||||
}
|
||||
|
||||
function cd (res, raw_command) {
|
||||
const command = "cd";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.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 == arguement && fileSystem[currentState.directory][key] == "dir"){
|
||||
currentState.directory = currentState.directory + arguement;
|
||||
|
||||
if (!currentState.directory.endsWith("/")) currentState.directory += "/";
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
return;
|
||||
}
|
||||
else if (key == arguement){
|
||||
respondToCommand(res, "You cannot open a file as a directory", currentState.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// check a few things:
|
||||
if (arguement == "/" || arguement == "~" || arguement == "~/"){
|
||||
currentState.directory = "/";
|
||||
respondToCommand(res, "", currentState.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;
|
||||
const secondLastSlashIndex = pathWithoutTrailingSlash.lastIndexOf('/');
|
||||
currentState.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
||||
|
||||
respondToCommand(res, "", currentState.directory, command);
|
||||
return;
|
||||
}
|
||||
|
||||
respondToCommand(res, "Unable to open directory", currentState.directory, command);
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
}
|
||||
}
|
||||
|
||||
function pwd (res){
|
||||
const command = "pwd";
|
||||
respondToCommand(res, currentState.directory, currentState.directory, command);
|
||||
}
|
||||
|
||||
function cat (res, raw_command){
|
||||
const command = "cat";
|
||||
let arguement = raw_command.split(" ")[1]
|
||||
|
||||
//check if folder to cd to is in current dir
|
||||
const contents = fileSystem[currentState.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 == arguement && fileSystem[currentState.directory][key] != "dir"){
|
||||
respondToCommand(res, fileSystem[currentState.directory][key], currentState.directory, command);
|
||||
return;
|
||||
}
|
||||
else if (key == arguement){
|
||||
respondToCommand(res, "You cannot open a directory as a file", currentState.directory, command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
respondToCommand(res, "Unable to open file", currentState.directory, command)
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function help(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>
|
||||
"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, 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);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getCurrentDir,
|
||||
customTextResponse,
|
||||
|
||||
ls
|
||||
ls,
|
||||
whoami,
|
||||
cd,
|
||||
pwd,
|
||||
cat,
|
||||
help,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
{
|
||||
"/": {
|
||||
"file1.txt": "this is the content of file1",
|
||||
"projects": "dir"
|
||||
"file1.txt": "this is the content of file1<br> i like cheese :)",
|
||||
"projects": "dir",
|
||||
"another_folder": "dir"
|
||||
},
|
||||
"/projects/": {
|
||||
"file2.txt": "this is file 2 :0",
|
||||
"file3.txt": "and file 3 :D"
|
||||
"file3.txt": "and file 3 :D",
|
||||
"kaas": "dir"
|
||||
},
|
||||
"/projects/kaas/": {
|
||||
"file4.txt": "HOW DARE YOU OPEN THIS FILE,<br> NOW YOU WILL BURN IN HELL >:("
|
||||
},
|
||||
"/another_folder/": {
|
||||
"main.py": "this is totally a python script :)"
|
||||
}
|
||||
}
|
||||
@@ -11,17 +11,20 @@ async function sendCommand(e){
|
||||
const command = e.target.value;
|
||||
deactivateAllStuff();
|
||||
|
||||
if (command == "clear"){
|
||||
if (command.trim() == "clear"){
|
||||
lines = document.querySelectorAll(".terminalLine").forEach((line)=>{
|
||||
line.remove();
|
||||
})
|
||||
createNewTerminalLine();
|
||||
return;
|
||||
}
|
||||
else if (command.trim() == "logout" || command.trim() == "exit"){
|
||||
window.location.href = "https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwj2jve6z_iDAxWb9rsIHflPD_sQwqsBegQIDxAF&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ&usg=AOvVaw0aHtehaphMhOCAkCydRLZU&opi=89978449"
|
||||
}
|
||||
|
||||
try {
|
||||
// Construct the URL with the command parameter in the query string
|
||||
const url = "/execute?command=" + encodeURIComponent(command) + "¤tDir=" + encodeURIComponent(currentFolder);
|
||||
const url = "/execute?command=" + encodeURIComponent(command);
|
||||
|
||||
// Make a GET request using the fetch API
|
||||
const response = await fetch(url);
|
||||
@@ -96,11 +99,14 @@ function createNewTerminalLine(){
|
||||
pathColon.innerHTML = ":";
|
||||
pathColon.classList.add("pathColon");
|
||||
|
||||
|
||||
let pathPath = document.createElement("h4");
|
||||
pathPath.innerHTML = "~"+currentFolder;
|
||||
pathPath.classList.add("pathPath");
|
||||
|
||||
fetch('/currentDir')
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
pathPath.innerHTML = "~"+data;
|
||||
pathPath.classList.add("pathPath");
|
||||
})
|
||||
.catch(error => console.error('Error:', error));
|
||||
|
||||
let pathDollar = document.createElement("h4");
|
||||
pathDollar.innerHTML = "$";
|
||||
|
||||
17
server.js
17
server.js
@@ -18,7 +18,7 @@ app.get("/execute", (req, res)=> {
|
||||
if (raw_command == ""){
|
||||
commands.customTextResponse(res, "");
|
||||
return;
|
||||
}
|
||||
}a
|
||||
|
||||
const formatted_command = raw_command.trim();
|
||||
const command = formatted_command.split(" ")[0]
|
||||
@@ -29,6 +29,21 @@ app.get("/execute", (req, res)=> {
|
||||
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);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user