spee.ch/server/models/tor.js

60 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const logger = require('winston');
const { details: { ipAddress } } = require('@config/siteConfig');
2018-06-28 00:11:25 +02:00
module.exports = (sequelize, { STRING }) => {
const Tor = sequelize.define(
2018-06-28 00:11:25 +02:00
'Tor',
{
address: {
2018-06-28 00:11:25 +02:00
type : STRING,
allowNull: false,
},
fingerprint: {
type : STRING,
allowNull: true,
},
2018-06-28 00:11:25 +02:00
},
{
freezeTableName: true,
}
);
Tor.refreshTable = function () {
let torList = [];
return fetch(`https://check.torproject.org/api/bulk?ip=${ipAddress}&port=80`)
.then(response => {
return response.json();
})
.then(jsonResponse => {
logger.debug('total tor nodes:', jsonResponse.length);
// prep the records
for (let i = 0; i < jsonResponse.length; i++) {
torList.push({
address : jsonResponse[i].Address,
fingerprint: jsonResponse[i].Fingerprint,
});
}
// clear the table
return this.destroy({
truncate: true,
});
})
.then(() => {
// fill the table
return this.bulkCreate(torList);
})
.then(() => {
// return the new table
return this.findAll({
attributes: ['address', 'fingerprint'],
raw : true,
});
})
.catch(error => {
throw error;
});
};
return Tor;
2018-06-28 00:11:25 +02:00
};