-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlogin-runner.js
More file actions
317 lines (276 loc) · 10.7 KB
/
Copy pathlogin-runner.js
File metadata and controls
317 lines (276 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Auto-OTP login runner. Parent provides email via PERPLEXITY_EMAIL; we
// drive the real Perplexity email+OTP flow (NextAuth on the live site,
// legacy /login/* on the local mock), prompt for the six-digit code via
// IPC, and persist the resulting session into the profile vault.
import { writeFileSync, existsSync, mkdirSync } from "node:fs";
import { chromium } from "patchright";
import { Vault } from "./vault.js";
import { resolveBrowserExecutable } from "./config.js";
import { getProfilePaths, getActiveName, recordLoginSuccess } from "./profiles.js";
import { redact } from "./redact.js";
import { minimizePageWindow, loginLaunchArgs } from "./browser-window.js";
import {
buildRuntimeEndpoints,
collectSessionMetadata,
pageRequest,
} from "./session-metadata.js";
const ORIGIN = process.env.PERPLEXITY_ORIGIN || "https://www.perplexity.ai";
const LOGIN_PATH = process.env.PERPLEXITY_LOGIN_PATH || "/account";
const EMAIL = process.env.PERPLEXITY_EMAIL;
const OTP_TIMEOUT_MS = Number(process.env.PERPLEXITY_OTP_TIMEOUT_MS ?? 300_000);
const CF_TIMEOUT_MS = Number(process.env.PERPLEXITY_CF_TIMEOUT_MS ?? 20_000);
const MAX_RETRIES = 2;
function resolveProfile() {
return process.env.PERPLEXITY_PROFILE || getActiveName() || "default";
}
function isLocalOrigin(origin) {
return /^https?:\/\/(127\.0\.0\.1|localhost)(:\d+)?(?:\/|$)/i.test(origin);
}
function ipc(msg) { if (process.send) process.send(msg); }
function emit(obj) { process.stdout.write(JSON.stringify(obj) + "\n"); }
async function awaitOtp() {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
process.removeListener("message", handler);
reject(new Error("otp_timeout"));
}, OTP_TIMEOUT_MS);
const handler = (m) => {
if (m && typeof m.otp === "string") {
clearTimeout(timer);
process.removeListener("message", handler);
resolve(m.otp);
}
};
process.on("message", handler);
});
}
async function main() {
if (!EMAIL) {
emit({ ok: false, reason: "no_email" });
process.exit(1);
}
const PROFILE = resolveProfile();
const paths = getProfilePaths(PROFILE);
const localOrigin = isLocalOrigin(ORIGIN);
let executablePath;
let channel;
if (!localOrigin) {
try {
({ path: executablePath, channel } = await resolveBrowserExecutable());
} catch (err) {
emit({ ok: false, reason: "chrome_missing", error: redact(String(err?.message ?? err)) });
process.exit(4);
}
}
const ctx = await chromium.launchPersistentContext(paths.loginBrowserData, {
headless: localOrigin,
ignoreHTTPSErrors: true,
...(executablePath ? { executablePath } : {}),
...(channel && ["chrome", "msedge", "chromium"].includes(channel) ? { channel } : {}),
args: loginLaunchArgs(localOrigin),
});
const page = await ctx.newPage();
try {
await page.goto(`${ORIGIN}${LOGIN_PATH}`, { waitUntil: "domcontentloaded", timeout: 30_000 });
} catch {}
if (!localOrigin) await minimizePageWindow(page);
const ready = await waitForLoginReady(page);
if (!ready) {
const title = await page.title().catch(() => "");
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: /just a moment/i.test(title) ? "cf_blocked" : "auto_unsupported" });
process.exit(/just a moment/i.test(title) ? 3 : 2);
}
const liveAttempt = await startLiveEmailFlow(page);
let authFlow = liveAttempt;
if (liveAttempt.kind === "unsupported") {
authFlow = await startLegacyMockFlow(page);
}
if (!localOrigin) await minimizePageWindow(page);
if (authFlow.kind === "sso_required") {
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: "sso_required" });
process.exit(2);
}
if (authFlow.kind === "unsupported") {
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: "auto_unsupported", detail: authFlow.detail });
process.exit(2);
}
if (authFlow.kind === "email_rejected") {
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: "email_rejected", detail: authFlow.detail });
process.exit(2);
}
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
ipc({ phase: "awaiting_otp", attempt });
let otp;
try {
otp = await awaitOtp();
} catch {
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: "otp_timeout" });
process.exit(2);
}
const submitResp = await submitOtp(page, authFlow, otp);
if (submitResp.ok) {
const allCookies = await ctx.cookies();
const metadata = await collectSessionMetadata(page, ORIGIN, {
sessionData: submitResp.sessionData,
sessionTimeoutMs: 10_000,
});
const vault = new Vault();
await vault.set(PROFILE, "cookies", JSON.stringify(allCookies));
if (metadata.sessionData?.user?.email) await vault.set(PROFILE, "email", metadata.sessionData.user.email);
if (metadata.sessionData?.user?.id) await vault.set(PROFILE, "userId", metadata.sessionData.user.id);
if (!existsSync(paths.dir)) mkdirSync(paths.dir, { recursive: true });
writeFileSync(paths.modelsCache, JSON.stringify(metadata.cache, null, 2));
recordLoginSuccess(PROFILE, { tier: metadata.tier, loginMode: "auto", lastLogin: new Date().toISOString() });
writeFileSync(paths.reinit, String(Date.now()));
await ctx.browser()?.close().catch(() => {});
emit({ ok: true, tier: metadata.tier, modelCount: Object.keys(metadata.models?.models ?? {}).length });
process.exit(0);
}
if (authFlow.kind === "live") {
await page.goto(authFlow.verifyUrl, { waitUntil: "domcontentloaded", timeout: 30_000 }).catch(() => {});
if (!localOrigin) await minimizePageWindow(page);
}
if (attempt === MAX_RETRIES) {
await ctx.browser()?.close().catch(() => {});
emit({ ok: false, reason: "otp_rejected" });
process.exit(2);
}
}
}
async function waitForLoginReady(page) {
const started = Date.now();
while (Date.now() - started < CF_TIMEOUT_MS) {
if (await page.locator('input[type="email"]').count().catch(() => 0)) {
return true;
}
const title = await page.title().catch(() => "");
if (title && !/just a moment/i.test(title)) {
const body = await page.locator("body").innerText().catch(() => "");
if (/continue with email/i.test(body) || /single sign-on/i.test(body) || /continue/i.test(body)) {
return true;
}
}
await page.waitForTimeout(500);
}
return false;
}
async function startLiveEmailFlow(page) {
const endpoints = buildRuntimeEndpoints(ORIGIN);
const csrf = await pageRequest(page, endpoints.csrf);
if (!(csrf.ok && csrf.contentType.includes("json") && csrf.json?.csrfToken)) {
return {
kind: "unsupported",
detail: { step: "csrf", status: csrf.status, contentType: csrf.contentType, error: csrf.error ?? undefined },
};
}
const sso = await pageRequest(page, endpoints.ssoDetails, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email: EMAIL }),
});
if (sso.ok && sso.json?.organization) {
return { kind: "sso_required" };
}
const redirectUrl = `${ORIGIN}/account?login-source=settings`;
const signIn = await pageRequest(page, endpoints.signInEmail, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: EMAIL,
useNumericOtp: "true",
csrfToken: csrf.json.csrfToken,
callbackUrl: `${redirectUrl}#locale=en-US`,
json: "true",
}),
});
if (!(signIn.ok && signIn.contentType.includes("json") && signIn.json?.url)) {
return {
kind: signIn.status >= 400 && signIn.status < 500 ? "email_rejected" : "unsupported",
detail: { step: "signin_email", status: signIn.status, contentType: signIn.contentType, error: signIn.error ?? undefined },
};
}
const verifyUrl = new URL(signIn.json.url, ORIGIN);
verifyUrl.searchParams.set("email", EMAIL);
verifyUrl.searchParams.set("redirectUrl", redirectUrl);
await page.goto(verifyUrl.toString(), { waitUntil: "domcontentloaded", timeout: 30_000 }).catch(() => {});
return {
kind: "live",
redirectUrl,
verifyUrl: verifyUrl.toString(),
};
}
async function startLegacyMockFlow(page) {
const emailResp = await pageRequest(page, `${ORIGIN}/login/email`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email: EMAIL }),
});
if (emailResp.redirected && (emailResp.url || "").includes("/sso")) {
return { kind: "sso_required" };
}
const looksUnsupported =
emailResp.status === 404 ||
emailResp.status === 405 ||
emailResp.status >= 500 ||
!emailResp.contentType.includes("json");
if (looksUnsupported) {
return {
kind: "unsupported",
detail: { step: "legacy_email", status: emailResp.status, contentType: emailResp.contentType, error: emailResp.error ?? undefined },
};
}
if (!emailResp.ok) {
return { kind: "email_rejected", detail: emailResp.status };
}
return { kind: "legacy" };
}
async function submitOtp(page, flow, otp) {
if (flow.kind === "legacy") {
const submitResp = await pageRequest(page, `${ORIGIN}/login/otp`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email: EMAIL, otp }),
});
if (submitResp.status !== 200) {
return { ok: false };
}
const metadata = await collectSessionMetadata(page, ORIGIN, { sessionTimeoutMs: 2_000 });
return { ok: !!metadata.sessionData?.user?.id, sessionData: metadata.sessionData };
}
const endpoints = buildRuntimeEndpoints(ORIGIN);
const redirectResp = await pageRequest(page, endpoints.otpRedirectLink, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: EMAIL,
otp,
redirectUrl: flow.redirectUrl,
emailLoginMethod: "web-otp",
loginSource: null,
}),
});
if (!(redirectResp.ok && redirectResp.contentType.includes("json") && redirectResp.json?.redirect)) {
return { ok: false };
}
const callbackUrl = new URL(redirectResp.json.redirect, ORIGIN).toString();
await page.goto(callbackUrl, { waitUntil: "domcontentloaded", timeout: 30_000 }).catch(() => {});
const metadata = await collectSessionMetadata(page, ORIGIN, { sessionTimeoutMs: 5_000 });
return { ok: !!metadata.sessionData?.user?.id, sessionData: metadata.sessionData };
}
main().catch((err) => {
const msg = err?.message ?? err;
const stack = err?.stack;
emit({
ok: false,
reason: "crash",
error: redact(String(msg ?? "unknown error")),
detail: redact(String(msg ?? "unknown error")),
...(stack ? { stack: redact(String(stack)) } : {}),
});
process.exit(5);
});