Merge branch 'ip-address-normalize' into 'master'

normalize IP addresses

See merge request fatchan/jschan!169
merge-requests/208/head
Thomas Lynch 4 years ago
commit f674f20390
  1. 22
      helpers/processip.js
  2. 20
      package-lock.json
  3. 1
      package.json

@ -1,25 +1,31 @@
'use strict';
const { ipHashPermLevel } = require(__dirname+'/../configs/main.js')
, { isIP } = require('net')
, { parse } = require('ip6addr')
, hashIp = require(__dirname+'/haship.js');
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 ipVersion = isIP(ip);
if (ipVersion) {
const delimiter = ipVersion === 4 ? '.' : ':';
let split = ip.split(delimiter);
try {
const ipParsed = parse(ip);
const ipStr = ipParsed.toString({
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 hrange = split.slice(0,Math.floor(split.length*0.5)).join(delimiter);
res.locals.ip = {
raw: ipHashPermLevel === -1 ? hashIp(ip) : ip,
single: hashIp(ip),
raw: ipHashPermLevel === -1 ? hashIp(ipStr) : ipStr,
single: hashIp(ipStr),
qrange: hashIp(qrange),
hrange: hashIp(hrange),
}
next();
} else {
} catch(e) {
console.error('Ip parse failed', e);
return res.status(400).render('message', {
'title': 'Bad request',
'message': 'Malformed IP' //should never get here

20
package-lock.json generated

@ -767,8 +767,7 @@
"assert-plus": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
"optional": true
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
},
"assign-symbols": {
"version": "1.0.0",
@ -2675,8 +2674,7 @@
"extsprintf": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=",
"optional": true
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU="
},
"fancy-log": {
"version": "1.3.3",
@ -3994,6 +3992,15 @@
"ipaddr.js": "^1.8.1"
}
},
"ip6addr": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/ip6addr/-/ip6addr-0.2.3.tgz",
"integrity": "sha512-qA9DXRAUW+lT47/i/4+Q3GHPwZjGt/atby1FH/THN6GVATA6+Pjp2nztH7k6iKeil7hzYnBwfSsxjthlJ8lJKw==",
"requires": {
"assert-plus": "^1.0.0",
"jsprim": "^1.4.0"
}
},
"ipaddr.js": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz",
@ -4259,8 +4266,7 @@
"json-schema": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz",
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=",
"optional": true
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
},
"json-schema-traverse": {
"version": "0.4.1",
@ -4292,7 +4298,6 @@
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
"integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=",
"optional": true,
"requires": {
"assert-plus": "1.0.0",
"extsprintf": "1.3.0",
@ -7909,7 +7914,6 @@
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
"integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=",
"optional": true,
"requires": {
"assert-plus": "^1.0.0",
"core-util-is": "1.0.2",

@ -29,6 +29,7 @@
"gulp-uglify-es": "^2.0.0",
"highlight.js": "^10.1.2",
"ioredis": "^4.14.1",
"ip6addr": "^0.2.3",
"mongodb": "^3.6.0",
"node-fetch": "^2.6.0",
"path": "^0.12.7",

Loading…
Cancel
Save