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.js
CHANGED
|
@@ -749,10 +749,17 @@ var require_last_seen = __commonJS({
|
|
|
749
749
|
"/api/register",
|
|
750
750
|
"/admin/"
|
|
751
751
|
];
|
|
752
|
+
var SELF_TERMINATING_PATHS = /* @__PURE__ */ new Set([
|
|
753
|
+
"/api/magic-sessionmanager/logout",
|
|
754
|
+
"/api/magic-sessionmanager/logout-all"
|
|
755
|
+
]);
|
|
752
756
|
function isAuthEndpoint(path) {
|
|
753
757
|
if (!path) return false;
|
|
754
758
|
return AUTH_PATH_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
755
759
|
}
|
|
760
|
+
function isSelfTerminatingEndpoint(path) {
|
|
761
|
+
return SELF_TERMINATING_PATHS.has(path);
|
|
762
|
+
}
|
|
756
763
|
module2.exports = ({ strapi: strapi2, sessionService }) => {
|
|
757
764
|
return async (ctx, next) => {
|
|
758
765
|
if (isAuthEndpoint(ctx.path)) {
|
|
@@ -796,6 +803,9 @@ var require_last_seen = __commonJS({
|
|
|
796
803
|
ctx.state.userDocumentId = userDocId;
|
|
797
804
|
ctx.state.__magicSessionId = thisSession.documentId;
|
|
798
805
|
await next();
|
|
806
|
+
if (isSelfTerminatingEndpoint(ctx.path)) {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
799
809
|
try {
|
|
800
810
|
await sessionService.touch({
|
|
801
811
|
userId: userDocId,
|
|
@@ -9800,6 +9810,18 @@ var require_bootstrap = __commonJS({
|
|
|
9800
9810
|
if (path.startsWith("/api/magic-link/login") && (method === "GET" || method === "POST")) return true;
|
|
9801
9811
|
return false;
|
|
9802
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
|
+
}
|
|
9803
9825
|
module2.exports = async ({ strapi: strapi2 }) => {
|
|
9804
9826
|
const log = createLogger(strapi2);
|
|
9805
9827
|
log.info("[START] Bootstrap starting...");
|
|
@@ -9897,12 +9919,7 @@ var require_bootstrap = __commonJS({
|
|
|
9897
9919
|
if (!isLoginPath(ctx.path, ctx.method)) {
|
|
9898
9920
|
return next();
|
|
9899
9921
|
}
|
|
9900
|
-
|
|
9901
|
-
try {
|
|
9902
|
-
settings = await getPluginSettings(strapi2);
|
|
9903
|
-
} catch {
|
|
9904
|
-
settings = {};
|
|
9905
|
-
}
|
|
9922
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
9906
9923
|
const needsGeoCheck = settings.blockSuspiciousSessions || settings.enableGeofencing || settings.enableGeolocation;
|
|
9907
9924
|
if (!needsGeoCheck) {
|
|
9908
9925
|
return next();
|
|
@@ -10040,13 +10057,8 @@ var require_bootstrap = __commonJS({
|
|
|
10040
10057
|
const clearFailures = (ip) => failed.delete(ip);
|
|
10041
10058
|
strapi2.server.use(async (ctx, next) => {
|
|
10042
10059
|
if (!isLoginPath(ctx.path, ctx.method)) return next();
|
|
10043
|
-
|
|
10044
|
-
|
|
10045
|
-
const settings = await getPluginSettings(strapi2);
|
|
10046
|
-
maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10047
|
-
} catch {
|
|
10048
|
-
maxFailed = 0;
|
|
10049
|
-
}
|
|
10060
|
+
const settings = await getRequestSettings(strapi2, ctx);
|
|
10061
|
+
const maxFailed = Number(settings.maxFailedLogins) || 0;
|
|
10050
10062
|
if (maxFailed <= 0) return next();
|
|
10051
10063
|
const ip = getClientIp(ctx);
|
|
10052
10064
|
if (!ip || ip === "unknown") return next();
|
|
@@ -10105,6 +10117,7 @@ var require_bootstrap = __commonJS({
|
|
|
10105
10117
|
if (!ctx.body || !ctx.body.jwt || !ctx.body.user) return;
|
|
10106
10118
|
try {
|
|
10107
10119
|
const user = ctx.body.user;
|
|
10120
|
+
await getRequestSettings(strapi2, ctx);
|
|
10108
10121
|
const ip = getClientIp(ctx);
|
|
10109
10122
|
const headers = ctx.request.headers || ctx.request.header || {};
|
|
10110
10123
|
const userAgent = headers["user-agent"] || "unknown";
|
|
@@ -13250,7 +13263,7 @@ var require_session2 = __commonJS({
|
|
|
13250
13263
|
var require_package = __commonJS({
|
|
13251
13264
|
"package.json"(exports2, module2) {
|
|
13252
13265
|
module2.exports = {
|
|
13253
|
-
version: "4.6.
|
|
13266
|
+
version: "4.6.4",
|
|
13254
13267
|
keywords: [
|
|
13255
13268
|
"strapi",
|
|
13256
13269
|
"plugin",
|
|
@@ -13589,12 +13602,25 @@ var require_geolocation = __commonJS({
|
|
|
13589
13602
|
"server/src/services/geolocation.js"(exports2, module2) {
|
|
13590
13603
|
"use strict";
|
|
13591
13604
|
var fs = require("node:fs");
|
|
13605
|
+
var net = require("node:net");
|
|
13592
13606
|
var path = require("node:path");
|
|
13593
13607
|
var { getPluginSettings } = require_settings_loader();
|
|
13594
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;
|
|
13595
13615
|
var DEFAULT_MMDB_PATH = path.resolve(process.cwd(), "data", "GeoLite2-Country.mmdb");
|
|
13596
13616
|
var VALID_PROVIDERS = /* @__PURE__ */ new Set(["auto", "local-mmdb", "ipapi", "disabled"]);
|
|
13597
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;
|
|
13598
13624
|
function normalizeProvider(value) {
|
|
13599
13625
|
return VALID_PROVIDERS.has(value) ? value : "auto";
|
|
13600
13626
|
}
|
|
@@ -13618,6 +13644,119 @@ var require_geolocation = __commonJS({
|
|
|
13618
13644
|
throw err;
|
|
13619
13645
|
}
|
|
13620
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
|
+
}
|
|
13621
13760
|
module2.exports = ({ strapi: strapi2 }) => ({
|
|
13622
13761
|
/**
|
|
13623
13762
|
* Looks up geolocation data for an IP address.
|
|
@@ -13637,9 +13776,10 @@ var require_geolocation = __commonJS({
|
|
|
13637
13776
|
* @returns {Promise<object>}
|
|
13638
13777
|
*/
|
|
13639
13778
|
async getIpInfo(ipAddress) {
|
|
13640
|
-
|
|
13779
|
+
const lookupIp = normalizeLookupIp(ipAddress);
|
|
13780
|
+
if (!lookupIp || this.isPrivateIp(lookupIp)) {
|
|
13641
13781
|
return {
|
|
13642
|
-
ip: ipAddress,
|
|
13782
|
+
ip: lookupIp || ipAddress,
|
|
13643
13783
|
country: "Local Network",
|
|
13644
13784
|
country_code: "XX",
|
|
13645
13785
|
country_flag: "",
|
|
@@ -13657,25 +13797,28 @@ var require_geolocation = __commonJS({
|
|
|
13657
13797
|
_source: "local"
|
|
13658
13798
|
};
|
|
13659
13799
|
}
|
|
13800
|
+
if (net.isIP(lookupIp) === 0) {
|
|
13801
|
+
return this.getFallbackData(ipAddress, "error", "validation", "Invalid IP address");
|
|
13802
|
+
}
|
|
13660
13803
|
const settings = await this.getGeoSettings();
|
|
13661
13804
|
const provider = normalizeProvider(settings.geoIpProvider);
|
|
13662
13805
|
if (provider === "disabled") {
|
|
13663
|
-
return this.getFallbackData(
|
|
13806
|
+
return this.getFallbackData(lookupIp, "disabled", "disabled");
|
|
13664
13807
|
}
|
|
13665
13808
|
if (provider === "local-mmdb") {
|
|
13666
|
-
return this.getLocalMmdbIpInfo(
|
|
13809
|
+
return this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13667
13810
|
}
|
|
13668
13811
|
if (provider === "auto") {
|
|
13669
13812
|
const databasePath = resolveDatabasePath(settings);
|
|
13670
13813
|
if (databasePath && fs.existsSync(databasePath)) {
|
|
13671
|
-
const localResult = await this.getLocalMmdbIpInfo(
|
|
13672
|
-
if (localResult?._status === "ok") {
|
|
13814
|
+
const localResult = await this.getLocalMmdbIpInfo(lookupIp, settings);
|
|
13815
|
+
if (localResult?._status === "ok" || settings.geoLookupFailureMode === "block") {
|
|
13673
13816
|
return localResult;
|
|
13674
13817
|
}
|
|
13675
13818
|
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed (${localResult?._reason || localResult?._status}), falling back to ipapi.co`);
|
|
13676
13819
|
}
|
|
13677
13820
|
}
|
|
13678
|
-
return this.getIpapiIpInfo(
|
|
13821
|
+
return this.getIpapiIpInfo(lookupIp);
|
|
13679
13822
|
},
|
|
13680
13823
|
/**
|
|
13681
13824
|
* Reads effective GEOIP provider settings.
|
|
@@ -13695,6 +13838,10 @@ var require_geolocation = __commonJS({
|
|
|
13695
13838
|
* @returns {Promise<object>}
|
|
13696
13839
|
*/
|
|
13697
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
|
+
}
|
|
13698
13845
|
const databasePath = resolveDatabasePath(settings);
|
|
13699
13846
|
if (!databasePath || !fs.existsSync(databasePath)) {
|
|
13700
13847
|
return this.getFallbackData(
|
|
@@ -13706,12 +13853,12 @@ var require_geolocation = __commonJS({
|
|
|
13706
13853
|
}
|
|
13707
13854
|
try {
|
|
13708
13855
|
const reader = await openLocalReader(databasePath);
|
|
13709
|
-
const data = reader.country(
|
|
13856
|
+
const data = reader.country(lookupIp);
|
|
13710
13857
|
const country = data.country || data.registeredCountry || data.representedCountry || {};
|
|
13711
13858
|
const countryCode = country.isoCode || data.registeredCountry?.isoCode || "XX";
|
|
13712
13859
|
const countryNames = country.names || {};
|
|
13713
13860
|
const result = {
|
|
13714
|
-
ip:
|
|
13861
|
+
ip: lookupIp,
|
|
13715
13862
|
country: countryNames.en || countryNames.de || countryCode || "Unknown",
|
|
13716
13863
|
country_code: countryCode,
|
|
13717
13864
|
country_flag: this.getCountryFlag(countryCode),
|
|
@@ -13734,11 +13881,11 @@ var require_geolocation = __commonJS({
|
|
|
13734
13881
|
_status: "ok",
|
|
13735
13882
|
_source: "local-mmdb"
|
|
13736
13883
|
};
|
|
13737
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${
|
|
13884
|
+
strapi2.log.debug(`[magic-sessionmanager/geolocation] Local MMDB IP ${lookupIp}: ${result.country_code}`);
|
|
13738
13885
|
return result;
|
|
13739
13886
|
} catch (error) {
|
|
13740
|
-
strapi2.log.warn(`[magic-sessionmanager/geolocation] Local MMDB lookup failed for ${
|
|
13741
|
-
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);
|
|
13742
13889
|
}
|
|
13743
13890
|
},
|
|
13744
13891
|
/**
|
|
@@ -13747,65 +13894,135 @@ var require_geolocation = __commonJS({
|
|
|
13747
13894
|
* @returns {Promise<object>}
|
|
13748
13895
|
*/
|
|
13749
13896
|
async getIpapiIpInfo(ipAddress) {
|
|
13750
|
-
const
|
|
13751
|
-
|
|
13752
|
-
|
|
13753
|
-
|
|
13754
|
-
|
|
13755
|
-
|
|
13756
|
-
|
|
13757
|
-
|
|
13758
|
-
|
|
13759
|
-
strapi2
|
|
13760
|
-
|
|
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");
|
|
13761
13934
|
}
|
|
13762
|
-
const
|
|
13763
|
-
|
|
13764
|
-
|
|
13765
|
-
const
|
|
13766
|
-
|
|
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();
|
|
13767
14019
|
}
|
|
13768
|
-
|
|
13769
|
-
|
|
13770
|
-
|
|
13771
|
-
|
|
13772
|
-
country_flag: this.getCountryFlag(data.country_code),
|
|
13773
|
-
city: data.city,
|
|
13774
|
-
region: data.region,
|
|
13775
|
-
timezone: data.timezone,
|
|
13776
|
-
latitude: data.latitude,
|
|
13777
|
-
longitude: data.longitude,
|
|
13778
|
-
postal: data.postal,
|
|
13779
|
-
org: data.org,
|
|
13780
|
-
asn: data.asn,
|
|
13781
|
-
isVpn: data.threat?.is_vpn || false,
|
|
13782
|
-
isProxy: data.threat?.is_proxy || false,
|
|
13783
|
-
isThreat: data.threat?.is_threat || false,
|
|
13784
|
-
isAnonymous: data.threat?.is_anonymous || false,
|
|
13785
|
-
isTor: data.threat?.is_tor || false,
|
|
13786
|
-
securityScore: this.calculateSecurityScore({
|
|
13787
|
-
isVpn: data.threat?.is_vpn,
|
|
13788
|
-
isProxy: data.threat?.is_proxy,
|
|
13789
|
-
isThreat: data.threat?.is_threat,
|
|
13790
|
-
isAnonymous: data.threat?.is_anonymous,
|
|
13791
|
-
isTor: data.threat?.is_tor
|
|
13792
|
-
}),
|
|
13793
|
-
riskLevel: this.getRiskLevel({
|
|
13794
|
-
isVpn: data.threat?.is_vpn,
|
|
13795
|
-
isProxy: data.threat?.is_proxy,
|
|
13796
|
-
isThreat: data.threat?.is_threat
|
|
13797
|
-
}),
|
|
13798
|
-
_status: "ok",
|
|
13799
|
-
_source: "ipapi"
|
|
13800
|
-
};
|
|
13801
|
-
strapi2.log.debug(`[magic-sessionmanager/geolocation] IP ${ipAddress}: ${result.city}, ${result.country} (Score: ${result.securityScore})`);
|
|
13802
|
-
return result;
|
|
13803
|
-
} catch (error) {
|
|
13804
|
-
const status = error?.name === "AbortError" ? "error" : "error";
|
|
13805
|
-
strapi2.log.error(`[magic-sessionmanager/geolocation] Error fetching IP info for ${ipAddress}:`, error.message);
|
|
13806
|
-
return this.getFallbackData(ipAddress, status, "ipapi", error.message);
|
|
14020
|
+
})();
|
|
14021
|
+
ipapiInFlight.set(lookupIp, lookupPromise);
|
|
14022
|
+
try {
|
|
14023
|
+
return cloneGeoData(await lookupPromise);
|
|
13807
14024
|
} finally {
|
|
13808
|
-
|
|
14025
|
+
ipapiInFlight.delete(lookupIp);
|
|
13809
14026
|
}
|
|
13810
14027
|
},
|
|
13811
14028
|
/**
|
|
@@ -13852,17 +14069,12 @@ var require_geolocation = __commonJS({
|
|
|
13852
14069
|
* @returns {boolean}
|
|
13853
14070
|
*/
|
|
13854
14071
|
isPrivateIp(ip) {
|
|
13855
|
-
|
|
13856
|
-
|
|
13857
|
-
if (
|
|
13858
|
-
|
|
13859
|
-
if (
|
|
13860
|
-
|
|
13861
|
-
if (second >= 16 && second <= 31) return true;
|
|
13862
|
-
}
|
|
13863
|
-
if (ip.startsWith("169.254.")) return true;
|
|
13864
|
-
if (ip.startsWith("fc00:") || ip.startsWith("fd00:")) return true;
|
|
13865
|
-
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);
|
|
13866
14078
|
return false;
|
|
13867
14079
|
},
|
|
13868
14080
|
/**
|