UNPKG

hfs

Version:
65 lines (64 loc) 3.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.geoFilter = exports.ip2country = void 0; const config_1 = require("./config"); const misc_1 = require("./misc"); const promises_1 = require("node:fs/promises"); const ip2location_nodejs_1 = require("ip2location-nodejs"); const lodash_1 = __importDefault(require("lodash")); const connections_1 = require("./connections"); const ip2location = new ip2location_nodejs_1.IP2Location(); const enabled = (0, config_1.defineConfig)(misc_1.CFG.geo_enable, false); const allow = (0, config_1.defineConfig)(misc_1.CFG.geo_allow, null); const list = (0, config_1.defineConfig)(misc_1.CFG.geo_list, []); const allowUnknown = (0, config_1.defineConfig)(misc_1.CFG.geo_allow_unknown, false); enabled.sub(checkFiles); setInterval(checkFiles, misc_1.DAY); // keep updated at run-time // benchmark: memoize can make this 44x faster exports.ip2country = lodash_1.default.memoize((ip) => ip2location.getCountryShortAsync(ip).then(v => v === '-' ? '' : v, () => '')); const geoFilter = async (ctx, next) => { var _a; if (enabled.get() && !(0, misc_1.isLocalHost)(ctx) && !(0, misc_1.isIpLan)(ctx.ip)) { const { connection } = ctx.state; const country = (_a = connection.country) !== null && _a !== void 0 ? _a : (connection.country = await (0, exports.ip2country)(ctx.ip)); if (country) (0, connections_1.updateConnection)(connection, { country }); if (!ctx.state.skipFilters && allow.get() !== null) if (country ? list.get().includes(country) !== allow.get() : !allowUnknown.get()) return (0, connections_1.disconnect)(ctx, 'geo-filter'); } await next(); }; exports.geoFilter = geoFilter; function isOpen() { return Boolean(ip2location.getPackageVersion()); } async function checkFiles() { var _a, _b; if (!enabled.get()) return; const ZIP_FILE = 'IP2LOCATION-LITE-DB1.IPV6.BIN'; const URL = `https://download.ip2location.com/lite/${ZIP_FILE}.ZIP`; const LOCAL_FILE = 'geo_ip.bin'; const TEMP = LOCAL_FILE + '.downloading'; const { mtime = 0 } = await (0, promises_1.stat)(LOCAL_FILE).catch(() => ({ mtime: 0 })); const now = Date.now(); if (+mtime < now - 31 * misc_1.DAY) { // month-old or non-existing console.log('downloading geo-ip db'); await (0, misc_1.unzip)(await (0, misc_1.httpStream)(URL), path => path.toUpperCase().endsWith(ZIP_FILE) && TEMP); if (await (0, promises_1.stat)(TEMP)) if (isOpen()) ip2location.close(); await (0, promises_1.unlink)(LOCAL_FILE).catch(() => { }); await (0, promises_1.rename)(TEMP, LOCAL_FILE); (_b = (_a = exports.ip2country.cache).clear) === null || _b === void 0 ? void 0 : _b.call(_a); console.log('download geo-ip db completed'); } else if (isOpen()) return; console.debug('loading geo-ip db'); ip2location.open(LOCAL_FILE); // using openAsync causes a DEP0137 error within 10 seconds }