added an endpoint to update tor node list

This commit is contained in:
bill bittner 2018-06-27 17:30:26 -07:00
parent bcca1e1df2
commit 6aaa6bd0aa
3 changed files with 59 additions and 1 deletions

View file

@ -0,0 +1,51 @@
const logger = require('winston');
const db = require('../../../models');
const ipAddress = '54.236.53.10';
/*
Route to update and return tor exit nodes that can connect to this ip address
*/
const getTorList = (req, res) => {
return fetch(`https://check.torproject.org/api/bulk?ip=${ipAddress}&port=80`)
.then(response => {
return response.json();
})
.then( jsonResponse => {
const torList = [];
for (let i = 0; i < jsonResponse.length; i++) {
torList.push({
address: jsonResponse[i].Address,
fingerprint: jsonResponse[i].Fingerprint,
});
}
return db.Tor.destroy({
truncate: true,
})
.then(() => {
return db.Tor.bulkCreate(torList)
})
})
.then(() => {
return db.Tor.findAll({
attributes: ['address', 'fingerprint'],
})
.map(el => el.get({ plain: true }));
})
.then( result => {
logger.debug('number of records', result.length);
res.status(200).json(result);
})
.catch((error) => {
logger.error(error);
res.status(500).json({
success: false,
error,
})
});
};
module.exports = getTorList;

View file

@ -2,10 +2,14 @@ module.exports = (sequelize, { STRING }) => {
return sequelize.define(
'Tor',
{
ip: {
address: {
type : STRING,
allowNull: false,
},
fingerprint: {
type : STRING,
allowNull: true,
},
},
{
freezeTableName: true,

View file

@ -14,6 +14,7 @@ const claimShortId = require('../../controllers/api/claim/shortId');
const fileAvailability = require('../../controllers/api/file/availability');
const userPassword = require('../../controllers/api/user/password');
const publishingConfig = require('../../controllers/api/config/site/publishing');
const getTorList = require('../../controllers/api/tor');
const multipartMiddleware = require('../../middleware/multipartMiddleware');
const torCheckMiddleware = require('../../middleware/torCheckMiddleware');
@ -40,4 +41,6 @@ module.exports = (app) => {
app.put('/api/user/password/', userPassword);
// configs
app.get('/api/config/site/publishing', publishingConfig);
// tor
app.get('/api/tor', getTorList);
};