playwright/packages/playwright-core/bin/container_novnc_proxy.js
Andrey Lushnikov 858c2453b3
feat(containers): implement reverse proxy for novnc and server (#18569)
This patch adds a reverse proxy in front of novnc and playwright
server inside the container.

As a result:
- container exposes a single HTTP port to the host
- all Playwright clients can connect using this exposed port, e.g.
  `http://127.0.0.1:5400`
- navigating to the exposed port in the browser lands on a nice HTML
page
  with a link to view container screen
- users can also manually navigate to `http://127.0.0.1:5400/screen` to
  view screen
2022-12-08 17:17:18 -08:00

70 lines
2.6 KiB
JavaScript

/**
* 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.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { debug, program } = require('../lib/utilsBundle');
const { ProxyServer } = require('../lib/third_party/http_proxy');
const debugLog = debug('pw:proxy');
program
.command('start')
.description('reverse proxy for novnc and playwright server')
.option('--port <number>', 'port number')
.option('--server-endpoint <url>', 'Playwright Server endpoint')
.option('--novnc-endpoint <url>', 'novnc server endpoint')
.option('--novnc-ws-path <string>', 'novnc websocket path')
.action(async function(options) {
launchReverseProxy(options.port, options.serverEndpoint, options.novncEndpoint, options.novncWsPath);
});
program.parse(process.argv);
async function launchReverseProxy(port, serverEndpoint, novncEndpoint, novncWSPath) {
const vncProxy = new ProxyServer(novncEndpoint, debugLog);
const serverProxy = new ProxyServer(serverEndpoint, debugLog);
const httpServer = http.createServer((request, response) => {
if (request.url === '/' && request.method === 'GET') {
response.writeHead(200, {
'content-type': 'text/html',
}).end(fs.readFileSync(path.join(__dirname, 'container_landing.html'), 'utf-8'));
} else if ((request.url === '/screen' || request.url === '/screen/') && request.method === 'GET') {
response.writeHead(307, {
Location: `/screen/?resize=scale&autoconnect=1&path=${novncWSPath}`,
}).end();
} else if (request.url?.startsWith('/screen')) {
request.url = request.url.substring('/screen'.length);
vncProxy.web(request, response);
} else {
serverProxy.web(request, response);
}
});
httpServer.on('error', error => debugLog(error));
httpServer.on('upgrade', (request, socket, head) => {
if (request.url === '/' + novncWSPath)
vncProxy.ws(request, socket, head);
else
serverProxy.ws(request, socket, head);
});
httpServer.listen(port, () => {
console.log('Playwright container listening on', port);
});
}