From 5baa352b04836039cab5f2c74d5f6545c526539f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Sat, 11 Jun 2022 20:49:24 +0200 Subject: [PATCH] convert to TypeScript and fix bugs --- .vscode/launch.json | 3 + .vscode/tasks.json | 15 ++ index.js | 159 ++++++------------ index.js.map | 1 + index.ts | 390 ++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 190 +++++++++++++++++++++ package.json | 5 + sessions.json | 1 + tsconfig.json | 12 ++ 9 files changed, 670 insertions(+), 106 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 index.js.map create mode 100644 index.ts create mode 100644 sessions.json create mode 100644 tsconfig.json diff --git a/.vscode/launch.json b/.vscode/launch.json index ab42783..6fbb605 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,9 +4,12 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "type": "pwa-node", "request": "launch", + //require compile typescript task + "preLaunchTask": "Compile TypeScript", "name": "Launch Program", "skipFiles": [ "/**" diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..f24f68b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,15 @@ +//add typescript compile task +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Compile TypeScript", + "type": "shell", + "command": "npx tsc", + "group": "build", + "presentation": { + "reveal": "always" + } + } + ] +} diff --git a/index.js b/index.js index 0b4d899..7c95d8b 100644 --- a/index.js +++ b/index.js @@ -1,91 +1,72 @@ -//start ws server with socket.io +"use strict"; +exports.__esModule = true; +var SerAny = require('serialize-anything'); var http = require('http'); var express = require('express'); var app = express(); var server = http.createServer(app); var port = process.env.PORT || 8480; -const { Server } = require("socket.io"); -const { abort } = require('process'); -const io = new Server(server); -app.get('/', (req, res) => { +var fs = require('fs'); +var Server = require("socket.io").Server; +var abort = require('process').abort; +var io = new Server(server); +app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); - }); -//initialize buffer -var buffer = []; -var filesystem = []; -var curdir = []; -var sessions = []; -var curdirx = []; -//on connection +}); +var buffer = {}; +var filesystem = {}; +var curdir = {}; +var sessions = {}; +var curdirx = {}; function sendbuffer(socket, bufferx) { var bufx = bufferx[socket.id]; - //add placeholder character(reserved unicode) for blinking cursor in client; bufx += "\u2588"; socket.emit('buffer', bufx); } -io.on('connection', (socket) => { +io.on('connection', function (socket) { console.log('a user connected'); buffer[socket.id] = ""; filesystem[socket.id] = {}; curdir[socket.id] = filesystem[socket.id]; curdirx[socket.id] = []; - //welcome message buffer[socket.id] = "Welcome to the console!\n"; buffer[socket.id] += "Type 'help' to see the available commands.\n"; buffer[socket.id] += "#"; - //send buffer sendbuffer(socket, buffer); - - socket.on('keypress', (data) => { + socket.on('keypress', function (data) { console.log('a key was pressed'); - //get key - //convert data to string var key = String.fromCharCode(data); - //convert key to lowercase key = key.toLowerCase(); console.log(data); - //check if key is printable character or digit or . or space etc - - //check if key is enter - if (data == 8){ - //backspace - //if line is not empty remove last character if it is not the first character + if (data == 8) { var lines = buffer[socket.id].split("\n"); if (lines[lines.length - 1] != "#") { lines[lines.length - 1] = lines[lines.length - 1].slice(0, -1); buffer[socket.id] = lines.join("\n"); } } - else if(data == 13) { - //execute command - let lines = buffer[socket.id].split('\n'); - let command = lines[lines.length-1]; - //remove # from command + else if (data == 13) { + var lines_1 = buffer[socket.id].split('\n'); + var command = lines_1[lines_1.length - 1]; command = command.substring(1); - //remove \n and everything after command = command.substring(command.indexOf('\n'), command.length); - let args = command.split(' '); - let commandName = args[0]; - let commandArgs = args.slice(1); - //remove trailing spaces - commandArgs = commandArgs.map(function(arg) { + var args = command.split(' '); + var commandName = args[0]; + var commandArgs = args.slice(1); + commandArgs = commandArgs.map(function (arg) { return arg.trim(); }); - //add \n to buffer buffer[socket.id] += "\n"; - //remove # in beginning of commandname console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\""); resolveCommand(commandName, socket, commandArgs); } - else{ - //add key to buffer + else { buffer[socket.id] += key; } - //send buffer to client sendbuffer(socket, buffer); }); }); -io.on('disconnect', (socket) => { +io.on('disconnect', function (socket) { console.log('user disconnected'); buffer[socket.id] = ""; }); @@ -106,11 +87,8 @@ function resolveCommand(commandname, socket, args) { buffer[socket.id] += "mv - moves a file\n"; buffer[socket.id] += "cp - copies a file\n"; buffer[socket.id] += "rmdir - removes a directory\n"; - //spam buffer[socket.id] += "spam - spams the console\n"; - //command to write rest of args to file buffer[socket.id] += "write - writes the arguments to a file\n"; - //command to take a screenshot which is saved in the current directory with filename from args buffer[socket.id] += "screenshot - takes a screenshot\n"; break; case "savesess": @@ -120,10 +98,8 @@ function resolveCommand(commandname, socket, args) { sessions[args[1]]["buffer"] = buffer[socket.id].slice(); sessions[args[1]]["filesystem"] = JSON.parse(JSON.stringify(filesystem[socket.id])); sessions[args[1]]["curdir"] = JSON.parse(JSON.stringify(curdir[socket.id])); - var data = JSON.stringify(sessions); - //write all sessions to a single file called sessions.json + var data = SerAny.serialize(sessions); fs.writeFileSync("./sessions.json", data); - buffer[socket.id] += "Session saved.\n"; } else { @@ -136,8 +112,8 @@ function resolveCommand(commandname, socket, args) { break; case "loadsess": if (args.length == 1) { - if(sessions[args[0]] != undefined) { - sessions = JSON.parse(fs.readFileSync("./sessions.json")); + sessions = SerAny.deserialize(fs.readFileSync("./sessions.json")); + if (sessions[args[0]] != undefined) { filesystem[socket.id] = sessions[args[0]]["filesystem"]; curdir[socket.id] = sessions[args[0]]["curdir"]; buffer[socket.id] = sessions[args[0]]["buffer"]; @@ -145,21 +121,17 @@ function resolveCommand(commandname, socket, args) { } } else { - } break; - case "wee": buffer[socket.id] += "woo\n"; break; case "woo": buffer[socket.id] += "wee\n"; break; - //implement more commands case "screenshot": curdir[socket.id][args[0]] = buffer[socket.id]; buffer[socket.id] += "Screenshot saved to " + args[0] + "\n"; - break; case "rm": if (curdir[socket.id][args[0]] != undefined) { @@ -219,8 +191,7 @@ function resolveCommand(commandname, socket, args) { } break; case "spam": - //spam arguments times from first argument - for (let i = 0; i < args[0]; i++) { + for (var i = 0; i < parseInt(args[0]); i++) { buffer[socket.id] += args.slice(1).join(" ") + "\n"; } break; @@ -229,39 +200,36 @@ function resolveCommand(commandname, socket, args) { buffer[socket.id] += "touch: missing operand\n"; } else { - filesystem[socket.id][args[0]] = ""; + curdir[socket.id][args[0]] = ""; } - break; case "write": if (args.length == 0) { buffer[socket.id] += "write: missing operand\n"; } else { - filesystem[socket.id][args[0]] = ""; - for (let i = 1; i < args.length; i++) { - filesystem[socket.id][args[0]] += args[i] + " "; + curdir[socket.id][args[0]] = ""; + for (var i = 1; i < args.length; i++) { + curdir[socket.id][args[0]] += args[i] + " "; } - } + } break; case "cat": if (args.length == 0) { buffer[socket.id] += "cat: missing operand\n"; } else { - let file = args[0]; + var file = args[0]; if (curdir[socket.id][file] == undefined) { buffer[socket.id] += "cat: " + file + ": No such file or directory\n"; } - //check if type string - else if(typeof curdir[socket.id][file] == "string") { + else if (typeof curdir[socket.id][file] == "string") { buffer[socket.id] += curdir[socket.id][file] + "\n"; } - else{ + else { buffer[socket.id] += "cat: " + file + ": Is not a file\n"; } } - break; case "echo": buffer[socket.id] += args.join(' '); @@ -269,20 +237,15 @@ function resolveCommand(commandname, socket, args) { break; case "clear": buffer[socket.id] = ""; - break; case "exit": buffer[socket.id] = ""; buffer[socket.id] += "Bye!\n"; - socket.disconnect(); break; case "ls": buffer[socket.id] += "Files in the current directory:\n"; - //list only first level files for (var key in curdir[socket.id]) { - //buffer[socket.id] += key + "\n"; - //also print if it is a directory or file if (typeof curdir[socket.id][key] == "string") { buffer[socket.id] += key + " (file)\n"; } @@ -293,86 +256,70 @@ function resolveCommand(commandname, socket, args) { buffer[socket.id] += key + " (unknown)\n"; } } - break; case "cd": if (args.length == 0) { buffer[socket.id] += "Please specify a directory.\n"; - } else { - if(args[0] == "..") { - //go up one directory - //recursively scan filesystem until we find curdir and then set curdir to its parent - //check curdirx is not empty + if (args[0] == "..") { if (curdirx[socket.id].length > 0) { - //pop off last element - sync = "filesystem[socket.id]"; - for(let i = 0; i < curdirx[socket.id].length; i++) { + var sync = "filesystem[socket.id]"; + for (var i = 0; i < curdirx[socket.id].length; i++) { sync += "['" + curdirx[socket.id][i] + "']"; } - eval(sync + " = curdir[socket.id]"); + sync += " = curdir[socket.id];"; + console.log(sync); + eval(sync); curdirx[socket.id].pop(); - //curdir = filesystem[socket.id][(curdirx[socket.id][0])][(curdirx[socket.id][1])].......; - //i know i shouldnt use eval but I don't know any other way to do this - cmdexec = "curdir = filesystem[socket.id]"; - //syncing = write curdir to a path in the filesystem - for (let i = 0; i < curdirx[socket.id].length; i++) { + var cmdexec = "curdir[socket.id] = filesystem[socket.id]"; + for (var i = 0; i < curdirx[socket.id].length; i++) { cmdexec += "[(curdirx[socket.id][" + i + "])]"; } - eval(cmdexec); - } + eval(cmdexec); + } } else { if (curdir[socket.id][args[0]] != undefined) { - //check if type dict if (typeof curdir[socket.id][args[0]] == "object") { curdir[socket.id] = curdir[socket.id][args[0]]; buffer[socket.id] += "Changed directory to " + args[0] + "\n"; curdirx[socket.id].push(args[0]); } - else{ + else { buffer[socket.id] += "cd: " + args[0] + ": Is not a directory\n"; } - } else { buffer[socket.id] += "Directory " + args[0] + " does not exist.\n"; - } } } break; case "pwd": var curdirasstring = curdirx[socket.id].join("/"); - buffer[socket.id] += "Current directory: " + curdirasstring + "\n"; - break; case "mkdir": if (args.length == 0) { buffer[socket.id] += "Please specify a directory.\n"; - } else { - if (filesystem[socket.id][args[0]]) { + if (curdir[socket.id][args[0]]) { buffer[socket.id] += "Directory " + args[0] + " already exists.\n"; - } else { curdir[socket.id][args[0]] = {}; buffer[socket.id] += "Created directory " + args[0] + "\n"; - } } break; - //invalid command default: buffer[socket.id] += "Invalid command.\n"; } buffer[socket.id] += "#"; - } -server.listen(port, function() { +server.listen(port, function () { console.log('Server listening at port %d', port); -}); \ No newline at end of file +}); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/index.js.map b/index.js.map new file mode 100644 index 0000000..6e22ac0 --- /dev/null +++ b/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AACA,IAAM,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAC7C,IAAI,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC3B,IAAI,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEjC,IAAI,GAAG,GAAG,OAAO,EAAE,CAAC;AACpB,IAAI,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;AACpC,IAAI,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC;AACpC,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACf,IAAA,MAAM,GAAK,OAAO,CAAC,WAAW,CAAC,OAAzB,CAA0B;AAChC,IAAA,KAAK,GAAK,OAAO,CAAC,SAAS,CAAC,MAAvB,CAAwB;AAErC,IAAM,EAAE,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;AAC9B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,UAAC,GAAW,EAAE,GAAY;IACnC,GAAG,CAAC,QAAQ,CAAC,SAAS,GAAG,aAAa,CAAC,CAAC;AAC1C,CAAC,CAAC,CAAC;AAIL,IAAI,MAAM,GAA6B,EAAE,CAAC;AAE1C,IAAI,UAAU,GAAQ,EAAE,CAAC;AAEzB,IAAI,MAAM,GAAQ,EAAE,CAAC;AAErB,IAAI,QAAQ,GAAQ,EAAE,CAAC;AAEvB,IAAI,OAAO,GAAQ,EAAE,CAAC;AAEtB,SAAS,UAAU,CAAC,MAAa,EAAE,OAAiC;IAChE,IAAI,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAE9B,IAAI,IAAI,QAAQ,CAAC;IAC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CAAC,MAAM,CAAC,IAAI,EAAE;IAChB,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..5cd8f1a --- /dev/null +++ b/index.ts @@ -0,0 +1,390 @@ +//start ws server with socket.io +const SerAny = require('serialize-anything'); +var http = require('http'); +var express = require('express'); +import { Request, Response } from 'express'; +var app = express(); +var server = http.createServer(app); +var port = process.env.PORT || 8480; +var fs = require('fs'); +const { Server } = require("socket.io"); +const { abort } = require('process'); +import { Socket } from 'socket.io'; +const io = new Server(server); +app.get('/', (req:Request, res:Response) => { + res.sendFile(__dirname + '/index.html'); + }); +//initialize buffer +//var buffer = []; +//add type for typescript +var buffer: { [id: string]: string } = {}; +//var filesystem = []; +var filesystem: any = {}; +//var curdir = []; +var curdir: any = {}; +//var sessions = []; +var sessions: any = {}; +//var curdirx = []; +var curdirx: any = {}; +//on connection +function sendbuffer(socket:Socket, bufferx: { [id: string]: string }) { + var bufx = bufferx[socket.id]; + //add placeholder character(reserved unicode) for blinking cursor in client; + bufx += "\u2588"; + socket.emit('buffer', bufx); +} +io.on('connection', (socket:Socket) => { + console.log('a user connected'); + buffer[socket.id] = ""; + filesystem[socket.id] = {}; + curdir[socket.id] = filesystem[socket.id]; + curdirx[socket.id] = []; + //welcome message + buffer[socket.id] = "Welcome to the console!\n"; + buffer[socket.id] += "Type 'help' to see the available commands.\n"; + buffer[socket.id] += "#"; + //send buffer + sendbuffer(socket, buffer); + + socket.on('keypress', (data) => { + console.log('a key was pressed'); + //get key + //convert data to string + var key = String.fromCharCode(data); + //convert key to lowercase + key = key.toLowerCase(); + console.log(data); + //check if key is printable character or digit or . or space etc + + //check if key is enter + if (data == 8){ + //backspace + //if line is not empty remove last character if it is not the first character + var lines = buffer[socket.id].split("\n"); + if (lines[lines.length - 1] != "#") { + lines[lines.length - 1] = lines[lines.length - 1].slice(0, -1); + buffer[socket.id] = lines.join("\n"); + } + } + else if(data == 13) { + //execute command + let lines = buffer[socket.id].split('\n'); + let command = lines[lines.length-1]; + //remove # from command + command = command.substring(1); + //remove \n and everything after + command = command.substring(command.indexOf('\n'), command.length); + let args = command.split(' '); + let commandName = args[0]; + let commandArgs = args.slice(1); + //remove trailing spaces + commandArgs = commandArgs.map(function(arg) { + return arg.trim(); + }); + //add \n to buffer + buffer[socket.id] += "\n"; + //remove # in beginning of commandname + console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\""); + resolveCommand(commandName, socket, commandArgs); + } + else{ + //add key to buffer + buffer[socket.id] += key; + } + //send buffer to client + sendbuffer(socket, buffer); + }); +}); +io.on('disconnect', (socket:Socket) => { + console.log('user disconnected'); + buffer[socket.id] = ""; +}); +function resolveCommand(commandname:string, socket:Socket, args:string[]) { + switch (commandname) { + case "help": + buffer[socket.id] += "Available commands:\n"; + buffer[socket.id] += "help - shows this help\n"; + buffer[socket.id] += "clear - clears the console\n"; + buffer[socket.id] += "exit - exits the console\n"; + buffer[socket.id] += "ls - lists the files in the current directory\n"; + buffer[socket.id] += "cd - changes the current directory\n"; + buffer[socket.id] += "echo - prints the arguments\n"; + buffer[socket.id] += "mkdir - creates a new directory\n"; + buffer[socket.id] += "cat - prints the contents of a file\n"; + buffer[socket.id] += "touch - creates a new file\n"; + buffer[socket.id] += "rm - removes a file\n"; + buffer[socket.id] += "mv - moves a file\n"; + buffer[socket.id] += "cp - copies a file\n"; + buffer[socket.id] += "rmdir - removes a directory\n"; + //spam + buffer[socket.id] += "spam - spams the console\n"; + //command to write rest of args to file + buffer[socket.id] += "write - writes the arguments to a file\n"; + //command to take a screenshot which is saved in the current directory with filename from args + buffer[socket.id] += "screenshot - takes a screenshot\n"; + break; + case "savesess": + if (args.length == 2) { + if (args[0] == "kryptic") { + sessions[args[1]] = []; + sessions[args[1]]["buffer"] = buffer[socket.id].slice(); + sessions[args[1]]["filesystem"] = JSON.parse(JSON.stringify(filesystem[socket.id])); + sessions[args[1]]["curdir"] = JSON.parse(JSON.stringify(curdir[socket.id])); + var data = SerAny.serialize(sessions); + //write all sessions to a single file called sessions.json + fs.writeFileSync("./sessions.json", data); + + buffer[socket.id] += "Session saved.\n"; + } + else { + buffer[socket.id] += "Invalid command."; + } + } + else { + buffer[socket.id] += "Invalid command."; + } + break; + case "loadsess": + if (args.length == 1) { + sessions = SerAny.deserialize(fs.readFileSync("./sessions.json")); + if(sessions[args[0]] != undefined) { + filesystem[socket.id] = sessions[args[0]]["filesystem"]; + curdir[socket.id] = sessions[args[0]]["curdir"]; + buffer[socket.id] = sessions[args[0]]["buffer"]; + buffer[socket.id] += "Session loaded.\n"; + } + } + else { + + } + break; + + case "wee": + buffer[socket.id] += "woo\n"; + break; + case "woo": + buffer[socket.id] += "wee\n"; + break; + //implement more commands + case "screenshot": + curdir[socket.id][args[0]] = buffer[socket.id]; + buffer[socket.id] += "Screenshot saved to " + args[0] + "\n"; + + break; + case "rm": + if (curdir[socket.id][args[0]] != undefined) { + if (typeof curdir[socket.id][args[0]] == "string") { + delete curdir[socket.id][args[0]]; + buffer[socket.id] += "File " + args[0] + " removed\n"; + } + else { + buffer[socket.id] += "Error: " + args[0] + " is not a file\n"; + } + } + else { + buffer[socket.id] += "Error: " + args[0] + " does not exist\n"; + } + break; + case "mv": + if (curdir[socket.id][args[0]] != undefined) { + if (typeof curdir[socket.id][args[0]] == "string") { + curdir[socket.id][args[1]] = curdir[socket.id][args[0]]; + delete curdir[socket.id][args[0]]; + buffer[socket.id] += "File " + args[0] + " moved to " + args[1] + "\n"; + } + else { + buffer[socket.id] += "Error: " + args[0] + " is not a file\n"; + } + } + else { + buffer[socket.id] += "Error: " + args[0] + " does not exist\n"; + } + break; + case "cp": + if (curdir[socket.id][args[0]] != undefined) { + if (typeof curdir[socket.id][args[0]] == "string") { + curdir[socket.id][args[1]] = curdir[socket.id][args[0]].slice(0); + buffer[socket.id] += "File " + args[0] + " copied to " + args[1] + "\n"; + } + else { + buffer[socket.id] += "Error: " + args[0] + " is not a file\n"; + } + } + else { + buffer[socket.id] += "Error: " + args[0] + " does not exist\n"; + } + break; + case "rmdir": + if (curdir[socket.id][args[0]] != undefined) { + if (typeof curdir[socket.id][args[0]] == "object") { + delete curdir[socket.id][args[0]]; + buffer[socket.id] += "Directory " + args[0] + " removed\n"; + } + else { + buffer[socket.id] += "Error: " + args[0] + " is not a directory\n"; + } + } + else { + buffer[socket.id] += "Error: " + args[0] + " does not exist\n"; + } + break; + case "spam": + //spam arguments times from first argument + for (let i = 0; i < parseInt(args[0]); i++) { + buffer[socket.id] += args.slice(1).join(" ") + "\n"; + } + break; + case "touch": + if (args.length == 0) { + buffer[socket.id] += "touch: missing operand\n"; + } + else { + curdir[socket.id][args[0]] = ""; + } + + break; + case "write": + if (args.length == 0) { + buffer[socket.id] += "write: missing operand\n"; + } + else { + curdir[socket.id][args[0]] = ""; + for (let i = 1; i < args.length; i++) { + curdir[socket.id][args[0]] += args[i] + " "; + } + } + break; + case "cat": + if (args.length == 0) { + buffer[socket.id] += "cat: missing operand\n"; + } + else { + let file = args[0]; + if (curdir[socket.id][file] == undefined) { + buffer[socket.id] += "cat: " + file + ": No such file or directory\n"; + } + //check if type string + else if(typeof curdir[socket.id][file] == "string") { + buffer[socket.id] += curdir[socket.id][file] + "\n"; + } + else{ + buffer[socket.id] += "cat: " + file + ": Is not a file\n"; + } + } + + break; + case "echo": + buffer[socket.id] += args.join(' '); + buffer[socket.id] += "\n"; + break; + case "clear": + buffer[socket.id] = ""; + + break; + case "exit": + buffer[socket.id] = ""; + buffer[socket.id] += "Bye!\n"; + + socket.disconnect(); + break; + case "ls": + buffer[socket.id] += "Files in the current directory:\n"; + //list only first level files + for (var key in curdir[socket.id]) { + //buffer[socket.id] += key + "\n"; + //also print if it is a directory or file + if (typeof curdir[socket.id][key] == "string") { + buffer[socket.id] += key + " (file)\n"; + } + else if (typeof curdir[socket.id][key] == "object") { + buffer[socket.id] += key + " (directory)\n"; + } + else { + buffer[socket.id] += key + " (unknown)\n"; + } + } + + break; + case "cd": + if (args.length == 0) { + buffer[socket.id] += "Please specify a directory.\n"; + + } + else { + if(args[0] == "..") { + //go up one directory + //recursively scan filesystem until we find curdir and then set curdir to its parent + //check curdirx is not empty + if (curdirx[socket.id].length > 0) { + //pop off last element + var sync = "filesystem[socket.id]"; + for(let i = 0; i < curdirx[socket.id].length; i++) { + sync += "['" + curdirx[socket.id][i] + "']"; + } + sync += " = curdir[socket.id];"; + console.log(sync); + eval(sync); + curdirx[socket.id].pop(); + //curdir = filesystem[socket.id][(curdirx[socket.id][0])][(curdirx[socket.id][1])].......; + //i know i shouldnt use eval but I don't know any other way to do this + var cmdexec = "curdir[socket.id] = filesystem[socket.id]"; + //syncing = write curdir to a path in the filesystem + for (let i = 0; i < curdirx[socket.id].length; i++) { + cmdexec += "[(curdirx[socket.id][" + i + "])]"; + } + eval(cmdexec); + } + } + else { + if (curdir[socket.id][args[0]] != undefined) { + //check if type dict + if (typeof curdir[socket.id][args[0]] == "object") { + curdir[socket.id] = curdir[socket.id][args[0]]; + buffer[socket.id] += "Changed directory to " + args[0] + "\n"; + curdirx[socket.id].push(args[0]); + } + else{ + buffer[socket.id] += "cd: " + args[0] + ": Is not a directory\n"; + } + + } + else { + buffer[socket.id] += "Directory " + args[0] + " does not exist.\n"; + + } + } + } + break; + case "pwd": + var curdirasstring = curdirx[socket.id].join("/"); + + buffer[socket.id] += "Current directory: " + curdirasstring + "\n"; + + break; + case "mkdir": + if (args.length == 0) { + buffer[socket.id] += "Please specify a directory.\n"; + + } + else { + if (curdir[socket.id][args[0]]) { + buffer[socket.id] += "Directory " + args[0] + " already exists.\n"; + + } + else { + curdir[socket.id][args[0]] = {}; + buffer[socket.id] += "Created directory " + args[0] + "\n"; + + } + } + break; + //invalid command + default: + buffer[socket.id] += "Invalid command.\n"; + } + buffer[socket.id] += "#"; + +} +server.listen(port, function() { + console.log('Server listening at port %d', port); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4b26ef7..1f9873c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,22 @@ "license": "ISC", "dependencies": { "express": "^4.18.1", + "serialize-anything": "^1.1.10", "socket.io": "^4.5.1" + }, + "devDependencies": { + "@types/express": "^4.17.13", + "typescript": "^4.7.3" + } + }, + "node_modules/@types/body-parser": { + "version": "1.19.2", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", + "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" } }, "node_modules/@types/component-emitter": { @@ -18,6 +33,15 @@ "resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz", "integrity": "sha512-SRXjM+tfsSlA9VuG8hGO2nft2p8zjXCK1VcC6N4NXbBbYbSia9kzCChYQajIjzIqOOOuh5Ock6MmV2oux4jDZQ==" }, + "node_modules/@types/connect": { + "version": "3.4.35", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", + "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/cookie": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.4.1.tgz", @@ -28,11 +52,62 @@ "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", "integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==" }, + "node_modules/@types/express": { + "version": "4.17.13", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.13.tgz", + "integrity": "sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.18", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.17.28", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.28.tgz", + "integrity": "sha512-P1BJAEAW3E2DJUlkgq4tOL3RyMunoWXqbSCygWo5ZIWTjUgN1YnaXWW4VWl/oc8vs/XoYibEGBKP0uZyF4AHig==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*" + } + }, + "node_modules/@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", + "dev": true + }, "node_modules/@types/node": { "version": "17.0.41", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.41.tgz", "integrity": "sha512-xA6drNNeqb5YyV5fO3OAEsnXLfO7uF0whiOfPTz5AeDo8KeZFmODKnvwPymMNO8qE/an8pVY/O50tig2SQCrGw==" }, + "node_modules/@types/qs": { + "version": "6.9.7", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", + "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz", + "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", + "dev": true + }, + "node_modules/@types/serve-static": { + "version": "1.13.10", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.10.tgz", + "integrity": "sha512-nCkHGI4w7ZgAdNkrEu0bv+4xNV/XDqW+DydknebMOQwkpDGx8G+HTlj7R7ABI8i8nKxVw0wtKPi1D+lPOkh4YQ==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -179,6 +254,11 @@ } } }, + "node_modules/deep-copy-all": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/deep-copy-all/-/deep-copy-all-1.3.4.tgz", + "integrity": "sha512-5hHGjeggREJDKWBvxVv5RPKtcgKpraHAPu4DcvUcP8usgmK93UzL9oOPsV3h6VCrD2+wmgq0kHha2rnXZHAV3A==" + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -654,6 +734,14 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, + "node_modules/serialize-anything": { + "version": "1.1.10", + "resolved": "https://registry.npmjs.org/serialize-anything/-/serialize-anything-1.1.10.tgz", + "integrity": "sha512-SdPtJZ44uCS9QK0u0z6UbE8hC9OWU4pbYb+gcJYo7vez7yTTZA6GRMSm0AiyYzY+5ZRuB2w1HHJzZAFt4uyl3Q==", + "dependencies": { + "deep-copy-all": "^1.3.3" + } + }, "node_modules/serve-static": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", @@ -748,6 +836,19 @@ "node": ">= 0.6" } }, + "node_modules/typescript": { + "version": "4.7.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.3.tgz", + "integrity": "sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -794,11 +895,30 @@ } }, "dependencies": { + "@types/body-parser": { + "version": "1.19.2", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", + "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", + "dev": true, + "requires": { + "@types/connect": "*", + "@types/node": "*" + } + }, "@types/component-emitter": { "version": "1.2.11", "resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz", "integrity": "sha512-SRXjM+tfsSlA9VuG8hGO2nft2p8zjXCK1VcC6N4NXbBbYbSia9kzCChYQajIjzIqOOOuh5Ock6MmV2oux4jDZQ==" }, + "@types/connect": { + "version": "3.4.35", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", + "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/cookie": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.4.1.tgz", @@ -809,11 +929,62 @@ "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", "integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==" }, + "@types/express": { + "version": "4.17.13", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.13.tgz", + "integrity": "sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==", + "dev": true, + "requires": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.18", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "@types/express-serve-static-core": { + "version": "4.17.28", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.28.tgz", + "integrity": "sha512-P1BJAEAW3E2DJUlkgq4tOL3RyMunoWXqbSCygWo5ZIWTjUgN1YnaXWW4VWl/oc8vs/XoYibEGBKP0uZyF4AHig==", + "dev": true, + "requires": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*" + } + }, + "@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", + "dev": true + }, "@types/node": { "version": "17.0.41", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.41.tgz", "integrity": "sha512-xA6drNNeqb5YyV5fO3OAEsnXLfO7uF0whiOfPTz5AeDo8KeZFmODKnvwPymMNO8qE/an8pVY/O50tig2SQCrGw==" }, + "@types/qs": { + "version": "6.9.7", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", + "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==", + "dev": true + }, + "@types/range-parser": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz", + "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", + "dev": true + }, + "@types/serve-static": { + "version": "1.13.10", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.10.tgz", + "integrity": "sha512-nCkHGI4w7ZgAdNkrEu0bv+4xNV/XDqW+DydknebMOQwkpDGx8G+HTlj7R7ABI8i8nKxVw0wtKPi1D+lPOkh4YQ==", + "dev": true, + "requires": { + "@types/mime": "^1", + "@types/node": "*" + } + }, "accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -926,6 +1097,11 @@ "ms": "2.1.2" } }, + "deep-copy-all": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/deep-copy-all/-/deep-copy-all-1.3.4.tgz", + "integrity": "sha512-5hHGjeggREJDKWBvxVv5RPKtcgKpraHAPu4DcvUcP8usgmK93UzL9oOPsV3h6VCrD2+wmgq0kHha2rnXZHAV3A==" + }, "depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -1289,6 +1465,14 @@ } } }, + "serialize-anything": { + "version": "1.1.10", + "resolved": "https://registry.npmjs.org/serialize-anything/-/serialize-anything-1.1.10.tgz", + "integrity": "sha512-SdPtJZ44uCS9QK0u0z6UbE8hC9OWU4pbYb+gcJYo7vez7yTTZA6GRMSm0AiyYzY+5ZRuB2w1HHJzZAFt4uyl3Q==", + "requires": { + "deep-copy-all": "^1.3.3" + } + }, "serve-static": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", @@ -1362,6 +1546,12 @@ "mime-types": "~2.1.24" } }, + "typescript": { + "version": "4.7.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.3.tgz", + "integrity": "sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA==", + "dev": true + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index b2e1df6..aca187c 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,11 @@ "license": "ISC", "dependencies": { "express": "^4.18.1", + "serialize-anything": "^1.1.10", "socket.io": "^4.5.1" + }, + "devDependencies": { + "@types/express": "^4.17.13", + "typescript": "^4.7.3" } } diff --git a/sessions.json b/sessions.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/sessions.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..df4c454 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "commonjs", + "noImplicitAny": true, + "removeComments": true, + "preserveConstEnums": true, + "sourceMap": true + }, + "files": [ + "index.ts" + ] + } \ No newline at end of file