-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdapp.js
72 lines (67 loc) · 2.09 KB
/
oldapp.js
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
const net = require('net');
const chalk = require('chalk');
const config = require('./config.json');
const exec = require('child_process').exec
let commands
require('./loader.js')(cmds => {
commands = cmds
console.log(`Telnet server started on :${config.port}\r\nLoaded ${Object.keys(commands).length} commands.`)
})
function command(socket, data) {
socket.cmdcount++
let ctx = {
data: data,
cmd: {
raw: data,
clean: data.toString().replace(/(\r\n|\n|\r)/gm,""),
args: data.toString().replace(/(\r\n|\n|\r)/gm,"").split(' ')
},
socket: socket
}
if (ctx.socket.awaitFn) {
ctx.socket.awaitFn(ctx)
} else {
if (Object.keys(commands).includes(ctx.cmd.args[0].toLowerCase())) {
commands[ctx.cmd.args[0].toLowerCase()].run(ctx)
} else if (!Object.keys(commands).includes(ctx.cmd.args[0].toLowerCase()) && socket.cmdcount > 1) {
socket.write(chalk.red('Command not found.\r\n'))
} else {
return ;
}
if (!ctx.socket.awaitFn) {
try {
ctx.socket.write('>>> ')
} catch(err) {}
}
}
}
function newSocket(socket) {
socket.cmdcount = 0
socket.ip = socket.address().address
console.log(`New connection from ${socket.ip}`)
exec('clear', (err, stdout, stderr) => {
socket.write(stdout)
socket.write([`${chalk.cyan(' ▒')}${chalk.bgCyan.black(' Welcome to Splixl\'s Telnet Thing! ')}${chalk.cyan('▒ ')}`,
` ${chalk.yellow('This assumes you use a 80 x 24 term.')}`,
` Try ${chalk.green('help')} to see commands.`,
`>>> `].join('\r\n'));
})
/*
▒ Welcome to Splixl's Telnet Thing! ▒
This assumes you us e a 80 x 24 term.
Try help to see commands.
*/
socket.on('data', (data) => {
command(socket, data);
})
socket.on('close',() => {
console.log(`Connection from ${socket.ip} closed.`)
})
socket.on("error", (err) => {
console.log(`Socket error:\n${err.stack}`)
})
}
// Create a new server and provide a callback for when a connection occurs
var server = net.createServer(newSocket);
// Listen on port 8888
server.listen(config.port);