Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix(browser-pool): use protocol+host instead of url.origin for SOCKS proxy server (#4290)

SwastikTripathi6911e6bb41f6
diff --git a/packages/browser-pool/src/abstract-classes/browser-plugin.ts b/packages/browser-pool/src/abstract-classes/browser-plugin.ts
index 5de62b320..d26195c0f 100644
--- a/packages/browser-pool/src/abstract-classes/browser-plugin.ts
+++ b/packages/browser-pool/src/abstract-classes/browser-plugin.ts
@@ -135,7 +135,7 @@ export abstract class BrowserPlugin<
this.log = serviceLocator.getLogger().child({ prefix: 'BrowserPool' });
this.library = library;
this.launchOptions = launchOptions;
- this.proxyUrl = proxyUrl && new URL(proxyUrl).href.slice(0, -1);
+ this.proxyUrl = proxyUrl && new URL(proxyUrl).href.replace(/\/$/, '');
this.userDataDir = userDataDir;
this.useIncognitoPages = useIncognitoPages;
this.browserPerProxy = browserPerProxy;
diff --git a/packages/browser-pool/src/playwright/playwright-plugin.ts b/packages/browser-pool/src/playwright/playwright-plugin.ts
index e3d9175a2..60788c50d 100644
--- a/packages/browser-pool/src/playwright/playwright-plugin.ts
+++ b/packages/browser-pool/src/playwright/playwright-plugin.ts
@@ -123,7 +123,7 @@ export class PlaywrightPlugin extends BrowserPlugin<
const url = new URL(proxyUrl);
launchOptions.proxy = {
- server: url.origin,
+ server: `${url.protocol}//${url.host}`,
username: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
};
diff --git a/packages/stagehand-crawler/src/internals/stagehand-plugin.ts b/packages/stagehand-crawler/src/internals/stagehand-plugin.ts
index 7b782f333..3ec38e86f 100644
--- a/packages/stagehand-crawler/src/internals/stagehand-plugin.ts
+++ b/packages/stagehand-crawler/src/internals/stagehand-plugin.ts
@@ -158,7 +158,7 @@ export class StagehandPlugin extends BrowserPlugin<BrowserType, LaunchOptions, P
const url = new URL(proxyUrl);
launchOptions.proxy = {
- server: url.origin,
+ server: `${url.protocol}//${url.host}`,
username: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
};
diff --git a/test/browser-pool/browser-plugins/plugins.test.ts b/test/browser-pool/browser-plugins/plugins.test.ts
index 761fa6ae9..bc7ca298d 100644
--- a/test/browser-pool/browser-plugins/plugins.test.ts
+++ b/test/browser-pool/browser-plugins/plugins.test.ts
@@ -117,6 +117,13 @@ const runPluginTest = <
expect(context.userDataDir).toEqual('test');
});
+ test.concurrent('should keep the port of a socks proxyUrl from plugin options', () => {
+ const plugin = new Plugin(library as never, { proxyUrl: 'socks5://127.0.0.1:1080' });
+ const context = plugin.createLaunchContext();
+
+ expect(context.proxyUrl).toEqual('socks5://127.0.0.1:1080');
+ });
+
test.concurrent('should allow unsetting launchContext proxyUrl', () => {
const plugin = new Plugin(library as never);
const context = plugin.createLaunchContext();
@@ -399,6 +406,20 @@ describe('Plugins', () => {
});
});
+ test.each([
+ ['socks5://user:[email protected]:1080', 'socks5://proxy.example.com:1080'],
+ ['socks://user:[email protected]:1080', 'socks://proxy.example.com:1080'],
+ ['http://user:[email protected]:8080', 'http://proxy.example.com:8080'],
+ ])(
+ 'PlaywrightPlugin.addProxyToLaunchOptions should set correct server for %s',
+ async (proxyUrl, expectedServer) => {
+ const plugin = new PlaywrightPlugin(playwright.chromium);
+ const context = plugin.createLaunchContext({ proxyUrl });
+ await (plugin as any).addProxyToLaunchOptions(context);
+ expect(context.launchOptions!.proxy!.server).toEqual(expectedServer);
+ },
+ );
+
test.each([
['socks5://user:[email protected]:1080', 'socks5://proxy.example.com:1080'],
['http://user:[email protected]:8080', 'http://proxy.example.com:8080'],
diff --git a/test/stagehand-crawler/stagehand-plugin.test.ts b/test/stagehand-crawler/stagehand-plugin.test.ts
index a117519b0..5dcbf7dd7 100644
--- a/test/stagehand-crawler/stagehand-plugin.test.ts
+++ b/test/stagehand-crawler/stagehand-plugin.test.ts
@@ -87,6 +87,21 @@ describe('StagehandPlugin', () => {
expect(launchContext.browserPlugin).toBe(plugin);
});
+ test.each([
+ ['socks5://user:[email protected]:1080', 'socks5://proxy.example.com:1080'],
+ ['socks://user:[email protected]:1080', 'socks://proxy.example.com:1080'],
+ ['http://user:[email protected]:8080', 'http://proxy.example.com:8080'],
+ ])('addProxyToLaunchOptions should set correct server for %s', async (proxyUrl, expectedServer) => {
+ const plugin = new StagehandPlugin(playwright.chromium, {
+ stagehandOptions: {
+ env: 'LOCAL',
+ },
+ });
+ const context = plugin.createLaunchContext({ proxyUrl });
+ await (plugin as any).addProxyToLaunchOptions(context);
+ expect(context.launchOptions!.proxy!.server).toEqual(expectedServer);
+ });
+
test('should create controller', () => {
const plugin = new StagehandPlugin(playwright.chromium, {
stagehandOptions: {