fugadalcontrollo/routes/utils.js
2018-08-11 01:26:31 +02:00

49 lines
1.5 KiB
JavaScript

const { promisify } = require('util')
const dns = require('dns')
const lookup = promisify(dns.lookup)
const request = require('request')
// https://www.torproject.org/projects/tordnsel.html.en
// check if request comes from tor
const fromTor = async (req, res, next) => {
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const ip = sourceIp.split('.').reverse().join('.')
const serverIp = '51.68.126.235'.split('.').reverse().join('.')
const domain = `${ip}.80.${serverIp}.ip-port.exitlist.torproject.org`
try {
const ret = await lookup(domain, {})
req.fromTor = (ret.address === '127.0.0.2')
} catch(e) {
req.fromTor = false
}
next()
}
// check if request comes from proxy/VPN/tor
const fromVpn = async (req, res, next) => {
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const baseUrl = 'https://check.getipintel.net/check.php?ip='
const url = `${baseUrl}${sourceIp}&contact=anna@fugadalcontrollo.org`
request(url,
(err, res, body) => {
if (!err && res.statusCode === 200) {
req.fromVpn = parseFloat(body)>0.5
} else {
req.fromVpn = false
}
next()
})
}
const checkCountry = (req, res, next) => {
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
request(`http://ip-api.com/json/${sourceIp}`, (err, res, body) => {
if (!err && res.statusCode === 200) {
req.geoinfo = JSON.parse(body)
}
next()
})
}
module.exports = { fromTor, fromVpn, checkCountry }