-
Notifications
You must be signed in to change notification settings - Fork 14
/
tls-client.js
45 lines (38 loc) · 1.04 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
#!/usr/bin/env node
'use strict';
const port = 8000;
const hostname = 'localhost';
const tls = require('tls');
const fs = require('fs');
const options = {
host: hostname,
port: port,
// Necessary only if using the client certificate authentication
key: fs.readFileSync('certs/client/client.key'),
cert: fs.readFileSync('certs/client/client.crt'),
// Necessary only if the server uses the self-signed certificate
ca: fs.readFileSync('certs/ca/ca.crt')
};
const socket = tls.connect(options, () => {
console.log('client connected', socket.authorized ? 'authorized' : 'unauthorized');
if (!socket.authorized) {
console.log("Error: ", socket.authorizationError());
socket.end();
}
})
.setEncoding('utf8')
.on('data', (data) => {
console.log("Received: ", data);
// Close after receive data
socket.end();
})
.on('close', () => {
console.log("Connection closed");
})
.on('end', () => {
console.log("End connection");
})
.on('error', (error) => {
console.error(error);
socket.destroy();
});