2020-07-08 18:42:04 -07:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-24 14:48:03 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-11-05 16:27:49 +01:00
|
|
|
import { GlobalAPIRequestContext } from '../server/fetch';
|
2020-12-09 15:06:57 -08:00
|
|
|
import { Playwright } from '../server/playwright';
|
2022-02-10 13:04:19 -08:00
|
|
|
import { SocksProxy } from '../server/socksProxy';
|
2021-09-22 12:44:22 -07:00
|
|
|
import * as types from '../server/types';
|
|
|
|
|
import { debugLogger } from '../utils/debugLogger';
|
2020-12-09 15:06:57 -08:00
|
|
|
import { AndroidDispatcher } from './androidDispatcher';
|
2020-07-08 18:42:04 -07:00
|
|
|
import { BrowserTypeDispatcher } from './browserTypeDispatcher';
|
|
|
|
|
import { Dispatcher, DispatcherScope } from './dispatcher';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { ElectronDispatcher } from './electronDispatcher';
|
2021-12-09 17:21:17 -08:00
|
|
|
import { LocalUtilsDispatcher } from './localUtilsDispatcher';
|
2021-11-05 16:27:49 +01:00
|
|
|
import { APIRequestContextDispatcher } from './networkDispatchers';
|
2021-09-22 12:44:22 -07:00
|
|
|
import { SelectorsDispatcher } from './selectorsDispatcher';
|
2020-07-08 18:42:04 -07:00
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class PlaywrightDispatcher extends Dispatcher<Playwright, channels.PlaywrightChannel> implements channels.PlaywrightChannel {
|
|
|
|
|
_type_Playwright;
|
2021-08-19 15:16:46 -07:00
|
|
|
private _socksProxy: SocksProxy | undefined;
|
|
|
|
|
|
2021-06-02 14:35:17 -07:00
|
|
|
constructor(scope: DispatcherScope, playwright: Playwright, customSelectors?: channels.SelectorsChannel, preLaunchedBrowser?: channels.BrowserChannel) {
|
2021-01-17 12:09:20 -08:00
|
|
|
const descriptors = require('../server/deviceDescriptors') as types.Devices;
|
|
|
|
|
const deviceDescriptors = Object.entries(descriptors)
|
2020-07-16 13:18:54 -07:00
|
|
|
.map(([name, descriptor]) => ({ name, descriptor }));
|
2020-07-22 10:37:21 -07:00
|
|
|
super(scope, playwright, 'Playwright', {
|
2020-07-24 16:36:00 -07:00
|
|
|
chromium: new BrowserTypeDispatcher(scope, playwright.chromium),
|
|
|
|
|
firefox: new BrowserTypeDispatcher(scope, playwright.firefox),
|
|
|
|
|
webkit: new BrowserTypeDispatcher(scope, playwright.webkit),
|
2020-12-09 15:06:57 -08:00
|
|
|
android: new AndroidDispatcher(scope, playwright.android),
|
|
|
|
|
electron: new ElectronDispatcher(scope, playwright.electron),
|
2021-12-09 17:21:17 -08:00
|
|
|
utils: new LocalUtilsDispatcher(scope),
|
2020-07-16 13:18:54 -07:00
|
|
|
deviceDescriptors,
|
2021-04-12 11:14:54 -07:00
|
|
|
selectors: customSelectors || new SelectorsDispatcher(scope, playwright.selectors),
|
|
|
|
|
preLaunchedBrowser,
|
2021-04-20 23:03:56 -07:00
|
|
|
}, false);
|
2021-11-17 15:26:01 -08:00
|
|
|
this._type_Playwright = true;
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 18:08:55 -07:00
|
|
|
async enableSocksProxy() {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy = new SocksProxy();
|
2021-08-19 18:08:55 -07:00
|
|
|
this._object.options.socksProxyPort = await this._socksProxy.listen(0);
|
2022-02-10 13:04:19 -08:00
|
|
|
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));
|
2021-08-19 18:08:55 -07:00
|
|
|
debugLogger.log('proxy', `Starting socks proxy server on port ${this._object.options.socksProxyPort}`);
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksConnected(params: channels.PlaywrightSocksConnectedParams): Promise<void> {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy?.socketConnected(params.uid, params.host, params.port);
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksFailed(params: channels.PlaywrightSocksFailedParams): Promise<void> {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy?.socketFailed(params.uid, params.errorCode);
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksData(params: channels.PlaywrightSocksDataParams): Promise<void> {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy?.sendSocketData(params.uid, Buffer.from(params.data, 'base64'));
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksError(params: channels.PlaywrightSocksErrorParams): Promise<void> {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy?.sendSocketError(params.uid, params.error);
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksEnd(params: channels.PlaywrightSocksEndParams): Promise<void> {
|
2022-02-10 13:04:19 -08:00
|
|
|
this._socksProxy?.sendSocketEnd(params.uid);
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
2021-09-14 18:31:35 -07:00
|
|
|
|
|
|
|
|
async newRequest(params: channels.PlaywrightNewRequestParams, metadata?: channels.Metadata): Promise<channels.PlaywrightNewRequestResult> {
|
2021-11-05 16:27:49 +01:00
|
|
|
const request = new GlobalAPIRequestContext(this._object, params);
|
|
|
|
|
return { request: APIRequestContextDispatcher.from(this._scope, request) };
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
2022-01-12 07:37:48 -08:00
|
|
|
|
|
|
|
|
async hideHighlight(params: channels.PlaywrightHideHighlightParams, metadata?: channels.Metadata): Promise<channels.PlaywrightHideHighlightResult> {
|
|
|
|
|
await this._object.hideHighlight();
|
|
|
|
|
}
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|