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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-08-19 18:08:55 -07:00
|
|
|
import net, { AddressInfo } from 'net';
|
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';
|
2021-09-22 12:44:22 -07:00
|
|
|
import * as types from '../server/types';
|
|
|
|
|
import { debugLogger } from '../utils/debugLogger';
|
|
|
|
|
import { SocksConnection, SocksConnectionClient } from '../utils/socksProxy';
|
|
|
|
|
import { createGuid } from '../utils/utils';
|
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-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-08-30 18:43:18 +02:00
|
|
|
export class PlaywrightDispatcher extends Dispatcher<Playwright, channels.PlaywrightInitializer, channels.PlaywrightEvents> implements channels.PlaywrightChannel {
|
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),
|
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-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 18:08:55 -07:00
|
|
|
async enableSocksProxy() {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy = new SocksProxy(this);
|
2021-08-19 18:08:55 -07:00
|
|
|
this._object.options.socksProxyPort = await this._socksProxy.listen(0);
|
|
|
|
|
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> {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy?.socketConnected(params);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksFailed(params: channels.PlaywrightSocksFailedParams): Promise<void> {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy?.socketFailed(params);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksData(params: channels.PlaywrightSocksDataParams): Promise<void> {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy?.sendSocketData(params);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksError(params: channels.PlaywrightSocksErrorParams): Promise<void> {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy?.sendSocketError(params);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:08:17 -07:00
|
|
|
async socksEnd(params: channels.PlaywrightSocksEndParams): Promise<void> {
|
2021-08-19 15:16:46 -07:00
|
|
|
this._socksProxy?.sendSocketEnd(params);
|
|
|
|
|
}
|
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
|
|
|
}
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2021-06-02 14:35:17 -07:00
|
|
|
});
|
2021-05-25 17:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 18:08:55 -07:00
|
|
|
async listen(port: number): Promise<number> {
|
|
|
|
|
return new Promise(f => {
|
|
|
|
|
this._server.listen(port, () => {
|
|
|
|
|
f((this._server.address() as AddressInfo).port);
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2020-07-08 18:42:04 -07:00
|
|
|
}
|
|
|
|
|
}
|