2020-11-20 15:19:39 -08: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.
|
|
|
|
*/
|
|
|
|
|
2022-04-18 19:20:49 -08:00
|
|
|
import { debug, wsServer } from '../utilsBundle';
|
|
|
|
import type { WebSocketServer } from '../utilsBundle';
|
2022-07-31 14:31:17 -07:00
|
|
|
import http from 'http';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Browser } from '../server/browser';
|
2022-07-31 14:31:17 -07:00
|
|
|
import type { Playwright } from '../server/playwright';
|
|
|
|
import { createPlaywright } from '../server/playwright';
|
2022-04-05 14:47:11 -07:00
|
|
|
import { PlaywrightConnection } from './playwrightConnection';
|
2022-10-20 21:30:37 -04:00
|
|
|
import type { ClientType } from './playwrightConnection';
|
2022-08-03 17:32:29 -07:00
|
|
|
import type { LaunchOptions } from '../server/types';
|
2022-08-03 19:37:06 -07:00
|
|
|
import { ManualPromise } from '../utils/manualPromise';
|
2022-10-24 17:23:11 -07:00
|
|
|
import type { AndroidDevice } from '../server/android/android';
|
2022-11-03 13:47:51 -07:00
|
|
|
import { SocksProxy } from '../common/socksProxy';
|
2020-11-20 15:19:39 -08:00
|
|
|
|
|
|
|
const debugLog = debug('pw:server');
|
|
|
|
|
2022-04-05 14:47:11 -07:00
|
|
|
let lastConnectionId = 0;
|
|
|
|
const kConnectionSymbol = Symbol('kConnection');
|
|
|
|
|
|
|
|
function newLogger() {
|
|
|
|
const id = ++lastConnectionId;
|
|
|
|
return (message: string) => debugLog(`[id=${id}] ${message}`);
|
|
|
|
}
|
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
type ServerOptions = {
|
|
|
|
path: string;
|
2022-10-20 21:30:37 -04:00
|
|
|
maxConnections: number;
|
2022-07-31 14:31:17 -07:00
|
|
|
preLaunchedBrowser?: Browser
|
2022-10-24 17:23:11 -07:00
|
|
|
preLaunchedAndroidDevice?: AndroidDevice
|
2022-11-03 13:47:51 -07:00
|
|
|
browserProxyMode: 'client' | 'tether' | 'disabled',
|
|
|
|
ownedByTetherClient?: boolean;
|
2022-07-31 14:31:17 -07:00
|
|
|
};
|
|
|
|
|
2020-11-20 15:19:39 -08:00
|
|
|
export class PlaywrightServer {
|
2022-10-24 17:23:11 -07:00
|
|
|
private _preLaunchedPlaywright: Playwright | undefined;
|
2022-04-18 19:20:49 -08:00
|
|
|
private _wsServer: WebSocketServer | undefined;
|
2022-11-03 13:47:51 -07:00
|
|
|
private _networkTetheringSocksProxy: SocksProxy | undefined;
|
2022-07-31 14:31:17 -07:00
|
|
|
private _options: ServerOptions;
|
2022-11-03 13:47:51 -07:00
|
|
|
private _networkTetheringClientTimeout: NodeJS.Timeout | undefined;
|
2021-04-12 11:14:54 -07:00
|
|
|
|
2022-10-20 21:30:37 -04:00
|
|
|
constructor(options: ServerOptions) {
|
2022-07-31 14:31:17 -07:00
|
|
|
this._options = options;
|
2022-10-20 21:30:37 -04:00
|
|
|
if (options.preLaunchedBrowser)
|
2022-07-31 14:31:17 -07:00
|
|
|
this._preLaunchedPlaywright = options.preLaunchedBrowser.options.rootSdkObject as Playwright;
|
2022-10-24 17:23:11 -07:00
|
|
|
if (options.preLaunchedAndroidDevice)
|
|
|
|
this._preLaunchedPlaywright = options.preLaunchedAndroidDevice._android._playwrightOptions.rootSdkObject as Playwright;
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
|
|
|
|
2022-09-15 15:53:18 -07:00
|
|
|
preLaunchedPlaywright(): Playwright {
|
|
|
|
if (!this._preLaunchedPlaywright)
|
|
|
|
this._preLaunchedPlaywright = createPlaywright('javascript');
|
2022-08-05 19:34:57 -07:00
|
|
|
return this._preLaunchedPlaywright;
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
async listen(port: number = 0): Promise<string> {
|
2022-09-22 14:59:58 -04:00
|
|
|
const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {
|
|
|
|
if (request.method === 'GET' && request.url === '/json') {
|
|
|
|
response.setHeader('Content-Type', 'application/json');
|
|
|
|
response.end(JSON.stringify({
|
|
|
|
wsEndpointPath: this._options.path,
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
2020-11-20 15:19:39 -08:00
|
|
|
response.end('Running');
|
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
server.on('error', error => debugLog(error));
|
|
|
|
|
2021-06-02 14:35:17 -07:00
|
|
|
const wsEndpoint = await new Promise<string>((resolve, reject) => {
|
2021-04-12 11:14:54 -07:00
|
|
|
server.listen(port, () => {
|
|
|
|
const address = server.address();
|
2021-10-01 19:40:47 -07:00
|
|
|
if (!address) {
|
|
|
|
reject(new Error('Could not bind server socket'));
|
|
|
|
return;
|
|
|
|
}
|
2022-07-31 14:31:17 -07:00
|
|
|
const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://127.0.0.1:${address.port}${this._options.path}`;
|
2021-04-12 11:14:54 -07:00
|
|
|
resolve(wsEndpoint);
|
2021-06-02 14:35:17 -07:00
|
|
|
}).on('error', reject);
|
2021-04-12 11:14:54 -07:00
|
|
|
});
|
2022-11-03 13:47:51 -07:00
|
|
|
if (this._options.browserProxyMode === 'tether') {
|
|
|
|
this._networkTetheringSocksProxy = new SocksProxy();
|
|
|
|
await this._networkTetheringSocksProxy.listen(0);
|
|
|
|
debugLog('Launched tethering proxy at ' + this._networkTetheringSocksProxy.port());
|
|
|
|
}
|
2021-04-12 11:14:54 -07:00
|
|
|
|
|
|
|
debugLog('Listening at ' + wsEndpoint);
|
2022-11-03 13:47:51 -07:00
|
|
|
if (this._options.ownedByTetherClient) {
|
|
|
|
this._networkTetheringClientTimeout = setTimeout(() => {
|
|
|
|
this.close();
|
|
|
|
}, 30_000);
|
|
|
|
}
|
2020-11-20 15:19:39 -08:00
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
this._wsServer = new wsServer({ server, path: this._options.path });
|
2022-10-20 21:30:37 -04:00
|
|
|
const browserSemaphore = new Semaphore(this._options.maxConnections);
|
2022-09-15 15:53:18 -07:00
|
|
|
const controllerSemaphore = new Semaphore(1);
|
2022-10-20 21:30:37 -04:00
|
|
|
const reuseBrowserSemaphore = new Semaphore(1);
|
2022-11-03 13:47:51 -07:00
|
|
|
const networkTetheringSemaphore = new Semaphore(1);
|
2022-08-03 19:37:06 -07:00
|
|
|
this._wsServer.on('connection', (ws, request) => {
|
2022-04-05 14:47:11 -07:00
|
|
|
const url = new URL('http://localhost' + (request.url || ''));
|
|
|
|
const browserHeader = request.headers['x-playwright-browser'];
|
2022-08-04 15:04:00 -07:00
|
|
|
const browserName = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader) || null;
|
2022-04-05 14:47:11 -07:00
|
|
|
const proxyHeader = request.headers['x-playwright-proxy'];
|
|
|
|
const proxyValue = url.searchParams.get('proxy') || (Array.isArray(proxyHeader) ? proxyHeader[0] : proxyHeader);
|
2022-11-03 13:47:51 -07:00
|
|
|
const enableSocksProxy = this._options.browserProxyMode !== 'disabled' && proxyValue === '*';
|
2022-08-03 17:32:29 -07:00
|
|
|
|
|
|
|
const launchOptionsHeader = request.headers['x-playwright-launch-options'] || '';
|
|
|
|
let launchOptions: LaunchOptions = {};
|
|
|
|
try {
|
|
|
|
launchOptions = JSON.parse(Array.isArray(launchOptionsHeader) ? launchOptionsHeader[0] : launchOptionsHeader);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:47:11 -07:00
|
|
|
const log = newLogger();
|
|
|
|
log(`serving connection: ${request.url}`);
|
2022-09-21 14:35:52 -08:00
|
|
|
const isDebugControllerClient = !!request.headers['x-playwright-debug-controller'];
|
2022-11-03 13:47:51 -07:00
|
|
|
const isNetworkTetheringClient = !!request.headers['x-playwright-network-tethering'];
|
2022-10-20 21:30:37 -04:00
|
|
|
const shouldReuseBrowser = !!request.headers['x-playwright-reuse-context'];
|
2022-09-15 15:53:18 -07:00
|
|
|
|
|
|
|
// If we started in the legacy reuse-browser mode, create this._preLaunchedPlaywright.
|
|
|
|
// If we get a reuse-controller request, create this._preLaunchedPlaywright.
|
2022-10-20 21:30:37 -04:00
|
|
|
if (isDebugControllerClient || shouldReuseBrowser)
|
2022-09-15 15:53:18 -07:00
|
|
|
this.preLaunchedPlaywright();
|
|
|
|
|
2022-10-20 21:30:37 -04:00
|
|
|
let clientType: ClientType = 'playwright';
|
2022-11-03 13:47:51 -07:00
|
|
|
let semaphore: Semaphore = browserSemaphore;
|
|
|
|
if (isNetworkTetheringClient) {
|
|
|
|
clientType = 'network-tethering';
|
|
|
|
semaphore = networkTetheringSemaphore;
|
|
|
|
} else if (isDebugControllerClient) {
|
2022-10-20 21:30:37 -04:00
|
|
|
clientType = 'controller';
|
2022-11-03 13:47:51 -07:00
|
|
|
semaphore = controllerSemaphore;
|
|
|
|
} else if (shouldReuseBrowser) {
|
2022-10-20 21:30:37 -04:00
|
|
|
clientType = 'reuse-browser';
|
2022-11-03 13:47:51 -07:00
|
|
|
semaphore = reuseBrowserSemaphore;
|
|
|
|
} else if (this._options.preLaunchedBrowser || this._options.preLaunchedAndroidDevice) {
|
2022-10-20 21:30:37 -04:00
|
|
|
clientType = 'pre-launched-browser';
|
2022-11-03 13:47:51 -07:00
|
|
|
semaphore = browserSemaphore;
|
|
|
|
} else if (browserName) {
|
2022-10-20 21:30:37 -04:00
|
|
|
clientType = 'launch-browser';
|
2022-11-03 13:47:51 -07:00
|
|
|
semaphore = browserSemaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientType === 'network-tethering' && this._options.ownedByTetherClient)
|
|
|
|
clearTimeout(this._networkTetheringClientTimeout);
|
2022-09-15 15:53:18 -07:00
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
const connection = new PlaywrightConnection(
|
2022-08-03 19:37:06 -07:00
|
|
|
semaphore.aquire(),
|
2022-10-20 21:30:37 -04:00
|
|
|
clientType, ws,
|
2022-08-04 15:04:00 -07:00
|
|
|
{ enableSocksProxy, browserName, launchOptions },
|
2022-11-03 13:47:51 -07:00
|
|
|
{
|
|
|
|
playwright: this._preLaunchedPlaywright,
|
|
|
|
browser: this._options.preLaunchedBrowser,
|
|
|
|
androidDevice: this._options.preLaunchedAndroidDevice,
|
|
|
|
networkTetheringSocksProxy: this._networkTetheringSocksProxy,
|
|
|
|
},
|
|
|
|
log, () => {
|
|
|
|
semaphore.release();
|
|
|
|
if (this._options.ownedByTetherClient && clientType === 'network-tethering')
|
|
|
|
this.close();
|
|
|
|
});
|
2022-02-10 16:36:23 -08:00
|
|
|
(ws as any)[kConnectionSymbol] = connection;
|
2020-11-20 15:19:39 -08:00
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
|
|
|
|
return wsEndpoint;
|
2020-11-20 15:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2022-02-10 16:36:23 -08:00
|
|
|
const server = this._wsServer;
|
|
|
|
if (!server)
|
2020-11-20 15:19:39 -08:00
|
|
|
return;
|
2022-11-03 13:47:51 -07:00
|
|
|
await this._networkTetheringSocksProxy?.close();
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closing websocket server');
|
|
|
|
const waitForClose = new Promise(f => server.close(f));
|
2021-04-16 17:07:56 -07:00
|
|
|
// First disconnect all remaining clients.
|
2022-02-10 16:36:23 -08:00
|
|
|
await Promise.all(Array.from(server.clients).map(async ws => {
|
2022-04-05 14:47:11 -07:00
|
|
|
const connection = (ws as any)[kConnectionSymbol] as PlaywrightConnection | undefined;
|
2022-02-10 16:36:23 -08:00
|
|
|
if (connection)
|
|
|
|
await connection.close();
|
|
|
|
try {
|
|
|
|
ws.terminate();
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}));
|
2022-02-01 21:27:34 +01:00
|
|
|
await waitForClose;
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closing http server');
|
|
|
|
await new Promise(f => server.options.server!.close(f));
|
2021-05-06 09:34:06 -07:00
|
|
|
this._wsServer = undefined;
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closed server');
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 19:37:06 -07:00
|
|
|
|
|
|
|
export class Semaphore {
|
|
|
|
private _max: number;
|
|
|
|
private _aquired = 0;
|
|
|
|
private _queue: ManualPromise[] = [];
|
2022-09-15 15:53:18 -07:00
|
|
|
|
2022-08-03 19:37:06 -07:00
|
|
|
constructor(max: number) {
|
|
|
|
this._max = max;
|
|
|
|
}
|
|
|
|
|
2022-09-15 15:53:18 -07:00
|
|
|
setMax(max: number) {
|
|
|
|
this._max = max;
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:37:06 -07:00
|
|
|
aquire(): Promise<void> {
|
|
|
|
const lock = new ManualPromise();
|
|
|
|
this._queue.push(lock);
|
|
|
|
this._flush();
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
release() {
|
|
|
|
--this._aquired;
|
|
|
|
this._flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _flush() {
|
|
|
|
while (this._aquired < this._max && this._queue.length) {
|
|
|
|
++this._aquired;
|
|
|
|
this._queue.shift()!.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|