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 fs = require('fs');
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
const getIP = require("./extra_modules/getip.js")
/**
* usignes a string
* @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
*/
function unsign(text,req,res) {
let ip = req.socket.remoteAddress
let ip = getIP(req)
let unsigned = signature.unsign(text,cookiesecret+ip)
if(!unsigned) {
return false

View File

@ -102,16 +102,7 @@ const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
const SHA = require("./extra_modules/SHA.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
}
const getIP = require("./extra_modules/getip.js")
/**
* quick function to convert data to base64

View File

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