-
Notifications
You must be signed in to change notification settings - Fork 0
/
n.js
112 lines (100 loc) · 3.12 KB
/
n.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
const pug = require('pug');
const express = require('express');
const fs = require("fs");
const https = require("https");
const axios = require('axios');
const port = process.env.PORT || 8089;
// Capture arguments
const args = process.argv.slice(2);
const input = JSON.parse(fs.readFileSync(__dirname + "/input/" + args[0] + '.json', 'utf8'));
// Load mock data
const data = JSON.parse(fs.readFileSync(__dirname + "/data/" + input.data, 'utf8'));
// Server options
const options = {
key: fs.readFileSync(`${__dirname}/${input.key}`),
cert: fs.readFileSync(`${__dirname}/${input.cert}`),
// Enable client authentication
// ca: [
// fs.readFileSync(`${__dirname}/${input.ca}`)
// ],
// Requesting the client to provide a certificate, to authenticate.
// requestCert: true,
// As specified as "true", so no unauthenticated traffic
// will make it to the specified route specified
// rejectUnauthorized: true
};
const httpsAgent = new https.Agent({
key: fs.readFileSync(`${__dirname}/${input.key}`),
cert: fs.readFileSync(`${__dirname}/${input.cert}`),
ca: fs.readFileSync(`${__dirname}/${input.ca}`),
// port: input.port,
secureProtocol: "TLSv1_2_method",
});
/**
*
* @param {*} data
* @returns
*/
function encode(data) {
const buff = new Buffer(data);
return buff.toString('base64');
}
/**
*
* @param {*} data
* @returns
*/
function decode(data) {
const buff = new Buffer(data, 'base64');
return text = buff.toString('ascii');
}
const app = express();
app.set('view engine', 'pug');
app.get('/', async function (req, res) {
try {
//
// Query the GW. Get all the nodes.
//
const result = await axios.get('https://gw.ma.appseed.io:8080/feds', { httpsAgent });
for (let node of result.data) {
const url = node.url + '/api/crl';
const nodedata = await axios.get(url, { httpsAgent });
console.log(nodedata);
}
//
// Query the nodes. Get all the batches
//
// https://futurestud.io/tutorials/node-js-how-to-run-an-asynchronous-function-in-array-map
let crl = result.data.map(async function(node){
const response = await axios.get(node.url + '/api/crl', { httpsAgent });
return {
reference: node.url,
crl: response.data,
}
})
// wait until all promises resolve
crl = await Promise.all(crl)
res.render('home', { title: input.name, nodes: result.data, crl: crl , encode: encode});
} catch (error) {
console.log(error);
res.render('error', { message: error });
}
});
app.get('/batches/:endpoint/:id', async function (req, res) {
const endpoint = decode(req.params.endpoint);
const id = req.params.id;
const result = await axios.get(endpoint + '/api/batches/' + id, { httpsAgent });
res.end(JSON.stringify(result.data, null, 2));
})
app.get('/api/crl', function (req, res) {
res.end(JSON.stringify(data.crl, null, 2));
})
app.get('/api/batches', function (req, res) {
res.end(JSON.stringify(data.batches, null, 2));
})
app.get('/api/batches/:id', function (req, res) {
res.end(JSON.stringify(data.batches[req.params.id], null, 2));
})
https
.createServer(options, app)
.listen(port);