volleyball-dev-backend/geoBlocker.js
2025-06-02 16:41:32 +00:00

21 lines
646 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require("axios");
module.exports = async function geoBlocker(req, res, next) {
const ip = req.headers["x-forwarded-for"]?.split(",")[0] || req.socket.remoteAddress;
try {
const { data } = await axios.get(`http://ip-api.com/json/${ip}`);
const country = data.countryCode;
if (country !== "DE") {
console.warn(`❌ Blocked visitor from ${country} (${ip})`);
return res.status(403).send("Zugriff verweigert nur aus Deutschland erlaubt.");
}
next();
} catch (err) {
console.error("🌐 Geo-IP Fehler:", err.message);
res.status(500).send("Geo-IP Service nicht erreichbar.");
}
};