This commit is contained in:
Bruno Rybársky 2022-06-11 19:57:41 +02:00
parent f4ad1fedd4
commit ed40d946b6
6 changed files with 1914 additions and 146 deletions

146
.gitignore vendored

@ -1,146 +0,0 @@
# ---> Node
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# ---> VisualStudioCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix

17
.vscode/launch.json vendored Normal file

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js"
}
]
}

116
index.html Normal file

File diff suppressed because one or more lines are too long

378
index.js Normal file

@ -0,0 +1,378 @@
//start ws server with socket.io
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) => {
res.sendFile(__dirname + '/index.html');
});
//initialize buffer
var buffer = [];
var filesystem = [];
var curdir = [];
var sessions = [];
var curdirx = [];
//on connection
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) => {
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) => {
console.log('user disconnected');
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";
//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 = JSON.stringify(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) {
if(sessions[args[0]] != undefined) {
sessions = JSON.parse(fs.readFileSync("./sessions.json"));
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 < 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 {
filesystem[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] + " ";
}
}
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
sync = "filesystem[socket.id]";
for(let i = 0; i < curdirx[socket.id].length; i++) {
sync += "['" + curdirx[socket.id][i] + "']";
}
eval(sync + " = curdir[socket.id]");
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++) {
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 (filesystem[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);
});

1387
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

16
package.json Normal file

@ -0,0 +1,16 @@
{
"name": "clisite",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"socket.io": "^4.5.1"
}
}