const blockSpecifiedCountryCodes = (req: express.Request, res: express.Response, next: express.NextFunction) => { const ip = req.ip; const geo = geoip.lookup(ip); const countryCodeWhitelist = config.countryCodeWhitelist || ''; // default to an empty string if undefined const countryCodeWhitelistArray = countryCodeWhitelist.split('/'); const allowedCountries = countryCodeWhitelistArray.length > 1 ? countryCodeWhitelistArray : [countryCodeWhitelist]; const countryCodeBlacklist = config.countryCodeBlacklist || ''; // default to an empty string if undefined const countryCodeBlacklistArray = countryCodeBlacklist.split('/'); const blockedCountries = countryCodeBlacklistArray.length > 1 ? countryCodeBlacklistArray : [countryCodeBlacklist]; if (config.countryCodeBlockMode.toString() !== "none") { if (config.countryCodeBlockMode.toString() === "whitelist" && countryCodeWhitelist.length !== 0 && geo && !allowedCountries.includes(geo.country)) { logger.warn(`Blocked request from IP attached to a non-whitelisted country: ${ip}`); res.status(403).send(ACCESS_DENIED_HTML); } else if (config.countryCodeBlockMode.toString() === "blacklist" && countryCodeBlacklist.length !== 0 && geo && blockedCountries.includes(geo.country)) { logger.warn(`Blocked request from IP attached to a blacklisted country: ${ip}`); res.status(403).send(ACCESS_DENIED_HTML); } else { next(); } } }