better checkTor middleware

This commit is contained in:
lesion 2018-08-09 23:13:02 +02:00
parent fdfba24a4f
commit 55106bb40d
No known key found for this signature in database
GPG Key ID: 352918250B012177

View File

@ -1,29 +1,15 @@
var express = require('express'); const { promisify } = require('util')
var router = express.Router(); const dns = require('dns')
const dns = require('dns').promises; const lookup = promisify(dns.lookup)
function isExitNode (ip) { // https://www.torproject.org/projects/tordnsel.html.en
ip = ip.split('.').reverse().join('.'); // check if request comes from tor
const domain = ip + '.8080.235.126.68.51.ip-port.exitlist.torproject.org' module.exports = async (req, res, next) => {
console.log(domain); const source_ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
return dns.lookup(domain, {}).then(ret => { const ip = source_ip.split('.').reverse().join('.')
if (ret.address === '127.0.0.2') return true; const server_ip = '51.68.126.235'.split('.').reverse().join('.')
return false; const domain = `${ip}.80.${server_ip}.ip-port.exitlist.torproject.org`
}).catch( e => false ) 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;