@xkwdstore/agent 2.1.4 → 2.1.6
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 +163 -0
- package/README.md +281 -278
- package/package.json +43 -42
- package/scripts/install-machine-agent.ps1 +385 -385
- package/src/actions/helpers.js +15 -21
- package/src/actions/session.js +142 -16
- package/src/brokerConnectionSupervisor.js +319 -263
- package/src/brokerHealth.js +113 -0
- package/src/brokerWorkerBridge.js +377 -352
- package/src/browserConnectivity.js +1461 -0
- package/src/browserIsolation.js +26 -1
- package/src/browserLaunch.js +136 -0
- package/src/cli.js +1290 -1108
- package/src/cookieInjection.js +255 -0
- package/src/enrollmentClient.js +51 -26
- package/src/index.js +75 -53
- package/src/proxyCapability.js +481 -0
- package/src/sessionAuth.js +439 -0
- package/src/sessionHandler.js +709 -108
- package/src/worker.js +707 -253
package/src/actions/helpers.js
CHANGED
|
@@ -613,6 +613,10 @@ const SELECTORS = {
|
|
|
613
613
|
loginButton: 'button[data-testid="LoginForm_Login_Button"]',
|
|
614
614
|
twoFactorInput: 'input[name="text"]',
|
|
615
615
|
homeIndicator: '[data-testid="SideNav_NewTweet_Button"]',
|
|
616
|
+
// Signed-in-account signals. A generic `primaryColumn` shell is NOT proof of
|
|
617
|
+
// authentication (it renders on signed-out surfaces too).
|
|
618
|
+
profileLink: 'a[data-testid="AppTabBar_Profile_Link"]',
|
|
619
|
+
accountSwitcher: '[data-testid="SideNav_AccountSwitcher_Button"]',
|
|
616
620
|
tweetButton: '[data-testid="tweetButton"]',
|
|
617
621
|
primaryColumn: '[data-testid="primaryColumn"]',
|
|
618
622
|
};
|
|
@@ -628,10 +632,11 @@ async function detectStage(page) {
|
|
|
628
632
|
if (hasUsername) return 'login_username_step';
|
|
629
633
|
return 'login_required';
|
|
630
634
|
}
|
|
631
|
-
// Check if logged in
|
|
635
|
+
// Check if logged in from a SIGNED-IN ACCOUNT signal, not a page layout.
|
|
632
636
|
const hasHome = await isLocatorVisible(page.locator(SELECTORS.homeIndicator), 3000);
|
|
633
|
-
const
|
|
634
|
-
|
|
637
|
+
const hasProfileLink = await isLocatorVisible(page.locator(SELECTORS.profileLink), 3000);
|
|
638
|
+
const hasSwitcher = await isLocatorVisible(page.locator(SELECTORS.accountSwitcher), 3000);
|
|
639
|
+
if (hasHome || hasProfileLink || hasSwitcher) return 'home_logged_in';
|
|
635
640
|
return 'unknown';
|
|
636
641
|
} catch { return 'unknown'; }
|
|
637
642
|
}
|
|
@@ -650,25 +655,14 @@ async function waitForLoggedIn(page, options = {}) {
|
|
|
650
655
|
}
|
|
651
656
|
|
|
652
657
|
// ── Cookie helpers ──────────────────────────────────────────────────────────
|
|
658
|
+
//
|
|
659
|
+
// Delegate to the canonical device-side contract. The previous implementation
|
|
660
|
+
// invented `domain: '.x.com'`, `httpOnly: true`, `secure: true` and
|
|
661
|
+
// `sameSite: 'Lax'` for any attribute the payload omitted — silently broadening
|
|
662
|
+
// scope and changing semantics. The canonical applier never does that.
|
|
653
663
|
async function injectCookies(context, cookiesData) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
let cookies = [];
|
|
657
|
-
if (typeof cookiesData === 'string') {
|
|
658
|
-
try { cookies = JSON.parse(cookiesData); }
|
|
659
|
-
catch { try { cookies = JSON.parse(Buffer.from(cookiesData, 'base64').toString()); } catch {} }
|
|
660
|
-
} else if (Array.isArray(cookiesData)) {
|
|
661
|
-
cookies = cookiesData;
|
|
662
|
-
}
|
|
663
|
-
if (!cookies.length) return;
|
|
664
|
-
const normalized = cookies.map(c => ({
|
|
665
|
-
name: c.name, value: c.value,
|
|
666
|
-
domain: c.domain || '.x.com', path: c.path || '/',
|
|
667
|
-
expires: c.expires || -1, httpOnly: c.httpOnly !== false,
|
|
668
|
-
secure: c.secure !== false, sameSite: c.sameSite || 'Lax',
|
|
669
|
-
}));
|
|
670
|
-
await context.addCookies(normalized);
|
|
671
|
-
} catch {}
|
|
664
|
+
const { applyCookies } = require('../cookieInjection');
|
|
665
|
+
return applyCookies(context, cookiesData);
|
|
672
666
|
}
|
|
673
667
|
|
|
674
668
|
async function saveCookies(context) {
|
package/src/actions/session.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
|
|
9
9
|
const { sleep, humanDelay, randDelay, safeFill, safeClick, humanClick, getBodyText, detectStage, waitForLoggedIn, generateTOTP, injectCookies, SELECTORS, humanHover, idleMouseJitter, humanScroll, logNormalDelay, highlightElement } = require('./helpers');
|
|
10
10
|
const dbc = require('./dbc');
|
|
11
|
+
const sessionAuth = require('../sessionAuth');
|
|
12
|
+
const { applyCookies } = require('../cookieInjection');
|
|
11
13
|
|
|
12
14
|
// Simple log — writes to agent log via console (captured by agent's log system)
|
|
13
15
|
function log(msg) { console.log(`[session-action] ${msg}`); }
|
|
@@ -115,13 +117,90 @@ async function solveCaptchaWithDBC(page, captchaInfo) {
|
|
|
115
117
|
|
|
116
118
|
// ── Auto-login ───────────────────────────────────────────────────────────────
|
|
117
119
|
async function autoLogin(page, payload) {
|
|
118
|
-
const { username, password, totpSecret, cookies, dbcUsername, dbcPassword } = payload;
|
|
120
|
+
const { username, password, totpSecret, cookies, dbcUsername, dbcPassword } = payload || {};
|
|
121
|
+
const context = typeof page.context === 'function' ? page.context() : null;
|
|
122
|
+
const verifyOpts = { expectedUsername: username, timeoutMs: 4000 };
|
|
123
|
+
|
|
124
|
+
const verify = async () => {
|
|
125
|
+
try {
|
|
126
|
+
return await sessionAuth.verifyXAuth(page, context, verifyOpts);
|
|
127
|
+
} catch {
|
|
128
|
+
return { authState: sessionAuth.AUTH_STATE.UNKNOWN, reason: 'verify_failed', stage: 'verify', handle: null };
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const authenticated = (verified, extra) => ({
|
|
132
|
+
success: true,
|
|
133
|
+
state: 'already_logged_in',
|
|
134
|
+
authState: sessionAuth.AUTH_STATE.AUTHENTICATED,
|
|
135
|
+
loggedIn: true,
|
|
136
|
+
browserReady: true,
|
|
137
|
+
handle: verified && verified.handle ? verified.handle : null,
|
|
138
|
+
message: 'Existing X session is already signed in.',
|
|
139
|
+
...(extra || {}),
|
|
140
|
+
});
|
|
141
|
+
const wrongAccount = (verified) => ({
|
|
142
|
+
success: false,
|
|
143
|
+
state: 'wrong_account',
|
|
144
|
+
authState: sessionAuth.AUTH_STATE.WRONG_ACCOUNT,
|
|
145
|
+
loggedIn: false,
|
|
146
|
+
browserReady: true,
|
|
147
|
+
handle: verified && verified.handle ? verified.handle : null,
|
|
148
|
+
code: 'WRONG_ACCOUNT',
|
|
149
|
+
message: 'The browser profile is signed in as a different X account; authenticated operations are blocked.',
|
|
150
|
+
});
|
|
151
|
+
const challenge = (verified, message) => ({
|
|
152
|
+
success: false,
|
|
153
|
+
state: 'challenge_required',
|
|
154
|
+
authState: sessionAuth.AUTH_STATE.CHALLENGE_REQUIRED,
|
|
155
|
+
loggedIn: false,
|
|
156
|
+
browserReady: true,
|
|
157
|
+
requiresOperator: true,
|
|
158
|
+
code: 'CHALLENGE_REQUIRED',
|
|
159
|
+
message: message || 'X requires operator action (a challenge is present). Complete it in the browser window.',
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ── 1. Recognize an existing session BEFORE any credential is required ────
|
|
163
|
+
const before = await verify();
|
|
164
|
+
if (before.authState === sessionAuth.AUTH_STATE.AUTHENTICATED) {
|
|
165
|
+
return authenticated(before, { recovered: false });
|
|
166
|
+
}
|
|
167
|
+
if (before.authState === sessionAuth.AUTH_STATE.WRONG_ACCOUNT) return wrongAccount(before);
|
|
168
|
+
if (before.authState === sessionAuth.AUTH_STATE.CHALLENGE_REQUIRED) return challenge(before);
|
|
119
169
|
|
|
120
|
-
// Configure DBC if credentials provided
|
|
121
170
|
if (dbcUsername && dbcPassword) {
|
|
171
|
+
// Configure DBC if credentials provided
|
|
122
172
|
dbc.configure(dbcUsername, dbcPassword);
|
|
123
173
|
}
|
|
124
174
|
|
|
175
|
+
// ── 2. Restore from stored cookies before asking for a password ───────────
|
|
176
|
+
if (Array.isArray(cookies) && cookies.length > 0 && context) {
|
|
177
|
+
const applied = await applyCookies(context, cookies);
|
|
178
|
+
if (applied.ok) {
|
|
179
|
+
try {
|
|
180
|
+
await page.goto(sessionAuth.CANONICAL_HOME, { waitUntil: 'domcontentloaded', timeout: 45000 });
|
|
181
|
+
} catch { /* verified below; a navigation failure is not a logout */ }
|
|
182
|
+
const after = await verify();
|
|
183
|
+
if (after.authState === sessionAuth.AUTH_STATE.AUTHENTICATED) {
|
|
184
|
+
return authenticated(after, { recovered: true, cookieState: applied.state, message: 'Session restored from stored cookies.' });
|
|
185
|
+
}
|
|
186
|
+
if (after.authState === sessionAuth.AUTH_STATE.WRONG_ACCOUNT) return wrongAccount(after);
|
|
187
|
+
if (after.authState === sessionAuth.AUTH_STATE.CHALLENGE_REQUIRED) return challenge(after);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ── 3. Credential fallback only when the profile is signed out ────────────
|
|
192
|
+
if (!username || !password) {
|
|
193
|
+
return {
|
|
194
|
+
success: false,
|
|
195
|
+
state: 'credentials_required',
|
|
196
|
+
authState: before.authState || sessionAuth.AUTH_STATE.SIGNED_OUT,
|
|
197
|
+
loggedIn: false,
|
|
198
|
+
browserReady: true,
|
|
199
|
+
code: 'CREDENTIALS_REQUIRED',
|
|
200
|
+
message: 'The stored X session could not be restored. Provide credentials or sign in manually in the browser.',
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
125
204
|
// Navigate to login — try clicking Sign in on the current page first
|
|
126
205
|
// (navigating directly to x.com/i/flow/login can crash Firefox with some proxies)
|
|
127
206
|
try {
|
|
@@ -198,7 +277,7 @@ async function autoLogin(page, payload) {
|
|
|
198
277
|
console.log(`[login] Captcha detected: ${captchaInfo.type}`);
|
|
199
278
|
const solved = await solveCaptchaWithDBC(page, captchaInfo);
|
|
200
279
|
if (!solved) {
|
|
201
|
-
return
|
|
280
|
+
return challenge(before, 'A challenge was detected but could not be solved automatically. Manual intervention is required.');
|
|
202
281
|
}
|
|
203
282
|
await randDelay(2000, 4000);
|
|
204
283
|
}
|
|
@@ -215,7 +294,16 @@ async function autoLogin(page, payload) {
|
|
|
215
294
|
await page.keyboard.press('Enter');
|
|
216
295
|
await randDelay(3000, 5000);
|
|
217
296
|
} else {
|
|
218
|
-
|
|
297
|
+
return {
|
|
298
|
+
success: false,
|
|
299
|
+
state: 'verification_required',
|
|
300
|
+
authState: sessionAuth.AUTH_STATE.SIGNED_OUT,
|
|
301
|
+
loggedIn: false,
|
|
302
|
+
browserReady: true,
|
|
303
|
+
requiresOperator: true,
|
|
304
|
+
code: 'TWO_FACTOR_REQUIRED',
|
|
305
|
+
message: 'X requires a two-factor code but no TOTP secret is stored.',
|
|
306
|
+
};
|
|
219
307
|
}
|
|
220
308
|
}
|
|
221
309
|
}
|
|
@@ -226,34 +314,72 @@ async function autoLogin(page, payload) {
|
|
|
226
314
|
console.log(`[login] Post-2FA captcha detected: ${captchaInfo2.type}`);
|
|
227
315
|
const solved = await solveCaptchaWithDBC(page, captchaInfo2);
|
|
228
316
|
if (!solved) {
|
|
229
|
-
return
|
|
317
|
+
return challenge(before, 'A challenge after two-factor authentication could not be solved automatically.');
|
|
230
318
|
}
|
|
231
319
|
await randDelay(2000, 4000);
|
|
232
320
|
}
|
|
233
321
|
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
322
|
+
// Step 6: Verify the RESULT (not a generic home layout).
|
|
323
|
+
await waitForLoggedIn(page, { timeoutMs: 25000 });
|
|
324
|
+
const verified = await verify();
|
|
325
|
+
if (verified.authState === sessionAuth.AUTH_STATE.AUTHENTICATED) {
|
|
326
|
+
return {
|
|
327
|
+
success: true,
|
|
328
|
+
state: 'authenticated',
|
|
329
|
+
authState: sessionAuth.AUTH_STATE.AUTHENTICATED,
|
|
330
|
+
loggedIn: true,
|
|
331
|
+
browserReady: true,
|
|
332
|
+
handle: verified.handle || null,
|
|
333
|
+
message: 'Login successful',
|
|
334
|
+
};
|
|
238
335
|
}
|
|
336
|
+
if (verified.authState === sessionAuth.AUTH_STATE.CHALLENGE_REQUIRED) return challenge(verified);
|
|
337
|
+
if (verified.authState === sessionAuth.AUTH_STATE.WRONG_ACCOUNT) return wrongAccount(verified);
|
|
239
338
|
|
|
240
339
|
const finalStage = await detectStage(page);
|
|
241
340
|
if (finalStage === 'login_username_step' || finalStage === 'login_password_step') {
|
|
242
|
-
return {
|
|
341
|
+
return {
|
|
342
|
+
success: false,
|
|
343
|
+
state: 'bad_credentials',
|
|
344
|
+
authState: sessionAuth.AUTH_STATE.SIGNED_OUT,
|
|
345
|
+
loggedIn: false,
|
|
346
|
+
browserReady: true,
|
|
347
|
+
code: 'BAD_CREDENTIALS',
|
|
348
|
+
message: 'Login failed — check credentials',
|
|
349
|
+
};
|
|
243
350
|
}
|
|
244
351
|
|
|
245
|
-
return {
|
|
352
|
+
return {
|
|
353
|
+
success: false,
|
|
354
|
+
state: 'unknown',
|
|
355
|
+
authState: sessionAuth.AUTH_STATE.UNKNOWN,
|
|
356
|
+
loggedIn: false,
|
|
357
|
+
browserReady: true,
|
|
358
|
+
code: 'LOGIN_OUTCOME_UNKNOWN',
|
|
359
|
+
message: 'Login may require manual intervention',
|
|
360
|
+
};
|
|
246
361
|
}
|
|
247
362
|
|
|
248
363
|
// ── Check login state ────────────────────────────────────────────────────────
|
|
249
364
|
async function checkLoginState(page, payload) {
|
|
250
|
-
const
|
|
251
|
-
const
|
|
365
|
+
const context = typeof page.context === 'function' ? page.context() : null;
|
|
366
|
+
const verified = await sessionAuth.verifyXAuth(page, context, {
|
|
367
|
+
expectedUsername: payload && payload.username,
|
|
368
|
+
timeoutMs: 4000,
|
|
369
|
+
});
|
|
370
|
+
const summary = sessionAuth.authSummary(verified);
|
|
252
371
|
return {
|
|
253
372
|
success: true,
|
|
254
|
-
loggedIn,
|
|
255
|
-
state:
|
|
256
|
-
|
|
373
|
+
loggedIn: summary.loggedIn,
|
|
374
|
+
state: summary.authState === sessionAuth.AUTH_STATE.AUTHENTICATED ? 'logged_in'
|
|
375
|
+
: summary.authState === sessionAuth.AUTH_STATE.SIGNED_OUT ? 'login_required'
|
|
376
|
+
: summary.authState,
|
|
377
|
+
authState: summary.authState,
|
|
378
|
+
browserReady: true,
|
|
379
|
+
reason: summary.reason,
|
|
380
|
+
stage: summary.stage,
|
|
381
|
+
handle: summary.handle,
|
|
382
|
+
url: (() => { try { return page.url(); } catch { return null; } })(),
|
|
257
383
|
};
|
|
258
384
|
}
|
|
259
385
|
|