Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: extract SocksProxy to a spearate file (#12011)
Dmitry Gozman2bc19ae07618
diff --git a/packages/playwright-core/src/dispatchers/playwrightDispatcher.ts b/packages/playwright-core/src/dispatchers/playwrightDispatcher.tsindex 75e6f4274e..c9335873b7 100644--- a/packages/playwright-core/src/dispatchers/playwrightDispatcher.ts+++ b/packages/playwright-core/src/dispatchers/playwrightDispatcher.ts@@ -14,14 +14,12 @@* limitations under the License.*/-import net, { AddressInfo } from 'net';import * as channels from '../protocol/channels';import { GlobalAPIRequestContext } from '../server/fetch';import { Playwright } from '../server/playwright';+import { SocksProxy } from '../server/socksProxy';import * as types from '../server/types';import { debugLogger } from '../utils/debugLogger';-import { SocksConnection, SocksConnectionClient } from '../utils/socksProxy';-import { createGuid } from '../utils/utils';import { AndroidDispatcher } from './androidDispatcher';import { BrowserTypeDispatcher } from './browserTypeDispatcher';import { Dispatcher, DispatcherScope } from './dispatcher';@@ -53,29 +51,32 @@ export class PlaywrightDispatcher extends Dispatcher<Playwright, channels.Playwr}async enableSocksProxy() {- this._socksProxy = new SocksProxy(this);+ this._socksProxy = new SocksProxy();this._object.options.socksProxyPort = await this._socksProxy.listen(0);+ this._socksProxy.on(SocksProxy.Events.SocksRequested, data => this._dispatchEvent('socksRequested', data));+ this._socksProxy.on(SocksProxy.Events.SocksData, data => this._dispatchEvent('socksData', data));+ this._socksProxy.on(SocksProxy.Events.SocksClosed, data => this._dispatchEvent('socksClosed', data));debugLogger.log('proxy', `Starting socks proxy server on port ${this._object.options.socksProxyPort}`);}async socksConnected(params: channels.PlaywrightSocksConnectedParams): Promise<void> {- this._socksProxy?.socketConnected(params);+ this._socksProxy?.socketConnected(params.uid, params.host, params.port);}async socksFailed(params: channels.PlaywrightSocksFailedParams): Promise<void> {- this._socksProxy?.socketFailed(params);+ this._socksProxy?.socketFailed(params.uid, params.errorCode);}async socksData(params: channels.PlaywrightSocksDataParams): Promise<void> {- this._socksProxy?.sendSocketData(params);+ this._socksProxy?.sendSocketData(params.uid, Buffer.from(params.data, 'base64'));}async socksError(params: channels.PlaywrightSocksErrorParams): Promise<void> {- this._socksProxy?.sendSocketError(params);+ this._socksProxy?.sendSocketError(params.uid, params.error);}async socksEnd(params: channels.PlaywrightSocksEndParams): Promise<void> {- this._socksProxy?.sendSocketEnd(params);+ this._socksProxy?.sendSocketEnd(params.uid);}async newRequest(params: channels.PlaywrightNewRequestParams, metadata?: channels.Metadata): Promise<channels.PlaywrightNewRequestResult> {@@ -87,58 +88,3 @@ export class PlaywrightDispatcher extends Dispatcher<Playwright, channels.Playwrawait this._object.hideHighlight();}}--class SocksProxy implements SocksConnectionClient {- private _server: net.Server;- private _connections = new Map<string, SocksConnection>();- private _dispatcher: PlaywrightDispatcher;-- constructor(dispatcher: PlaywrightDispatcher) {- this._dispatcher = dispatcher;- this._server = new net.Server((socket: net.Socket) => {- const uid = createGuid();- const connection = new SocksConnection(uid, socket, this);- this._connections.set(uid, connection);- });- }-- async listen(port: number): Promise<number> {- return new Promise(f => {- this._server.listen(port, () => {- f((this._server.address() as AddressInfo).port);- });- });- }-- onSocketRequested(uid: string, host: string, port: number): void {- this._dispatcher._dispatchEvent('socksRequested', { uid, host, port });- }-- onSocketData(uid: string, data: Buffer): void {- this._dispatcher._dispatchEvent('socksData', { uid, data: data.toString('base64') });- }-- onSocketClosed(uid: string): void {- this._dispatcher._dispatchEvent('socksClosed', { uid });- }-- socketConnected(params: channels.PlaywrightSocksConnectedParams) {- this._connections.get(params.uid)?.socketConnected(params.host, params.port);- }-- socketFailed(params: channels.PlaywrightSocksFailedParams) {- this._connections.get(params.uid)?.socketFailed(params.errorCode);- }-- sendSocketData(params: channels.PlaywrightSocksDataParams) {- this._connections.get(params.uid)?.sendData(Buffer.from(params.data, 'base64'));- }-- sendSocketEnd(params: channels.PlaywrightSocksEndParams) {- this._connections.get(params.uid)?.end();- }-- sendSocketError(params: channels.PlaywrightSocksErrorParams) {- this._connections.get(params.uid)?.error(params.error);- }-}diff --git a/packages/playwright-core/src/server/socksProxy.ts b/packages/playwright-core/src/server/socksProxy.tsnew file mode 100644index 0000000000..58313eac75--- /dev/null+++ b/packages/playwright-core/src/server/socksProxy.ts@@ -0,0 +1,87 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import net, { AddressInfo } from 'net';+import { debugLogger } from '../utils/debugLogger';+import { SocksConnection, SocksConnectionClient } from '../utils/socksProxy';+import { createGuid } from '../utils/utils';+import EventEmitter from 'events';++export class SocksProxy extends EventEmitter implements SocksConnectionClient {+ static Events = {+ SocksRequested: 'socksRequested',+ SocksData: 'socksData',+ SocksClosed: 'socksClosed',+ };++ private _server: net.Server;+ private _connections = new Map<string, SocksConnection>();++ constructor() {+ super();+ this._server = new net.Server((socket: net.Socket) => {+ const uid = createGuid();+ const connection = new SocksConnection(uid, socket, this);+ this._connections.set(uid, connection);+ });+ }++ async listen(port: number): Promise<number> {+ return new Promise(f => {+ this._server.listen(port, () => {+ const port = (this._server.address() as AddressInfo).port;+ debugLogger.log('proxy', `Starting socks proxy server on port ${port}`);+ f(port);+ });+ });+ }++ async close() {+ await new Promise(f => this._server.close(f));+ }++ onSocketRequested(uid: string, host: string, port: number): void {+ this.emit(SocksProxy.Events.SocksRequested, { uid, host, port });+ }++ onSocketData(uid: string, data: Buffer): void {+ this.emit(SocksProxy.Events.SocksData, { uid, data: data.toString('base64') });+ }++ onSocketClosed(uid: string): void {+ this.emit(SocksProxy.Events.SocksClosed, { uid });+ }++ socketConnected(uid: string, host: string, port: number) {+ this._connections.get(uid)?.socketConnected(host, port);+ }++ socketFailed(uid: string, errorCode: string) {+ this._connections.get(uid)?.socketFailed(errorCode);+ }++ sendSocketData(uid: string, buffer: Buffer) {+ this._connections.get(uid)?.sendData(buffer);+ }++ sendSocketEnd(uid: string) {+ this._connections.get(uid)?.end();+ }++ sendSocketError(uid: string, error: string) {+ this._connections.get(uid)?.error(error);+ }+}