2018-06-30 05:34:48 +02:00
|
|
|
const logger = require('winston');
|
2018-06-28 19:39:46 +02:00
|
|
|
const { details: { ipAddress } } = require('@config/siteConfig');
|
|
|
|
|
2018-06-28 00:11:25 +02:00
|
|
|
module.exports = (sequelize, { STRING }) => {
|
2018-06-28 19:39:46 +02:00
|
|
|
const Tor = sequelize.define(
|
2018-06-28 00:11:25 +02:00
|
|
|
'Tor',
|
|
|
|
{
|
2018-06-28 02:30:26 +02:00
|
|
|
address: {
|
2018-06-28 00:11:25 +02:00
|
|
|
type : STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
2018-06-28 02:30:26 +02:00
|
|
|
fingerprint: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
2018-06-28 00:11:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
2018-06-28 19:39:46 +02:00
|
|
|
|
|
|
|
Tor.refreshTable = function () {
|
|
|
|
let torList = [];
|
|
|
|
return fetch(`https://check.torproject.org/api/bulk?ip=${ipAddress}&port=80`)
|
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(jsonResponse => {
|
2018-06-30 05:34:48 +02:00
|
|
|
logger.debug('total tor nodes:', jsonResponse.length);
|
|
|
|
// prep the records
|
2018-06-28 19:39:46 +02:00
|
|
|
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
|
|
|
};
|