-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_tcp_tls_server.js
35 lines (32 loc) · 1.03 KB
/
06_tcp_tls_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
const host = '127.0.0.1'
const tcpPort = 3002
const oneSecond = 1000
const clientAddress = s => `${s.remoteAddress}:${s.remotePort}`
const announceLaunch = (p, h, s = 'http') => console.log(`Listening at ${s}://${h}:${p}/`)
const randomChoice = (yes, no) => Math.round(Math.random()) == 1 ? yes() : no()
const wait = ms => new Promise(r => setTimeout(r, ms))
const fs = require('fs')
const usingCerts = n => {
return {
key: fs.readFileSync(`${n}_key.pem`),
cert: fs.readFileSync(`${n}_cert.pem`),
// ca: [readFileSync(`${__dirname}/path/to/cert/ca.crt`)]
}}
const tls = require('tls')
.createServer(usingCerts("server"), socket => {
const a = clientAddress(socket)
console.log(`${a} connected`)
socket
.on('data', d => {
console.log(`${a} said '${d}'`)
randomChoice(
() => wait(oneSecond).then(() => socket.destroy()),
() => socket.write(d.reverse()))
})
.on('close', () => console.log(`${a} disconnected`))
})
.on('error', e => {
console.error(e)
tls.destroy()
})
.listen(tcpPort, host, () => announceLaunch(tcpPort, host, 'tcps'))