-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
228 lines (192 loc) · 5.57 KB
/
index.js
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
import { startSession, closeSession } from "./module/chromium.js";
import puppeteer from "puppeteer-extra";
import { notice, sleep } from "./module/general.js";
import { checkStat } from "./module/turnstile.js";
import { protectPage, protectedBrowser } from "puppeteer-afp";
import { puppeteerRealBrowser } from "./module/old.js";
export { puppeteerRealBrowser };
import StealthPlugin from "puppeteer-extra-plugin-stealth";
import "dotenv/config";
puppeteer.use(StealthPlugin());
async function handleNewPage({ page, config = {} }) {
// fp(page);
protectPage(page, {
webRTCProtect: false,
...config,
});
return page;
}
export const connect = ({
args = [],
headless = "auto",
customConfig = {},
proxy = {},
skipTarget = [],
fingerprint = false,
turnstile = false,
connectOption = {},
fpconfig = {},
}) => {
return new Promise(async (resolve, reject) => {
var global_target_status = false;
function targetFilter({ target, skipTarget }) {
if (global_target_status === false) {
return true;
}
var response = false;
try {
response = !!target.url();
if (skipTarget.find((item) => String(target.url()).indexOf(String(item) > -1))) {
response = true;
}
} catch (err) {}
return response;
}
const setTarget = ({ status = true }) => {
global_target_status = status;
};
const { chromeSession, cdpSession, chrome, xvfbsession } = await startSession({
args: args,
headless: headless,
customConfig: customConfig,
proxy: proxy,
});
const browser = await puppeteer.connect({
targetFilter: (target) => targetFilter({ target: target, skipTarget: skipTarget }),
browserWSEndpoint: chromeSession.browserWSEndpoint,
...connectOption,
});
var page = await browser.pages();
page = page[0];
setTarget({ status: true });
if (proxy && proxy.username && proxy.username.length > 0) {
await page.authenticate({ username: proxy.username, password: proxy.password });
}
var solve_status = true;
const setSolveStatus = ({ status }) => {
solve_status = status;
};
const autoSolve = ({ page }) => {
return new Promise(async (resolve, reject) => {
while (solve_status) {
try {
await sleep(1500);
await checkStat({ page: page }).catch((err) => {});
} catch (err) {}
}
resolve();
});
};
if (fingerprint === true) {
handleNewPage({ page: page, config: fpconfig });
}
if (turnstile === true) {
setSolveStatus({ status: true });
autoSolve({ page: page, browser: browser });
}
await page.setUserAgent(chromeSession.agent);
await page.setViewport({
width: 1920,
height: 1080,
});
browser.on("disconnected", async () => {
notice({
message: "Browser Disconnected",
type: "info",
});
try {
setSolveStatus({ status: false });
} catch (err) {}
await closeSession({
xvfbsession: xvfbsession,
cdpSession: cdpSession,
chrome: chrome,
}).catch((err) => {
console.log(err.message);
});
});
browser.on("targetcreated", async (target) => {
var newPage = await target.page();
try {
await newPage.setUserAgent(chromeSession.agent);
} catch (err) {
// console.log(err.message);
}
try {
await newPage.setViewport({
width: 1920,
height: 1080,
});
} catch (err) {
// console.log(err.message);
}
if (newPage && fingerprint === true) {
try {
handleNewPage({ page: newPage, config: fpconfig });
} catch (err) {}
}
if (turnstile === true) {
autoSolve({ page: newPage });
}
});
resolve({
browser: browser,
page: page,
xvfbsession: xvfbsession,
cdpSession: cdpSession,
chrome: chrome,
setTarget: setTarget,
});
});
};
import express from "express";
const app = express();
app.get("/", async (req, res) => {
const { url } = req.query;
if (!url) {
return res.status(400).send("Please provide a URL");
}
if (typeof url !== "string") {
url = String(url);
}
const params = new URLSearchParams(req.query);
for (const [key, value] of params.entries()) {
if (key !== "url") {
url += `&${key}=${encodeURIComponent(value)}`;
}
}
if (!url.startsWith("https://") && !url.startsWith("http://")) {
return res.status(400).send("Please provide a valid URL");
}
try {
const response = await connect({
turnstile: true,
fingerprint: true,
headless: "auto",
proxy: {
host: process.env.PROXY_HOST,
port: process.env.PROXY_PORT,
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD,
},
customConfig: {
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
},
args: ["--no-sandbox", "--disable-setuid-sandbox", "--single-process", "--no-zygote"],
});
const { page, browser } = response;
await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
await page.goto(url, {
timeout: 0,
});
await page.waitForSelector("body");
const html = await page.evaluate(() => document.documentElement.outerHTML);
res.send(html);
await browser.close();
} catch (error) {
res.send(error);
}
});
app.listen(3000, () => {
console.log("Server started on port 3000");
});