-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathper_context_proxy.js
More file actions
137 lines (115 loc) · 4.56 KB
/
per_context_proxy.js
File metadata and controls
137 lines (115 loc) · 4.56 KB
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
/**
* ⚠️ PRIVACY RESEARCH USE ONLY
* Run exclusively in authorized privacy research labs that comply with all applicable laws.
* See: https://github.com/botswin/BotBrowser/blob/main/DISCLAIMER.md
*/
/**
* BotBrowser Per-Context Proxy Example
*
* This example demonstrates how to use different proxies for each browser context
* while maintaining automatic geo-detection for timezone, locale, and languages.
*
* Key Features:
* - Multiple contexts with different proxies in a single browser instance
* - Automatic timezone/locale detection per proxy
* - Cost-effective alternative to launching multiple browser processes
*/
const { chromium } = require('playwright');
(async () => {
// Launch BotBrowser with profile but no global proxy
const browser = await chromium.launch({
headless: false, // Set to true for headless mode
executablePath: process.env.BOTBROWSER_EXEC_PATH || '/path/to/chrome', // Update this path
args: [
`--bot-profile=${process.env.BOT_PROFILE_PATH || '/absolute/path/to/profile.enc'}`, // Update this path
'--user-data-dir=' + require('os').tmpdir() + '/botbrowser-' + Date.now(),
],
});
console.log('🚀 Browser launched successfully');
try {
// Context 1: US Proxy
console.log('\n📍 Creating Context 1 with US proxy...');
const context1 = await browser.newContext({
proxy: {
server: 'http://username:password@us-proxy.example.com:8080' // Replace with your US proxy
}
});
// Context 2: EU Proxy
console.log('📍 Creating Context 2 with EU proxy...');
const context2 = await browser.newContext({
proxy: {
server: 'socks5://username:password@eu-proxy.example.com:1080' // Replace with your EU proxy
}
});
// Context 3: APAC Proxy
console.log('📍 Creating Context 3 with APAC proxy...');
const context3 = await browser.newContext({
proxy: {
server: 'http://username:password@apac-proxy.example.com:8080' // Replace with your APAC proxy
}
});
// Test each context
await testContext(context1, 'US Context', 'https://httpbin.org/ip');
await testContext(context2, 'EU Context', 'https://httpbin.org/ip');
await testContext(context3, 'APAC Context', 'https://httpbin.org/ip');
// Demonstrate timezone detection
console.log('\n🌍 Testing automatic timezone detection...');
await testTimezone(context1, 'US Context');
await testTimezone(context2, 'EU Context');
await testTimezone(context3, 'APAC Context');
} catch (error) {
console.error('❌ Error:', error.message);
} finally {
console.log('\n🔄 Closing browser...');
await browser.close();
console.log('✅ Done!');
}
})();
async function testContext(context, label, testUrl) {
console.log(`\n🧪 Testing ${label}:`);
const page = await context.newPage();
// Remove Playwright bindings to maintain consistent fingerprint
await page.addInitScript(() => {
delete window.__playwright_binding__;
delete window.__pwInitScripts;
});
try {
await page.goto(testUrl, { waitUntil: 'networkidle' });
// Get IP information
const ipInfo = await page.textContent('pre');
console.log(` 📡 IP Response: ${ipInfo.trim()}`);
// Get browser timezone (automatically set by BotBrowser based on proxy IP)
const timezone = await page.evaluate(() => Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log(` 🕐 Detected Timezone: ${timezone}`);
// Get browser language (automatically set by BotBrowser based on proxy IP)
const language = await page.evaluate(() => navigator.language);
console.log(` 🌐 Detected Language: ${language}`);
} catch (error) {
console.log(` ❌ Error testing ${label}: ${error.message}`);
} finally {
await page.close();
}
}
async function testTimezone(context, label) {
const page = await context.newPage();
try {
// Test timezone consistency
const timezoneInfo = await page.evaluate(() => {
const now = new Date();
return {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
offset: now.getTimezoneOffset(),
localTime: now.toLocaleString(),
utcTime: now.toUTCString()
};
});
console.log(` 🕐 ${label} Timezone Details:`);
console.log(` Timezone: ${timezoneInfo.timezone}`);
console.log(` UTC Offset: ${timezoneInfo.offset} minutes`);
console.log(` Local Time: ${timezoneInfo.localTime}`);
} catch (error) {
console.log(` ❌ Error getting timezone for ${label}: ${error.message}`);
} finally {
await page.close();
}
}