nexus-fca 3.1.3 → 3.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/refreshFb_dtsg.js +115 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexus-fca",
3
- "version": "3.1.3",
3
+ "version": "3.1.4",
4
4
  "description": "Nexus-FCA 3.1 – THE BEST, SAFEST, MOST STABLE Facebook Messenger API! Email/password + appState login, proxy support (HTTP/HTTPS/SOCKS5), random user agent, proactive cookie refresh, MQTT stability, session protection, and TypeScript support.",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -27,33 +27,44 @@ module.exports = function (defaultFuncs, api, ctx) {
27
27
  callback = (err, data) => (err ? rejectFunc(err) : resolveFunc(data));
28
28
  }
29
29
  if (Object.keys(obj).length === 0) {
30
- utils
31
- .get("https://www.facebook.com/", ctx.jar, null, ctx.globalOptions, {
32
- noRef: true,
33
- })
34
- .then((resData) => {
35
- const fb_dtsg = utils.getFrom(
36
- resData.body,
37
- '["DTSGInitData",[],{"token":"',
38
- '","'
39
- );
40
- const jazoest = utils.getFrom(resData.body, "jazoest=", '",');
41
- if (!fb_dtsg) {
42
- throw new utils.CustomError(
43
- "Could not find fb_dtsg in HTML after requesting Facebook."
44
- );
30
+ const sources = [
31
+ { url: "https://www.facebook.com/", label: "facebook.com" },
32
+ { url: "https://business.facebook.com/", label: "business.facebook.com" },
33
+ { url: "https://mbasic.facebook.com/", label: "mbasic.facebook.com" }
34
+ ];
35
+
36
+ const tryRefresh = async () => {
37
+ let lastErr = null;
38
+ for (const source of sources) {
39
+ try {
40
+ const resData = await utils.get(source.url, ctx.jar, null, ctx.globalOptions, { noRef: true });
41
+ const tokens = extractTokensFromHtml(resData.body);
42
+ if (tokens.fb_dtsg) {
43
+ ctx.fb_dtsg = tokens.fb_dtsg;
44
+ if (tokens.jazoest) ctx.jazoest = tokens.jazoest;
45
+ return {
46
+ data: { fb_dtsg: ctx.fb_dtsg, jazoest: ctx.jazoest },
47
+ message: `Refreshed fb_dtsg via ${source.label}`
48
+ };
49
+ }
50
+ lastErr = new utils.CustomError(`Could not find fb_dtsg in HTML from ${source.label}`);
51
+ } catch (err) {
52
+ lastErr = err;
53
+ log.verbose("refreshFb_dtsg", `Fetch from ${source.url} failed: ${err.message}`);
45
54
  }
46
- ctx.fb_dtsg = fb_dtsg;
47
- ctx.jazoest = jazoest;
48
- callback(null, {
49
- data: { fb_dtsg, jazoest },
50
- message: "Refreshed fb_dtsg and jazoest",
51
- });
52
- })
53
- .catch((err) => {
55
+ }
56
+ throw lastErr || new utils.CustomError("Could not find fb_dtsg in any fallback HTML sources.");
57
+ };
58
+
59
+ (async () => {
60
+ try {
61
+ const result = await tryRefresh();
62
+ callback(null, result);
63
+ } catch (err) {
54
64
  log.error("refreshFb_dtsg", err);
55
65
  callback(err);
56
- });
66
+ }
67
+ })();
57
68
  } else {
58
69
  Object.assign(ctx, obj);
59
70
  callback(null, {
@@ -63,4 +74,83 @@ module.exports = function (defaultFuncs, api, ctx) {
63
74
  }
64
75
  return returnPromise;
65
76
  };
66
- };
77
+ };
78
+
79
+ function extractTokensFromHtml(html) {
80
+ const tokens = { fb_dtsg: null, jazoest: null };
81
+
82
+ const dtsgRegexes = [
83
+ /DTSGInitialData.*?token":"(.*?)"/,
84
+ /"DTSGInitData",\[\],{"token":"(.*?)"/,
85
+ /\["DTSGInitData",\[\],{"token":"(.*?)"/,
86
+ /name="fb_dtsg" value="(.*?)"/,
87
+ /name="dtsg_ag" value="(.*?)"/
88
+ ];
89
+
90
+ for (const regex of dtsgRegexes) {
91
+ const match = html.match(regex);
92
+ if (match && match[1]) {
93
+ tokens.fb_dtsg = match[1];
94
+ break;
95
+ }
96
+ }
97
+
98
+ if (!tokens.fb_dtsg) {
99
+ try {
100
+ const scriptRegex = /<script type="application\/json"[^>]*>(.*?)<\/script>/g;
101
+ let match;
102
+ const netData = [];
103
+ while ((match = scriptRegex.exec(html)) !== null) {
104
+ try {
105
+ netData.push(JSON.parse(match[1]));
106
+ } catch (_) {}
107
+ }
108
+ const findConfig = (key) => {
109
+ for (const scriptData of netData) {
110
+ if (scriptData.require) {
111
+ for (const req of scriptData.require) {
112
+ if (Array.isArray(req) && req[0] === key && req[2]) {
113
+ return req[2];
114
+ }
115
+ if (
116
+ Array.isArray(req) &&
117
+ req[3] &&
118
+ req[3][0] &&
119
+ req[3][0].__bbox &&
120
+ req[3][0].__bbox.define
121
+ ) {
122
+ for (const def of req[3][0].__bbox.define) {
123
+ if (Array.isArray(def) && def[0].endsWith(key) && def[2]) {
124
+ return def[2];
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
131
+ return null;
132
+ };
133
+ const dtsgData = findConfig("DTSGInitialData");
134
+ if (dtsgData && dtsgData.token) {
135
+ tokens.fb_dtsg = dtsgData.token;
136
+ }
137
+ } catch (_) {}
138
+ }
139
+
140
+ if (!tokens.jazoest) {
141
+ const jazoRegexes = [
142
+ /name="jazoest" value="(\d+)"/,
143
+ /"jazoest","(\d+)"/,
144
+ /jazoest=(\d+)/
145
+ ];
146
+ for (const regex of jazoRegexes) {
147
+ const match = html.match(regex);
148
+ if (match && match[1]) {
149
+ tokens.jazoest = match[1];
150
+ break;
151
+ }
152
+ }
153
+ }
154
+
155
+ return tokens;
156
+ }