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')
|
|
|
|
|
2018-08-26 23:45:30 +02:00
|
|
|
const { DEV, SERVER_IP } = require('../config')
|
|
|
|
|
2018-08-10 23:14:56 +02:00
|
|
|
// https://www.torproject.org/projects/tordnsel.html.en
|
|
|
|
// check if request comes from tor
|
|
|
|
const fromTor = async (req, res, next) => {
|
2018-08-26 23:45:30 +02:00
|
|
|
if (DEV) {
|
|
|
|
req.fromTor = req.query.fromTor || false
|
|
|
|
return next()
|
|
|
|
}
|
2018-08-10 23:14:56 +02:00
|
|
|
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
|
|
|
const ip = sourceIp.split('.').reverse().join('.')
|
2018-08-26 23:45:30 +02:00
|
|
|
const reversedServerIp = SERVER_IP.split('.').reverse().join('.')
|
|
|
|
const domain = `${ip}.80.${reversedServerIp}.ip-port.exitlist.torproject.org`
|
2018-08-10 23:14:56 +02:00
|
|
|
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) => {
|
2018-08-26 23:45:30 +02:00
|
|
|
if (DEV) {
|
|
|
|
req.fromVpn = req.query.fromVpn || false
|
|
|
|
return next()
|
|
|
|
}
|
2018-08-10 23:14:56 +02:00
|
|
|
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-26 23:45:30 +02:00
|
|
|
if (DEV) {
|
|
|
|
return 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-26 23:45:30 +02:00
|
|
|
try {
|
|
|
|
req.geoinfo = JSON.parse(body)
|
|
|
|
} catch (e) {}
|
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 }
|