n8n-nodes-nvk-browser 1.0.75 → 1.0.76
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/dist/utils/BrowserManager.js +100 -35
- package/package.json +1 -1
|
@@ -70,20 +70,17 @@ class BrowserManager {
|
|
|
70
70
|
const profilePath = this.profilesManager.getProfilePath(profileId);
|
|
71
71
|
// Set profile name trong Chrome preferences
|
|
72
72
|
this.setProfileName(profilePath, profile.name);
|
|
73
|
-
//
|
|
74
|
-
|
|
73
|
+
// Parse proxy config trước (sẽ dùng cho CDP authentication)
|
|
74
|
+
let proxyConfig = null;
|
|
75
75
|
if (profile.proxy) {
|
|
76
|
-
|
|
76
|
+
proxyConfig = ProxyHandler_1.ProxyHandler.parseProxyString(profile.proxy);
|
|
77
77
|
if (proxyConfig) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
console.error(`[BrowserManager] ERROR saving proxy authentication:`, error);
|
|
85
|
-
// Không throw - vẫn cho phép browser khởi động
|
|
86
|
-
}
|
|
78
|
+
console.log(`[BrowserManager] Proxy config parsed:`, {
|
|
79
|
+
type: proxyConfig.type,
|
|
80
|
+
host: proxyConfig.host,
|
|
81
|
+
port: proxyConfig.port,
|
|
82
|
+
hasAuth: !!(proxyConfig.username && proxyConfig.password),
|
|
83
|
+
});
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
86
|
// Chuẩn bị Chrome arguments
|
|
@@ -95,12 +92,9 @@ class BrowserManager {
|
|
|
95
92
|
'--no-sandbox',
|
|
96
93
|
'--disable-setuid-sandbox',
|
|
97
94
|
];
|
|
98
|
-
// Proxy configuration
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
if (proxyConfig) {
|
|
102
|
-
args.push(...ProxyHandler_1.ProxyHandler.getChromeProxyArgs(proxyConfig));
|
|
103
|
-
}
|
|
95
|
+
// Proxy configuration - KHÔNG bao gồm credentials trong URL
|
|
96
|
+
if (proxyConfig) {
|
|
97
|
+
args.push(...ProxyHandler_1.ProxyHandler.getChromeProxyArgs(proxyConfig));
|
|
104
98
|
}
|
|
105
99
|
// Extension configuration
|
|
106
100
|
if (profile.extensions && profile.extensions.length > 0) {
|
|
@@ -116,6 +110,7 @@ class BrowserManager {
|
|
|
116
110
|
}
|
|
117
111
|
}
|
|
118
112
|
// Khởi động Chrome
|
|
113
|
+
console.log(`[BrowserManager] Launching browser for profile ${profileId}...`);
|
|
119
114
|
const browser = await puppeteer_core_1.default.launch({
|
|
120
115
|
executablePath: this.browserPath,
|
|
121
116
|
args,
|
|
@@ -127,29 +122,69 @@ class BrowserManager {
|
|
|
127
122
|
}
|
|
128
123
|
: null,
|
|
129
124
|
});
|
|
130
|
-
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
125
|
+
console.log(`[BrowserManager] Browser launched successfully`);
|
|
126
|
+
// ==========================================
|
|
127
|
+
// GIẢI PHÁP 1: CDP PROXY AUTHENTICATION
|
|
128
|
+
// ==========================================
|
|
129
|
+
if (proxyConfig && proxyConfig.username && proxyConfig.password) {
|
|
130
|
+
console.log(`[BrowserManager] Setting up CDP proxy authentication...`);
|
|
131
|
+
try {
|
|
132
|
+
// Lấy tất cả pages hiện tại
|
|
133
|
+
const pages = await browser.pages();
|
|
134
|
+
console.log(`[BrowserManager] Found ${pages.length} initial page(s)`);
|
|
135
|
+
// Setup authentication cho mỗi page hiện tại
|
|
136
|
+
for (const page of pages) {
|
|
137
|
+
try {
|
|
138
|
+
await page.authenticate({
|
|
139
|
+
username: proxyConfig.username,
|
|
140
|
+
password: proxyConfig.password,
|
|
141
|
+
});
|
|
142
|
+
console.log(`[BrowserManager] ✓ Authenticated page: ${page.url()}`);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
console.error(`[BrowserManager] ✗ Failed to authenticate page:`, error);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Setup listener cho các page/tab mới được tạo trong tương lai
|
|
149
|
+
browser.on('targetcreated', async (target) => {
|
|
140
150
|
try {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
151
|
+
// Chỉ xử lý các target type là 'page' (bỏ qua background_page, service_worker, etc.)
|
|
152
|
+
if (target.type() === 'page') {
|
|
153
|
+
const newPage = await target.page();
|
|
154
|
+
if (newPage && proxyConfig) {
|
|
155
|
+
await newPage.authenticate({
|
|
156
|
+
username: proxyConfig.username,
|
|
157
|
+
password: proxyConfig.password,
|
|
158
|
+
});
|
|
159
|
+
console.log(`[BrowserManager] ✓ Authenticated new page: ${newPage.url()}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
144
162
|
}
|
|
145
163
|
catch (error) {
|
|
146
|
-
|
|
164
|
+
// Ignore errors cho pages đã đóng hoặc không thể access
|
|
165
|
+
// Điều này bình thường với một số internal pages
|
|
166
|
+
if (error instanceof Error && !error.message.includes('Target closed')) {
|
|
167
|
+
console.warn(`[BrowserManager] Warning: Could not authenticate new target:`, error.message);
|
|
168
|
+
}
|
|
147
169
|
}
|
|
148
|
-
}
|
|
170
|
+
});
|
|
171
|
+
console.log(`[BrowserManager] ✓ CDP proxy authentication setup completed`);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
console.error(`[BrowserManager] ✗ ERROR setting up CDP proxy authentication:`, error);
|
|
175
|
+
// Không throw - vẫn cho phép browser chạy
|
|
176
|
+
// User có thể phải nhập credentials thủ công nếu CDP fails
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
if (profile.proxy) {
|
|
181
|
+
console.log(`[BrowserManager] Proxy configured but no authentication required`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
console.log(`[BrowserManager] No proxy configured`);
|
|
149
185
|
}
|
|
150
186
|
}
|
|
151
187
|
// Lấy debug port từ browser
|
|
152
|
-
const targets = browser.targets();
|
|
153
188
|
const debugPort = await this.getDebugPort(browser);
|
|
154
189
|
// Tạo instance
|
|
155
190
|
const instance = {
|
|
@@ -165,6 +200,7 @@ class BrowserManager {
|
|
|
165
200
|
if (windowConfig?.position && !windowConfig.headless) {
|
|
166
201
|
await this.setWindowPosition(browser, windowConfig.position);
|
|
167
202
|
}
|
|
203
|
+
console.log(`[BrowserManager] Profile ${profileId} started successfully`);
|
|
168
204
|
return instance;
|
|
169
205
|
}
|
|
170
206
|
async stopProfile(profileId) {
|
|
@@ -175,9 +211,11 @@ class BrowserManager {
|
|
|
175
211
|
try {
|
|
176
212
|
await instance.browser.close();
|
|
177
213
|
this.instances.delete(profileId);
|
|
214
|
+
console.log(`[BrowserManager] Profile ${profileId} stopped`);
|
|
178
215
|
return true;
|
|
179
216
|
}
|
|
180
217
|
catch (error) {
|
|
218
|
+
console.error(`[BrowserManager] Error stopping profile ${profileId}:`, error);
|
|
181
219
|
this.instances.delete(profileId);
|
|
182
220
|
return false;
|
|
183
221
|
}
|
|
@@ -225,7 +263,16 @@ class BrowserManager {
|
|
|
225
263
|
}
|
|
226
264
|
// Kiểm tra cache
|
|
227
265
|
if (instance.pages.has(tabIndex)) {
|
|
228
|
-
|
|
266
|
+
const cachedPage = instance.pages.get(tabIndex);
|
|
267
|
+
// Verify page is still valid
|
|
268
|
+
try {
|
|
269
|
+
await cachedPage.browser();
|
|
270
|
+
return cachedPage;
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
// Page is closed, remove from cache
|
|
274
|
+
instance.pages.delete(tabIndex);
|
|
275
|
+
}
|
|
229
276
|
}
|
|
230
277
|
// Lấy pages từ browser
|
|
231
278
|
const pages = await instance.browser.pages();
|
|
@@ -237,6 +284,23 @@ class BrowserManager {
|
|
|
237
284
|
// Tạo page mới nếu không có
|
|
238
285
|
const newPage = await instance.browser.newPage();
|
|
239
286
|
instance.pages.set(tabIndex, newPage);
|
|
287
|
+
// Setup proxy authentication cho page mới (nếu có)
|
|
288
|
+
const profile = this.profilesManager.getProfile(profileId);
|
|
289
|
+
if (profile?.proxy) {
|
|
290
|
+
const proxyConfig = ProxyHandler_1.ProxyHandler.parseProxyString(profile.proxy);
|
|
291
|
+
if (proxyConfig && proxyConfig.username && proxyConfig.password) {
|
|
292
|
+
try {
|
|
293
|
+
await newPage.authenticate({
|
|
294
|
+
username: proxyConfig.username,
|
|
295
|
+
password: proxyConfig.password,
|
|
296
|
+
});
|
|
297
|
+
console.log(`[BrowserManager] ✓ Authenticated manually created page`);
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
console.warn(`[BrowserManager] Warning: Could not authenticate manually created page:`, error);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
240
304
|
return newPage;
|
|
241
305
|
}
|
|
242
306
|
setProfileName(profilePath, profileName) {
|
|
@@ -292,6 +356,7 @@ class BrowserManager {
|
|
|
292
356
|
async startProfileIfNotRunning(profileId, windowConfig) {
|
|
293
357
|
// Kiểm tra xem profile đã đang chạy chưa
|
|
294
358
|
if (this.instances.has(profileId)) {
|
|
359
|
+
console.log(`[BrowserManager] Profile ${profileId} is already running`);
|
|
295
360
|
return this.instances.get(profileId);
|
|
296
361
|
}
|
|
297
362
|
// Nếu chưa chạy, start nó
|
package/package.json
CHANGED