Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(remote): let client enable/disable sock proxy (#12086)

Dmitry Gozman5a0445b8dac2
diff --git a/packages/playwright-core/src/browserServerImpl.ts b/packages/playwright-core/src/browserServerImpl.ts
index 99ec42ed7a..e3dcda6687 100644
--- a/packages/playwright-core/src/browserServerImpl.ts
+++ b/packages/playwright-core/src/browserServerImpl.ts
@@ -53,7 +53,7 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher {
path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`;
// 2. Start the server
- const server = new PlaywrightServer(path, Infinity, browser);
+ const server = new PlaywrightServer(path, Infinity, false, browser);
const wsEndpoint = await server.listen(options.port);
// 3. Return the BrowserServer interface
diff --git a/packages/playwright-core/src/client/browserType.ts b/packages/playwright-core/src/client/browserType.ts
index d666d39fd2..2ed3cb4186 100644
--- a/packages/playwright-core/src/client/browserType.ts
+++ b/packages/playwright-core/src/client/browserType.ts
@@ -129,10 +129,8 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
const deadline = params.timeout ? monotonicTime() + params.timeout : 0;
let browser: Browser;
const connectParams: channels.BrowserTypeConnectParams = { wsEndpoint, headers: params.headers, slowMo: params.slowMo, timeout: params.timeout };
- if ((params as any).__testHookPortForwarding) {
- connectParams.enableSocksProxy = true;
- connectParams.socksProxyRedirectPortForTest = (params as any).__testHookPortForwarding.redirectPortForTest;
- }
+ if ((params as any).__testHookRedirectPortForwarding)
+ connectParams.socksProxyRedirectPortForTest = (params as any).__testHookRedirectPortForwarding;
const { pipe } = await this._channel.connect(connectParams);
const closePipe = () => pipe.close().catch(() => {});
const connection = new Connection();
diff --git a/packages/playwright-core/src/dispatchers/browserTypeDispatcher.ts b/packages/playwright-core/src/dispatchers/browserTypeDispatcher.ts
index c79d903a43..0ffea080ac 100644
--- a/packages/playwright-core/src/dispatchers/browserTypeDispatcher.ts
+++ b/packages/playwright-core/src/dispatchers/browserTypeDispatcher.ts
@@ -93,7 +93,7 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, channels.Brow
waitForNextTask(() => {
try {
const json = JSON.parse(event.data as string);
- if (params.enableSocksProxy && json.method === '__create__' && json.params.type === 'SocksSupport')
+ if (json.method === '__create__' && json.params.type === 'SocksSupport')
socksInterceptor = new SocksInterceptor(ws, params.socksProxyRedirectPortForTest, json.params.guid);
if (!socksInterceptor?.interceptMessage(json))
pipe.dispatch(json);
diff --git a/packages/playwright-core/src/protocol/channels.ts b/packages/playwright-core/src/protocol/channels.ts
index a42c5d58bc..a891d6f562 100644
--- a/packages/playwright-core/src/protocol/channels.ts
+++ b/packages/playwright-core/src/protocol/channels.ts
@@ -603,14 +603,12 @@ export type BrowserTypeConnectParams = {
headers?: any,
slowMo?: number,
timeout?: number,
- enableSocksProxy?: boolean,
socksProxyRedirectPortForTest?: number,
};
export type BrowserTypeConnectOptions = {
headers?: any,
slowMo?: number,
timeout?: number,
- enableSocksProxy?: boolean,
socksProxyRedirectPortForTest?: number,
};
export type BrowserTypeConnectResult = {
diff --git a/packages/playwright-core/src/protocol/protocol.yml b/packages/playwright-core/src/protocol/protocol.yml
index 481720d6dd..0dd4d6075c 100644
--- a/packages/playwright-core/src/protocol/protocol.yml
+++ b/packages/playwright-core/src/protocol/protocol.yml
@@ -596,7 +596,6 @@ BrowserType:
headers: json?
slowMo: number?
timeout: number?
- enableSocksProxy: boolean?
socksProxyRedirectPortForTest: number?
returns:
pipe: JsonPipe
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index 2714d17988..2376781e3a 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -249,7 +249,6 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
headers: tOptional(tAny),
slowMo: tOptional(tNumber),
timeout: tOptional(tNumber),
- enableSocksProxy: tOptional(tBoolean),
socksProxyRedirectPortForTest: tOptional(tNumber),
});
scheme.BrowserTypeLaunchParams = tObject({
diff --git a/packages/playwright-core/src/remote/playwrightServer.ts b/packages/playwright-core/src/remote/playwrightServer.ts
index e773602d94..f9a31dbbb0 100644
--- a/packages/playwright-core/src/remote/playwrightServer.ts
+++ b/packages/playwright-core/src/remote/playwrightServer.ts
@@ -31,18 +31,20 @@ const debugLog = debug('pw:server');
export class PlaywrightServer {
private _path: string;
private _maxClients: number;
+ private _enableSocksProxy: boolean;
private _browser: Browser | undefined;
private _wsServer: WebSocket.Server | undefined;
private _clientsCount = 0;
- static async startDefault(options: { path?: string, maxClients?: number } = {}): Promise<PlaywrightServer> {
- const { path = '/ws', maxClients = 1 } = options;
- return new PlaywrightServer(path, maxClients);
+ static async startDefault(options: { path?: string, maxClients?: number, enableSocksProxy?: boolean } = {}): Promise<PlaywrightServer> {
+ const { path = '/ws', maxClients = 1, enableSocksProxy = true } = options;
+ return new PlaywrightServer(path, maxClients, enableSocksProxy);
}
- constructor(path: string, maxClients: number, browser?: Browser) {
+ constructor(path: string, maxClients: number, enableSocksProxy: boolean, browser?: Browser) {
this._path = path;
this._maxClients = maxClients;
+ this._enableSocksProxy = enableSocksProxy;
this._browser = browser;
}
@@ -75,7 +77,7 @@ export class PlaywrightServer {
return;
}
this._clientsCount++;
- const connection = new Connection(ws, request, this._browser, () => this._clientsCount--);
+ const connection = new Connection(ws, request, this._enableSocksProxy, this._browser, () => this._clientsCount--);
(ws as any)[kConnectionSymbol] = connection;
});
@@ -117,7 +119,7 @@ class Connection {
private _id: number;
private _disconnected = false;
- constructor(ws: WebSocket, request: http.IncomingMessage, browser: Browser | undefined, onClose: () => void) {
+ constructor(ws: WebSocket, request: http.IncomingMessage, enableSocksProxy: boolean, browser: Browser | undefined, onClose: () => void) {
this._ws = ws;
this._onClose = onClose;
this._id = ++lastConnectionId;
@@ -139,32 +141,34 @@ class Connection {
if (browser)
return await this._initPreLaunchedBrowserMode(scope, browser);
const url = new URL('http://localhost' + (request.url || ''));
- const header = request.headers['X-Playwright-Browser'];
- const browserAlias = url.searchParams.get('browser') || (Array.isArray(header) ? header[0] : header);
+ const browserHeader = request.headers['X-Playwright-Browser'];
+ const browserAlias = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader);
+ const proxyHeader = request.headers['X-Playwright-Proxy'];
+ const proxyValue = url.searchParams.get('proxy') || (Array.isArray(proxyHeader) ? proxyHeader[0] : proxyHeader);
if (!browserAlias)
- return await this._initPlaywrightConnectMode(scope);
- return await this._initLaunchBrowserMode(scope, browserAlias);
+ return await this._initPlaywrightConnectMode(scope, enableSocksProxy && proxyValue === '*');
+ return await this._initLaunchBrowserMode(scope, enableSocksProxy && proxyValue === '*', browserAlias);
});
}
- private async _initPlaywrightConnectMode(scope: DispatcherScope) {
+ private async _initPlaywrightConnectMode(scope: DispatcherScope, enableSocksProxy: boolean) {
debugLog(`[id=${this._id}] engaged playwright.connect mode`);
const playwright = createPlaywright('javascript');
// Close all launched browsers on disconnect.
this._cleanups.push(() => gracefullyCloseAll());
- const socksProxy = await this._enableSocksProxyIfNeeded(playwright);
+ const socksProxy = enableSocksProxy ? await this._enableSocksProxy(playwright) : undefined;
return new PlaywrightDispatcher(scope, playwright, socksProxy);
}
- private async _initLaunchBrowserMode(scope: DispatcherScope, browserAlias: string) {
+ private async _initLaunchBrowserMode(scope: DispatcherScope, enableSocksProxy: boolean, browserAlias: string) {
debugLog(`[id=${this._id}] engaged launch mode for "${browserAlias}"`);
const executable = registry.findExecutable(browserAlias);
if (!executable || !executable.browserName)
throw new Error(`Unsupported browser "${browserAlias}`);
const playwright = createPlaywright('javascript');
- const socksProxy = await this._enableSocksProxyIfNeeded(playwright);
+ const socksProxy = enableSocksProxy ? await this._enableSocksProxy(playwright) : undefined;
const browser = await playwright[executable.browserName].launch(internalCallMetadata(), {
channel: executable.type === 'browser' ? undefined : executable.name,
});
@@ -194,9 +198,7 @@ class Connection {
return playwrightDispatcher;
}
- private async _enableSocksProxyIfNeeded(playwright: Playwright) {
- if (!process.env.PW_SOCKS_PROXY_PORT)
- return;
+ private async _enableSocksProxy(playwright: Playwright) {
const socksProxy = new SocksProxy();
playwright.options.socksProxyPort = await socksProxy.listen(0);
debugLog(`[id=${this._id}] started socks proxy on port ${playwright.options.socksProxyPort}`);
diff --git a/tests/port-forwarding-server.spec.ts b/tests/port-forwarding-server.spec.ts
index 27eedb817f..23ca2c33e6 100644
--- a/tests/port-forwarding-server.spec.ts
+++ b/tests/port-forwarding-server.spec.ts
@@ -67,8 +67,8 @@ const it = contextTest.extend<{ pageFactory: (redirectPortForTest?: number) => P
const server = new OutOfProcessPlaywrightServer(0, 3200 + testInfo.workerIndex);
playwrightServers.push(server);
const browser = await browserType.connect({
- wsEndpoint: await server.wsEndpoint() + '?browser=' + (channel || browserName),
- __testHookPortForwarding: { redirectPortForTest },
+ wsEndpoint: await server.wsEndpoint() + '?proxy=*&browser=' + (channel || browserName),
+ __testHookRedirectPortForwarding: redirectPortForTest,
} as any);
browsers.push(browser);
return await browser.newPage();