maylinejix commited on
Commit
e8abb59
·
verified ·
1 Parent(s): ed98fea

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +257 -0
server.js ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const puppeteer = require("puppeteer-extra");
3
+ const pluginStealth = require("puppeteer-extra-plugin-stealth");
4
+ const { hcaptcha, hcaptchaToken } = require("puppeteer-hcaptcha");
5
+
6
+ // Gunakan puppeteer stealth
7
+ puppeteer.use(pluginStealth());
8
+
9
+ const app = express();
10
+ app.use(express.json());
11
+
12
+ // Endpoint health check
13
+ app.get('/', (req, res) => {
14
+ res.json({
15
+ status: 'ok',
16
+ message: 'hCaptcha Solver API is running',
17
+ endpoints: {
18
+ 'POST /solve': 'Solve hCaptcha with browser (Body: { url: string })',
19
+ 'POST /solve-token': 'Get hCaptcha token only (Body: { url: string })',
20
+ 'GET /solve-demo': 'Solve demo hCaptcha'
21
+ }
22
+ });
23
+ });
24
+
25
+ // Endpoint untuk solve hCaptcha dengan browser page
26
+ app.post('/solve', async (req, res) => {
27
+ const { url } = req.body;
28
+
29
+ if (!url) {
30
+ return res.status(400).json({
31
+ error: 'Missing required parameter: url'
32
+ });
33
+ }
34
+
35
+ let browser;
36
+ const startTime = Date.now();
37
+
38
+ try {
39
+ browser = await puppeteer.launch({
40
+ ignoreHTTPSErrors: true,
41
+ headless: true,
42
+ args: [
43
+ '--window-size=1920,1080',
44
+ '--disable-dev-shm-usage',
45
+ '--no-sandbox',
46
+ '--disable-setuid-sandbox',
47
+ '--disable-web-security',
48
+ '--disable-features=site-per-process',
49
+ '--disable-blink-features=AutomationControlled',
50
+ '--disable-gpu'
51
+ ],
52
+ });
53
+
54
+ const [page] = await browser.pages();
55
+
56
+ await page.goto(url, {
57
+ waitUntil: 'networkidle2',
58
+ timeout: 60000
59
+ });
60
+
61
+ await page.setDefaultNavigationTimeout(0);
62
+
63
+ // Solve hCaptcha
64
+ await hcaptcha(page);
65
+
66
+ // Get token dari page
67
+ const token = await page.evaluate(() => {
68
+ const textarea = document.querySelector('[name="h-captcha-response"]');
69
+ return textarea ? textarea.value : null;
70
+ });
71
+
72
+ const endTime = Date.now();
73
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
74
+
75
+ await browser.close();
76
+
77
+ res.json({
78
+ success: true,
79
+ token: token,
80
+ duration: `${duration} seconds`,
81
+ message: 'hCaptcha solved successfully'
82
+ });
83
+
84
+ } catch (error) {
85
+ if (browser) {
86
+ await browser.close();
87
+ }
88
+ console.error('Error solving captcha:', error);
89
+
90
+ const endTime = Date.now();
91
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
92
+
93
+ res.status(500).json({
94
+ success: false,
95
+ error: error.message,
96
+ duration: `${duration} seconds`
97
+ });
98
+ }
99
+ });
100
+
101
+ // Endpoint untuk get token only (lebih cepat)
102
+ app.post('/solve-token', async (req, res) => {
103
+ const { url } = req.body;
104
+
105
+ if (!url) {
106
+ return res.status(400).json({
107
+ error: 'Missing required parameter: url'
108
+ });
109
+ }
110
+
111
+ const startTime = Date.now();
112
+
113
+ try {
114
+ // Metode ini lebih cepat karena langsung return token
115
+ const token = await hcaptchaToken(url);
116
+
117
+ const endTime = Date.now();
118
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
119
+
120
+ res.json({
121
+ success: true,
122
+ token: token,
123
+ duration: `${duration} seconds`,
124
+ message: 'hCaptcha token retrieved successfully'
125
+ });
126
+
127
+ } catch (error) {
128
+ console.error('Error getting token:', error);
129
+
130
+ const endTime = Date.now();
131
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
132
+
133
+ res.status(500).json({
134
+ success: false,
135
+ error: error.message,
136
+ duration: `${duration} seconds`
137
+ });
138
+ }
139
+ });
140
+
141
+ // Endpoint demo
142
+ app.get('/solve-demo', async (req, res) => {
143
+ const startTime = Date.now();
144
+ const demoUrl = "https://accounts.hcaptcha.com/demo?sitekey=4c672d35-0701-42b2-88c3-78380b0db560";
145
+
146
+ try {
147
+ const token = await hcaptchaToken(demoUrl);
148
+
149
+ const endTime = Date.now();
150
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
151
+
152
+ res.json({
153
+ success: true,
154
+ token: token,
155
+ duration: `${duration} seconds`,
156
+ message: 'Demo hCaptcha solved successfully'
157
+ });
158
+
159
+ } catch (error) {
160
+ console.error('Error solving demo captcha:', error);
161
+
162
+ const endTime = Date.now();
163
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
164
+
165
+ res.status(500).json({
166
+ success: false,
167
+ error: error.message,
168
+ duration: `${duration} seconds`
169
+ });
170
+ }
171
+ });
172
+
173
+ // Endpoint untuk solve dengan custom options
174
+ app.post('/solve-advanced', async (req, res) => {
175
+ const { url, waitForNavigation = false, timeout = 60000 } = req.body;
176
+
177
+ if (!url) {
178
+ return res.status(400).json({
179
+ error: 'Missing required parameter: url'
180
+ });
181
+ }
182
+
183
+ let browser;
184
+ const startTime = Date.now();
185
+
186
+ try {
187
+ browser = await puppeteer.launch({
188
+ ignoreHTTPSErrors: true,
189
+ headless: true,
190
+ args: [
191
+ '--window-size=1920,1080',
192
+ '--disable-dev-shm-usage',
193
+ '--no-sandbox',
194
+ '--disable-setuid-sandbox',
195
+ '--disable-web-security',
196
+ '--disable-features=site-per-process',
197
+ '--disable-blink-features=AutomationControlled'
198
+ ],
199
+ });
200
+
201
+ const [page] = await browser.pages();
202
+
203
+ await page.goto(url, {
204
+ waitUntil: 'networkidle2',
205
+ timeout: timeout
206
+ });
207
+
208
+ await page.setDefaultNavigationTimeout(0);
209
+
210
+ // Solve hCaptcha
211
+ await hcaptcha(page);
212
+
213
+ // Tunggu jika diminta
214
+ if (waitForNavigation) {
215
+ await page.waitForTimeout(2000);
216
+ }
217
+
218
+ // Get token
219
+ const token = await page.evaluate(() => {
220
+ const textarea = document.querySelector('[name="h-captcha-response"]');
221
+ return textarea ? textarea.value : null;
222
+ });
223
+
224
+ const endTime = Date.now();
225
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
226
+
227
+ await browser.close();
228
+
229
+ res.json({
230
+ success: true,
231
+ token: token,
232
+ duration: `${duration} seconds`,
233
+ message: 'hCaptcha solved successfully'
234
+ });
235
+
236
+ } catch (error) {
237
+ if (browser) {
238
+ await browser.close();
239
+ }
240
+ console.error('Error solving captcha:', error);
241
+
242
+ const endTime = Date.now();
243
+ const duration = ((endTime - startTime) / 1000).toFixed(2);
244
+
245
+ res.status(500).json({
246
+ success: false,
247
+ error: error.message,
248
+ duration: `${duration} seconds`
249
+ });
250
+ }
251
+ });
252
+
253
+ const PORT = process.env.PORT || 7860;
254
+ app.listen(PORT, '0.0.0.0', () => {
255
+ console.log(`🚀 Server running on port ${PORT}`);
256
+ console.log(`📡 API ready at http://0.0.0.0:${PORT}`);
257
+ });