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');
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;