avoid tor/vpn check in dev mode

use fromTor/fromVpn in query string
This commit is contained in:
lesion 2018-08-26 23:45:30 +02:00
parent e3b04de244
commit e404c509dc
No known key found for this signature in database
GPG Key ID: 352918250B012177
2 changed files with 18 additions and 12 deletions

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
router.get('/', (req, res) => {
res.render('metadata');
});
module.exports = router;

View File

@ -3,13 +3,19 @@ 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 serverIp = '51.68.126.235'.split('.').reverse().join('.')
const domain = `${ip}.80.${serverIp}.ip-port.exitlist.torproject.org`
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')
@ -21,6 +27,10 @@ const fromTor = async (req, res, 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`
@ -36,10 +46,15 @@ const fromVpn = async (req, res, 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()
})