|
|
@ -1,25 +1,31 @@ |
|
|
|
'use strict'; |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
const { ipHashPermLevel } = require(__dirname+'/../configs/main.js') |
|
|
|
const { ipHashPermLevel } = require(__dirname+'/../configs/main.js') |
|
|
|
, { isIP } = require('net') |
|
|
|
, { parse } = require('ip6addr') |
|
|
|
, hashIp = require(__dirname+'/haship.js'); |
|
|
|
, hashIp = require(__dirname+'/haship.js'); |
|
|
|
|
|
|
|
|
|
|
|
module.exports = (req, res, next) => { |
|
|
|
module.exports = (req, res, next) => { |
|
|
|
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress; //need to consider forwarded-for, etc here and in nginx
|
|
|
|
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress; //need to consider forwarded-for, etc here and in nginx
|
|
|
|
const ipVersion = isIP(ip); |
|
|
|
try { |
|
|
|
if (ipVersion) { |
|
|
|
const ipParsed = parse(ip); |
|
|
|
const delimiter = ipVersion === 4 ? '.' : ':'; |
|
|
|
const ipStr = ipParsed.toString({ |
|
|
|
let split = ip.split(delimiter); |
|
|
|
format: ipParsed.kind() === 'ipv4' ? 'v4' : 'v6', |
|
|
|
|
|
|
|
zeroElide: false, |
|
|
|
|
|
|
|
zeroPad: false, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
const delimiter = ipParsed.kind() === 'ipv4' ? '.' : ':'; |
|
|
|
|
|
|
|
let split = ipStr.split(delimiter); |
|
|
|
const qrange = split.slice(0,Math.floor(split.length*0.75)).join(delimiter); |
|
|
|
const qrange = split.slice(0,Math.floor(split.length*0.75)).join(delimiter); |
|
|
|
const hrange = split.slice(0,Math.floor(split.length*0.5)).join(delimiter); |
|
|
|
const hrange = split.slice(0,Math.floor(split.length*0.5)).join(delimiter); |
|
|
|
res.locals.ip = { |
|
|
|
res.locals.ip = { |
|
|
|
raw: ipHashPermLevel === -1 ? hashIp(ip) : ip, |
|
|
|
raw: ipHashPermLevel === -1 ? hashIp(ipStr) : ipStr, |
|
|
|
single: hashIp(ip), |
|
|
|
single: hashIp(ipStr), |
|
|
|
qrange: hashIp(qrange), |
|
|
|
qrange: hashIp(qrange), |
|
|
|
hrange: hashIp(hrange), |
|
|
|
hrange: hashIp(hrange), |
|
|
|
} |
|
|
|
} |
|
|
|
next(); |
|
|
|
next(); |
|
|
|
} else { |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error('Ip parse failed', e); |
|
|
|
return res.status(400).render('message', { |
|
|
|
return res.status(400).render('message', { |
|
|
|
'title': 'Bad request', |
|
|
|
'title': 'Bad request', |
|
|
|
'message': 'Malformed IP' //should never get here
|
|
|
|
'message': 'Malformed IP' //should never get here
|
|
|
|