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