16 lines
626 B
JavaScript
16 lines
626 B
JavaScript
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) => {
|
|
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()
|
|
}
|