strapi-plugin-magic-sessionmanager 4.6.2 → 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 +14 -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 +305 -93
- package/dist/server/index.mjs +305 -93
- package/package.json +1 -1
package/dist/server/index.mjs
CHANGED
|
@@ -755,10 +755,17 @@ var require_last_seen = __commonJS({
|
|
|
755
755
|
"/api/register",
|
|
756
756
|
"/admin/"
|
|
757
757
|
];
|
|
758
|
+
var SELF_TERMINATING_PATHS = /* @__PURE__ */ new Set([
|
|
759
|
+
"/api/magic-sessionmanager/logout",
|
|
760
|
+
"/api/magic-sessionmanager/logout-all"
|
|
761
|
+
]);
|
|
758
762
|
function isAuthEndpoint(path) {
|
|
759
763
|
if (!path) return false;
|
|
760
764
|
return AUTH_PATH_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
761
765
|
}
|
|
766
|
+
function isSelfTerminatingEndpoint(path) {
|
|
767
|
+
return SELF_TERMINATING_PATHS.has(path);
|
|
768
|
+
}
|
|
762
769
|
module.exports = ({ strapi: strapi2, sessionService }) => {
|
|
763
770
|
return async (ctx, next) => {
|
|
764
771
|
if (isAuthEndpoint(ctx.path)) {
|
|
@@ -802,6 +809,9 @@ var require_last_seen = __commonJS({
|
|
|
802
809
|
ctx.state.userDocumentId = userDocId;
|
|
803
810
|
ctx.state.__magicSessionId = thisSession.documentId;
|
|
804
811
|
await next();
|
|
812
|
+
if (isSelfTerminatingEndpoint(ctx.path)) {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
805
815
|
try {
|
|
806
816
|
await sessionService.touch({
|
|
807
817
|
userId: userDocId,
|
|
@@ -9806,6 +9816,18 @@ var require_bootstrap = __commonJS({
|
|
|
9806
9816
|
if (path.startsWith("/api/magic-link/login") && (method === "GET" || method === "POST")) return true;
|
|
9807
9817
|
return false;
|
|
9808
9818
|
}
|
|
9819
|
+
async function getRequestSettings(strapi2, ctx) {
|
|
9820
|
+
let settings = {};
|
|
9821
|
+
try {
|
|
9822
|
+
settings = await getPluginSettings(strapi2);
|
|
9823
|
+
} catch {
|
|
9824
|
+
settings = {};
|
|
9825
|
+
}
|
|
9826
|
+
if (ctx && ctx.state) {
|
|
9827
|
+
ctx.state.__magicSessionSettings = settings;
|
|
9828
|
+
}
|
|
9829
|
+
return settings;
|
|
9830
|
+
}
|
|
9809
9831
|
module.exports = async ({ strapi: strapi2 }) => {
|
|
9810
9832
|
const log = createLogger(strapi2);
|
|
9811
9833
|
log.info("[START] Bootstrap starting...");
|
|
@@ -9903,12 +9925,7 @@ var require_bootstrap = __commonJS({
|
|
|
9903
9925
|
if (!isLoginPath(ctx.path, ctx.method)) {
|
|
9904
9926
|
return next();
|
|
9905
9927
|
}
|
|
9906
|
-
|
|
9907
|
-
try {
|
|
9908
|
-
settings = await getPluginSettings(strapi2);
|
|
9909
|
-
} catch {
|
|
9910
|
-
settings = {};
|
|
9911
|
-
}
|
|
9928
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
9912
9929
|
const needsGeoCheck = settings.blockSuspiciousSessions || settings.enableGeofencing || settings.enableGeolocation;
|
|
9913
9930
|
if (!needsGeoCheck) {
|
|
9914
9931
|
return next();
|
|
@@ -10046,13 +10063,8 @@ var require_bootstrap = __commonJS({
|
|
|
10046
10063
|
const clearFailures = (ip) => failed.delete(ip);
|
|
10047
10064
|
strapi2.server.use(async (ctx, next) => {
|
|
10048
10065
|
if (!isLoginPath(ctx.path, ctx.method)) return next();
|
|
10049
|
-
|
|
10050
|
-
|
|
10051
|
-
const settings = await getPluginSettings(strapi2);
|
|
10052
|
-
maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10053
|
-
} catch {
|
|
10054
|
-
maxFailed = 0;
|
|
10055
|
-
}
|
|
10066
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
10067
|
+
const maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10056
10068
|
if (maxFailed <= 0) return next();
|
|
10057
10069
|
const ip = getClientIp(ctx);
|
|
10058
10070
|
if (!ip || ip === "unknown") return next();
|
|
@@ -10111,6 +10123,7 @@ var require_bootstrap = __commonJS({
|
|
|
10111
10123
|
if (!ctx.body || !ctx.body.jwt || !ctx.body.user) return;
|
|
10112
10124
|
try {
|
|
10113
10125
|
const user = ctx.body.user;
|
|
10126
|
+
await getRequestSettings(strapi2, ctx);
|
|
10114
10127
|
const ip = getClientIp(ctx);
|
|
10115
10128
|
const headers = ctx.request.headers || ctx.request.header || {};
|
|
10116
10129
|
const userAgent = headers["user-agent"] || "unknown";
|
|
@@ -13256,7 +13269,7 @@ var require_session2 = __commonJS({
|
|
|
13256
13269
|
var require_package = __commonJS({
|
|
13257
13270
|
"package.json"(exports, module) {
|
|
13258
13271
|
module.exports = {
|
|
13259
|
-
version: "4.6.
|
|
13272
|
+
version: "4.6.4",
|
|
13260
13273
|
keywords: [
|
|
13261
13274
|
"strapi",
|
|
13262
13275
|
"plugin",
|
|
@@ -13595,12 +13608,25 @@ var require_geolocation = __commonJS({
|
|
|
13595
13608
|
"server/src/services/geolocation.js"(exports, module) {
|
|
13596
13609
|
"use strict";
|
|
13597
13610
|
var fs = __require("node:fs");
|
|
13611
|
+
var net = __require("node:net");
|
|
13598
13612
|
var path = __require("node:path");
|
|
13599
13613
|
var { getPluginSettings } = require_settings_loader();
|
|
13600
13614
|
var GEO_API_TIMEOUT_MS = 4e3;
|
|
13615
|
+
var IPAPI_SUCCESS_CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
13616
|
+
var IPAPI_FAILURE_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
13617
|
+
var IPAPI_RATE_LIMIT_BACKOFF_MS = 15 * 60 * 1e3;
|
|
13618
|
+
var IPAPI_RATE_LIMIT_LOG_INTERVAL_MS = 60 * 1e3;
|
|
13619
|
+
var IPAPI_MAX_CONCURRENT_REQUESTS = 4;
|
|
13620
|
+
var IPAPI_MAX_QUEUE_SIZE = 50;
|
|
13601
13621
|
var DEFAULT_MMDB_PATH = path.resolve(process.cwd(), "data", "GeoLite2-Country.mmdb");
|
|
13602
13622
|
var VALID_PROVIDERS = /* @__PURE__ */ new Set(["auto", "local-mmdb", "ipapi", "disabled"]);
|
|
13603
13623
|
var readerCache = /* @__PURE__ */ new Map();
|
|
13624
|
+
var ipapiCache = /* @__PURE__ */ new Map();
|
|
13625
|
+
var ipapiInFlight = /* @__PURE__ */ new Map();
|
|
13626
|
+
var ipapiQueue = [];
|
|
13627
|
+
var ipapiActiveRequests = 0;
|
|
13628
|
+
var ipapiRateLimitedUntil = 0;
|
|
13629
|
+
var ipapiLastRateLimitLogAt = 0;
|
|
13604
13630
|
function normalizeProvider(value) {
|
|
13605
13631
|
return VALID_PROVIDERS.has(value) ? value : "auto";
|
|
13606
13632
|
}
|
|
@@ -13624,6 +13650,119 @@ var require_geolocation = __commonJS({
|
|
|
13624
13650
|
throw err;
|
|
13625
13651
|
}
|
|
13626
13652
|
}
|
|
13653
|
+
function cloneGeoData(data) {
|
|
13654
|
+
return data ? { ...data } : data;
|
|
13655
|
+
}
|
|
13656
|
+
function getCachedIpapiResult(lookupIp) {
|
|
13657
|
+
const cached = ipapiCache.get(lookupIp);
|
|
13658
|
+
if (!cached) return null;
|
|
13659
|
+
if (cached.expiresAt <= Date.now()) {
|
|
13660
|
+
ipapiCache.delete(lookupIp);
|
|
13661
|
+
return null;
|
|
13662
|
+
}
|
|
13663
|
+
return cloneGeoData(cached.data);
|
|
13664
|
+
}
|
|
13665
|
+
function cacheIpapiResult(lookupIp, data, ttlMs) {
|
|
13666
|
+
if (!lookupIp || !data || ttlMs <= 0) return;
|
|
13667
|
+
if (ipapiCache.size > 1e3) {
|
|
13668
|
+
const oldestKey = ipapiCache.keys().next().value;
|
|
13669
|
+
if (oldestKey) ipapiCache.delete(oldestKey);
|
|
13670
|
+
}
|
|
13671
|
+
ipapiCache.set(lookupIp, {
|
|
13672
|
+
data: cloneGeoData(data),
|
|
13673
|
+
expiresAt: Date.now() + ttlMs
|
|
13674
|
+
});
|
|
13675
|
+
}
|
|
13676
|
+
function getRetryAfterMs(response) {
|
|
13677
|
+
const retryAfter = response?.headers?.get?.("retry-after");
|
|
13678
|
+
if (!retryAfter) return null;
|
|
13679
|
+
const seconds = Number.parseInt(retryAfter, 10);
|
|
13680
|
+
if (Number.isFinite(seconds)) {
|
|
13681
|
+
return Math.max(1e3, Math.min(seconds * 1e3, 60 * 60 * 1e3));
|
|
13682
|
+
}
|
|
13683
|
+
const retryDate = Date.parse(retryAfter);
|
|
13684
|
+
if (Number.isFinite(retryDate)) {
|
|
13685
|
+
return Math.max(1e3, Math.min(retryDate - Date.now(), 60 * 60 * 1e3));
|
|
13686
|
+
}
|
|
13687
|
+
return null;
|
|
13688
|
+
}
|
|
13689
|
+
function markIpapiRateLimited(response) {
|
|
13690
|
+
const backoffMs = getRetryAfterMs(response) || IPAPI_RATE_LIMIT_BACKOFF_MS;
|
|
13691
|
+
ipapiRateLimitedUntil = Math.max(ipapiRateLimitedUntil, Date.now() + backoffMs);
|
|
13692
|
+
return backoffMs;
|
|
13693
|
+
}
|
|
13694
|
+
function getIpapiRateLimitRemainingMs() {
|
|
13695
|
+
return Math.max(0, ipapiRateLimitedUntil - Date.now());
|
|
13696
|
+
}
|
|
13697
|
+
function logIpapiRateLimit(strapi2, message) {
|
|
13698
|
+
const now = Date.now();
|
|
13699
|
+
if (now - ipapiLastRateLimitLogAt < IPAPI_RATE_LIMIT_LOG_INTERVAL_MS) return;
|
|
13700
|
+
ipapiLastRateLimitLogAt = now;
|
|
13701
|
+
strapi2.log.warn(message);
|
|
13702
|
+
}
|
|
13703
|
+
async function acquireIpapiSlot() {
|
|
13704
|
+
if (ipapiActiveRequests < IPAPI_MAX_CONCURRENT_REQUESTS) {
|
|
13705
|
+
ipapiActiveRequests++;
|
|
13706
|
+
return releaseIpapiSlot;
|
|
13707
|
+
}
|
|
13708
|
+
if (ipapiQueue.length >= IPAPI_MAX_QUEUE_SIZE) {
|
|
13709
|
+
return null;
|
|
13710
|
+
}
|
|
13711
|
+
return new Promise((resolve) => {
|
|
13712
|
+
ipapiQueue.push(resolve);
|
|
13713
|
+
});
|
|
13714
|
+
}
|
|
13715
|
+
function releaseIpapiSlot() {
|
|
13716
|
+
ipapiActiveRequests = Math.max(0, ipapiActiveRequests - 1);
|
|
13717
|
+
const next = ipapiQueue.shift();
|
|
13718
|
+
if (next) {
|
|
13719
|
+
ipapiActiveRequests++;
|
|
13720
|
+
next(releaseIpapiSlot);
|
|
13721
|
+
}
|
|
13722
|
+
}
|
|
13723
|
+
function normalizeLookupIp(ipAddress) {
|
|
13724
|
+
if (typeof ipAddress !== "string") return "";
|
|
13725
|
+
let normalized = ipAddress.trim();
|
|
13726
|
+
if (!normalized) return "";
|
|
13727
|
+
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
|
13728
|
+
normalized = normalized.slice(1, -1).trim();
|
|
13729
|
+
}
|
|
13730
|
+
const ipv4Mapped = normalized.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i);
|
|
13731
|
+
if (ipv4Mapped && net.isIP(ipv4Mapped[1]) === 4) {
|
|
13732
|
+
return ipv4Mapped[1];
|
|
13733
|
+
}
|
|
13734
|
+
return normalized;
|
|
13735
|
+
}
|
|
13736
|
+
function parseIpv4(ip) {
|
|
13737
|
+
if (net.isIP(ip) !== 4) return null;
|
|
13738
|
+
const parts = ip.split(".").map((part) => Number(part));
|
|
13739
|
+
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
|
|
13740
|
+
return null;
|
|
13741
|
+
}
|
|
13742
|
+
return parts;
|
|
13743
|
+
}
|
|
13744
|
+
function isPrivateIpv4(ip) {
|
|
13745
|
+
const parts = parseIpv4(ip);
|
|
13746
|
+
if (!parts) return false;
|
|
13747
|
+
const [a, b, c] = parts;
|
|
13748
|
+
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;
|
|
13749
|
+
}
|
|
13750
|
+
function getFirstHextet(ip) {
|
|
13751
|
+
const first = ip.split(":")[0];
|
|
13752
|
+
if (!first) return 0;
|
|
13753
|
+
const parsed = Number.parseInt(first, 16);
|
|
13754
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
13755
|
+
}
|
|
13756
|
+
function isPrivateIpv6(ip) {
|
|
13757
|
+
const lower = ip.toLowerCase();
|
|
13758
|
+
if (lower === "::" || lower === "::1") return true;
|
|
13759
|
+
if (/^(?:0:){7}0$/.test(lower) || /^(?:0:){7}1$/.test(lower)) return true;
|
|
13760
|
+
if (lower.startsWith("::ffff:")) {
|
|
13761
|
+
return isPrivateIpv4(normalizeLookupIp(lower));
|
|
13762
|
+
}
|
|
13763
|
+
const firstHextet = getFirstHextet(lower);
|
|
13764
|
+
return (firstHextet & 65024) === 64512 || (firstHextet & 65472) === 65152;
|
|
13765
|
+
}
|
|
13627
13766
|
module.exports = ({ strapi: strapi2 }) => ({
|
|
13628
13767
|
/**
|
|
13629
13768
|
* Looks up geolocation data for an IP address.
|
|
@@ -13643,9 +13782,10 @@ var require_geolocation = __commonJS({
|
|
|
13643
13782
|
* @returns {Promise<object>}
|
|
13644
13783
|
*/
|
|
13645
13784
|
async getIpInfo(ipAddress) {
|
|
13646
|
-
|
|
13785
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13786
|
+
if (!lookupIp || this.isPrivateIp(lookupIp)) {
|
|
13647
13787
|
return {
|
|
13648
|
-
ip: ipAddress,
|
|
13788
|
+
ip: lookupIp || ipAddress,
|
|
13649
13789
|
country: "Local Network",
|
|
13650
13790
|
country_code: "XX",
|
|
13651
13791
|
country_flag: "",
|
|
@@ -13663,25 +13803,28 @@ var require_geolocation = __commonJS({
|
|
|
13663
13803
|
_source: "local"
|
|
13664
13804
|
};
|
|
13665
13805
|
}
|
|
13806
|
+
if (net.isIP(lookupIp) === 0) {
|
|
13807
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13808
|
+
}
|
|
13666
13809
|
const settings = await this.getGeoSettings();
|
|
13667
13810
|
const provider = normalizeProvider(settings.geoIpProvider);
|
|
13668
13811
|
if (provider === "disabled") {
|
|
13669
|
-
return this.getFallbackData(
|
|
13812
|
+
return this.getFallbackData(lookupIp, "disabled", "disabled");
|
|
13670
13813
|
}
|
|
13671
13814
|
if (provider === "local-mmdb") {
|
|
13672
|
-
return this.getLocalMmdbIpInfo(
|
|
13815
|
+
return this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13673
13816
|
}
|
|
13674
13817
|
if (provider === "auto") {
|
|
13675
13818
|
const databasePath = resolveDatabasePath(settings);
|
|
13676
13819
|
if (databasePath && fs.existsSync(databasePath)) {
|
|
13677
|
-
const localResult = await this.getLocalMmdbIpInfo(
|
|
13678
|
-
if (localResult?._status === "ok") {
|
|
13820
|
+
const localResult = await this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13821
|
+
if (localResult?._status === "ok" || settings.geoLookupFailureMode === "block") {
|
|
13679
13822
|
return localResult;
|
|
13680
13823
|
}
|
|
13681
13824
|
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed (${localResult?._reason || localResult?._status}), falling back to ipapi.co`);
|
|
13682
13825
|
}
|
|
13683
13826
|
}
|
|
13684
|
-
return this.getIpapiIpInfo(
|
|
13827
|
+
return this.getIpapiIpInfo(lookupIp);
|
|
13685
13828
|
},
|
|
13686
13829
|
/**
|
|
13687
13830
|
* Reads effective GEOIP provider settings.
|
|
@@ -13701,6 +13844,10 @@ var require_geolocation = __commonJS({
|
|
|
13701
13844
|
* @returns {Promise<object>}
|
|
13702
13845
|
*/
|
|
13703
13846
|
async getLocalMmdbIpInfo(ipAddress, settings = {}) {
|
|
13847
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13848
|
+
if (!lookupIp || net.isIP(lookupIp) === 0) {
|
|
13849
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13850
|
+
}
|
|
13704
13851
|
const databasePath = resolveDatabasePath(settings);
|
|
13705
13852
|
if (!databasePath || !fs.existsSync(databasePath)) {
|
|
13706
13853
|
return this.getFallbackData(
|
|
@@ -13712,12 +13859,12 @@ var require_geolocation = __commonJS({
|
|
|
13712
13859
|
}
|
|
13713
13860
|
try {
|
|
13714
13861
|
const reader = await openLocalReader(databasePath);
|
|
13715
|
-
const data = reader.country(
|
|
13862
|
+
const data = reader.country(lookupIp);
|
|
13716
13863
|
const country = data.country || data.registeredCountry || data.representedCountry || {};
|
|
13717
13864
|
const countryCode = country.isoCode || data.registeredCountry?.isoCode || "XX";
|
|
13718
13865
|
const countryNames = country.names || {};
|
|
13719
13866
|
const result = {
|
|
13720
|
-
ip:
|
|
13867
|
+
ip: lookupIp,
|
|
13721
13868
|
country: countryNames.en || countryNames.de || countryCode || "Unknown",
|
|
13722
13869
|
country_code: countryCode,
|
|
13723
13870
|
country_flag: this.getCountryFlag(countryCode),
|
|
@@ -13740,11 +13887,11 @@ var require_geolocation = __commonJS({
|
|
|
13740
13887
|
_status: "ok",
|
|
13741
13888
|
_source: "local-mmdb"
|
|
13742
13889
|
};
|
|
13743
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${
|
|
13890
|
+
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${lookupIp}: ${result.country_code}`);
|
|
13744
13891
|
return result;
|
|
13745
13892
|
} catch (error) {
|
|
13746
|
-
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed for ${
|
|
13747
|
-
return this.getFallbackData(
|
|
13893
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed for ${lookupIp}:`, error.message);
|
|
13894
|
+
return this.getFallbackData(lookupIp, "error", "local-mmdb", error.message);
|
|
13748
13895
|
}
|
|
13749
13896
|
},
|
|
13750
13897
|
/**
|
|
@@ -13753,65 +13900,135 @@ var require_geolocation = __commonJS({
|
|
|
13753
13900
|
* @returns {Promise<object>}
|
|
13754
13901
|
*/
|
|
13755
13902
|
async getIpapiIpInfo(ipAddress) {
|
|
13756
|
-
const
|
|
13757
|
-
|
|
13758
|
-
|
|
13759
|
-
|
|
13760
|
-
|
|
13761
|
-
|
|
13762
|
-
|
|
13763
|
-
|
|
13764
|
-
|
|
13765
|
-
strapi2
|
|
13766
|
-
|
|
13903
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13904
|
+
if (!lookupIp || net.isIP(lookupIp) === 0) {
|
|
13905
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13906
|
+
}
|
|
13907
|
+
const cached = getCachedIpapiResult(lookupIp);
|
|
13908
|
+
if (cached) return cached;
|
|
13909
|
+
const rateLimitRemainingMs = getIpapiRateLimitRemainingMs();
|
|
13910
|
+
if (rateLimitRemainingMs > 0) {
|
|
13911
|
+
logIpapiRateLimit(
|
|
13912
|
+
strapi2,
|
|
13913
|
+
`[magic-sessionmanager/geolocation] ipapi.co is rate-limited; skipping remote lookups for ${Math.ceil(rateLimitRemainingMs / 1e3)}s`
|
|
13914
|
+
);
|
|
13915
|
+
return this.getFallbackData(lookupIp, "rate_limited", "ipapi", "Remote GeoIP provider is backing off after HTTP 429");
|
|
13916
|
+
}
|
|
13917
|
+
if (ipapiInFlight.has(lookupIp)) {
|
|
13918
|
+
return cloneGeoData(await ipapiInFlight.get(lookupIp));
|
|
13919
|
+
}
|
|
13920
|
+
const lookupPromise = (async () => {
|
|
13921
|
+
const releaseSlot = await acquireIpapiSlot();
|
|
13922
|
+
if (!releaseSlot) {
|
|
13923
|
+
const fallback = this.getFallbackData(
|
|
13924
|
+
lookupIp,
|
|
13925
|
+
"rate_limited",
|
|
13926
|
+
"ipapi",
|
|
13927
|
+
"Remote GeoIP provider concurrency limit reached"
|
|
13928
|
+
);
|
|
13929
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13930
|
+
return fallback;
|
|
13931
|
+
}
|
|
13932
|
+
const queuedRateLimitRemainingMs = getIpapiRateLimitRemainingMs();
|
|
13933
|
+
if (queuedRateLimitRemainingMs > 0) {
|
|
13934
|
+
releaseSlot();
|
|
13935
|
+
logIpapiRateLimit(
|
|
13936
|
+
strapi2,
|
|
13937
|
+
`[magic-sessionmanager/geolocation] ipapi.co is rate-limited; skipping remote lookups for ${Math.ceil(queuedRateLimitRemainingMs / 1e3)}s`
|
|
13938
|
+
);
|
|
13939
|
+
return this.getFallbackData(lookupIp, "rate_limited", "ipapi", "Remote GeoIP provider is backing off after HTTP 429");
|
|
13767
13940
|
}
|
|
13768
|
-
const
|
|
13769
|
-
|
|
13770
|
-
|
|
13771
|
-
const
|
|
13772
|
-
|
|
13941
|
+
const controller = new AbortController();
|
|
13942
|
+
const timer = setTimeout(() => controller.abort(), GEO_API_TIMEOUT_MS);
|
|
13943
|
+
try {
|
|
13944
|
+
const response = await fetch(`https://ipapi.co/${encodeURIComponent(lookupIp)}/json/`, {
|
|
13945
|
+
method: "GET",
|
|
13946
|
+
headers: { "User-Agent": "Strapi-Magic-SessionManager/1.0" },
|
|
13947
|
+
signal: controller.signal
|
|
13948
|
+
});
|
|
13949
|
+
if (!response.ok) {
|
|
13950
|
+
const reason = response.status === 429 ? "rate_limited" : "error";
|
|
13951
|
+
if (response.status === 429) {
|
|
13952
|
+
const backoffMs = markIpapiRateLimited(response);
|
|
13953
|
+
logIpapiRateLimit(
|
|
13954
|
+
strapi2,
|
|
13955
|
+
`[magic-sessionmanager/geolocation] API returned HTTP 429; backing off ipapi.co lookups for ${Math.ceil(backoffMs / 1e3)}s`
|
|
13956
|
+
);
|
|
13957
|
+
} else {
|
|
13958
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] API returned HTTP ${response.status}`);
|
|
13959
|
+
}
|
|
13960
|
+
const fallback = this.getFallbackData(lookupIp, reason, "ipapi", `HTTP ${response.status}`);
|
|
13961
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13962
|
+
return fallback;
|
|
13963
|
+
}
|
|
13964
|
+
const data = await response.json();
|
|
13965
|
+
if (data.error) {
|
|
13966
|
+
const reason = /rate/i.test(data.reason || "") ? "rate_limited" : "error";
|
|
13967
|
+
if (reason === "rate_limited") {
|
|
13968
|
+
const backoffMs = markIpapiRateLimited();
|
|
13969
|
+
logIpapiRateLimit(
|
|
13970
|
+
strapi2,
|
|
13971
|
+
`[magic-sessionmanager/geolocation] API reported rate limit; backing off ipapi.co lookups for ${Math.ceil(backoffMs / 1e3)}s`
|
|
13972
|
+
);
|
|
13973
|
+
} else {
|
|
13974
|
+
strapi2.log.warn(`[magic-sessionmanager/geolocation] API Error: ${data.reason}`);
|
|
13975
|
+
}
|
|
13976
|
+
const fallback = this.getFallbackData(lookupIp, reason, "ipapi", data.reason);
|
|
13977
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
13978
|
+
return fallback;
|
|
13979
|
+
}
|
|
13980
|
+
const result = {
|
|
13981
|
+
ip: data.ip,
|
|
13982
|
+
country: data.country_name,
|
|
13983
|
+
country_code: data.country_code,
|
|
13984
|
+
country_flag: this.getCountryFlag(data.country_code),
|
|
13985
|
+
city: data.city,
|
|
13986
|
+
region: data.region,
|
|
13987
|
+
timezone: data.timezone,
|
|
13988
|
+
latitude: data.latitude,
|
|
13989
|
+
longitude: data.longitude,
|
|
13990
|
+
postal: data.postal,
|
|
13991
|
+
org: data.org,
|
|
13992
|
+
asn: data.asn,
|
|
13993
|
+
isVpn: data.threat?.is_vpn || false,
|
|
13994
|
+
isProxy: data.threat?.is_proxy || false,
|
|
13995
|
+
isThreat: data.threat?.is_threat || false,
|
|
13996
|
+
isAnonymous: data.threat?.is_anonymous || false,
|
|
13997
|
+
isTor: data.threat?.is_tor || false,
|
|
13998
|
+
securityScore: this.calculateSecurityScore({
|
|
13999
|
+
isVpn: data.threat?.is_vpn,
|
|
14000
|
+
isProxy: data.threat?.is_proxy,
|
|
14001
|
+
isThreat: data.threat?.is_threat,
|
|
14002
|
+
isAnonymous: data.threat?.is_anonymous,
|
|
14003
|
+
isTor: data.threat?.is_tor
|
|
14004
|
+
}),
|
|
14005
|
+
riskLevel: this.getRiskLevel({
|
|
14006
|
+
isVpn: data.threat?.is_vpn,
|
|
14007
|
+
isProxy: data.threat?.is_proxy,
|
|
14008
|
+
isThreat: data.threat?.is_threat
|
|
14009
|
+
}),
|
|
14010
|
+
_status: "ok",
|
|
14011
|
+
_source: "ipapi"
|
|
14012
|
+
};
|
|
14013
|
+
strapi2.log.debug(`[magic-sessionmanager/geolocation] IP ${ipAddress}: ${result.city}, ${result.country} (Score: ${result.securityScore})`);
|
|
14014
|
+
cacheIpapiResult(lookupIp, result, IPAPI_SUCCESS_CACHE_TTL_MS);
|
|
14015
|
+
return result;
|
|
14016
|
+
} catch (error) {
|
|
14017
|
+
const status = error?.name === "AbortError" ? "error" : "error";
|
|
14018
|
+
strapi2.log.error(`[magic-sessionmanager/geolocation] Error fetching IP info for ${lookupIp}:`, error.message);
|
|
14019
|
+
const fallback = this.getFallbackData(lookupIp, status, "ipapi", error.message);
|
|
14020
|
+
cacheIpapiResult(lookupIp, fallback, IPAPI_FAILURE_CACHE_TTL_MS);
|
|
14021
|
+
return fallback;
|
|
14022
|
+
} finally {
|
|
14023
|
+
clearTimeout(timer);
|
|
14024
|
+
releaseSlot();
|
|
13773
14025
|
}
|
|
13774
|
-
|
|
13775
|
-
|
|
13776
|
-
|
|
13777
|
-
|
|
13778
|
-
country_flag: this.getCountryFlag(data.country_code),
|
|
13779
|
-
city: data.city,
|
|
13780
|
-
region: data.region,
|
|
13781
|
-
timezone: data.timezone,
|
|
13782
|
-
latitude: data.latitude,
|
|
13783
|
-
longitude: data.longitude,
|
|
13784
|
-
postal: data.postal,
|
|
13785
|
-
org: data.org,
|
|
13786
|
-
asn: data.asn,
|
|
13787
|
-
isVpn: data.threat?.is_vpn || false,
|
|
13788
|
-
isProxy: data.threat?.is_proxy || false,
|
|
13789
|
-
isThreat: data.threat?.is_threat || false,
|
|
13790
|
-
isAnonymous: data.threat?.is_anonymous || false,
|
|
13791
|
-
isTor: data.threat?.is_tor || false,
|
|
13792
|
-
securityScore: this.calculateSecurityScore({
|
|
13793
|
-
isVpn: data.threat?.is_vpn,
|
|
13794
|
-
isProxy: data.threat?.is_proxy,
|
|
13795
|
-
isThreat: data.threat?.is_threat,
|
|
13796
|
-
isAnonymous: data.threat?.is_anonymous,
|
|
13797
|
-
isTor: data.threat?.is_tor
|
|
13798
|
-
}),
|
|
13799
|
-
riskLevel: this.getRiskLevel({
|
|
13800
|
-
isVpn: data.threat?.is_vpn,
|
|
13801
|
-
isProxy: data.threat?.is_proxy,
|
|
13802
|
-
isThreat: data.threat?.is_threat
|
|
13803
|
-
}),
|
|
13804
|
-
_status: "ok",
|
|
13805
|
-
_source: "ipapi"
|
|
13806
|
-
};
|
|
13807
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] IP ${ipAddress}: ${result.city}, ${result.country} (Score: ${result.securityScore})`);
|
|
13808
|
-
return result;
|
|
13809
|
-
} catch (error) {
|
|
13810
|
-
const status = error?.name === "AbortError" ? "error" : "error";
|
|
13811
|
-
strapi2.log.error(`[magic-sessionmanager/geolocation] Error fetching IP info for ${ipAddress}:`, error.message);
|
|
13812
|
-
return this.getFallbackData(ipAddress, status, "ipapi", error.message);
|
|
14026
|
+
})();
|
|
14027
|
+
ipapiInFlight.set(lookupIp, lookupPromise);
|
|
14028
|
+
try {
|
|
14029
|
+
return cloneGeoData(await lookupPromise);
|
|
13813
14030
|
} finally {
|
|
13814
|
-
|
|
14031
|
+
ipapiInFlight.delete(lookupIp);
|
|
13815
14032
|
}
|
|
13816
14033
|
},
|
|
13817
14034
|
/**
|
|
@@ -13858,17 +14075,12 @@ var require_geolocation = __commonJS({
|
|
|
13858
14075
|
* @returns {boolean}
|
|
13859
14076
|
*/
|
|
13860
14077
|
isPrivateIp(ip) {
|
|
13861
|
-
|
|
13862
|
-
|
|
13863
|
-
if (
|
|
13864
|
-
|
|
13865
|
-
if (
|
|
13866
|
-
|
|
13867
|
-
if (second >= 16 && second <= 31) return true;
|
|
13868
|
-
}
|
|
13869
|
-
if (ip.startsWith("169.254.")) return true;
|
|
13870
|
-
if (ip.startsWith("fc00:") || ip.startsWith("fd00:")) return true;
|
|
13871
|
-
if (ip.startsWith("fe80:")) return true;
|
|
14078
|
+
const normalized = normalizeLookupIp(ip);
|
|
14079
|
+
const lower = normalized.toLowerCase();
|
|
14080
|
+
if (!normalized || lower === "unknown" || lower === "localhost") return true;
|
|
14081
|
+
const version = net.isIP(normalized);
|
|
14082
|
+
if (version === 4) return isPrivateIpv4(normalized);
|
|
14083
|
+
if (version === 6) return isPrivateIpv6(normalized);
|
|
13872
14084
|
return false;
|
|
13873
14085
|
},
|
|
13874
14086
|
/**
|
package/package.json
CHANGED