Create endpoints/cloudflare.js
Browse files- endpoints/cloudflare.js +79 -0
endpoints/cloudflare.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
async function cloudflare(data, page) {
|
| 2 |
+
return new Promise(async (resolve, reject) => {
|
| 3 |
+
if (!data.domain) return reject(new Error("Missing domain parameter"))
|
| 4 |
+
|
| 5 |
+
const startTime = Date.now()
|
| 6 |
+
let isResolved = false
|
| 7 |
+
let userAgent = null
|
| 8 |
+
|
| 9 |
+
const cl = setTimeout(() => {
|
| 10 |
+
if (!isResolved) {
|
| 11 |
+
isResolved = true
|
| 12 |
+
const elapsedTime = (Date.now() - startTime) / 1000
|
| 13 |
+
resolve({
|
| 14 |
+
cf_clearance: null,
|
| 15 |
+
user_agent: userAgent,
|
| 16 |
+
elapsed_time: elapsedTime,
|
| 17 |
+
})
|
| 18 |
+
}
|
| 19 |
+
}, 20000)
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
if (data.proxy?.username && data.proxy?.password) {
|
| 23 |
+
await page.authenticate({
|
| 24 |
+
username: data.proxy.username,
|
| 25 |
+
password: data.proxy.password,
|
| 26 |
+
})
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
page.removeAllListeners("request")
|
| 30 |
+
page.removeAllListeners("response")
|
| 31 |
+
await page.setRequestInterception(true)
|
| 32 |
+
|
| 33 |
+
page.on("request", async (req) => {
|
| 34 |
+
try {
|
| 35 |
+
await req.continue()
|
| 36 |
+
} catch (_) {}
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
page.on("response", async (res) => {
|
| 40 |
+
try {
|
| 41 |
+
const url = res.url()
|
| 42 |
+
if (url.includes("/cdn-cgi/challenge-platform/")) {
|
| 43 |
+
const headers = res.headers()
|
| 44 |
+
if (headers["set-cookie"]) {
|
| 45 |
+
const match = headers["set-cookie"].match(/cf_clearance=([^;]+)/)
|
| 46 |
+
if (match) {
|
| 47 |
+
const cf_clearance = match[1]
|
| 48 |
+
const userAgent = (await res.request().headers())["user-agent"]
|
| 49 |
+
const elapsedTime = (Date.now() - startTime) / 1000
|
| 50 |
+
|
| 51 |
+
if (!isResolved) {
|
| 52 |
+
isResolved = true
|
| 53 |
+
clearTimeout(cl)
|
| 54 |
+
|
| 55 |
+
resolve({
|
| 56 |
+
cf_clearance,
|
| 57 |
+
user_agent: userAgent,
|
| 58 |
+
elapsed_time: elapsedTime,
|
| 59 |
+
})
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
} catch (_) {}
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
await page.goto(data.domain, { waitUntil: "domcontentloaded" })
|
| 68 |
+
userAgent = await page.evaluate(() => navigator.userAgent)
|
| 69 |
+
} catch (err) {
|
| 70 |
+
if (!isResolved) {
|
| 71 |
+
isResolved = true
|
| 72 |
+
clearTimeout(cl)
|
| 73 |
+
reject(err)
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
})
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
module.exports = cloudflare
|