move getIP function to its own module

This commit is contained in:
Mystikfluu 2022-07-31 11:49:11 +02:00
parent 47654513ba
commit 76f1522bf6
4 changed files with 17 additions and 13 deletions

12
extra_modules/getip.js Normal file
View File

@ -0,0 +1,12 @@
/**
* gets ip of a request
* @param {request} req
* @returns ip of the given request, after taking preferred headers into account
*/
function getIP(req) {
let ip = req.socket.remoteAddress;
if(req.headers[config.preferred_ip_header] != undefined && ip == config.only_prefer_when_ip)ip = req.headers[config.preferred_ip_header]
return ip
}
module.exports = getIP

View File

@ -1,6 +1,7 @@
const signature = require('cookie-signature') const signature = require('cookie-signature')
const fs = require('fs'); const fs = require('fs');
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString() const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
const getIP = require("./extra_modules/getip.js")
/** /**
* usignes a string * usignes a string
* @param {string} text text to unsign * @param {string} text text to unsign
@ -9,7 +10,7 @@ const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
* @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false * @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false
*/ */
function unsign(text,req,res) { function unsign(text,req,res) {
let ip = req.socket.remoteAddress let ip = getIP(req)
let unsigned = signature.unsign(text,cookiesecret+ip) let unsigned = signature.unsign(text,cookiesecret+ip)
if(!unsigned) { if(!unsigned) {
return false return false

View File

@ -102,16 +102,7 @@ const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
const SHA = require("./extra_modules/SHA.js") const SHA = require("./extra_modules/SHA.js")
/** const getIP = require("./extra_modules/getip.js")
* gets ip of a request
* @param {request} req
* @returns ip of the given request, after taking preferred headers into account
*/
function getIP(req) {
let ip = req.socket.remoteAddress;
if(req.headers[config.preferred_ip_header] != undefined && ip == config.only_prefer_when_ip)ip = req.headers[config.preferred_ip_header]
return ip
}
/** /**
* quick function to convert data to base64 * quick function to convert data to base64

View File

@ -1,7 +1,7 @@
{ {
"allow_getotheruser_without_cookie": true, "allow_getotheruser_without_cookie": true,
"preferred_ip_header": "X-REAL-IP", "preferred_ip_header": "x-real-ip",
"only_prefer_when_ip": "::ffff:192.168.0.1", "only_prefer_when_ip": "::ffff:127.0.0.1",
"mysql": { "mysql": {
"connections":1000, "connections":1000,
"host":"localhost", "host":"localhost",