Skip to content

Commit

Permalink
chore: use own socks5 server for tests (#31639)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jul 11, 2024
1 parent 2b77ed4 commit 89eef55
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 230 deletions.
152 changes: 0 additions & 152 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
"node-stream-zip": "^1.15.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"socksv5": "0.0.6",
"ssim.js": "^3.5.0",
"typescript": "^5.5.3",
"vite": "^5.0.13",
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/common/socksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ export class SocksProxy extends EventEmitter implements SocksConnectionClient {
return this._port;
}

async listen(port: number): Promise<number> {
async listen(port: number, hostname?: string): Promise<number> {
return new Promise(f => {
this._server.listen(port, () => {
this._server.listen(port, hostname, () => {
const port = (this._server.address() as AddressInfo).port;
this._port = port;
f(port);
Expand Down
66 changes: 40 additions & 26 deletions tests/config/serverFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

import type { Fixtures } from '@playwright/test';
import path from 'path';
import socks from 'socksv5';
import { TestServer } from './testserver';
import { TestProxy } from './proxy';
import type { SocksSocketRequestedPayload } from '../../packages/playwright-core/src/common/socksProxy';

import { SocksProxy } from '../../packages/playwright-core/lib/common/socksProxy';

export type ServerWorkerOptions = {
loopback?: string;
__servers: ServerFixtures & { socksServer: socks.SocksServer };
__servers: ServerFixtures;
};

export type ServerFixtures = {
Expand All @@ -47,30 +49,9 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort, loopback);
httpsServer.enableHTTPCache(cachedPath);

const socksServer = socks.createServer((info, accept, deny) => {
const socket = accept(true);
if (!socket)
return;
socket.on('data', data => {
if (!data.toString().includes('\r\n\r\n'))
return;
const body = '<html><title>Served by the SOCKS proxy</title></html>';
socket.end([
'HTTP/1.1 200 OK',
'Connection: close',
'Content-Type: text/html',
'Content-Length: ' + Buffer.byteLength(body),
'',
body
].join('\r\n'));
});
// Catch and ignore ECONNRESET errors.
socket.on('error', () => {});
setTimeout(() => socket.end(), 1000);
});
const socksServer = new MockSocksServer();
const socksPort = port + 2;
socksServer.listen(socksPort, 'localhost');
socksServer.useAuth(socks.auth.None());
await socksServer.listen(socksPort, 'localhost');

const proxyPort = port + 3;
const proxyServer = await TestProxy.create(proxyPort);
Expand All @@ -81,7 +62,6 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
httpsServer,
socksPort,
proxyServer,
socksServer,
});

await Promise.all([
Expand Down Expand Up @@ -116,3 +96,37 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
},
};

export class MockSocksServer {
private _socksProxy: SocksProxy;

constructor() {
this._socksProxy = new SocksProxy();
this._socksProxy.setPattern('*');
this._socksProxy.addListener(SocksProxy.Events.SocksRequested, async (payload: SocksSocketRequestedPayload) => {
this._socksProxy.socketConnected({
uid: payload.uid,
host: '127.0.0.1',
port: 0,
});
const body = '<html><title>Served by the SOCKS proxy</title></html>';
const data = Buffer.from([
'HTTP/1.1 200 OK',
'Connection: close',
'Content-Type: text/html',
'Content-Length: ' + Buffer.byteLength(body),
'',
body
].join('\r\n'));
this._socksProxy.sendSocketData({ uid: payload.uid, data });
this._socksProxy.sendSocketEnd({ uid: payload.uid });
});
}

async listen(port: number, hostname: string) {
await this._socksProxy.listen(port, hostname);
}

async close() {
await this._socksProxy.close();
}
}
26 changes: 0 additions & 26 deletions tests/index.d.ts

This file was deleted.

Loading

0 comments on commit 89eef55

Please sign in to comment.