fugadalcontrollo/routes/utils.js
lesion e404c509dc
avoid tor/vpn check in dev mode
use fromTor/fromVpn in query string
2018-08-26 23:45:30 +02:00

64 lines
1.8 KiB
JavaScript

const { promisify } = require('util')
const dns = require('dns')
const lookup = promisify(dns.lookup)
const request = require('request')
const { DEV, SERVER_IP } = require('../config')
// https://www.torproject.org/projects/tordnsel.html.en
// check if request comes from tor
const fromTor = async (req, res, next) => {
if (DEV) {
req.fromTor = req.query.fromTor || false
return next()
}
const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const ip = sourceIp.split('.').reverse().join('.')
const reversedServerIp = SERVER_IP.split('.').reverse().join('.')
const domain = `${ip}.80.${reversedServerIp}.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) => {
if (DEV) {
req.fromVpn = req.query.fromVpn || false
return 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) => {
if (DEV) {
return 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) {
try {
req.geoinfo = JSON.parse(body)
} catch (e) {}
}
next()
})
}
module.exports = { fromTor, fromVpn, checkCountry }