-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (111 loc) · 3.79 KB
/
server.js
File metadata and controls
133 lines (111 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import net from "node:net";
import chalk from "chalk"
import { runCommand } from "./helpers.js";
const [, , portArg] = process.argv;
if (!portArg) {
console.log("Usage: node nc-server.js <port>");
process.exit(1);
}
function keyOf(socket) {
return "Address:" + socket.remoteAddress + " Port:" + socket.remotePort
}
function serverLog({ command, socket, stdout, stderr }) {
if (stderr) {
socket.write(stderr)
}
console.log("-----------------------------")
console.log("From: " + keyOf(socket))
console.log(chalk.bold("Command: " + command))
console.log(chalk.cyan("Stdout: "))
console.log(chalk.cyan("-----------------------------"))
console.log(chalk.yellow(stdout))
console.log(chalk.cyan("-----------------------------"))
console.log(chalk.red("Stderr: "))
console.log(chalk.red("-----------------------------"))
console.log(stderr || "No Errors")
console.log(chalk.red("-----------------------------"))
console.log("-----------------------------")
}
const sessions = {}
const port = Number(portArg);
const builtInCommands = {
"session": (socket) => {
const stdout = keyOf(socket) + "\n"
socket.write(stdout)
return { stdout }
},
"history": (socket) => {
const stdout = JSON.stringify(sessions[keyOf(socket)].history) + "\n"
socket.write(stdout)
return { stdout }
},
"exit": (socket) => {
delete sessions[keyOf(socket)];
socket.write("Goodbye !")
socket.end()
return { stdout: "Session closed from " + keyOf(socket) }
},
"help": async (socket) => {
const { stdout, stderr } = await runCommand("cat ./help.txt")
socket.write(stdout)
return { stdout, stderr }
},
"info": async (socket) => {
const { stdout, stderr } = await runCommand("cat ./info.txt")
socket.write(chalk.cyan(stdout))
return { stdout, stderr }
},
"author": (socket) => {
const stdout = "https://github.com/hrach347"
socket.write(stdout)
return { stdout }
},
"users": async (socket) => {
const stdout = JSON.stringify(Object.keys(sessions))
socket.write(stdout)
return { stdout }
},
"broadcast": async (socket, options) => {
options.shift()
const message = "From - " + keyOf(socket) + " \n" + options.join(" ") + "\n"
for (let session in sessions) {
sessions[session].socket.write(chalk.bgRedBright(message))
}
return { stdout: "Broadcasted to all active sessions" }
},
"clearhistory": (socket) => {
sessions[keyOf(socket)].history = []
socket.write("Cleared!")
return { stdout: "Cleared!" }
}
}
const server = net.createServer((socket) => {
console.log("Client Connected from: " + keyOf(socket));
sessions[keyOf(socket)] = { socket }
sessions[keyOf(socket)].history = []
socket.on("data", async data => {
const command = data.toString().trim()
if (!command) {
socket.write("\n")
return null
}
sessions[keyOf(socket)].history.push(command)
if (builtInCommands[command.split(" ")[0]]) {
const { stdout, stderr } = await builtInCommands[command.split(" ")[0]](socket, command.split(" "))
serverLog({ command, socket, stdout, stderr })
return null
}
const { stdout, stderr } = await runCommand(command)
socket.write(stdout || " ")
serverLog({ command, socket, stdout, stderr })
});
socket.on("end", () => {
console.log("\nclient disconnected");
});
socket.on("error", (err) => {
console.error("socket error:", err.message);
});
});
server.listen(port, () => {
console.log(`Listening on port ${port}`);
});