diff --git a/server/controllers/api/tor/index.js b/server/controllers/api/tor/index.js new file mode 100644 index 00000000..8c354a99 --- /dev/null +++ b/server/controllers/api/tor/index.js @@ -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; diff --git a/server/models/tor.js b/server/models/tor.js index 90df0e2b..008817f0 100644 --- a/server/models/tor.js +++ b/server/models/tor.js @@ -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, diff --git a/server/routes/api/index.js b/server/routes/api/index.js index a8bc233c..e32dbb8f 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -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); };