websegura/crawler/results.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-02-04 21:35:05 +01:00
/**
* Solicita los resultados (estado actual + histórico) de los sitios web dados de alta en el proyecto.
* El análisis se hace utilizando el API de Mozilla.
*
* Más información en:
* https://github.com/mozilla/http-observatory/blob/master/httpobs/docs/api.md
*/
2021-03-07 10:21:58 +01:00
const fs = require("fs/promises");
const axios = require("axios");
const Bottleneck = require("bottleneck");
const parser = require("./sites-parser");
2021-02-04 00:10:37 +01:00
2021-03-07 10:21:58 +01:00
const MOZILLA_API_BASE_URL =
"https://http-observatory.security.mozilla.org/api/v1";
2021-02-04 22:31:37 +01:00
const MAX_CONCURRENT_REQUESTS = 20;
2021-02-04 00:10:37 +01:00
2021-02-04 22:31:37 +01:00
// TODO use the limiter (see analyze)
2021-03-07 10:21:58 +01:00
const limiter = new Bottleneck({ maxConcurrent: MAX_CONCURRENT_REQUESTS });
2021-02-04 00:10:37 +01:00
async function results() {
2021-02-04 21:35:05 +01:00
const sites = await parser.parse();
2021-02-04 00:10:37 +01:00
for (const site of sites) {
2021-03-07 10:21:58 +01:00
const fileName = site.replace(/\./g, "!") + ".json";
2021-02-04 00:10:37 +01:00
try {
console.log(`Request ${site} scan history`);
2021-03-07 10:21:58 +01:00
const history = await axios.get(
`${MOZILLA_API_BASE_URL}/getHostHistory?host=${site}&rescan=true`
);
await fs.writeFile(
`_data/results/history/${fileName}`,
JSON.stringify(history.data, null, 2)
);
2021-02-04 00:10:37 +01:00
} catch (e) {
if (e.response) {
2021-03-07 10:21:58 +01:00
console.error(
`Failed request (${e.response.status}): ${e.response.statusText}`
);
2021-02-04 00:10:37 +01:00
} else {
console.error(e);
}
}
try {
console.log(`Request ${site} scan results`);
2021-03-07 10:21:58 +01:00
const result = await axios.get(
`${MOZILLA_API_BASE_URL}/analyze?host=${site}`
);
if (result.data.state === "FINISHED") {
await fs.writeFile(
`_data/results/${fileName}`,
JSON.stringify(result.data, null, 2)
);
2021-02-04 00:10:37 +01:00
}
} catch (e) {
if (e.response) {
2021-03-07 10:21:58 +01:00
console.error(
`Failed (${e.response.status}): ${e.response.statusText}`
);
2021-02-04 00:10:37 +01:00
} else {
console.error(e);
}
}
}
}
2021-03-07 10:21:58 +01:00
results().catch((err) => {
2021-02-04 22:31:37 +01:00
console.error(err);
process.exit(1);
});