fca-nx 6.7.7 → 6.7.9
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/README.md +0 -6
- package/module/login.js +1 -1
- package/module/loginHelper.js +156 -66
- package/package.json +2 -2
- package/src/api/socket/core/connectMqtt.js +14 -17
- package/src/api/socket/core/getSeqID.js +3 -40
- package/src/api/socket/e2ee/index.js +1 -0
- package/src/api/socket/listenMqtt.js +65 -25
- package/src/api/socket/sendMessage.js +172 -78
- package/src/api/users/getUserInfoV2.js +9 -3
- package/src/utils/clientIdentity.js +2 -2
- package/src/utils/instanceLock.js +41 -0
- package/src/utils/ipGuard.js +72 -0
- package/src/utils/loginParser/autoLogin.js +0 -38
- package/src/utils/request/methods.js +16 -0
- package/src/utils/request/retry.js +11 -0
- package/src/utils/sendThrottle.js +64 -0
- package/src/utils/sessionStatus.js +0 -72
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Single-instance lock per account. Two processes logged into the same account at the same time
|
|
4
|
+
* (overlapping Railway deploys, a forgotten second start) produce Facebook's
|
|
5
|
+
* "You can't create multiple sessions" restriction. The lock file lives in FCA_NX_STATE_DIR and is
|
|
6
|
+
* refreshed every 30s; a lock not refreshed for 120s (crashed process) is considered stale.
|
|
7
|
+
* FCA_NX_INSTANCE_LOCK=off disables it.
|
|
8
|
+
*/
|
|
9
|
+
const fs = require("fs");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
const STALE_MS = 120000;
|
|
12
|
+
let timer = null;
|
|
13
|
+
|
|
14
|
+
function acquire(userID) {
|
|
15
|
+
if (/^(off|0|false)$/i.test(String(process.env.FCA_NX_INSTANCE_LOCK || ""))) return { ok: true };
|
|
16
|
+
const dir = process.env.FCA_NX_STATE_DIR || path.join(process.cwd(), ".fca-nx");
|
|
17
|
+
const file = path.join(dir, "instance_" + String(userID || "default") + ".lock");
|
|
18
|
+
try {
|
|
19
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
20
|
+
try {
|
|
21
|
+
const cur = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
22
|
+
if (cur && cur.pid !== process.pid && Date.now() - cur.at < STALE_MS) {
|
|
23
|
+
let alive = true;
|
|
24
|
+
try { process.kill(cur.pid, 0); } catch (e) { alive = e.code === "EPERM"; }
|
|
25
|
+
// pid namespaces differ between containers, so trust a fresh heartbeat over kill(0)
|
|
26
|
+
if (alive || Date.now() - cur.at < 45000) return { ok: false, pid: cur.pid };
|
|
27
|
+
}
|
|
28
|
+
} catch (_) {}
|
|
29
|
+
const beat = () => { try { fs.writeFileSync(file, JSON.stringify({ pid: process.pid, at: Date.now() })); } catch (_) {} };
|
|
30
|
+
beat();
|
|
31
|
+
if (timer) clearInterval(timer);
|
|
32
|
+
timer = setInterval(beat, 30000);
|
|
33
|
+
if (timer.unref) timer.unref();
|
|
34
|
+
const release = () => { try { const c = JSON.parse(fs.readFileSync(file, "utf8")); if (c.pid === process.pid) fs.unlinkSync(file); } catch (_) {} };
|
|
35
|
+
process.once("exit", release);
|
|
36
|
+
for (const sig of ["SIGINT", "SIGTERM"]) process.once(sig, () => { release(); process.exit(0); });
|
|
37
|
+
} catch (_) { /* never block login because of the lock itself */ }
|
|
38
|
+
return { ok: true };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = { acquire };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Login-location consistency guard.
|
|
5
|
+
*
|
|
6
|
+
* Facebook flags accounts that are logged in from many different IPs/locations. Before a
|
|
7
|
+
* cookie login we look up the public IP this process uses (through the same proxy/agent the
|
|
8
|
+
* bot uses for Facebook), compare it with the IP of the previous login (stored on disk) and:
|
|
9
|
+
* - warn loudly when it changed (default), or
|
|
10
|
+
* - refuse to log in when FCA_NX_IP_GUARD=block (use with a static proxy / fixed-IP host).
|
|
11
|
+
*
|
|
12
|
+
* FCA_NX_IP_GUARD=off disables it. FCA_NX_STATE_DIR (default ./.fca-nx) holds the state file;
|
|
13
|
+
* on Railway point it at a Volume so it survives deploys.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require("fs");
|
|
17
|
+
const path = require("path");
|
|
18
|
+
const logger = require("../../func/logger");
|
|
19
|
+
|
|
20
|
+
const ENDPOINTS = [
|
|
21
|
+
"https://api.ipify.org?format=json",
|
|
22
|
+
"https://ipinfo.io/json"
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
function stateFile() {
|
|
26
|
+
const dir = process.env.FCA_NX_STATE_DIR || path.join(process.cwd(), ".fca-nx");
|
|
27
|
+
return { dir, file: path.join(dir, "last_login_ip.json") };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function lookupIP(client) {
|
|
31
|
+
for (const url of ENDPOINTS) {
|
|
32
|
+
try {
|
|
33
|
+
const res = await client.get(url, { timeout: 5000, headers: { Accept: "application/json" } });
|
|
34
|
+
const ip = res && res.data && (res.data.ip || res.data.query);
|
|
35
|
+
if (typeof ip === "string" && ip) return { ip, country: res.data.country || null, region: res.data.region || null };
|
|
36
|
+
} catch (_) { /* try next */ }
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @returns {Promise<{ok:boolean, changed:boolean, ip:string|null, previous:string|null}>} */
|
|
42
|
+
async function checkLoginIP(client, userID) {
|
|
43
|
+
const mode = String(process.env.FCA_NX_IP_GUARD || "warn").toLowerCase();
|
|
44
|
+
if (mode === "off" || mode === "0" || mode === "false") return { ok: true, changed: false, ip: null, previous: null };
|
|
45
|
+
const now = await lookupIP(client);
|
|
46
|
+
if (!now) {
|
|
47
|
+
logger("ipGuard: could not determine public IP (continuing)", "warn");
|
|
48
|
+
return { ok: true, changed: false, ip: null, previous: null };
|
|
49
|
+
}
|
|
50
|
+
const { dir, file } = stateFile();
|
|
51
|
+
let all = {};
|
|
52
|
+
try { all = JSON.parse(fs.readFileSync(file, "utf8")) || {}; } catch (_) {}
|
|
53
|
+
const key = String(userID || "default");
|
|
54
|
+
const prev = all[key] && all[key].ip ? all[key] : null;
|
|
55
|
+
const changed = !!prev && prev.ip !== now.ip;
|
|
56
|
+
if (changed) {
|
|
57
|
+
const msg = `ipGuard: login IP changed ${prev.ip} -> ${now.ip}. Logging into the same account from different IPs/locations is a top cause of "automated behaviour", checkpoints and suspensions. Use ONE host with a fixed IP (or a static proxy via the 'proxy' option).`;
|
|
58
|
+
if (mode === "block") {
|
|
59
|
+
logger(msg + " Login refused (FCA_NX_IP_GUARD=block). Set FCA_NX_IP_GUARD=warn to override.", "error");
|
|
60
|
+
return { ok: false, changed: true, ip: now.ip, previous: prev.ip };
|
|
61
|
+
}
|
|
62
|
+
logger(msg, "warn");
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
66
|
+
all[key] = { ip: now.ip, country: now.country, region: now.region, at: new Date().toISOString() };
|
|
67
|
+
fs.writeFileSync(file, JSON.stringify(all, null, 2));
|
|
68
|
+
} catch (_) {}
|
|
69
|
+
return { ok: true, changed, ip: now.ip, previous: prev ? prev.ip : null };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { checkLoginIP, lookupIP };
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const logger = require("../../../func/logger");
|
|
4
|
-
const {
|
|
5
|
-
isSessionEnforcement,
|
|
6
|
-
createSessionEnforcementError,
|
|
7
|
-
isSessionEnforcementError
|
|
8
|
-
} = require("../sessionStatus");
|
|
9
4
|
|
|
10
5
|
/**
|
|
11
6
|
* Tạo hàm maybeAutoLogin: khi session hết hạn thì thử đăng nhập lại và retry request.
|
|
@@ -19,23 +14,6 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
19
14
|
const { buildUrl, headerOf, formatCookie } = helpers;
|
|
20
15
|
|
|
21
16
|
return async function maybeAutoLogin(resData, resConfig) {
|
|
22
|
-
if (ctx._sessionEnforcement) {
|
|
23
|
-
throw createSessionEnforcementError();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const responseUrl = resConfig ? buildUrl(resConfig) : "";
|
|
27
|
-
if (isSessionEnforcement(resData) || isSessionEnforcement(responseUrl)) {
|
|
28
|
-
ctx._sessionEnforcement = true;
|
|
29
|
-
const error = createSessionEnforcementError();
|
|
30
|
-
if (typeof emit === "function") {
|
|
31
|
-
emit("verificationRequired", {
|
|
32
|
-
code: error.code,
|
|
33
|
-
message: error.message
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
throw error;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
17
|
const retryAfterLogin = async function () {
|
|
40
18
|
if (!resConfig) {
|
|
41
19
|
const e = new Error(
|
|
@@ -64,10 +42,6 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
64
42
|
}
|
|
65
43
|
return await parseAndCheckLogin(ctx, http)(newData);
|
|
66
44
|
} catch (retryErr) {
|
|
67
|
-
if (isSessionEnforcementError(retryErr)) {
|
|
68
|
-
ctx._sessionEnforcement = true;
|
|
69
|
-
throw retryErr;
|
|
70
|
-
}
|
|
71
45
|
if (
|
|
72
46
|
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
73
47
|
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
@@ -105,10 +79,6 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
105
79
|
try {
|
|
106
80
|
ok = await ctx._autoLoginPromise;
|
|
107
81
|
} catch (autoLoginErr) {
|
|
108
|
-
if (isSessionEnforcementError(autoLoginErr)) {
|
|
109
|
-
ctx._sessionEnforcement = true;
|
|
110
|
-
throw autoLoginErr;
|
|
111
|
-
}
|
|
112
82
|
const e = new Error("Not logged in. Auto login failed.");
|
|
113
83
|
e.error = "Not logged in.";
|
|
114
84
|
e.res = resData;
|
|
@@ -152,14 +122,6 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
152
122
|
throw e;
|
|
153
123
|
}
|
|
154
124
|
} catch (autoLoginErr) {
|
|
155
|
-
if (isSessionEnforcementError(autoLoginErr)) {
|
|
156
|
-
ctx._sessionEnforcement = true;
|
|
157
|
-
emit("verificationRequired", {
|
|
158
|
-
code: autoLoginErr.code,
|
|
159
|
-
message: autoLoginErr.message
|
|
160
|
-
});
|
|
161
|
-
throw autoLoginErr;
|
|
162
|
-
}
|
|
163
125
|
if (autoLoginErr.error === "Not logged in.") {
|
|
164
126
|
throw autoLoginErr;
|
|
165
127
|
}
|
|
@@ -14,6 +14,22 @@ const {
|
|
|
14
14
|
isPairArrayList,
|
|
15
15
|
} = require("./helpers");
|
|
16
16
|
|
|
17
|
+
|
|
18
|
+
// [Hardened5] mild global spacing between FB Graph/API posts to avoid bursty bot patterns.
|
|
19
|
+
// Does NOT block forever - only adds a small delay when calls are very close together.
|
|
20
|
+
let __lastFbPostAt = 0;
|
|
21
|
+
const __MIN_FB_POST_GAP_MS = 180; // ~5-6 req/s max sustained, still fast enough for bots
|
|
22
|
+
async function __paceFbPost(url) {
|
|
23
|
+
try {
|
|
24
|
+
const u = String(url || "");
|
|
25
|
+
if (!/facebook\.com|messenger\.com/i.test(u)) return;
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const wait = __MIN_FB_POST_GAP_MS - (now - __lastFbPostAt);
|
|
28
|
+
if (wait > 0) await new Promise(r => setTimeout(r, wait + Math.floor(Math.random() * 80)));
|
|
29
|
+
__lastFbPostAt = Date.now();
|
|
30
|
+
} catch (_) {}
|
|
31
|
+
}
|
|
32
|
+
|
|
17
33
|
function cleanGet(url, ctx) {
|
|
18
34
|
return requestWithRetry(() => client.get(url, cfg()), 3, 1000, ctx);
|
|
19
35
|
}
|
|
@@ -2,8 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
const { delay } = require("./client");
|
|
4
4
|
|
|
5
|
+
// [Hardened5] light spacing for FB hosts to reduce burst fingerprints
|
|
6
|
+
let __lastFbAt = 0;
|
|
7
|
+
async function __pace() {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
const gap = 150 + Math.floor(Math.random() * 100);
|
|
10
|
+
const wait = gap - (now - __lastFbAt);
|
|
11
|
+
if (wait > 0) await delay(wait);
|
|
12
|
+
__lastFbAt = Date.now();
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx) {
|
|
6
16
|
let lastError;
|
|
17
|
+
try { await __pace(); } catch (_) {}
|
|
7
18
|
const emit = (event, payload) => {
|
|
8
19
|
try {
|
|
9
20
|
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Outgoing-message pacing.
|
|
5
|
+
*
|
|
6
|
+
* Every outgoing message (MQTT, OldMessage and E2EE) is serialised through one queue and is
|
|
7
|
+
* released no sooner than `delay` ms after the previous one. This avoids sending bursts that
|
|
8
|
+
* look like a script and that Facebook's rate limiting reacts to.
|
|
9
|
+
*
|
|
10
|
+
* FCA_NX_SEND_DELAY_MS base gap between sends, default 1000 (set 0 to disable)
|
|
11
|
+
* FCA_NX_SEND_JITTER_MS extra random 0..N ms added to each gap, default 400
|
|
12
|
+
* or at runtime: api.setSendDelay(ms, jitterMs)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const state = { last: 0, chain: Promise.resolve(), delay: null, jitter: null, stamps: [] };
|
|
16
|
+
// Hard ceiling per rolling minute (FCA_NX_SEND_MAX_PER_MIN, default 20, 0 = off).
|
|
17
|
+
function maxPerMin() {
|
|
18
|
+
const n = Number(process.env.FCA_NX_SEND_MAX_PER_MIN);
|
|
19
|
+
return Number.isFinite(n) && n >= 0 ? n : 20;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readNum(v, d) {
|
|
23
|
+
const n = Number(v);
|
|
24
|
+
return Number.isFinite(n) && n >= 0 ? n : d;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getDelay() {
|
|
28
|
+
return state.delay != null ? state.delay : readNum(process.env.FCA_NX_SEND_DELAY_MS, 1000);
|
|
29
|
+
}
|
|
30
|
+
function getJitter() {
|
|
31
|
+
return state.jitter != null ? state.jitter : readNum(process.env.FCA_NX_SEND_JITTER_MS, 400);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function setSendDelay(ms, jitterMs) {
|
|
35
|
+
if (ms != null) state.delay = readNum(ms, 1000);
|
|
36
|
+
if (jitterMs != null) state.jitter = readNum(jitterMs, 400);
|
|
37
|
+
return { delay: getDelay(), jitter: getJitter() };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Resolves when it is this caller's turn to send. */
|
|
41
|
+
function waitTurn() {
|
|
42
|
+
const delay = getDelay();
|
|
43
|
+
if (delay <= 0) return Promise.resolve();
|
|
44
|
+
const turn = state.chain.then(async () => {
|
|
45
|
+
const gap = delay + Math.floor(Math.random() * (getJitter() + 1));
|
|
46
|
+
const wait = state.last + gap - Date.now();
|
|
47
|
+
if (wait > 0) await new Promise(r => setTimeout(r, wait));
|
|
48
|
+
const cap = maxPerMin();
|
|
49
|
+
if (cap > 0) {
|
|
50
|
+
for (;;) {
|
|
51
|
+
const now = Date.now();
|
|
52
|
+
state.stamps = state.stamps.filter(t => now - t < 60000);
|
|
53
|
+
if (state.stamps.length < cap) break;
|
|
54
|
+
await new Promise(r => setTimeout(r, state.stamps[0] + 60000 - now + 50));
|
|
55
|
+
}
|
|
56
|
+
state.stamps.push(Date.now());
|
|
57
|
+
}
|
|
58
|
+
state.last = Date.now();
|
|
59
|
+
});
|
|
60
|
+
state.chain = turn.catch(() => {});
|
|
61
|
+
return turn;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { waitTurn, setSendDelay };
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const RESPONSE_FIELDS = [
|
|
4
|
-
"url",
|
|
5
|
-
"responseUrl",
|
|
6
|
-
"location",
|
|
7
|
-
"redirect",
|
|
8
|
-
"config",
|
|
9
|
-
"message",
|
|
10
|
-
"error",
|
|
11
|
-
"errorSummary",
|
|
12
|
-
"errorDescription",
|
|
13
|
-
"reason",
|
|
14
|
-
"data",
|
|
15
|
-
"body",
|
|
16
|
-
"text",
|
|
17
|
-
"response",
|
|
18
|
-
"originalResponse",
|
|
19
|
-
"request",
|
|
20
|
-
"res"
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
const ENFORCEMENT_MARKER =
|
|
24
|
-
/(?:\/checkpoint(?:\/|[?#])|XCheckpointFBScrapingWarningController|checkpoint_(?:282|956)\b|blocked the login|manual account verification|(?:account|profile)\b[\s\S]{0,50}\b(?:suspended|disabled)\b|\b(?:suspended|disabled)\s+(?:your|this|the)\s+(?:account|profile)\b|robot verification|security check required|confirm (?:that )?you(?:'re| are) (?:a human|not a robot))/i;
|
|
25
|
-
|
|
26
|
-
function collectResponseText(value, seen = new Set(), depth = 0) {
|
|
27
|
-
if (value === null || value === undefined || depth > 4) return "";
|
|
28
|
-
if (typeof value === "string") return value;
|
|
29
|
-
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
30
|
-
if (Buffer.isBuffer(value)) return value.toString("utf8");
|
|
31
|
-
if (typeof value !== "object" || seen.has(value)) return "";
|
|
32
|
-
|
|
33
|
-
seen.add(value);
|
|
34
|
-
if (Array.isArray(value)) {
|
|
35
|
-
return value
|
|
36
|
-
.slice(0, 20)
|
|
37
|
-
.map(item => collectResponseText(item, seen, depth + 1))
|
|
38
|
-
.join("\n");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return RESPONSE_FIELDS
|
|
42
|
-
.filter(key => Object.prototype.hasOwnProperty.call(value, key))
|
|
43
|
-
.map(key => collectResponseText(value[key], seen, depth + 1))
|
|
44
|
-
.join("\n");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isSessionEnforcement(value) {
|
|
48
|
-
return ENFORCEMENT_MARKER.test(collectResponseText(value));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function createSessionEnforcementError() {
|
|
52
|
-
const error = new Error(
|
|
53
|
-
"Facebook requires manual account verification; automatic login and recovery stopped."
|
|
54
|
-
);
|
|
55
|
-
error.code = "SESSION_VERIFICATION_REQUIRED";
|
|
56
|
-
error.isSessionEnforcement = true;
|
|
57
|
-
return error;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function isSessionEnforcementError(error) {
|
|
61
|
-
return Boolean(
|
|
62
|
-
error &&
|
|
63
|
-
(error.code === "SESSION_VERIFICATION_REQUIRED" ||
|
|
64
|
-
error.isSessionEnforcement === true)
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
module.exports = {
|
|
69
|
-
isSessionEnforcement,
|
|
70
|
-
createSessionEnforcementError,
|
|
71
|
-
isSessionEnforcementError
|
|
72
|
-
};
|