do some stuff

and prepare for a system each command to a file
This commit is contained in:
Bruno Rybársky 2022-06-11 22:12:19 +02:00
parent 5baa352b04
commit 67c1e35608
27 changed files with 783 additions and 543 deletions

0
commands/append.ts Normal file

0
commands/cat.ts Normal file

0
commands/cd.ts Normal file

0
commands/clear.ts Normal file

372
commands/commands.js Normal file

@ -0,0 +1,372 @@
"use strict";
exports.__esModule = true;
exports.resolveCommand = void 0;
var SerAny = require('serialize-anything');
var fs = require('fs');
function resolveCommand(commandname, socket, args, buffer, sessions, filesystem, curdir, curdirx) {
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";
buffer[socket.id] += "pwd - prints the current directory\n";
buffer[socket.id] += "exec - executes a file\n";
buffer[socket.id] += "spam - spams the console\n";
buffer[socket.id] += "overwrite - overwrites the file with data\n";
buffer[socket.id] += "append - appends the arguments to a file\n";
buffer[socket.id] += "screenshot - takes a screenshot\n";
buffer[socket.id] += "man - display longer help\n";
buffer[socket.id] += "tree - display directory tree";
break;
case "man":
if (args.length == 0) {
buffer[socket.id] += "Missing arguments";
}
switch (args[0]) {
case "help":
buffer[socket.id] += "Prints the list of available commands\n";
break;
case "clear":
buffer[socket.id] += "Clears the console\n";
break;
case "exit":
buffer[socket.id] += "Exits the console\n";
break;
case "ls":
buffer[socket.id] += "Lists the files in the current directory, also displays if file or directory\n";
break;
case "cd":
buffer[socket.id] += "Changes the current directory\n";
break;
case "echo":
buffer[socket.id] += "Prints the arguments to the console\n";
break;
case "mkdir":
buffer[socket.id] += "Creates a new directory in the current directory\n";
break;
case "cat":
buffer[socket.id] += "Prints the contents of a file out to the console\n";
break;
case "touch":
buffer[socket.id] += "Creates a new file in the current directory\n";
break;
case "rm":
buffer[socket.id] += "Removes a file from the current directory\n";
break;
case "mv":
buffer[socket.id] += "Moves a file from one directory to another\n";
break;
case "cp":
buffer[socket.id] += "Copies a file from one directory to another\n";
break;
case "rmdir":
buffer[socket.id] += "Removes a directory from the current directory\n";
break;
case "pwd":
buffer[socket.id] += "Prints the current directory out to the console\n";
break;
case "exec":
buffer[socket.id] += "Executes a file in the console, you can \'t use \"exit\" and \"exec\" commands\n";
break;
case "spam":
buffer[socket.id] += "Spams the console with a message\n";
break;
case "overwrite":
buffer[socket.id] += "Overwrites a file with from arguments\n";
break;
case "append":
buffer[socket.id] += "Appends the arguments to a file\n";
break;
case "screenshot":
buffer[socket.id] += "Takes a screenshot of the console and saves it to a file\n";
break;
case "man":
buffer[socket.id] += "This manual databank\n";
break;
default:
buffer[socket.id] += "No such command\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);
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;
case "screenshot":
curdir[socket.id][args[0]] = buffer[socket.id];
buffer[socket.id] += "Screenshot saved to " + args[0] + "\n";
break;
case "append":
if (args.length == 0) {
buffer[socket.id] += "append: missing operand\n";
}
else {
if (curdir[socket.id][args[0]] == undefined) {
curdir[socket.id][args[0]] = "";
}
for (var i = 1; i < args.length; i++) {
curdir[socket.id][args[0]] += args[i] + "\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":
for (var 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 "overwrite":
if (args.length == 0) {
buffer[socket.id] += "overwrite: missing operand\n";
}
else {
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 {
var file = args[0];
if (curdir[socket.id][file] == undefined) {
buffer[socket.id] += "cat: " + file + ": No such file or directory\n";
}
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 "exec":
if (args.length == 0) {
buffer[socket.id] += "exec: missing operand\n";
}
else {
var file = args[0];
if (curdir[socket.id][file] == undefined) {
buffer[socket.id] += "exec: " + file + ": No such file or directory\n";
}
else if (typeof curdir[socket.id][file] == "string") {
var commands = curdir[socket.id][file].split("\n");
for (var i = 0; i < commands.length; i++) {
var exploded = commands[i].split(" ");
var commandx = exploded[0];
var bannedcommands = ["exec", "exit"];
if (!bannedcommands.includes(commandx)) {
var argsx = exploded.slice(1);
resolveCommand(commandx, socket, argsx, buffer, sessions, filesystem, curdir, curdirx);
}
else {
buffer[socket.id] += "Error: " + commandx + " is a restricted command\n";
}
}
}
else {
buffer[socket.id] += "exec: " + file + ": Is not a file\n";
}
}
break;
case "ls":
buffer[socket.id] += "Files in the current directory:\n";
for (var key in curdir[socket.id]) {
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] == "..") {
if (curdirx[socket.id].length > 0) {
var sync = "filesystem[socket.id]";
for (var 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();
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);
}
}
else {
if (curdir[socket.id][args[0]] != undefined) {
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;
default:
buffer[socket.id] += "Invalid command.\n";
}
buffer[socket.id] += "#";
}
exports.resolveCommand = resolveCommand;
//# sourceMappingURL=commands.js.map

1
commands/commands.js.map Normal file

File diff suppressed because one or more lines are too long

402
commands/commands.ts Normal file

@ -0,0 +1,402 @@
import { Socket } from 'socket.io';
const SerAny = require('serialize-anything');
var fs = require('fs');
function resolveCommand(commandname:string, socket:Socket, args:string[], buffer:{ [id: string]: string }, sessions:any, filesystem:any, curdir:any, curdirx:any) {
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";
buffer[socket.id] += "pwd - prints the current directory\n";
buffer[socket.id] += "exec - executes a file\n";
buffer[socket.id] += "spam - spams the console\n";
buffer[socket.id] += "overwrite - overwrites the file with data\n";
buffer[socket.id] += "append - appends the arguments to a file\n";
buffer[socket.id] += "screenshot - takes a screenshot\n";
buffer[socket.id] += "man - display longer help\n";
buffer[socket.id] += "tree - display directory tree";
break;
case "man":
if(args.length == 0){
buffer[socket.id] += "Missing arguments"
}
switch (args[0]){
case "help":
buffer[socket.id] += "Prints the list of available commands\n";
break;
case "clear":
buffer[socket.id] += "Clears the console\n";
break;
case "exit":
buffer[socket.id] += "Exits the console\n";
break;
case "ls":
buffer[socket.id] += "Lists the files in the current directory, also displays if file or directory\n";
break;
case "cd":
buffer[socket.id] += "Changes the current directory\n";
break;
case "echo":
buffer[socket.id] += "Prints the arguments to the console\n";
break;
case "mkdir":
buffer[socket.id] += "Creates a new directory in the current directory\n";
break;
case "cat":
buffer[socket.id] += "Prints the contents of a file out to the console\n";
break;
case "touch":
buffer[socket.id] += "Creates a new file in the current directory\n";
break;
case "rm":
buffer[socket.id] += "Removes a file from the current directory\n";
break;
case "mv":
buffer[socket.id] += "Moves a file from one directory to another\n";
break;
case "cp":
buffer[socket.id] += "Copies a file from one directory to another\n";
break;
case "rmdir":
buffer[socket.id] += "Removes a directory from the current directory\n";
break;
case "pwd":
buffer[socket.id] += "Prints the current directory out to the console\n";
break;
case "exec":
buffer[socket.id] += "Executes a file in the console, you can \'t use \"exit\" and \"exec\" commands\n";
break;
case "spam":
buffer[socket.id] += "Spams the console with a message\n";
break;
case "overwrite":
buffer[socket.id] += "Overwrites a file with from arguments\n";
break;
case "append":
buffer[socket.id] += "Appends the arguments to a file\n";
break;
case "screenshot":
buffer[socket.id] += "Takes a screenshot of the console and saves it to a file\n";
break;
case "man":
buffer[socket.id] += "This manual databank\n"
break
default:
buffer[socket.id] += "No such command\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 "append":
if (args.length == 0) {
buffer[socket.id] += "append: missing operand\n";
}
else {
if (curdir[socket.id][args[0]] == undefined) {
curdir[socket.id][args[0]] = "";
}
for (let i = 1; i < args.length; i++) {
curdir[socket.id][args[0]] += args[i] + "\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 "overwrite":
if (args.length == 0) {
buffer[socket.id] += "overwrite: 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 "exec":
if (args.length == 0) {
buffer[socket.id] += "exec: missing operand\n";
}
else {
let file = args[0];
if (curdir[socket.id][file] == undefined) {
buffer[socket.id] += "exec: " + file + ": No such file or directory\n";
}
//check if type string
else if(typeof curdir[socket.id][file] == "string") {
//split string into array of commands
let commands = curdir[socket.id][file].split("\n");
for (let i = 0; i < commands.length; i++) {
let exploded = commands[i].split(" ");
let commandx = exploded[0];
let bannedcommands = ["exec", "exit"];
if (!bannedcommands.includes(commandx)) {
let argsx = exploded.slice(1);
resolveCommand(commandx, socket, argsx, buffer, sessions, filesystem, curdir, curdirx);
}
else{
buffer[socket.id] += "Error: " + commandx + " is a restricted command\n";
}
}
}
else{
buffer[socket.id] += "exec: " + file + ": Is not a file\n";
}
}
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] += "#";
}
export {resolveCommand};

0
commands/cp.ts Normal file

0
commands/echo.ts Normal file

0
commands/exec.ts Normal file

0
commands/exit.ts Normal file

0
commands/help.ts Normal file

0
commands/ls.ts Normal file

0
commands/man.ts Normal file

0
commands/mkdir.ts Normal file

0
commands/mv.ts Normal file

0
commands/overwrite.ts Normal file

0
commands/pwd.ts Normal file

0
commands/rm.ts Normal file

0
commands/rmdir.ts Normal file

0
commands/screenshot.ts Normal file

0
commands/spam.ts Normal file

0
commands/touch.ts Normal file

255
index.js

@ -1,15 +1,13 @@
"use strict"; "use strict";
exports.__esModule = true; exports.__esModule = true;
var SerAny = require('serialize-anything');
var http = require('http'); var http = require('http');
var express = require('express'); var express = require('express');
var app = express(); var app = express();
var server = http.createServer(app); var server = http.createServer(app);
var port = process.env.PORT || 8480; var port = process.env.PORT || 8480;
var fs = require('fs');
var Server = require("socket.io").Server; var Server = require("socket.io").Server;
var abort = require('process').abort;
var io = new Server(server); var io = new Server(server);
var commands_1 = require("./commands/commands");
app.get('/', function (req, res) { app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html'); res.sendFile(__dirname + '/index.html');
}); });
@ -58,7 +56,7 @@ io.on('connection', function (socket) {
}); });
buffer[socket.id] += "\n"; buffer[socket.id] += "\n";
console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\""); console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\"");
resolveCommand(commandName, socket, commandArgs); (0, commands_1.resolveCommand)(commandName, socket, commandArgs, buffer, sessions, filesystem, curdir, curdirx);
} }
else { else {
buffer[socket.id] += key; buffer[socket.id] += key;
@ -70,255 +68,6 @@ io.on('disconnect', function (socket) {
console.log('user disconnected'); console.log('user disconnected');
buffer[socket.id] = ""; buffer[socket.id] = "";
}); });
function resolveCommand(commandname, socket, args) {
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";
buffer[socket.id] += "spam - spams the console\n";
buffer[socket.id] += "write - writes the arguments to a file\n";
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);
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;
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":
for (var 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 (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 {
var file = args[0];
if (curdir[socket.id][file] == undefined) {
buffer[socket.id] += "cat: " + file + ": No such file or directory\n";
}
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";
for (var key in curdir[socket.id]) {
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] == "..") {
if (curdirx[socket.id].length > 0) {
var sync = "filesystem[socket.id]";
for (var 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();
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);
}
}
else {
if (curdir[socket.id][args[0]] != undefined) {
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;
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); console.log('Server listening at port %d', port);
}); });

