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 15:16:46 -07:00
|
|
|
import dns from 'dns';
|
|
|
|
import net from 'net';
|
|
|
|
import util from 'util';
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-08-19 15:16:46 -07:00
|
|
|
import { TimeoutError } from '../utils/errors';
|
|
|
|
import { createSocket } from '../utils/netUtils';
|
|
|
|
import { Android } from './android';
|
2020-07-08 18:42:04 -07:00
|
|
|
import { BrowserType } from './browserType';
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { Electron } from './electron';
|
2021-10-05 17:53:19 -08:00
|
|
|
import { Fetch } from './fetch';
|
2021-10-15 09:21:56 -07:00
|
|
|
import { Selectors, SelectorsOwner } from './selectors';
|
2021-10-05 17:53:19 -08:00
|
|
|
import { Size } from './types';
|
2021-08-19 15:16:46 -07:00
|
|
|
const dnsLookupAsync = util.promisify(dns.lookup);
|
2020-07-29 17:26:59 -07:00
|
|
|
|
|
|
|
type DeviceDescriptor = {
|
|
|
|
userAgent: string,
|
|
|
|
viewport: Size,
|
|
|
|
deviceScaleFactor: number,
|
|
|
|
isMobile: boolean,
|
2020-09-03 22:12:43 +02:00
|
|
|
hasTouch: boolean,
|
|
|
|
defaultBrowserType: 'chromium' | 'firefox' | 'webkit'
|
2020-07-29 17:26:59 -07:00
|
|
|
};
|
|
|
|
type Devices = { [name: string]: DeviceDescriptor };
|
2020-07-08 18:42:04 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
export class Playwright extends ChannelOwner<channels.PlaywrightChannel, channels.PlaywrightInitializer> {
|
2020-12-09 15:06:57 -08:00
|
|
|
readonly _android: Android;
|
|
|
|
readonly _electron: Electron;
|
2020-07-16 22:38:52 -07:00
|
|
|
readonly chromium: BrowserType;
|
|
|
|
readonly firefox: BrowserType;
|
|
|
|
readonly webkit: BrowserType;
|
2020-07-29 17:26:59 -07:00
|
|
|
readonly devices: Devices;
|
2021-10-15 09:21:56 -07:00
|
|
|
selectors: Selectors;
|
2021-10-05 17:53:19 -08:00
|
|
|
readonly request: Fetch;
|
2020-07-16 22:38:52 -07:00
|
|
|
readonly errors: { TimeoutError: typeof TimeoutError };
|
2021-08-19 15:16:46 -07:00
|
|
|
private _sockets = new Map<string, net.Socket>();
|
|
|
|
private _redirectPortForTest: number | undefined;
|
2020-07-08 18:42:04 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2021-10-05 17:53:19 -08:00
|
|
|
this.request = new Fetch(this);
|
2020-07-08 18:42:04 -07:00
|
|
|
this.chromium = BrowserType.from(initializer.chromium);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.chromium._playwright = this;
|
2020-07-08 18:42:04 -07:00
|
|
|
this.firefox = BrowserType.from(initializer.firefox);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.firefox._playwright = this;
|
2020-07-08 18:42:04 -07:00
|
|
|
this.webkit = BrowserType.from(initializer.webkit);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.webkit._playwright = this;
|
2020-12-09 15:06:57 -08:00
|
|
|
this._android = Android.from(initializer.android);
|
|
|
|
this._electron = Electron.from(initializer.electron);
|
2020-07-16 13:18:54 -07:00
|
|
|
this.devices = {};
|
|
|
|
for (const { name, descriptor } of initializer.deviceDescriptors)
|
|
|
|
this.devices[name] = descriptor;
|
2021-10-15 09:21:56 -07:00
|
|
|
this.selectors = new Selectors();
|
2020-07-16 22:38:52 -07:00
|
|
|
this.errors = { TimeoutError };
|
2021-04-12 11:14:54 -07:00
|
|
|
|
2021-10-14 20:58:09 -07:00
|
|
|
const selectorsOwner = SelectorsOwner.from(initializer.selectors);
|
|
|
|
this.selectors._addChannel(selectorsOwner);
|
|
|
|
this._connection.on('close', () => {
|
|
|
|
this.selectors._removeChannel(selectorsOwner);
|
|
|
|
for (const uid of this._sockets.keys())
|
|
|
|
this._onSocksClosed(uid);
|
|
|
|
});
|
2021-08-19 15:16:46 -07:00
|
|
|
}
|
|
|
|
|
2021-10-15 09:21:56 -07:00
|
|
|
_setSelectors(selectors: Selectors) {
|
|
|
|
const selectorsOwner = SelectorsOwner.from(this._initializer.selectors);
|
|
|
|
this.selectors._removeChannel(selectorsOwner);
|
|
|
|
this.selectors = selectors;
|
|
|
|
this.selectors._addChannel(selectorsOwner);
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:16:46 -07:00
|
|
|
_enablePortForwarding(redirectPortForTest?: number) {
|
|
|
|
this._redirectPortForTest = redirectPortForTest;
|
|
|
|
this._channel.on('socksRequested', ({ uid, host, port }) => this._onSocksRequested(uid, host, port));
|
|
|
|
this._channel.on('socksData', ({ uid, data }) => this._onSocksData(uid, Buffer.from(data, 'base64')));
|
|
|
|
this._channel.on('socksClosed', ({ uid }) => this._onSocksClosed(uid));
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _onSocksRequested(uid: string, host: string, port: number): Promise<void> {
|
2021-08-19 16:56:06 -07:00
|
|
|
if (host === 'local.playwright')
|
|
|
|
host = 'localhost';
|
2021-08-19 15:16:46 -07:00
|
|
|
try {
|
|
|
|
if (this._redirectPortForTest)
|
|
|
|
port = this._redirectPortForTest;
|
|
|
|
const { address } = await dnsLookupAsync(host);
|
|
|
|
const socket = await createSocket(address, port);
|
|
|
|
socket.on('data', data => this._channel.socksData({ uid, data: data.toString('base64') }).catch(() => {}));
|
|
|
|
socket.on('error', error => {
|
|
|
|
this._channel.socksError({ uid, error: error.message }).catch(() => { });
|
|
|
|
this._sockets.delete(uid);
|
|
|
|
});
|
|
|
|
socket.on('end', () => {
|
|
|
|
this._channel.socksEnd({ uid }).catch(() => {});
|
|
|
|
this._sockets.delete(uid);
|
|
|
|
});
|
|
|
|
const localAddress = socket.localAddress;
|
|
|
|
const localPort = socket.localPort;
|
|
|
|
this._sockets.set(uid, socket);
|
|
|
|
this._channel.socksConnected({ uid, host: localAddress, port: localPort }).catch(() => {});
|
|
|
|
} catch (error) {
|
|
|
|
this._channel.socksFailed({ uid, errorCode: error.code }).catch(() => {});
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 17:11:32 +02:00
|
|
|
|
2021-08-19 15:16:46 -07:00
|
|
|
private _onSocksData(uid: string, data: Buffer): void {
|
|
|
|
this._sockets.get(uid)?.write(data);
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
|
|
|
|
2021-08-19 17:31:14 +02:00
|
|
|
static from(channel: channels.PlaywrightChannel): Playwright {
|
|
|
|
return (channel as any)._object;
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:16:46 -07:00
|
|
|
private _onSocksClosed(uid: string): void {
|
|
|
|
this._sockets.get(uid)?.destroy();
|
|
|
|
this._sockets.delete(uid);
|
2021-06-02 14:35:17 -07:00
|
|
|
}
|
2020-07-08 18:42:04 -07:00
|
|
|
}
|