-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (55 loc) · 1.9 KB
/
server.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
// Based on code found at:
// https://gist.github.com/mcroydon/519344/09979e74352594f670477efda7f0306519e6cd1b
const net = require('net')
const art = require('./art')
// User data
const users = {
charlie: {
username: 'charlie',
name: 'Charlie Owen',
twitter: 'sonniesedge',
plan: 'Keep surviving the churn.'
}
}
// Padding for user lists.
const pad = function (string, length) {
length = length || 20
while (string.length <= length) {
string = string + ' '
}
return string
}
// Disregard verbosity and strip trailing <CR><LF>
const cleanData = function (string) {
return string.replace('/W', '').replace('\r\n', '')
}
net.createServer(function (socket) {
socket.setEncoding('ascii')
socket.on('data', function (data) {
console.log('Starting connection.')
if (data === '\r\n') {
console.log('Serving index.')
socket.write(pad('Login') + pad('Name') + 'Twitter' + '\r\n')
for (const index in users) {
const user = users[index]
socket.write(pad(user.username) + pad(user.name) + user.twitter + '\r\n')
}
} else if (cleanData(data) in users) {
console.log(' Serving user: ' + cleanData(data))
const user = users[cleanData(data)]
if (user.username) socket.write('Username: ' + user.username + '\r\n')
if (user.name) socket.write('Name: ' + user.name + '\r\n')
if (user.twitter) socket.write('Twitter: ' + user.twitter + '\r\n')
if (user.plan) socket.write('Plan: ' + user.plan + '\r\n')
if (user.project) socket.write('Project: ' + user.project + '\r\n')
socket.write(art.floppyFist)
} else {
console.log('Unhandled: ' + cleanData(data))
}
socket.end()
})
socket.on('end', function () {
console.log('Ending connection.')
})
}).listen(process.env['NODE_ENV'] !== 'production' ? 79 : 7979, '127.0.0.1')
console.log('finger_server.js running. To test, run \'finger @127.0.0.1\'')