-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls-server.js
133 lines (105 loc) · 3.5 KB
/
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
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
//
// tls-server.js
//
// Example of a Transport Layer Security (or TSL) server
//
// References:
// http://nodejs.org/api/tls.html
// http://docs.nodejitsu.com/articles/cryptography/how-to-use-the-tls-module
//
// Always use JavaScript strict mode.
"use strict";
// Modules used here
var tls = require('tls'),
fs = require('fs');
var TERM = '\uFFFD';
var options = {
// Chain of certificate autorities
// Client and server have these to authenticate keys
ca: [
fs.readFileSync('ssl/root-cert.pem'),
fs.readFileSync('ssl/ca1-cert.pem'),
fs.readFileSync('ssl/ca2-cert.pem'),
fs.readFileSync('ssl/ca3-cert.pem'),
fs.readFileSync('ssl/ca4-cert.pem')
],
// Private key of the server
key: fs.readFileSync('ssl/agent1-key.pem'),
// Public key of the server (certificate key)
cert: fs.readFileSync('ssl/agent1-cert.pem'),
// Request a certificate from a connecting client
requestCert: true,
// Automatically reject clients with invalide certificates.
rejectUnauthorized: false // Set false to see what happens.
};
// The data structure to be sent to connected clients
var message = {
tag : 'Helsinki ' /* + String.fromCharCode(65533) */,
date : new Date(),
latitude : 60.1708,
longitude : 24.9375,
seqNo : 0
};
// A secure (TLS) socket server.
tls.createServer(options, function (s) {
var intervalId;
console.log("TLS Client authorized:", s.authorized);
if (!s.authorized) {
console.log("TLS authorization error:", s.authorizationError);
}
console.log("Cipher: ", s.getCipher());
console.log("Address: ", s.address());
console.log("Remote address: ", s.remoteAddress);
console.log("Remote port: ", s.remotePort);
message.seqNo = 0;
var fragment = '';
//console.log(s.getPeerCertificate());
intervalId = setInterval(function () {
message.date = new Date();
var ms = JSON.stringify(message) + TERM;
message.seqNo += 1;
message.date = new Date();
ms += JSON.stringify(message) + TERM;
message.seqNo += 1;
s.write(ms);
if ((message.seqNo % 100) === 0)
{
console.log(process.memoryUsage());
}
}, 5000);
// Echo data incomming dats from stream back out to stream
//s.pipe(s);
s.on('data', function(data) {
// Split incoming data into messages around TERM
var info = data.toString().split(TERM);
// Add any previous trailing chars to the start of the first message
info[0] = fragment + info[0];
fragment = '';
// Parse all the messages into objects
for ( var index = 0; index < info.length; index++) {
if (info[index]) {
try {
var message = JSON.parse(info[index]);
console.log(message.name);
console.log(message.passwd);
} catch (error) {
// The last message may be cut short so save its chars for later.
fragment = info[index];
continue;
}
}
}
// s.socket.end();
});
// Handle events on the underlying socket
s.on("error", function (err) {
console.log("Eeek:", err.toString());
});
s.on("end", function () {
console.log("End:");
});
s.on("close", function () {
clearInterval(intervalId);
console.log("Close:");
});
}).listen(8000);