added Tor list referesh method to model

This commit is contained in:
bill bittner 2018-06-28 10:39:46 -07:00
parent faf7f5702b
commit ad23708cd6
3 changed files with 59 additions and 36 deletions

View file

@ -107,18 +107,29 @@ function Server () {
this.initialize(); this.initialize();
this.createApp(); this.createApp();
this.createServer(); this.createServer();
/* start the server */ logger.info(`Syncing database...`);
logger.info('getting LBC balance & syncing database...'); this.syncDatabase()
Promise.all([ .then(() => {
this.syncDatabase(), logger.info(`Starting server on ${PORT}...`);
getWalletBalance(),
])
.then(([syncResult, walletBalance]) => {
logger.info('starting LBC balance:', walletBalance);
return this.server.listen(PORT, () => { return this.server.listen(PORT, () => {
logger.info(`Server is listening on PORT ${PORT}`); logger.info(`Server is listening on PORT ${PORT}`);
}) })
}) })
.then(() => {
logger.info(`Getting starting balance`);
logger.info(`Updating blocked list`);
logger.info(`Updating tor node list`);
Promise.all([
getWalletBalance(),
updateBlockedList(),
updateTorList(),
])
})
.then(([walletBalance, updatedBlockedList, updatedTorList]) => {
logger.info('Starting LBC balance:', walletBalance);
logger.info('Blocked list length:', updatedBlockedList.length);
logger.info('Tor list length:', updatedTorList.length);
})
.catch(error => { .catch(error => {
if (error.code === 'ECONNREFUSED') { if (error.code === 'ECONNREFUSED') {
return logger.error('Connection refused. The daemon may not be running.') return logger.error('Connection refused. The daemon may not be running.')

View file

@ -1,8 +1,6 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../../../models'); const db = require('../../../models');
const { details: { ipAddress } } = require('@config/siteConfig');
/* /*
Route to update and return tor exit nodes that can connect to this ip address Route to update and return tor exit nodes that can connect to this ip address
@ -10,31 +8,7 @@ const { details: { ipAddress } } = require('@config/siteConfig');
*/ */
const getTorList = (req, res) => { const getTorList = (req, res) => {
return fetch(`https://check.torproject.org/api/bulk?ip=${ipAddress}&port=80`) db.Tor.refreshTable()
.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'],
raw: true,
});
})
.then( result => { .then( result => {
logger.debug('number of records', result.length); logger.debug('number of records', result.length);
res.status(200).json(result); res.status(200).json(result);

View file

@ -1,5 +1,7 @@
const { details: { ipAddress } } = require('@config/siteConfig');
module.exports = (sequelize, { STRING }) => { module.exports = (sequelize, { STRING }) => {
return sequelize.define( const Tor = sequelize.define(
'Tor', 'Tor',
{ {
address: { address: {
@ -15,4 +17,40 @@ module.exports = (sequelize, { STRING }) => {
freezeTableName: true, 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 => {
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;
}; };