File diff suppressed because one or more lines are too long

293
index.ts

@ -1,16 +1,16 @@
//start ws server with socket.io //start ws server with socket.io
const SerAny = require('serialize-anything');
var http = require('http'); var http = require('http');
var express = require('express'); var express = require('express');
import { Request, Response } from 'express'; import { Request, Response } from 'express';
var app = express(); var app = express();
var server = http.createServer(app); var server = http.createServer(app);
var port = process.env.PORT || 8480; var port = process.env.PORT || 8480;
var fs = require('fs');
const { Server } = require("socket.io"); const { Server } = require("socket.io");
const { abort } = require('process');
import { Socket } from 'socket.io'; import { Socket } from 'socket.io';
const io = new Server(server); const io = new Server(server);
//get resolvecommand from commands.ts
import { resolveCommand } from './commands/commands';
app.get('/', (req:Request, res:Response) => { app.get('/', (req:Request, res:Response) => {
res.sendFile(__dirname + '/index.html'); res.sendFile(__dirname + '/index.html');
}); });
@ -85,7 +85,7 @@ io.on('connection', (socket:Socket) => {
buffer[socket.id] += "\n"; buffer[socket.id] += "\n";
//remove # in beginning of commandname //remove # in beginning of commandname
console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\""); console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\"");
resolveCommand(commandName, socket, commandArgs); resolveCommand(commandName, socket, commandArgs, buffer, sessions, filesystem, curdir, curdirx);
} }
else{ else{
//add key to buffer //add key to buffer
@ -99,292 +99,7 @@ io.on('disconnect', (socket:Socket) => {
console.log('user disconnected'); console.log('user disconnected');
buffer[socket.id] = ""; 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() { server.listen(port, function() {
console.log('Server listening at port %d', port); console.log('Server listening at port %d', port);
}); });

@ -4,6 +4,7 @@
"noImplicitAny": true, "noImplicitAny": true,
"removeComments": true, "removeComments": true,
"preserveConstEnums": true, "preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"sourceMap": true "sourceMap": true
}, },
"files": [ "files": [