fugadalcontrollo/routes/tor.js

20 lines
667 B
JavaScript
Raw Normal View History

2018-08-09 23:13:02 +02:00
const { promisify } = require('util')
const dns = require('dns')
const lookup = promisify(dns.lookup)
// https://www.torproject.org/projects/tordnsel.html.en
// check if request comes from tor
module.exports = async (req, res, next) => {
2018-08-10 00:31:44 +02:00
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`
try {
2018-08-09 23:13:02 +02:00
const ret = await lookup(domain, {})
req.fromTor = (ret.address === '127.0.0.2')
2018-08-10 00:31:44 +02:00
} catch(e) {
req.fromTor = false
}
next()
2018-07-21 01:14:47 +02:00
}