From 55106bb40d92c3db0900706430c8e56d4557500f Mon Sep 17 00:00:00 2001 From: lesion Date: Thu, 9 Aug 2018 23:13:02 +0200 Subject: [PATCH] better checkTor middleware --- routes/tor.js | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/routes/tor.js b/routes/tor.js index f0e6bf9..04700e3 100644 --- a/routes/tor.js +++ b/routes/tor.js @@ -1,29 +1,15 @@ -var express = require('express'); -var router = express.Router(); -const dns = require('dns').promises; +const { promisify } = require('util') +const dns = require('dns') +const lookup = promisify(dns.lookup) -function isExitNode (ip) { - ip = ip.split('.').reverse().join('.'); - const domain = ip + '.8080.235.126.68.51.ip-port.exitlist.torproject.org' - console.log(domain); - return dns.lookup(domain, {}).then(ret => { - if (ret.address === '127.0.0.2') return true; - return false; - }).catch( e => false ) +// https://www.torproject.org/projects/tordnsel.html.en +// check if request comes from tor +module.exports = async (req, res, next) => { + const source_ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress + const ip = source_ip.split('.').reverse().join('.') + const server_ip = '51.68.126.235'.split('.').reverse().join('.') + const domain = `${ip}.80.${server_ip}.ip-port.exitlist.torproject.org` + const ret = await lookup(domain, {}) + req.fromTor = (ret.address === '127.0.0.2') + next() } - - -router.get('/', (req, res) => { - const source_ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; - - isExitNode(source_ip).then( isExit => { - if (isExit) { - res.render('torok'); - } else { - res.render('tornook'); - } - }); -}); - - -module.exports = router;