-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls-client.js
121 lines (96 loc) · 3.46 KB
/
tls-client.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
//
// tls-client.js
//
// Example of a Transport Layer Security (or TSL) client
//
// 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 required here
var tls = require('tls'),
fs = require('fs'),
util = require('util'),
events = require('events');
// TLS Client object
var TLSClient = function (host, port) {
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 client
key: fs.readFileSync('ssl/agent2-key.pem'),
// Public key of the client (certificate key)
cert: fs.readFileSync('ssl/agent2-cert.pem'),
// Automatically reject clients with invalid certificates.
rejectUnauthorized: false // Set false to see what happens.
};
var self = this;
// Incoming JSON chunks are terminated with the Unicode replacement character.
this.TERM = '\uFFFD';
// Call the event emitter constructor.
events.EventEmitter.call(this);
var connect = (function connect() {
var fragment = '';
var s;
self.s = tls.connect(port, host, options, function () {
self.emit('connect', null);
console.log("TLS Server authorized:", self.s.authorized);
if (!self.s.authorized) {
console.log("TLS authorization error:", self.s.authorizationError);
}
// console.log(s.getPeerCertificate());
});
self.s.on("error", function (err) {
console.log("Eeek:", err.toString());
});
self.s.on("data", function (data) {
// Split incoming data into messages around TERM
var info = data.toString().split(self.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]);
self.emit('message', message);
} catch (error) {
// The last message may be cut short so save its chars for later.
fragment = info[index];
continue;
}
}
}
});
self.s.on("end", function () {
console.log("End:");
});
self.s.on("close", function () {
console.log("Close:");
self.emit('disconnect', null);
// Try to reconnect after a delay
setTimeout(function () {
connect();
}, 1000);
});
})();
};
// TLSClient inherits EventEmitter
util.inherits(TLSClient, events.EventEmitter);
TLSClient.prototype.write = function (message) {
if (this.s.writable) {
var data = JSON.stringify(message) + this.TERM;
this.s.write(data);
}
}
module.exports = TLSClient;