fugadalcontrollo/routes/utils.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-10 23:14:56 +02:00
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) {
2018-08-10 23:53:41 +02:00
req.fromVpn = parseFloat(body)>0.5
2018-08-10 23:14:56 +02:00
} else {
2018-08-10 23:53:41 +02:00
req.fromVpn = false
2018-08-10 23:14:56 +02:00
}
2018-08-10 23:53:41 +02:00
next()
2018-08-10 23:14:56 +02:00
})
}
2018-08-10 23:53:41 +02:00
const checkCountry = (req, res, next) => {
2018-08-11 01:08:00 +02:00
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
2018-08-10 23:53:41 +02:00
request(`http://ip-api.com/json/${sourceIp}`, (err, res, body) => {
if (!err && res.statusCode === 200) {
2018-08-11 01:26:31 +02:00
req.geoinfo = JSON.parse(body)
2018-08-10 23:53:41 +02:00
}
2018-08-11 01:08:00 +02:00
next()
2018-08-10 23:53:41 +02:00
})
}
2018-08-10 23:14:56 +02:00
2018-08-11 01:08:00 +02:00
module.exports = { fromTor, fromVpn, checkCountry }