From 7dc8ab252d38ab80930e58a826be1465c80fbeba Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 9 Apr 2023 10:58:23 +0600 Subject: [PATCH] change the way user ip address is retrieved --- package.json | 2 +- src/cobalt.js | 10 +++++----- src/modules/sub/utils.js | 3 +++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 5738e29d..c0cb02da 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cobalt", "description": "save what you love", - "version": "5.3.2", + "version": "5.3.3", "author": "wukko", "exports": "./src/cobalt.js", "type": "module", diff --git a/src/cobalt.js b/src/cobalt.js index b0f0a33b..3a7423e3 100644 --- a/src/cobalt.js +++ b/src/cobalt.js @@ -12,7 +12,7 @@ const __dirname = path.dirname(__filename).slice(0, -4); // go up another level import { getCurrentBranch, shortCommit } from "./modules/sub/currentCommit.js"; import { appName, genericUserAgent, version } from "./modules/config.js"; import { getJSON } from "./modules/api.js"; -import { apiJSON, checkJSONPost, languageCode } from "./modules/sub/utils.js"; +import { apiJSON, checkJSONPost, getIP, languageCode } from "./modules/sub/utils.js"; import { Bright, Cyan, Green, Red } from "./modules/sub/consoleText.js"; import stream from "./modules/stream/stream.js"; import loc from "./localization/manager.js"; @@ -35,7 +35,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) { max: 25, standardHeaders: false, legacyHeaders: false, - keyGenerator: (req, res) => sha256(req.ip.replace('::ffff:', ''), process.env.streamSalt), + keyGenerator: (req, res) => sha256(getIP(req), process.env.streamSalt), handler: (req, res, next, opt) => { res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); return; @@ -46,7 +46,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) { max: 28, standardHeaders: false, legacyHeaders: false, - keyGenerator: (req, res) => sha256(req.ip.replace('::ffff:', ''), process.env.streamSalt), + keyGenerator: (req, res) => sha256(getIP(req), process.env.streamSalt), handler: (req, res, next, opt) => { res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); return; @@ -93,7 +93,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) { app.post('/api/json', async (req, res) => { try { - let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt); + let ip = sha256(getIP(req), process.env.streamSalt); let lang = languageCode(req); let j = apiJSON(0, { t: "Bad request" }); try { @@ -119,7 +119,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) { app.get('/api/:type', (req, res) => { try { - let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt); + let ip = sha256(getIP(req), process.env.streamSalt); switch (req.params.type) { case 'stream': if (req.query.p) { diff --git a/src/modules/sub/utils.js b/src/modules/sub/utils.js index 32f2618b..db275032 100644 --- a/src/modules/sub/utils.js +++ b/src/modules/sub/utils.js @@ -134,3 +134,6 @@ export function checkJSONPost(obj) { return false } } +export function getIP(req) { + return req.header('cf-connecting-ip') ? req.header('cf-connecting-ip') : req.ip; +}