-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
71 lines (68 loc) · 1.93 KB
/
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
const fs = require("fs");
const https = require("https");
const message = { msg: "Hello!" };
// Capture arguments
const args = process.argv.slice(2);
// Load input
const options = JSON.parse(fs.readFileSync(__dirname + "/input/" + args[0] + '.json', 'utf8'));
const req = https.request(
{
host: options.host, //"server.ma.appseed.io",
port: options.port, // 8080
secureProtocol: "TLSv1_2_method",
key: fs.readFileSync(`${__dirname}/${options.key}`),
cert: fs.readFileSync(`${__dirname}/${options.cert}`),
ca: [
fs.readFileSync(`${__dirname}/${options.ca}`)
],
path: "/api",
method: "GET",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(JSON.stringify(message))
}
},
function(response) {
console.log("Response statusCode: ", response.statusCode);
console.log("Response headers: ", response.headers);
console.log(
"Server Host Name: " + response.connection.getPeerCertificate().subject.CN
);
if (response.statusCode !== 200) {
console.log(`Wrong status code`);
return;
}
let rawData = "";
response.on("data", function(data) {
rawData += data;
});
response.on("end", function() {
if (rawData.length > 0) {
console.log(`Received message: ${rawData}`);
}
console.log(`TLS Connection closed!`);
req.end();
return;
});
}
);
req.on("socket", function(socket) {
socket.on("secureConnect", function() {
if (socket.authorized === false) {
console.log(`SOCKET AUTH FAILED ${socket.authorizationError}`);
}
console.log("TLS Connection established successfully!");
});
socket.setTimeout(10000);
socket.on("timeout", function() {
console.log("TLS Socket Timeout!");
req.end();
return;
});
});
req.on("error", function(err) {
console.log(`TLS Socket ERROR (${err})`);
req.end();
return;
});
req.write(JSON.stringify(message));