strapi-plugin-magic-sessionmanager 4.6.3 → 4.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +5 -4
- package/dist/admin/{Analytics-CwNJ3TNI.mjs → Analytics-B5IR0Awr.mjs} +1 -1
- package/dist/admin/{Analytics-BMkhiuRX.js → Analytics-BJlc-yE3.js} +1 -1
- package/dist/admin/{App-Sk0F9UEN.mjs → App-BEniJePD.mjs} +36 -6
- package/dist/admin/{App-DazHh7Zi.js → App-DisCm99X.js} +36 -6
- package/dist/admin/{License-6SIpR7Rk.mjs → License-BjP7GvCt.mjs} +1 -1
- package/dist/admin/{License-BwbmzbP_.js → License-DqDBPjZH.js} +1 -1
- package/dist/admin/{OnlineUsersWidget-Qq_uCTi1.mjs → OnlineUsersWidget-Cm9O0Fjb.mjs} +1 -1
- package/dist/admin/{OnlineUsersWidget-BIfnbFcZ.js → OnlineUsersWidget-DXdeGmfV.js} +1 -1
- package/dist/admin/{Settings-BA7S_8DF.mjs → Settings-CnO501iu.mjs} +1 -6
- package/dist/admin/{Settings-CCYScjxv.js → Settings-DoLec2MD.js} +1 -6
- package/dist/admin/{index-UTLA1r40.mjs → index-CpMDG2XI.mjs} +5 -5
- package/dist/admin/{index-DTskQVDl.js → index-DJHhzin_.js} +5 -5
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/server/index.js +295 -93
- package/dist/server/index.mjs +295 -93
- package/package.json +1 -1
package/dist/server/index.js
CHANGED
|
@@ -9810,6 +9810,18 @@ var require_bootstrap = __commonJS({
|
|
|
9810
9810
|
if (path.startsWith("/api/magic-link/login") && (method === "GET" || method === "POST")) return true;
|
|
9811
9811
|
return false;
|
|
9812
9812
|
}
|
|
9813
|
+
async function getRequestSettings(strapi2, ctx) {
|
|
9814
|
+
let settings = {};
|
|
9815
|
+
try {
|
|
9816
|
+
settings = await getPluginSettings(strapi2);
|
|
9817
|
+
} catch {
|
|
9818
|
+
settings = {};
|
|
9819
|
+
}
|
|
9820
|
+
if (ctx && ctx.state) {
|
|
9821
|
+
ctx.state.__magicSessionSettings = settings;
|
|
9822
|
+
}
|
|
9823
|
+
return settings;
|
|
9824
|
+
}
|
|
9813
9825
|
module2.exports = async ({ strapi: strapi2 }) => {
|
|
9814
9826
|
const log = createLogger(strapi2);
|
|
9815
9827
|
log.info("[START] Bootstrap starting...");
|
|
@@ -9907,12 +9919,7 @@ var require_bootstrap = __commonJS({
|
|
|
9907
9919
|
if (!isLoginPath(ctx.path, ctx.method)) {
|
|
9908
9920
|
return next();
|
|
9909
9921
|
}
|
|
9910
|
-
|
|
9911
|
-
try {
|
|
9912
|
-
settings = await getPluginSettings(strapi2);
|
|
9913
|
-
} catch {
|
|
9914
|
-
settings = {};
|
|
9915
|
-
}
|
|
9922
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
9916
9923
|
const needsGeoCheck = settings.blockSuspiciousSessions || settings.enableGeofencing || settings.enableGeolocation;
|
|
9917
9924
|
if (!needsGeoCheck) {
|
|
9918
9925
|
return next();
|
|
@@ -10050,13 +10057,8 @@ var require_bootstrap = __commonJS({
|
|
|
10050
10057
|
const clearFailures = (ip) => failed.delete(ip);
|
|
10051
10058
|
strapi2.server.use(async (ctx, next) => {
|
|
10052
10059
|
if (!isLoginPath(ctx.path, ctx.method)) return next();
|
|
10053
|
-
|
|
10054
|
-
|
|
10055
|
-
const settings = await getPluginSettings(strapi2);
|
|
10056
|
-
maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10057
|
-
} catch {
|
|
10058
|
-
maxFailed = 0;
|
|
10059
|
-
}
|
|
10060
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
10061
|
+
const maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10060
10062
|
if (maxFailed <= 0) return next();
|
|
10061
10063
|
const ip = getClientIp(ctx);
|
|
10062
10064
|
if (!ip || ip === "unknown") return next();
|
|
@@ -10115,6 +10117,7 @@ var require_bootstrap = __commonJS({
|
|
|
10115
10117
|
if (!ctx.body || !ctx.body.jwt || !ctx.body.user) return;
|
|
10116
10118
|
try {
|
|
10117
10119
|
const user = ctx.body.user;
|
|
10120
|
+
await getRequestSettings(strapi2, ctx);
|
|
10118
10121
|
const ip = getClientIp(ctx);
|
|
10119
10122
|
const headers = ctx.request.headers || ctx.request.header || {};
|
|
10120
10123
|
const userAgent = headers["user-agent"] || "unknown";
|
|
@@ -13260,7 +13263,7 @@ var require_session2 = __commonJS({
|
|
|
13260
13263
|
var require_package = __commonJS({
|
|
13261
13264
|
"package.json"(exports2, module2) {
|
|
13262
13265
|
module2.exports = {
|
|
13263
|
-
version: "4.6.
|
|
13266
|
+
version: "4.6.4",
|
|
13264
13267
|
keywords: [
|
|
13265
13268
|
"strapi",
|
|
13266
13269
|
"plugin",
|
|
@@ -13599,12 +13602,25 @@ var require_geolocation = __commonJS({
|
|
|
13599
13602
|
"server/src/services/geolocation.js"(exports2, module2) {
|
|
13600
13603
|
"use strict";
|
|
13601
13604
|
var fs = require("node:fs");
|
|
13605
|
+
var net = require("node:net");
|
|
13602
13606
|
var path = require("node:path");
|
|
13603
13607
|
var { getPluginSettings } = require_settings_loader();
|
|
13604
13608
|
var GEO_API_TIMEOUT_MS = 4e3;
|
|
13609
|
+
var IPAPI_SUCCESS_CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
13610
|
+
var IPAPI_FAILURE_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
13611
|
+
var IPAPI_RATE_LIMIT_BACKOFF_MS = 15 * 60 * 1e3;
|
|
13612
|
+
var IPAPI_RATE_LIMIT_LOG_INTERVAL_MS = 60 * 1e3;
|
|
13613
|
+
var IPAPI_MAX_CONCURRENT_REQUESTS = 4;
|
|
13614
|
+
var IPAPI_MAX_QUEUE_SIZE = 50;
|
|
13605
13615
|
var DEFAULT_MMDB_PATH = path.resolve(process.cwd(), "data", "GeoLite2-Country.mmdb");
|
|
13606
13616
|
var VALID_PROVIDERS = /* @__PURE__ */ new Set(["auto", "local-mmdb", "ipapi", "disabled"]);
|
|
13607
13617
|
var readerCache = /* @__PURE__ */ new Map();
|
|
13618
|
+
var ipapiCache = /* @__PURE__ */ new Map();
|
|
13619
|
+
var ipapiInFlight = /* @__PURE__ */ new Map();
|
|
13620
|
+
var ipapiQueue = [];
|
|
13621
|
+
var ipapiActiveRequests = 0;
|
|
13622
|
+
var ipapiRateLimitedUntil = 0;
|
|
13623
|
+
var ipapiLastRateLimitLogAt = 0;
|
|
13608
13624
|
function normalizeProvider(value) {
|
|
13609
13625
|
return VALID_PROVIDERS.has(value) ? value : "auto";
|
|
13610
13626
|
}
|
|
@@ -13628,6 +13644,119 @@ var require_geolocation = __commonJS({
|
|
|
13628
13644
|
throw err;
|
|
13629
13645
|
}
|
|
13630
13646
|
}
|
|
13647
|
+
function cloneGeoData(data) {
|
|
13648
|
+
return data ? { ...data } : data;
|
|
13649
|
+
}
|
|
13650
|
+
function getCachedIpapiResult(lookupIp) {
|
|
13651
|
+
const cached = ipapiCache.get(lookupIp);
|
|
13652
|
+
if (!cached) return null;
|
|
13653
|
+
if (cached.expiresAt <= Date.now()) {
|
|
13654
|
+
ipapiCache.delete(lookupIp);
|
|
13655
|
+
return null;
|
|
13656
|
+
}
|
|
13657
|
+
return cloneGeoData(cached.data);
|
|
13658
|
+
}
|
|
13659
|
+
function cacheIpapiResult(lookupIp, data, ttlMs) {
|
|
13660
|
+
if (!lookupIp || !data || ttlMs <= 0) return;
|
|
13661
|
+
if (ipapiCache.size > 1e3) {
|
|
13662
|
+
const oldestKey = ipapiCache.keys().next().value;
|
|
13663
|
+
if (oldestKey) ipapiCache.delete(oldestKey);
|
|
13664
|
+
}
|
|
13665
|
+
ipapiCache.set(lookupIp, {
|
|
13666
|
+
data: cloneGeoData(data),
|
|
13667
|
+
expiresAt: Date.now() + ttlMs
|
|
13668
|
+
});
|
|
13669
|
+
}
|
|
13670
|
+
function getRetryAfterMs(response) {
|
|
13671
|
+
const retryAfter = response?.headers?.get?.("retry-after");
|
|
13672
|
+
if (!retryAfter) return null;
|
|
13673
|
+
const seconds = Number.parseInt(retryAfter, 10);
|
|
13674
|
+
if (Number.isFinite(seconds)) {
|
|
13675
|
+
return Math.max(1e3, Math.min(seconds * 1e3, 60 * 60 * 1e3));
|
|
13676
|
+
}
|
|
13677
|
+
const retryDate = Date.parse(retryAfter);
|
|
13678
|
+
if (Number.isFinite(retryDate)) {
|
|
13679
|
+
return Math.max(1e3, Math.min(retryDate - Date.now(), 60 * 60 * 1e3));
|
|
13680
|
+
}
|
|
13681
|
+
return null;
|
|
13682
|
+
}
|
|
13683
|
+
function markIpapiRateLimited(response) {
|
|
13684
|
+
const backoffMs = getRetryAfterMs(response) || IPAPI_RATE_LIMIT_BACKOFF_MS;
|
|
13685
|
+
ipapiRateLimitedUntil = Math.max(ipapiRateLimitedUntil, Date.now() + backoffMs);
|
|
13686
|
+
return backoffMs;
|
|
13687
|
+
}
|
|
13688
|
+
function getIpapiRateLimitRemainingMs() {
|
|
13689
|
+
return Math.max(0, ipapiRateLimitedUntil - Date.now());
|
|
13690
|
+
}
|
|
13691
|
+
function logIpapiRateLimit(strapi2, message) {
|
|
13692
|
+
const now = Date.now();
|
|
13693
|
+
if (now - ipapiLastRateLimitLogAt < IPAPI_RATE_LIMIT_LOG_INTERVAL_MS) return;
|
|
13694
|
+
ipapiLastRateLimitLogAt = now;
|
|
13695
|
+
strapi2.log.warn(message);
|
|
13696
|
+
}
|
|
13697
|
+
async function acquireIpapiSlot() {
|
|
13698
|
+
if (ipapiActiveRequests < IPAPI_MAX_CONCURRENT_REQUESTS) {
|
|
13699
|
+
ipapiActiveRequests++;
|
|
13700
|
+
return releaseIpapiSlot;
|
|
13701
|
+
}
|
|
13702
|
+
if (ipapiQueue.length >= IPAPI_MAX_QUEUE_SIZE) {
|
|
13703
|
+
return null;
|
|
13704
|
+
}
|
|
13705
|
+
return new Promise((resolve) => {
|
|
13706
|
+
ipapiQueue.push(resolve);
|
|
13707
|
+
});
|
|
13708
|
+
}
|
|
13709
|
+
function releaseIpapiSlot() {
|
|
13710
|
+
ipapiActiveRequests = Math.max(0, ipapiActiveRequests - 1);
|
|
13711
|
+
const next = ipapiQueue.shift();
|
|
13712
|
+
if (next) {
|
|
13713
|
+
ipapiActiveRequests++;
|
|
13714
|
+
next(releaseIpapiSlot);
|
|
13715
|
+
}
|
|
13716
|
+
}
|
|
13717
|
+
function normalizeLookupIp(ipAddress) {
|
|
13718
|
+
if (typeof ipAddress !== "string") return "";
|
|
13719
|
+
let normalized = ipAddress.trim();
|
|
13720
|
+
if (!normalized) return "";
|
|
13721
|
+
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
|
13722
|
+
normalized = normalized.slice(1, -1).trim();
|
|
13723
|
+
}
|
|
13724
|
+
const ipv4Mapped = normalized.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i);
|
|
13725
|
+
if (ipv4Mapped && net.isIP(ipv4Mapped[1]) === 4) {
|
|
13726
|
+
return ipv4Mapped[1];
|
|
13727
|
+
}
|
|
13728
|
+
return normalized;
|
|
13729
|
+
}
|
|
13730
|
+
function parseIpv4(ip) {
|
|
13731
|
+
if (net.isIP(ip) !== 4) return null;
|
|
13732
|
+
const parts = ip.split(".").map((part) => Number(part));
|
|
13733
|
+
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
|
|
13734
|
+
return null;
|
|
13735
|
+
}
|
|
13736
|
+
return parts;
|
|
13737
|
+
}
|
|
13738
|
+
function isPrivateIpv4(ip) {
|
|
13739
|
+
const parts = parseIpv4(ip);
|
|
13740
|
+
if (!parts) return false;
|
|
13741
|
+
const [a, b, c] = parts;
|
|
13742
|
+
return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 192 && b === 0 && c === 0 || a === 192 && b === 0 && c === 2 || a === 198 && (b === 18 || b === 19) || a === 198 && b === 51 && c === 100 || a === 203 && b === 0 && c === 113 || a >= 224;
|
|
13743
|
+
}
|
|
13744
|
+
function getFirstHextet(ip) {
|
|
13745
|
+
const first = ip.split(":")[0];
|
|
13746
|
+
if (!first) return 0;
|
|
13747
|
+
const parsed = Number.parseInt(first, 16);
|
|
13748
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
13749
|
+
}
|
|
13750
|
+
function isPrivateIpv6(ip) {
|
|
13751
|
+
const lower = ip.toLowerCase();
|
|
13752
|
+
if (lower === "::" || lower === "::1") return true;
|
|
13753
|
+
if (/^(?:0:){7}0$/.test(lower) || /^(?:0:){7}1$/.test(lower)) return true;
|
|
13754
|
+
if (lower.startsWith("::ffff:")) {
|
|
13755
|
+
return isPrivateIpv4(normalizeLookupIp(lower));
|
|
13756
|
+
}
|
|
13757
|
+
const firstHextet = getFirstHextet(lower);
|
|
13758
|
+
return (firstHextet & 65024) === 64512 || (firstHextet & 65472) === 65152;
|
|
13759
|
+
}
|
|
13631
13760
|
module2.exports = ({ strapi: strapi2 }) => ({
|
|
13632
13761
|
/**
|
|
13633
13762
|
* Looks up geolocation data for an IP address.
|
|
@@ -13647,9 +13776,10 @@ var require_geolocation = __commonJS({
|
|
|
13647
13776
|
* @returns {Promise<object>}
|
|
13648
13777
|
*/
|
|
13649
13778
|
async getIpInfo(ipAddress) {
|
|
13650
|
-
|
|
13779
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13780
|
+
if (!lookupIp || this.isPrivateIp(lookupIp)) {
|
|
13651
13781
|
return {
|
|
13652
|
-
ip: ipAddress,
|
|
13782
|
+
ip: lookupIp || ipAddress,
|
|
13653
13783
|
country: "Local Network",
|
|
13654
13784
|
country_code: "XX",
|
|
13655
13785
|
country_flag: "",
|
|
@@ -13667,25 +13797,28 @@ var require_geolocation = __commonJS({
|
|
|
13667
13797
|
_source: "local"
|
|
13668
13798
|
};
|
|
13669
13799
|
}
|
|
13800
|
+
if (net.isIP(lookupIp) === 0) {
|
|
13801
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13802
|
+
}
|
|
13670
13803
|
const settings = await this.getGeoSettings();
|
|
13671
13804
|
const provider = normalizeProvider(settings.geoIpProvider);
|
|
13672
13805
|
if (provider === "disabled") {
|
|
13673
|
-
return this.getFallbackData(
|
|
13806
|
+
return this.getFallbackData(lookupIp, "disabled", "disabled");
|
|
13674
13807
|
}
|
|
13675
13808
|
if (provider === "local-mmdb") {
|
|
13676
|
-
return this.getLocalMmdbIpInfo(
|
|
13809
|
+
return this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13677
13810
|
}
|
|
13678
13811
|
if (provider === "auto") {
|
|
13679
13812
|
const databasePath = resolveDatabasePath(settings);
|
|
13680
13813
|
if (databasePath && fs.existsSync(databasePath)) {
|
|
13681
|
-
const localResult = await this.getLocalMmdbIpInfo(
|
|
13682
|
-
if (localResult?._status === "ok") {
|
|
13814
|
+
const localResult = await this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13815
|
+
if (localResult?._status === "ok" || settings.geoLookupFailureMode === "block") {
|
|
13683
13816
|
return localResult;
|
|
13684
13817
|
}
|
|
13685
13818
|
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed (${localResult?._reason || localResult?._status}), falling back to ipapi.co`);
|
|
13686
13819
|
}
|
|
13687
13820
|
}
|
|
13688
|
-
return this.getIpapiIpInfo(
|
|
13821
|
+
return this.getIpapiIpInfo(lookupIp);
|
|
13689
13822
|
},
|
|
13690
13823
|
/**
|
|
13691
13824
|
* Reads effective GEOIP provider settings.
|
|
@@ -13705,6 +13838,10 @@ var require_geolocation = __commonJS({
|
|
|
13705
13838
|
* @returns {Promise<object>}
|
|
13706
13839
|
*/
|
|
13707
13840
|
async getLocalMmdbIpInfo(ipAddress, settings = {}) {
|
|
13841
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13842
|
+
if (!lookupIp || net.isIP(lookupIp) === 0) {
|
|
13843
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13844
|
+
}
|
|
13708
13845
|
const databasePath = resolveDatabasePath(settings);
|
|
13709
13846
|
if (!databasePath || !fs.existsSync(databasePath)) {
|
|
13710
13847
|
return this.getFallbackData(
|
|
@@ -13716,12 +13853,12 @@ var require_geolocation = __commonJS({
|
|
|
13716
13853
|
}
|
|
13717
13854
|
try {
|
|
13718
13855
|
const reader = await openLocalReader(databasePath);
|
|
13719
|
-
const data = reader.country(
|
|
13856
|
+
const data = reader.country(lookupIp);
|
|
13720
13857
|
const country = data.country || data.registeredCountry || data.representedCountry || {};
|
|
13721
13858
|
const countryCode = country.isoCode || data.registeredCountry?.isoCode || "XX";
|
|
13722
13859
|
const countryNames = country.names || {};
|
|
13723
13860
|
const result = {
|
|
13724
|
-
ip:
|
|
13861
|
+
ip: lookupIp,
|
|
13725
13862
|
country: countryNames.en || countryNames.de || countryCode || "Unknown",
|
|
13726
13863
|
country_code: countryCode,
|
|
13727
13864
|
country_flag: this.getCountryFlag(countryCode),
|
|
@@ -13744,11 +13881,11 @@ var require_geolocation = __commonJS({
|
|
|
13744
13881
|
_status: "ok",
|
|
13745
13882
|
_source: "local-mmdb"
|
|
13746
13883
|
};
|
|
13747
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${
|
|
13884
|
+
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${lookupIp}: ${result.country_code}`);
|
|
13748
13885
|
return result;
|
|
13749
13886
|
} catch (error) {
|
|
13750
|
-
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed for ${
|
|
13751
|
-
return this.getFallbackData(
|
|
13887
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed for ${lookupIp}:`, error.message);
|
|
13888
|
+
return this.getFallbackData(lookupIp, "error", "local-mmdb", error.message);
|
|
13752
13889
|
}
|
|
13753
13890
|
},
|
|
13754
13891
|
/**
|
|
@@ -13757,65 +13894,135 @@ var require_geolocation = __commonJS({
|
|
|
13757
13894
|
* @returns {Promise<object>}
|
|
13758
13895
|
*/
|
|
13759
13896
|
async getIpapiIpInfo(ipAddress) {
|
|
13760
|
-
const
|
|
13761
|
-
|
|
13762
|
-
|
|
13763
|
-
|
|
13764
|
-
|
|
13765
|
-
|
|
13766
|
-
|
|
13767
|
-
|
|
13768
|
-
|
|
13769
|
-
strapi2
|
|
13770
|
-
|
|
13897
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13898
|
+
if (!lookupIp || net.isIP(lookupIp) === 0) {
|
|
13899
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13900
|
+
}
|
|
13901
|
+
const cached = getCachedIpapiResult(lookupIp);
|
|
13902
|
+
if (cached) return cached;
|
|
13903
|
+
const rateLimitRemainingMs = getIpapiRateLimitRemainingMs();
|
|
13904
|
+
if (rateLimitRemainingMs > 0) {
|
|
13905
|
+
logIpapiRateLimit(
|
|
13906
|
+
strapi2,
|
|
13907
|
+
`[magic-sessionmanager/geolocation] ipapi.co is rate-limited; skipping remote lookups for ${Math.ceil(rateLimitRemainingMs / 1e3)}s`
|
|
13908
|
+
);
|
|
13909
|
+
return this.getFallbackData(lookupIp, "rate_limited", "ipapi", "Remote GeoIP provider is backing off after HTTP 429");
|
|
13910
|
+
}
|
|
13911
|
+
if (ipapiInFlight.has(lookupIp)) {
|
|
13912
|
+
return cloneGeoData(await ipapiInFlight.get(lookupIp));
|
|
13913
|
+
}
|
|
13914
|
+
const lookupPromise = (async () => {
|
|
13915
|
+
const releaseSlot = await acquireIpapiSlot();
|
|
13916
|
+
if (!releaseSlot) {
|
|
13917
|
+
const fallback = this.getFallbackData(
|
|
13918
|
+
lookupIp,
|
|
13919
|
+
"rate_limited",
|
|
13920
|
+
"ipapi",
|
|
13921
|
+
"Remote GeoIP provider concurrency limit reached"
|
|
13922
|
+
);
|
|
13923
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13924
|
+
return fallback;
|
|
13925
|
+
}
|
|
13926
|
+
const queuedRateLimitRemainingMs = getIpapiRateLimitRemainingMs();
|
|
13927
|
+
if (queuedRateLimitRemainingMs > 0) {
|
|
13928
|
+
releaseSlot();
|
|
13929
|
+
logIpapiRateLimit(
|
|
13930
|
+
strapi2,
|
|
13931
|
+
`[magic-sessionmanager/geolocation] ipapi.co is rate-limited; skipping remote lookups for ${Math.ceil(queuedRateLimitRemainingMs / 1e3)}s`
|
|
13932
|
+
);
|
|
13933
|
+
return this.getFallbackData(lookupIp, "rate_limited", "ipapi", "Remote GeoIP provider is backing off after HTTP 429");
|
|
13771
13934
|
}
|
|
13772
|
-
const
|
|
13773
|
-
|
|
13774
|
-
|
|
13775
|
-
const
|
|
13776
|
-
|
|
13935
|
+
const controller = new AbortController();
|
|
13936
|
+
const timer = setTimeout(() => controller.abort(), GEO_API_TIMEOUT_MS);
|
|
13937
|
+
try {
|
|
13938
|
+
const response = await fetch(`https://ipapi.co/${encodeURIComponent(lookupIp)}/json/`, {
|
|
13939
|
+
method: "GET",
|
|
13940
|
+
headers: { "User-Agent": "Strapi-Magic-SessionManager/1.0" },
|
|
13941
|
+
signal: controller.signal
|
|
13942
|
+
});
|
|
13943
|
+
if (!response.ok) {
|
|
13944
|
+
const reason = response.status === 429 ? "rate_limited" : "error";
|
|
13945
|
+
if (response.status === 429) {
|
|
13946
|
+
const backoffMs = markIpapiRateLimited(response);
|
|
13947
|
+
logIpapiRateLimit(
|
|
13948
|
+
strapi2,
|
|
13949
|
+
`[magic-sessionmanager/geolocation] API returned HTTP 429; backing off ipapi.co lookups for ${Math.ceil(backoffMs / 1e3)}s`
|
|
13950
|
+
);
|
|
13951
|
+
} else {
|
|
13952
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] API returned HTTP ${response.status}`);
|
|
13953
|
+
}
|
|
13954
|
+
const fallback = this.getFallbackData(lookupIp, reason, "ipapi", `HTTP ${response.status}`);
|
|
13955
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13956
|
+
return fallback;
|
|
13957
|
+
}
|
|
13958
|
+
const data = await response.json();
|
|
13959
|
+
if (data.error) {
|
|
13960
|
+
const reason = /rate/i.test(data.reason || "") ? "rate_limited" : "error";
|
|
13961
|
+
if (reason === "rate_limited") {
|
|
13962
|
+
const backoffMs = markIpapiRateLimited();
|
|
13963
|
+
logIpapiRateLimit(
|
|
13964
|
+
strapi2,
|
|
13965
|
+
`[magic-sessionmanager/geolocation] API reported rate limit; backing off ipapi.co lookups for ${Math.ceil(backoffMs / 1e3)}s`
|
|
13966
|
+
);
|
|
13967
|
+
} else {
|
|
13968
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] API Error: ${data.reason}`);
|
|
13969
|
+
}
|
|
13970
|
+
const fallback = this.getFallbackData(lookupIp, reason, "ipapi", data.reason);
|
|
13971
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13972
|
+
return fallback;
|
|
13973
|
+
}
|
|
13974
|
+
const result = {
|
|
13975
|
+
ip: data.ip,
|
|
13976
|
+
country: data.country_name,
|
|
13977
|
+
country_code: data.country_code,
|
|
13978
|
+
country_flag: this.getCountryFlag(data.country_code),
|
|
13979
|
+
city: data.city,
|
|
13980
|
+
region: data.region,
|
|
13981
|
+
timezone: data.timezone,
|
|
13982
|
+
latitude: data.latitude,
|
|
13983
|
+
longitude: data.longitude,
|
|
13984
|
+
postal: data.postal,
|
|
13985
|
+
org: data.org,
|
|
13986
|
+
asn: data.asn,
|
|
13987
|
+
isVpn: data.threat?.is_vpn || false,
|
|
13988
|
+
isProxy: data.threat?.is_proxy || false,
|
|
13989
|
+
isThreat: data.threat?.is_threat || false,
|
|
13990
|
+
isAnonymous: data.threat?.is_anonymous || false,
|
|
13991
|
+
isTor: data.threat?.is_tor || false,
|
|
13992
|
+
securityScore: this.calculateSecurityScore({
|
|
13993
|
+
isVpn: data.threat?.is_vpn,
|
|
13994
|
+
isProxy: data.threat?.is_proxy,
|
|
13995
|
+
isThreat: data.threat?.is_threat,
|
|
13996
|
+
isAnonymous: data.threat?.is_anonymous,
|
|
13997
|
+
isTor: data.threat?.is_tor
|
|
13998
|
+
}),
|
|
13999
|
+
riskLevel: this.getRiskLevel({
|
|
14000
|
+
isVpn: data.threat?.is_vpn,
|
|
14001
|
+
isProxy: data.threat?.is_proxy,
|
|
14002
|
+
isThreat: data.threat?.is_threat
|
|
14003
|
+
}),
|
|
14004
|
+
_status: "ok",
|
|
14005
|
+
_source: "ipapi"
|
|
14006
|
+
};
|
|
14007
|
+
strapi2.log.debug(`[magic-sessionmanager/geolocation] IP ${ipAddress}: ${result.city}, ${result.country} (Score: ${result.securityScore})`);
|
|
14008
|
+
cacheIpapiResult(lookupIp, result, IPAPI_SUCCESS_CACHE_TTL_MS);
|
|
14009
|
+
return result;
|
|
14010
|
+
} catch (error) {
|
|
14011
|
+
const status = error?.name === "AbortError" ? "error" : "error";
|
|
14012
|
+
strapi2.log.error(`[magic-sessionmanager/geolocation] Error fetching IP info for ${lookupIp}:`, error.message);
|
|
14013
|
+
const fallback = this.getFallbackData(lookupIp, status, "ipapi", error.message);
|
|
14014
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
14015
|
+
return fallback;
|
|
14016
|
+
} finally {
|
|
14017
|
+
clearTimeout(timer);
|
|
14018
|
+
releaseSlot();
|
|
13777
14019
|
}
|
|
13778
|
-
|
|
13779
|
-
|
|
13780
|
-
|
|
13781
|
-
|
|
13782
|
-
country_flag: this.getCountryFlag(data.country_code),
|
|
13783
|
-
city: data.city,
|
|
13784
|
-
region: data.region,
|
|
13785
|
-
timezone: data.timezone,
|
|
13786
|
-
latitude: data.latitude,
|
|
13787
|
-
longitude: data.longitude,
|
|
13788
|
-
postal: data.postal,
|
|
13789
|
-
org: data.org,
|
|
13790
|
-
asn: data.asn,
|
|
13791
|
-
isVpn: data.threat?.is_vpn || false,
|
|
13792
|
-
isProxy: data.threat?.is_proxy || false,
|
|
13793
|
-
isThreat: data.threat?.is_threat || false,
|
|
13794
|
-
isAnonymous: data.threat?.is_anonymous || false,
|
|
13795
|
-
isTor: data.threat?.is_tor || false,
|
|
13796
|
-
securityScore: this.calculateSecurityScore({
|
|
13797
|
-
isVpn: data.threat?.is_vpn,
|
|
13798
|
-
isProxy: data.threat?.is_proxy,
|
|
13799
|
-
isThreat: data.threat?.is_threat,
|
|
13800
|
-
isAnonymous: data.threat?.is_anonymous,
|
|
13801
|
-
isTor: data.threat?.is_tor
|
|
13802
|
-
}),
|
|
13803
|
-
riskLevel: this.getRiskLevel({
|
|
13804
|
-
isVpn: data.threat?.is_vpn,
|
|
13805
|
-
isProxy: data.threat?.is_proxy,
|
|
13806
|
-
isThreat: data.threat?.is_threat
|
|
13807
|
-
}),
|
|
13808
|
-
_status: "ok",
|
|
13809
|
-
_source: "ipapi"
|
|
13810
|
-
};
|
|
13811
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] IP ${ipAddress}: ${result.city}, ${result.country} (Score: ${result.securityScore})`);
|
|
13812
|
-
return result;
|
|
13813
|
-
} catch (error) {
|
|
13814
|
-
const status = error?.name === "AbortError" ? "error" : "error";
|
|
13815
|
-
strapi2.log.error(`[magic-sessionmanager/geolocation] Error fetching IP info for ${ipAddress}:`, error.message);
|
|
13816
|
-
return this.getFallbackData(ipAddress, status, "ipapi", error.message);
|
|
14020
|
+
})();
|
|
14021
|
+
ipapiInFlight.set(lookupIp, lookupPromise);
|
|
14022
|
+
try {
|
|
14023
|
+
return cloneGeoData(await lookupPromise);
|
|
13817
14024
|
} finally {
|
|
13818
|
-
|
|
14025
|
+
ipapiInFlight.delete(lookupIp);
|
|
13819
14026
|
}
|
|
13820
14027
|
},
|
|
13821
14028
|
/**
|
|
@@ -13862,17 +14069,12 @@ var require_geolocation = __commonJS({
|
|
|
13862
14069
|
* @returns {boolean}
|
|
13863
14070
|
*/
|
|
13864
14071
|
isPrivateIp(ip) {
|
|
13865
|
-
|
|
13866
|
-
|
|
13867
|
-
if (
|
|
13868
|
-
|
|
13869
|
-
if (
|
|
13870
|
-
|
|
13871
|
-
if (second >= 16 && second <= 31) return true;
|
|
13872
|
-
}
|
|
13873
|
-
if (ip.startsWith("169.254.")) return true;
|
|
13874
|
-
if (ip.startsWith("fc00:") || ip.startsWith("fd00:")) return true;
|
|
13875
|
-
if (ip.startsWith("fe80:")) return true;
|
|
14072
|
+
const normalized = normalizeLookupIp(ip);
|
|
14073
|
+
const lower = normalized.toLowerCase();
|
|
14074
|
+
if (!normalized || lower === "unknown" || lower === "localhost") return true;
|
|
14075
|
+
const version = net.isIP(normalized);
|
|
14076
|
+
if (version === 4) return isPrivateIpv4(normalized);
|
|
14077
|
+
if (version === 6) return isPrivateIpv6(normalized);
|
|
13876
14078
|
return false;
|
|
13877
14079
|
},
|
|
13878
14080
|
/**
|