| 1 | |
| 2 | import { homedir } from "node:os"; |
| 3 | import path from "node:path"; |
| 4 | import { pathToFileURL } from "node:url"; |
| 5 | import { PhoneBrowser } from "./modules/browser-phone.mjs"; |
| 6 | import { phoneError } from "./modules/browser-phone-network.mjs"; |
| 7 | import { lines } from "./windows-browser.mjs"; |
| 8 | |
| 9 | export async function runMacOSPhoneBrowser(input = process.stdin, output = process.stdout, options = {}) { |
| 10 | if (process.platform !== "darwin" && options.testing !== true) throw new Error("此浏览器需要 macOS 客机"); |
| 11 | const phone = new PhoneBrowser({ |
| 12 | binary: options.binary || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", |
| 13 | directory: options.directory || path.join(homedir(), "Library", "Application Support", "Cage", "PhoneBrowser"), |
| 14 | language: options.language || Intl.DateTimeFormat().resolvedOptions().locale, |
| 15 | connect: options.connect, |
| 16 | }); |
| 17 | |
| 18 | |
| 19 | let opening; |
| 20 | async function cancelOpen() { |
| 21 | if (opening) { |
| 22 | opening.abort.abort(); |
| 23 | await opening.done; |
| 24 | opening = undefined; |
| 25 | } |
| 26 | await phone.dispose(); |
| 27 | } |
| 28 | async function operate(operation, input) { |
| 29 | if (opening && input?.id !== opening.id) throw phoneError(410, "手机浏览器会话已结束"); |
| 30 | if (opening && operation === "close") { |
| 31 | await cancelOpen(); |
| 32 | return { id: input.id }; |
| 33 | } |
| 34 | if (!opening && operation === "open") { |
| 35 | const entry = { id: input?.id, abort: new AbortController(), settled: false }; |
| 36 | opening = entry; |
| 37 | const deadline = setTimeout(() => entry.abort.abort(), 75000); |
| 38 | entry.done = phone.run("open", input, entry.abort.signal).catch(error => { |
| 39 | entry.error = entry.abort.signal.aborted ? phoneError(503, "客机浏览器启动超时") : error; |
| 40 | }).finally(() => { |
| 41 | entry.settled = true; |
| 42 | clearTimeout(deadline); |
| 43 | }); |
| 44 | |
| 45 | let timer; |
| 46 | try { await Promise.race([entry.done, new Promise(resolve => { timer = setTimeout(resolve, 200); })]); } |
| 47 | finally { clearTimeout(timer); } |
| 48 | } |
| 49 | if (opening) { |
| 50 | if (opening.settled) { |
| 51 | const { error } = opening; |
| 52 | opening = undefined; |
| 53 | if (error) throw error; |
| 54 | } else if (operation !== "open") { |
| 55 | throw phoneError(425, "正在启动虚拟 Mac 中的浏览器"); |
| 56 | } |
| 57 | if (operation === "open") return { id: input.id }; |
| 58 | } |
| 59 | return phone.run(operation, input); |
| 60 | } |
| 61 | try { |
| 62 | for await (const line of lines(input)) { |
| 63 | let result; |
| 64 | try { |
| 65 | const request = JSON.parse(line); |
| 66 | if (request.method !== "POST" || !/^\/phone-browser\/(open|frame|action|close)$/.test(request.path || "")) throw new Error("手机浏览器请求无效"); |
| 67 | result = await operate(request.path.split("/").at(-1), request.input); |
| 68 | } catch (error) { |
| 69 | result = { error: "手机浏览器操作未完成,请核对授权和最新画面", code: "phone_browser_error", status: [403, 409, 410, 425, 503].includes(error.status) ? error.status : 400 }; |
| 70 | } |
| 71 | await new Promise((resolve, reject) => output.write(JSON.stringify(result) + "\n", error => error ? reject(error) : resolve())); |
| 72 | } |
| 73 | } finally { await cancelOpen(); } |
| 74 | } |
| 75 | |
| 76 | if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) { |
| 77 | await runMacOSPhoneBrowser().catch(() => { process.exitCode = 1; }); |
| 78 | } |