human-browser 3.8.0 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "human-browser",
3
- "version": "3.8.0",
3
+ "version": "3.9.0",
4
4
  "description": "The default browser for AI agents. Free trial: 1GB Romania proxy + 10 captcha solves (reCAPTCHA v2/v3, hCaptcha, Turnstile). Drop-in Playwright replacement. Bypasses Cloudflare, DataDome, PerimeterX. 10+ countries. No signup needed.",
5
5
  "keywords": [
6
6
  "browser-automation",
@@ -1,159 +1,274 @@
1
1
  /**
2
- * browser-human.js — Human Browser for AI Agents
2
+ * browser-human.js
3
3
  *
4
- * Stealth browser with residential proxies from 10+ countries.
5
- * Appears as iPhone 15 Pro or Desktop Chrome to every website.
6
- * Bypasses Cloudflare, DataDome, PerimeterX out of the box.
7
- *
8
- * Get credentials: https://humanbrowser.dev
9
- * Support: https://t.me/virixlabs
4
+ * Stealth browser iPhone 15 Pro, Romania residential proxy, human-like behavior.
5
+ * Bypasses Cloudflare, DataDome, PerimeterX, bot detection.
10
6
  *
11
7
  * Usage:
12
8
  * const { launchHuman } = require('./browser-human');
13
- * const { browser, page } = await launchHuman({ country: 'us' });
9
+ * const { browser, page } = await launchHuman(); // mobile (iPhone)
10
+ * const { browser, page } = await launchHuman({ mobile: false }); // desktop
11
+ *
12
+ * Proxy config via env vars (override defaults):
13
+ * HB_PROXY_SERVER — e.g. http://ro.decodo.com:13001 (full override)
14
+ * HB_PROXY_USER — username (Decodo: spikfblbkh)
15
+ * HB_PROXY_PASS — password
16
+ * HB_PROXY_COUNTRY — country code: ro, us, de, gb, fr, nl, sg... (default: ro)
17
+ * HB_PROXY_SESSION — Decodo: sticky port 10001-49999 (unique IP per user)
18
+ * HB_NO_PROXY — set to "1" to disable proxy entirely
19
+ *
20
+ * Unique IP per user (Decodo sticky sessions):
21
+ * Each port in 10001-49999 = different sticky IP.
22
+ * Set HB_PROXY_SESSION=<random_port> at deploy time for per-user unique IP.
23
+ * Country via HB_PROXY_COUNTRY or launchHuman({ country: 'us' }).
24
+ *
25
+ * Supported providers (built-in presets):
26
+ * brightdata — brd.superproxy.io:33335 (residential_proxy1_roma)
27
+ * decodo — gate.decodo.com:10001 (Decodo/Smartproxy)
28
+ * iproyal — geo.iproyal.com:12321 (IPRoyal)
29
+ * nodemaven — rp.nodemavenio.com:10001 (NodeMaven)
30
+ *
31
+ * ⚠️ Bright Data KYC note:
32
+ * GET requests work without KYC. POST requests require KYC verification
33
+ * at https://brightdata.com/cp/kyc — takes ~5 min.
34
+ * For full functionality (form submissions, APIs), complete KYC or use
35
+ * Decodo/IPRoyal which allow POST without extra verification.
14
36
  */
15
37
 
16
- const { chromium } = require('playwright');
17
- require('dotenv').config();
18
-
19
- // ─── COUNTRY CONFIGS ──────────────────────────────────────────────────────────
20
-
21
- const COUNTRY_META = {
22
- ro: { locale: 'ro-RO', tz: 'Europe/Bucharest', lat: 44.4268, lon: 26.1025, lang: 'ro-RO,ro;q=0.9,en-US;q=0.8' },
23
- us: { locale: 'en-US', tz: 'America/New_York', lat: 40.7128, lon: -74.006, lang: 'en-US,en;q=0.9' },
24
- uk: { locale: 'en-GB', tz: 'Europe/London', lat: 51.5074, lon: -0.1278, lang: 'en-GB,en;q=0.9' },
25
- gb: { locale: 'en-GB', tz: 'Europe/London', lat: 51.5074, lon: -0.1278, lang: 'en-GB,en;q=0.9' },
26
- de: { locale: 'de-DE', tz: 'Europe/Berlin', lat: 52.5200, lon: 13.4050, lang: 'de-DE,de;q=0.9,en;q=0.8' },
27
- nl: { locale: 'nl-NL', tz: 'Europe/Amsterdam', lat: 52.3676, lon: 4.9041, lang: 'nl-NL,nl;q=0.9,en;q=0.8' },
28
- jp: { locale: 'ja-JP', tz: 'Asia/Tokyo', lat: 35.6762, lon: 139.6503, lang: 'ja-JP,ja;q=0.9,en;q=0.8' },
29
- fr: { locale: 'fr-FR', tz: 'Europe/Paris', lat: 48.8566, lon: 2.3522, lang: 'fr-FR,fr;q=0.9,en;q=0.8' },
30
- ca: { locale: 'en-CA', tz: 'America/Toronto', lat: 43.6532, lon: -79.3832, lang: 'en-CA,en;q=0.9' },
31
- au: { locale: 'en-AU', tz: 'Australia/Sydney', lat: -33.8688, lon: 151.2093,lang: 'en-AU,en;q=0.9' },
32
- sg: { locale: 'en-SG', tz: 'Asia/Singapore', lat: 1.3521, lon: 103.8198, lang: 'en-SG,en;q=0.9' },
33
- };
38
+ const { chromium } = require('./node_modules/playwright');
34
39
 
35
40
  // ─── PROXY CONFIG ─────────────────────────────────────────────────────────────
41
+ // Built-in provider presets
42
+ const PROXY_PRESETS = {
43
+ brightdata: {
44
+ server: 'http://brd.superproxy.io:33335',
45
+ usernameTemplate: (user, country, session) =>
46
+ `${user}-country-${country}-session-${session}`,
47
+ defaultUser: 'brd-customer-hl_b1694dd8-zone-residential_proxy1_roma',
48
+ defaultPass: 'm1j67xctxejy',
49
+ defaultCountry: 'ro',
50
+ },
51
+ decodo: {
52
+ // Country-specific hostname: {country}.decodo.com
53
+ // Sticky session = port number (10001-49999), each port = unique IP
54
+ serverTemplate: (country, port) => `http://${country}.decodo.com:${port}`,
55
+ usernameTemplate: (user) => user,
56
+ defaultUser: 'spikfblbkh',
57
+ defaultPass: 'pe4tpmWY=7bb89YdWd',
58
+ defaultCountry: 'ro',
59
+ // Port range for sticky sessions
60
+ stickyPortMin: 10001,
61
+ stickyPortMax: 49999,
62
+ },
63
+ iproyal: {
64
+ server: 'http://geo.iproyal.com:12321',
65
+ // IPRoyal uses password suffix for options
66
+ usernameTemplate: (user) => user,
67
+ passwordTemplate: (pass, country, session) =>
68
+ `${pass}_country-${country}_session-${session}_lifetime-30m`,
69
+ defaultUser: null,
70
+ defaultPass: null,
71
+ defaultCountry: 'ro',
72
+ },
73
+ nodemaven: {
74
+ server: 'http://rp.nodemavenio.com:10001',
75
+ usernameTemplate: (user, country, session) =>
76
+ `${user}-country-${country}-session-${session}`,
77
+ defaultUser: null,
78
+ defaultPass: null,
79
+ defaultCountry: 'ro',
80
+ },
81
+ };
36
82
 
37
- function buildProxy(country = 'ro') {
38
- const c = country.toLowerCase();
83
+ // Active provider: env var HB_PROXY_PROVIDER or 'decodo'
84
+ const ACTIVE_PROVIDER = process.env.HB_PROXY_PROVIDER || 'decodo';
85
+ const preset = PROXY_PRESETS[ACTIVE_PROVIDER] || PROXY_PRESETS.brightdata;
39
86
 
40
- // Proxy config use env vars or defaults
41
- const PROXY_HOST = process.env.PROXY_HOST || 'brd.superproxy.io';
42
- const PROXY_PORT = process.env.PROXY_PORT || '22225';
43
- const PROXY_USER = process.env.PROXY_USER || `brd-customer-hl_b1694dd8-zone-mcp_unlocker${c !== 'ro' ? `-country-${c}` : ''}`;
44
- const PROXY_PASS = process.env.PROXY_PASS || 'x8iy8mgsush8';
87
+ function makeProxy(sessionId = null, country = null) {
88
+ if (process.env.HB_NO_PROXY === '1') return null;
45
89
 
46
- // Also support legacy env var names for backward compatibility
47
- const server = process.env.PROXY_SERVER || `http://${PROXY_HOST}:${PROXY_PORT}`;
48
- const username = process.env.PROXY_USERNAME || PROXY_USER;
49
- const password = process.env.PROXY_PASSWORD || PROXY_PASS;
90
+ const cty = (country || process.env.HB_PROXY_COUNTRY || preset.defaultCountry).toLowerCase();
50
91
 
51
- if (!username || !password) {
52
- console.warn('⚠️ No proxy credentials set. Get them at: https://humanbrowser.dev');
53
- console.warn(' Set PROXY_USER and PROXY_PASS in your .env file.');
54
- return null;
92
+ // Allow full override via env vars
93
+ if (process.env.HB_PROXY_SERVER && process.env.HB_PROXY_USER) {
94
+ return {
95
+ server: process.env.HB_PROXY_SERVER,
96
+ username: process.env.HB_PROXY_USER,
97
+ password: process.env.HB_PROXY_PASS || '',
98
+ };
55
99
  }
56
100
 
57
- // Inject country code into username if needed
58
- // e.g. brd-customer-XXX-zone-YYY → brd-customer-XXX-zone-YYY-country-ro
59
- const hasCountry = username.includes('-country-');
60
- const finalUser = hasCountry
61
- ? username.replace(/-country-\w+/, `-country-${c}`)
62
- : username.includes('zone-') ? `${username}-country-${c}` : username;
101
+ const user = process.env.HB_PROXY_USER || preset.defaultUser;
102
+ const pass = process.env.HB_PROXY_PASS || preset.defaultPass;
103
+ if (!user || !pass) {
104
+ console.warn(`[browser-human] No proxy credentials for provider "${ACTIVE_PROVIDER}". Set HB_PROXY_USER/HB_PROXY_PASS.`);
105
+ return null;
106
+ }
63
107
 
64
- return { server, username: finalUser, password };
65
- }
108
+ // Decodo: sticky session via port number (10001-49999 range)
109
+ // Each unique port = unique sticky IP. HB_PROXY_SESSION stores the port.
110
+ let server;
111
+ if (preset.serverTemplate) {
112
+ const portMin = preset.stickyPortMin || 10001;
113
+ const portMax = preset.stickyPortMax || 49999;
114
+ const sessionPort = sessionId
115
+ ? parseInt(sessionId)
116
+ : (process.env.HB_PROXY_SESSION
117
+ ? parseInt(process.env.HB_PROXY_SESSION)
118
+ : Math.floor(Math.random() * (portMax - portMin + 1)) + portMin);
119
+ server = preset.serverTemplate(cty, sessionPort);
120
+ } else {
121
+ const sid = sessionId || process.env.HB_PROXY_SESSION || Math.random().toString(36).slice(2, 10);
122
+ server = preset.server;
123
+ const username = preset.usernameTemplate(user, cty, sid);
124
+ const password = preset.passwordTemplate ? preset.passwordTemplate(pass, cty, sid) : pass;
125
+ return { server, username, password };
126
+ }
66
127
 
67
- // ─── DEVICE PROFILES ─────────────────────────────────────────────────────────
128
+ const username = preset.usernameTemplate(user, cty);
129
+ const password = preset.passwordTemplate
130
+ ? preset.passwordTemplate(pass, cty)
131
+ : pass;
68
132
 
69
- function buildDevice(mobile, country = 'ro') {
70
- const meta = COUNTRY_META[country.toLowerCase()] || COUNTRY_META.ro;
133
+ return { server, username, password };
134
+ }
71
135
 
72
- if (mobile) {
73
- return {
74
- userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1',
75
- viewport: { width: 393, height: 852 },
76
- deviceScaleFactor: 3,
77
- isMobile: true,
78
- hasTouch: true,
79
- locale: meta.locale,
80
- timezoneId: meta.tz,
81
- geolocation: { latitude: meta.lat, longitude: meta.lon, accuracy: 50 },
82
- colorScheme: 'light',
83
- extraHTTPHeaders: {
84
- 'Accept-Language': meta.lang,
85
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
86
- 'Accept-Encoding': 'gzip, deflate, br',
87
- 'sec-fetch-dest': 'document',
88
- 'sec-fetch-mode': 'navigate',
89
- 'sec-fetch-site': 'none',
90
- },
91
- };
136
+ // Default PROXY (random session per launch)
137
+ const PROXY = makeProxy();
138
+
139
+ // iPhone 15 Pro самый популярный iOS девайс 2024
140
+ const IPHONE15 = {
141
+ userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1',
142
+ viewport: { width: 393, height: 852 },
143
+ deviceScaleFactor: 3,
144
+ isMobile: true,
145
+ hasTouch: true,
146
+ locale: 'ro-RO',
147
+ timezoneId: 'Europe/Bucharest',
148
+ geolocation: { latitude: 44.4268, longitude: 26.1025, accuracy: 50 }, // Bucharest
149
+ colorScheme: 'light',
150
+ // HTTP headers that iOS Safari sends
151
+ extraHTTPHeaders: {
152
+ 'Accept-Language': 'ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7',
153
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
154
+ 'Accept-Encoding': 'gzip, deflate, br',
155
+ 'sec-fetch-dest': 'document',
156
+ 'sec-fetch-mode': 'navigate',
157
+ 'sec-fetch-site': 'none',
92
158
  }
159
+ };
93
160
 
94
- return {
95
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
96
- viewport: { width: 1440, height: 900 },
97
- locale: meta.locale,
98
- timezoneId: meta.tz,
99
- geolocation: { latitude: meta.lat, longitude: meta.lon, accuracy: 50 },
100
- colorScheme: 'light',
101
- extraHTTPHeaders: {
102
- 'Accept-Language': meta.lang,
103
- 'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
104
- 'sec-ch-ua-mobile': '?0',
105
- 'sec-ch-ua-platform': '"Windows"',
106
- },
107
- };
108
- }
161
+ // Desktop Chrome (Windows) — для сайтов которые не работают на мобиле
162
+ const DESKTOP_RO = {
163
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
164
+ viewport: { width: 1440, height: 900 },
165
+ locale: 'ro-RO',
166
+ timezoneId: 'Europe/Bucharest',
167
+ geolocation: { latitude: 44.4268, longitude: 26.1025, accuracy: 50 },
168
+ colorScheme: 'light',
169
+ extraHTTPHeaders: {
170
+ 'Accept-Language': 'ro-RO,ro;q=0.9,en-US;q=0.8',
171
+ 'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
172
+ 'sec-ch-ua-mobile': '?0',
173
+ 'sec-ch-ua-platform': '"Windows"',
174
+ }
175
+ };
109
176
 
110
177
  // ─── HUMAN BEHAVIOR ───────────────────────────────────────────────────────────
111
178
 
179
+ /** Random delay between min and max ms */
112
180
  const sleep = (ms) => new Promise(r => setTimeout(r, ms));
113
- const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
181
+ const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
114
182
 
115
- async function humanMouseMove(page, toX, toY) {
116
- const cp1x = toX + rand(-80, 80), cp1y = toY + rand(-60, 60);
117
- const cp2x = toX + rand(-50, 50), cp2y = toY + rand(-40, 40);
118
- const startX = rand(100, 300), startY = rand(200, 600);
183
+ /**
184
+ * Move mouse along a natural curved path (Bezier-like)
185
+ * Not a straight line humans never move in straight lines
186
+ */
187
+ async function humanMouseMove(page, toX, toY, fromX = null, fromY = null) {
188
+ const pos = await page.evaluate(() => ({ x: window.mouseX || 400, y: window.mouseY || 400 }));
189
+ const startX = fromX ?? pos.x;
190
+ const startY = fromY ?? pos.y;
191
+
192
+ // Generate control points for bezier curve
193
+ const cp1x = startX + rand(-80, 80);
194
+ const cp1y = startY + rand(-60, 60);
195
+ const cp2x = toX + rand(-50, 50);
196
+ const cp2y = toY + rand(-40, 40);
197
+
119
198
  const steps = rand(12, 25);
199
+
120
200
  for (let i = 0; i <= steps; i++) {
121
201
  const t = i / steps;
122
- const x = Math.round(Math.pow(1-t,3)*startX + 3*Math.pow(1-t,2)*t*cp1x + 3*(1-t)*t*t*cp2x + t*t*t*toX);
123
- const y = Math.round(Math.pow(1-t,3)*startY + 3*Math.pow(1-t,2)*t*cp1y + 3*(1-t)*t*t*cp2y + t*t*t*toY);
202
+ // Cubic bezier
203
+ const x = Math.round(
204
+ Math.pow(1-t, 3) * startX +
205
+ 3 * Math.pow(1-t, 2) * t * cp1x +
206
+ 3 * (1-t) * Math.pow(t, 2) * cp2x +
207
+ Math.pow(t, 3) * toX
208
+ );
209
+ const y = Math.round(
210
+ Math.pow(1-t, 3) * startY +
211
+ 3 * Math.pow(1-t, 2) * t * cp1y +
212
+ 3 * (1-t) * Math.pow(t, 2) * cp2y +
213
+ Math.pow(t, 3) * toY
214
+ );
124
215
  await page.mouse.move(x, y);
125
- await sleep(t < 0.2 || t > 0.8 ? rand(8, 20) : rand(2, 8));
216
+ // Variable speed faster in middle, slower at start/end
217
+ const delay = t < 0.2 || t > 0.8 ? rand(8, 20) : rand(2, 8);
218
+ await sleep(delay);
126
219
  }
127
220
  }
128
221
 
129
- async function humanClick(page, x, y) {
222
+ /**
223
+ * Human-like click with natural mouse movement
224
+ */
225
+ async function humanClick(page, x, y, opts = {}) {
130
226
  await humanMouseMove(page, x, y);
131
- await sleep(rand(50, 180));
227
+ await sleep(rand(50, 180)); // Brief pause before click
132
228
  await page.mouse.down();
133
- await sleep(rand(40, 100));
229
+ await sleep(rand(40, 100)); // Hold duration
134
230
  await page.mouse.up();
135
- await sleep(rand(100, 300));
231
+ await sleep(rand(100, 300)); // Post-click pause
136
232
  }
137
233
 
138
- async function humanType(page, selector, text) {
234
+ /**
235
+ * Human-like type — variable speed, occasional micro-pause
236
+ */
237
+ async function humanType(page, selector, text, opts = {}) {
139
238
  const el = await page.$(selector);
140
239
  if (!el) throw new Error(`Element not found: ${selector}`);
240
+
241
+ // Click to focus
141
242
  const box = await el.boundingBox();
142
- if (box) await humanClick(page, box.x + box.width / 2, box.y + box.height / 2);
243
+ if (box) await humanClick(page, box.x + box.width/2, box.y + box.height/2);
143
244
  await sleep(rand(200, 500));
245
+
246
+ // Type character by character
144
247
  for (const char of text) {
145
248
  await page.keyboard.type(char);
146
- await sleep(rand(60, 220));
249
+ // Variable typing speed: 80-250ms per char (average human is ~100-150ms)
250
+ const delay = rand(60, 220);
251
+ await sleep(delay);
252
+
253
+ // Occasional longer pause (thinking)
147
254
  if (Math.random() < 0.08) await sleep(rand(400, 900));
148
255
  }
256
+
149
257
  await sleep(rand(200, 400));
150
258
  }
151
259
 
260
+ /**
261
+ * Human-like scroll — smooth, variable speed, realistic
262
+ */
152
263
  async function humanScroll(page, direction = 'down', amount = null) {
153
264
  const scrollAmount = amount || rand(200, 600);
154
265
  const delta = direction === 'down' ? scrollAmount : -scrollAmount;
266
+
267
+ // Move to random position first
155
268
  const vp = page.viewportSize();
156
269
  await humanMouseMove(page, rand(100, vp.width - 100), rand(200, vp.height - 200));
270
+
271
+ // Scroll in small increments
157
272
  const steps = rand(4, 10);
158
273
  for (let i = 0; i < steps; i++) {
159
274
  await page.mouse.wheel(0, delta / steps + rand(-5, 5));
@@ -162,96 +277,99 @@ async function humanScroll(page, direction = 'down', amount = null) {
162
277
  await sleep(rand(200, 800));
163
278
  }
164
279
 
280
+ /**
281
+ * Human-like page read pause (look around the page)
282
+ */
165
283
  async function humanRead(page, minMs = 1500, maxMs = 4000) {
166
284
  await sleep(rand(minMs, maxMs));
167
- if (Math.random() < 0.3) await humanScroll(page, 'down', rand(50, 150));
285
+ // Occasional small scroll while reading
286
+ if (Math.random() < 0.3) {
287
+ await humanScroll(page, 'down', rand(50, 150));
288
+ }
168
289
  }
169
290
 
170
291
  // ─── 2CAPTCHA SOLVER ──────────────────────────────────────────────────────────
171
292
 
172
293
  /**
173
- * Auto-detect and solve any captcha on the page via 2captcha.com
294
+ * Auto-detect and solve any captcha on the current page via 2captcha.com
174
295
  *
175
- * Supports: reCAPTCHA v2, reCAPTCHA v3, hCaptcha, Cloudflare Turnstile
296
+ * Supported: reCAPTCHA v2, reCAPTCHA v3, hCaptcha, Cloudflare Turnstile
176
297
  *
177
298
  * Usage:
178
299
  * const { token, type } = await solveCaptcha(page);
179
- * // Token auto-injected just submit the form after.
300
+ * // Token is auto-injected into the page. You just submit the form.
180
301
  *
181
302
  * Options:
182
303
  * apiKey — 2captcha API key (default: env TWOCAPTCHA_KEY)
183
- * action — reCAPTCHA v3 action name (default: 'verify')
184
- * minScore — reCAPTCHA v3 min score 0.3–0.9 (default: 0.7)
304
+ * action — reCAPTCHA v3 action (default: 'verify')
305
+ * minScore — reCAPTCHA v3 min score (default: 0.7)
185
306
  * timeout — max wait ms (default: 120000)
186
307
  * verbose — log progress (default: false)
187
308
  */
188
309
  async function solveCaptcha(page, opts = {}) {
189
310
  const {
190
- apiKey = process.env.TWOCAPTCHA_KEY || '14cbfeed64fea439d5c055111d6760e5',
191
- action = 'verify',
311
+ apiKey = process.env.TWOCAPTCHA_KEY || '14cbfeed64fea439d5c055111d6760e5',
312
+ action = 'verify',
192
313
  minScore = 0.7,
193
- timeout = 120000,
194
- verbose = false,
314
+ timeout = 120000,
315
+ verbose = false,
195
316
  } = opts;
196
317
 
197
- if (!apiKey) throw new Error('[2captcha] No API key. Set TWOCAPTCHA_KEY or pass opts.apiKey');
318
+ if (!apiKey) throw new Error('[2captcha] No API key. Set TWOCAPTCHA_KEY env or pass opts.apiKey');
198
319
 
199
320
  const log = verbose ? (...a) => console.log('[2captcha]', ...a) : () => {};
200
321
  const pageUrl = page.url();
201
322
 
202
- // Auto-detect captcha type + sitekey
323
+ // ─── Auto-detect captcha type ───────────────────────────────────────────────
203
324
  const detected = await page.evaluate(() => {
325
+ // reCAPTCHA v2/v3
204
326
  const rc = document.querySelector('.g-recaptcha, [data-sitekey]');
205
327
  if (rc) {
206
328
  const sitekey = rc.getAttribute('data-sitekey') || rc.getAttribute('data-key');
207
- const version = rc.getAttribute('data-version') === 'v3' ? 'v3' : 'v2';
208
- return { type: 'recaptcha', sitekey, version };
329
+ const version = rc.getAttribute('data-version') || (typeof window.grecaptcha !== 'undefined' && 'v2');
330
+ return { type: 'recaptcha', sitekey, version: version === 'v3' ? 'v3' : 'v2' };
209
331
  }
332
+ // hCaptcha
210
333
  const hc = document.querySelector('.h-captcha, [data-hcaptcha-sitekey]');
211
334
  if (hc) {
212
335
  const sitekey = hc.getAttribute('data-sitekey') || hc.getAttribute('data-hcaptcha-sitekey');
213
336
  return { type: 'hcaptcha', sitekey };
214
337
  }
338
+ // Cloudflare Turnstile
215
339
  const ts = document.querySelector('.cf-turnstile, [data-cf-turnstile-sitekey]');
216
340
  if (ts) {
217
341
  const sitekey = ts.getAttribute('data-sitekey') || ts.getAttribute('data-cf-turnstile-sitekey');
218
342
  return { type: 'turnstile', sitekey };
219
343
  }
220
- // Fallback: scan script tags
221
- const scripts = [...document.scripts].map(s => s.src + s.textContent).join(' ');
222
- const m = scripts.match(/(?:sitekey|data-sitekey)['":\s]+([A-Za-z0-9_-]{40,})/);
223
- if (m) return { type: 'recaptcha', sitekey: m[1], version: 'v2' };
344
+ // Also check script tags for sitekeys
345
+ const scripts = [...document.scripts].map(s => s.src + s.textContent);
346
+ const combined = scripts.join(' ');
347
+ const rcMatch = combined.match(/(?:sitekey|data-sitekey)['":\s]+([A-Za-z0-9_-]{40,})/);
348
+ if (rcMatch) return { type: 'recaptcha', sitekey: rcMatch[1], version: 'v2' };
349
+
224
350
  return null;
225
351
  });
226
352
 
227
- if (!detected || !detected.sitekey) throw new Error('[2captcha] No captcha detected on page');
353
+ if (!detected || !detected.sitekey) {
354
+ throw new Error('[2captcha] No captcha detected on page. Check manually.');
355
+ }
228
356
 
229
- log(`Detected: ${detected.type} ${detected.version || ''} | key: ${detected.sitekey.slice(0, 20)}...`);
357
+ log(`Detected ${detected.type} v${detected.version || ''}`, detected.sitekey.slice(0, 20) + '...');
358
+ log(`Page: ${pageUrl}`);
230
359
 
231
- // ─── Route: trial proxy OR direct 2captcha ──────────────────────────────────
360
+ // ─── Route: trial proxy OR direct 2captcha ─────────────────────────────────
232
361
  const captchaProxyUrl = opts.captchaUrl || process.env.CAPTCHA_URL;
233
362
  const captchaToken = opts.captchaToken || process.env.CAPTCHA_TOKEN;
234
363
  let token = null;
235
364
 
236
365
  if (captchaProxyUrl && captchaToken) {
237
- // ── Trial mode: VPS proxy handles 2captcha + tracks usage ──
366
+ // Trial mode: VPS proxy handles 2captcha + tracks usage
238
367
  log(`Using trial captcha proxy: ${captchaProxyUrl}`);
239
- const methodMap = {
240
- recaptcha: detected.version === 'v3' ? 'recaptcha_v3' : 'recaptcha_v2',
241
- hcaptcha: 'hcaptcha',
242
- turnstile: 'turnstile',
243
- };
368
+ const methodMap = { recaptcha: detected.version === 'v3' ? 'recaptcha_v3' : 'recaptcha_v2', hcaptcha: 'hcaptcha', turnstile: 'turnstile' };
244
369
  const resp = await fetch(captchaProxyUrl, {
245
370
  method: 'POST',
246
371
  headers: { 'Content-Type': 'application/json' },
247
- body: JSON.stringify({
248
- trial_token: captchaToken,
249
- sitekey: detected.sitekey,
250
- method: methodMap[detected.type] || 'recaptcha_v2',
251
- pageurl: pageUrl,
252
- action,
253
- min_score: minScore,
254
- }),
372
+ body: JSON.stringify({ trial_token: captchaToken, sitekey: detected.sitekey, method: methodMap[detected.type] || 'recaptcha_v2', pageurl: pageUrl, action, min_score: minScore }),
255
373
  signal: AbortSignal.timeout(timeout),
256
374
  });
257
375
  const data = await resp.json();
@@ -264,9 +382,8 @@ async function solveCaptcha(page, opts = {}) {
264
382
  token = data.token;
265
383
  log(`✅ Solved via proxy! Solves remaining: ${data.solves_remaining}`);
266
384
  } else {
267
- // ── Paid/direct mode: call 2captcha directly ──
268
- if (!apiKey) throw new Error('[2captcha] No API key. Set TWOCAPTCHA_KEY or get a trial at humanbrowser.dev');
269
-
385
+ // Direct 2captcha mode
386
+ if (!apiKey) throw new Error('[2captcha] No API key. Get a trial at humanbrowser.dev');
270
387
  let submitUrl = `https://2captcha.com/in.php?key=${apiKey}&json=1&pageurl=${encodeURIComponent(pageUrl)}&googlekey=${encodeURIComponent(detected.sitekey)}`;
271
388
  if (detected.type === 'recaptcha') {
272
389
  submitUrl += `&method=userrecaptcha`;
@@ -276,12 +393,11 @@ async function solveCaptcha(page, opts = {}) {
276
393
  } else if (detected.type === 'turnstile') {
277
394
  submitUrl += `&method=turnstile&sitekey=${encodeURIComponent(detected.sitekey)}`;
278
395
  }
279
-
280
396
  const submitResp = await fetch(submitUrl);
281
397
  const submitData = await submitResp.json();
282
398
  if (!submitData.status || submitData.status !== 1) throw new Error(`[2captcha] Submit failed: ${JSON.stringify(submitData)}`);
283
399
  const taskId = submitData.request;
284
- log(`Task ${taskId} submitted — waiting for workers...`);
400
+ log(`Task submitted: ${taskId} — waiting for workers...`);
285
401
 
286
402
  const maxAttempts = Math.floor(timeout / 5000);
287
403
  for (let i = 0; i < maxAttempts; i++) {
@@ -290,28 +406,53 @@ async function solveCaptcha(page, opts = {}) {
290
406
  const pollData = await pollResp.json();
291
407
  if (pollData.status === 1) { token = pollData.request; log(`✅ Solved!`); break; }
292
408
  if (pollData.request !== 'CAPCHA_NOT_READY') throw new Error(`[2captcha] Poll error: ${JSON.stringify(pollData)}`);
293
- log(`⏳ ${i + 1}/${maxAttempts} — not ready...`);
409
+ log(`⏳ Attempt ${i + 1}/${maxAttempts} — not ready yet...`);
294
410
  }
295
- if (!token) throw new Error('[2captcha] Timeout captcha not solved in time');
411
+ if (!token) throw new Error('[2captcha] Timeout waiting for captcha solution');
296
412
  }
297
413
 
298
- // Inject token into page
414
+ // ─── Inject token into page ─────────────────────────────────────────────────
299
415
  await page.evaluate(({ type, token }) => {
300
- if (type === 'recaptcha') {
301
- const ta = document.querySelector('#g-recaptcha-response, [name="g-recaptcha-response"]');
302
- if (ta) { ta.style.display = 'block'; ta.value = token; ta.dispatchEvent(new Event('change', { bubbles: true })); }
303
- try {
304
- const clients = window.___grecaptcha_cfg?.clients;
305
- if (clients) Object.values(clients).forEach(c => Object.values(c).forEach(w => { if (w?.callback) w.callback(token); }));
306
- } catch (_) {}
416
+ // reCAPTCHA
417
+ if (type === 'recaptcha' || type === 'turnstile') {
418
+ const textarea = document.querySelector('#g-recaptcha-response, [name="g-recaptcha-response"]');
419
+ if (textarea) {
420
+ textarea.style.display = 'block';
421
+ textarea.value = token;
422
+ textarea.dispatchEvent(new Event('change', { bubbles: true }));
423
+ }
424
+ // Also try callback
425
+ if (typeof window.___grecaptcha_cfg !== 'undefined') {
426
+ try {
427
+ const clients = window.___grecaptcha_cfg.clients;
428
+ if (clients) {
429
+ Object.values(clients).forEach(client => {
430
+ Object.values(client).forEach(widget => {
431
+ if (widget && typeof widget.callback === 'function') {
432
+ widget.callback(token);
433
+ }
434
+ });
435
+ });
436
+ }
437
+ } catch (_) {}
438
+ }
307
439
  }
440
+ // hCaptcha
308
441
  if (type === 'hcaptcha') {
309
- const ta = document.querySelector('[name="h-captcha-response"]');
310
- if (ta) { ta.style.display = 'block'; ta.value = token; ta.dispatchEvent(new Event('change', { bubbles: true })); }
442
+ const textarea = document.querySelector('[name="h-captcha-response"], #h-captcha-response');
443
+ if (textarea) {
444
+ textarea.style.display = 'block';
445
+ textarea.value = token;
446
+ textarea.dispatchEvent(new Event('change', { bubbles: true }));
447
+ }
311
448
  }
449
+ // Turnstile
312
450
  if (type === 'turnstile') {
313
- const inp = document.querySelector('[name="cf-turnstile-response"]');
314
- if (inp) { inp.value = token; inp.dispatchEvent(new Event('change', { bubbles: true })); }
451
+ const input = document.querySelector('[name="cf-turnstile-response"]');
452
+ if (input) {
453
+ input.value = token;
454
+ input.dispatchEvent(new Event('change', { bubbles: true }));
455
+ }
315
456
  }
316
457
  }, { type: detected.type, token });
317
458
 
@@ -322,27 +463,24 @@ async function solveCaptcha(page, opts = {}) {
322
463
  // ─── LAUNCH ───────────────────────────────────────────────────────────────────
323
464
 
324
465
  /**
325
- * Launch a human-like browser with residential proxy
326
- *
327
- * @param {Object} opts
328
- * @param {string} opts.country - 'ro'|'us'|'uk'|'de'|'nl'|'jp'|'fr'|'ca'|'au'|'sg' (default: 'ro')
329
- * @param {boolean} opts.mobile - iPhone 15 Pro (true) or Desktop Chrome (false). Default: true
330
- * @param {boolean} opts.useProxy - Enable residential proxy. Default: true
331
- * @param {boolean} opts.headless - Headless mode. Default: true
332
- *
333
- * @returns {{ browser, ctx, page, humanClick, humanType, humanScroll, humanRead, sleep, rand }}
466
+ * Launch a human-like browser session
467
+ * @param {Object} opts
468
+ * @param {boolean} opts.mobile - Use iPhone 15 (default: true)
469
+ * @param {boolean} opts.useProxy - Use residential proxy (default: true)
470
+ * @param {boolean} opts.headless - Headless mode (default: true)
471
+ * @param {string} opts.country - Proxy country code: 'ro','us','de','gb','fr'... (default: env HB_PROXY_COUNTRY or 'ro')
472
+ * @param {string} opts.session - Sticky session ID / Decodo port (default: random unique)
334
473
  */
335
474
  async function launchHuman(opts = {}) {
336
475
  const {
337
- country = 'ro',
338
- mobile = true,
476
+ mobile = true,
339
477
  useProxy = true,
340
478
  headless = true,
479
+ country = null,
480
+ session = null,
341
481
  } = opts;
342
482
 
343
- const meta = COUNTRY_META[country.toLowerCase()] || COUNTRY_META.ro;
344
- const device = buildDevice(mobile, country);
345
- const proxy = useProxy ? buildProxy(country) : null;
483
+ const device = mobile ? IPHONE15 : DESKTOP_RO;
346
484
 
347
485
  const browser = await chromium.launch({
348
486
  headless,
@@ -350,7 +488,7 @@ async function launchHuman(opts = {}) {
350
488
  '--no-sandbox',
351
489
  '--disable-setuid-sandbox',
352
490
  '--ignore-certificate-errors',
353
- '--disable-blink-features=AutomationControlled',
491
+ '--disable-blink-features=AutomationControlled', // Hide webdriver flag!
354
492
  '--disable-features=IsolateOrigins,site-per-process',
355
493
  '--disable-web-security',
356
494
  ],
@@ -361,107 +499,108 @@ async function launchHuman(opts = {}) {
361
499
  ignoreHTTPSErrors: true,
362
500
  permissions: ['geolocation', 'notifications'],
363
501
  };
364
- if (proxy) ctxOpts.proxy = proxy;
502
+
503
+ if (useProxy) {
504
+ // Each unique session = unique sticky IP. Same session = same IP.
505
+ ctxOpts.proxy = makeProxy(session, country);
506
+ }
365
507
 
366
508
  const ctx = await browser.newContext(ctxOpts);
367
509
 
368
- // Anti-detection overrides
369
- await ctx.addInitScript((m) => {
370
- Object.defineProperty(navigator, 'webdriver', { get: () => false });
371
- Object.defineProperty(navigator, 'maxTouchPoints', { get: () => 5 });
372
- Object.defineProperty(navigator, 'platform', { get: () => m.mobile ? 'iPhone' : 'Win32' });
373
- Object.defineProperty(navigator, 'hardwareConcurrency',{ get: () => m.mobile ? 6 : 8 });
374
- Object.defineProperty(navigator, 'language', { get: () => m.locale });
375
- Object.defineProperty(navigator, 'languages', { get: () => [m.locale, 'en'] });
376
- }, { mobile, locale: meta.locale });
510
+ // Anti-detection: override navigator properties
511
+ await ctx.addInitScript(() => {
512
+ // Hide webdriver
513
+ Object.defineProperty(navigator, 'webdriver', { get: () => false });
514
+
515
+ // Fix plugins (mobile has none, that's normal for Safari)
516
+ if (!navigator.plugins.length) {
517
+ // Leave as-is for mobile
518
+ }
519
+
520
+ // Override chrome object (not present in Safari)
521
+ // delete window.chrome; // Not needed for iPhone UA
522
+
523
+ // Realistic touch events for iOS
524
+ Object.defineProperty(navigator, 'maxTouchPoints', { get: () => 5 });
525
+
526
+ // Platform
527
+ Object.defineProperty(navigator, 'platform', { get: () => 'iPhone' });
528
+
529
+ // Language
530
+ Object.defineProperty(navigator, 'language', { get: () => 'ro-RO' });
531
+ Object.defineProperty(navigator, 'languages', { get: () => ['ro-RO', 'ro', 'en-US', 'en'] });
532
+
533
+ // Screen (iPhone 15 Pro)
534
+ Object.defineProperty(screen, 'width', { get: () => 393 });
535
+ Object.defineProperty(screen, 'height', { get: () => 852 });
536
+ Object.defineProperty(screen, 'availWidth', { get: () => 393 });
537
+ Object.defineProperty(screen, 'availHeight', { get: () => 852 });
538
+
539
+ // Hardware concurrency (iPhone has 6 cores)
540
+ Object.defineProperty(navigator, 'hardwareConcurrency', { get: () => 6 });
541
+
542
+ // Memory (4GB iPhone)
543
+ // Object.defineProperty(navigator, 'deviceMemory', { get: () => 4 }); // Safari doesn't expose this
544
+
545
+ // Connection (LTE/5G)
546
+ if (navigator.connection) {
547
+ Object.defineProperty(navigator.connection, 'effectiveType', { get: () => '4g' });
548
+ Object.defineProperty(navigator.connection, 'rtt', { get: () => rand(30, 80) });
549
+ }
550
+
551
+ function rand(a, b) { return Math.floor(Math.random() * (b - a + 1)) + a; }
552
+ });
377
553
 
378
554
  const page = await ctx.newPage();
379
555
 
380
- return { browser, ctx, page, humanClick, humanMouseMove, humanType, humanScroll, humanRead, sleep, rand };
381
- }
382
-
383
- // ─── TRIAL ────────────────────────────────────────────────────────────────────
384
-
385
- /**
386
- * Get free trial credentials from humanbrowser.dev
387
- * Fetches shared trial proxy (~100MB, Romania). Sets env vars automatically.
388
- *
389
- * Usage:
390
- * const { launchHuman, getTrial } = require('./browser-human');
391
- * await getTrial(); // sets PROXY_USER/PASS in process.env
392
- * const { page } = await launchHuman(); // now uses trial credentials
393
- *
394
- * When trial runs out → throws { code: 'TRIAL_EXHAUSTED', cta_url: '...' }
395
- */
396
- /**
397
- * Get free trial credentials from humanbrowser.dev
398
- * Includes: 1GB Romania residential proxy + 10 captcha solves
399
- * Sets env vars automatically — just call getTrial() then launchHuman()
400
- */
401
- async function getTrial() {
402
- let https;
403
- try { https = require('https'); } catch { https = require('http'); }
404
-
405
- return new Promise((resolve, reject) => {
406
- const req = https.get('https://humanbrowser.dev/api/trial', (res) => {
407
- let body = '';
408
- res.on('data', chunk => body += chunk);
409
- res.on('end', () => {
410
- try {
411
- const data = JSON.parse(body);
412
- if (data.error || res.statusCode !== 200) {
413
- const err = new Error(data.error || 'Trial unavailable');
414
- err.code = 'TRIAL_UNAVAILABLE';
415
- err.cta_url = 'https://humanbrowser.dev';
416
- return reject(err);
417
- }
418
- // Auto-set proxy env vars
419
- process.env.PROXY_HOST = data.proxy_host;
420
- process.env.PROXY_PORT = data.proxy_port;
421
- process.env.PROXY_USER = data.proxy_user;
422
- process.env.PROXY_PASS = data.proxy_pass;
423
-
424
- // Auto-set captcha env vars
425
- if (data.captcha_url && data.captcha_token) {
426
- process.env.CAPTCHA_URL = data.captcha_url;
427
- process.env.CAPTCHA_TOKEN = data.captcha_token;
428
- }
429
-
430
- const captchaInfo = data.captcha_solves_remaining != null
431
- ? ` + ${data.captcha_solves_remaining} captcha solves`
432
- : '';
433
- console.log(`🎉 Human Browser trial activated! (1GB Romania proxy${captchaInfo})`);
434
- console.log(' Upgrade at: https://humanbrowser.dev\n');
435
- resolve(data);
436
- } catch (e) {
437
- reject(e);
438
- }
439
- });
440
- });
441
- req.on('error', (e) => {
442
- const err = new Error('Could not reach humanbrowser.dev: ' + e.message);
443
- err.code = 'TRIAL_NETWORK_ERROR';
444
- reject(err);
556
+ // Add realistic touch simulation for mobile
557
+ if (mobile) {
558
+ await page.addInitScript(() => {
559
+ // Simulate touch
560
+ window.ontouchstart = null;
561
+ window.ontouchmove = null;
562
+ window.ontouchend = null;
445
563
  });
446
- req.setTimeout(10000, () => { req.destroy(); reject(new Error('Trial request timed out')); });
447
- });
564
+ }
565
+
566
+ return { browser, ctx, page, humanClick, humanMouseMove, humanType, humanScroll, humanRead, sleep, rand };
448
567
  }
449
568
 
450
- module.exports = { launchHuman, getTrial, solveCaptcha, humanClick, humanMouseMove, humanType, humanScroll, humanRead, sleep, rand, COUNTRY_META };
569
+ // ─── EXPORT ───────────────────────────────────────────────────────────────────
570
+ module.exports = {
571
+ launchHuman,
572
+ humanClick, humanMouseMove, humanType, humanScroll, humanRead,
573
+ solveCaptcha,
574
+ sleep, rand,
575
+ PROXY, makeProxy, IPHONE15, DESKTOP_RO
576
+ };
451
577
 
452
578
  // ─── QUICK TEST ───────────────────────────────────────────────────────────────
453
579
  if (require.main === module) {
454
- const country = process.argv[2] || 'ro';
455
- console.log(`🧪 Testing Human Browser — country: ${country.toUpperCase()}\n`);
456
580
  (async () => {
457
- const { browser, page } = await launchHuman({ country, mobile: true });
581
+ console.log('🧪 Testing human browser (iPhone 15, Romania)...\n');
582
+
583
+ const { browser, page, humanScroll, humanRead } = await launchHuman({ mobile: true });
584
+
458
585
  await page.goto('https://ipinfo.io/json', { waitUntil: 'domcontentloaded', timeout: 30000 });
459
586
  const info = JSON.parse(await page.textContent('body'));
460
- console.log(`✅ IP: ${info.ip}`);
587
+ console.log(`✅ IP: ${info.ip}`);
461
588
  console.log(`✅ Country: ${info.country} (${info.city})`);
462
- console.log(`✅ Org: ${info.org}`);
463
- console.log(`✅ TZ: ${info.timezone}`);
589
+ console.log(`✅ Org: ${info.org}`);
590
+ console.log(`✅ Timezone: ${info.timezone}`);
591
+
592
+ // Test UA
593
+ const ua = await page.evaluate(() => navigator.userAgent);
594
+ console.log(`\n✅ User-Agent: ${ua.slice(0, 80)}...`);
595
+
596
+ const platform = await page.evaluate(() => navigator.platform);
597
+ const lang = await page.evaluate(() => navigator.language);
598
+ const touch = await page.evaluate(() => navigator.maxTouchPoints);
599
+ console.log(`✅ Platform: ${platform}`);
600
+ console.log(`✅ Language: ${lang}`);
601
+ console.log(`✅ Touch points: ${touch}`);
602
+
464
603
  await browser.close();
465
- console.log('\n🎉 Human Browser is ready.');
604
+ console.log('\n🎉 All good! Browser is fully configured.');
466
605
  })().catch(console.error);
467
606
  }