change the way user ip address is retrieved

This commit is contained in:
wukko 2023-04-09 10:58:23 +06:00
parent b645abc34a
commit 7dc8ab252d
3 changed files with 9 additions and 6 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "cobalt", "name": "cobalt",
"description": "save what you love", "description": "save what you love",
"version": "5.3.2", "version": "5.3.3",
"author": "wukko", "author": "wukko",
"exports": "./src/cobalt.js", "exports": "./src/cobalt.js",
"type": "module", "type": "module",

View file

@ -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 { getCurrentBranch, shortCommit } from "./modules/sub/currentCommit.js";
import { appName, genericUserAgent, version } from "./modules/config.js"; import { appName, genericUserAgent, version } from "./modules/config.js";
import { getJSON } from "./modules/api.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 { Bright, Cyan, Green, Red } from "./modules/sub/consoleText.js";
import stream from "./modules/stream/stream.js"; import stream from "./modules/stream/stream.js";
import loc from "./localization/manager.js"; import loc from "./localization/manager.js";
@ -35,7 +35,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
max: 25, max: 25,
standardHeaders: false, standardHeaders: false,
legacyHeaders: 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) => { handler: (req, res, next, opt) => {
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
return; return;
@ -46,7 +46,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
max: 28, max: 28,
standardHeaders: false, standardHeaders: false,
legacyHeaders: 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) => { handler: (req, res, next, opt) => {
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
return; return;
@ -93,7 +93,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
app.post('/api/json', async (req, res) => { app.post('/api/json', async (req, res) => {
try { 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 lang = languageCode(req);
let j = apiJSON(0, { t: "Bad request" }); let j = apiJSON(0, { t: "Bad request" });
try { try {
@ -119,7 +119,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
app.get('/api/:type', (req, res) => { app.get('/api/:type', (req, res) => {
try { 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) { switch (req.params.type) {
case 'stream': case 'stream':
if (req.query.p) { if (req.query.p) {

View file

@ -134,3 +134,6 @@ export function checkJSONPost(obj) {
return false return false
} }
} }
export function getIP(req) {
return req.header('cf-connecting-ip') ? req.header('cf-connecting-ip') : req.ip;
}