Skip to content

Commit 89eef55

Browse files
authored
chore: use own socks5 server for tests (microsoft#31639)
1 parent 2b77ed4 commit 89eef55

File tree

8 files changed

+76
-230
lines changed

8 files changed

+76
-230
lines changed

package-lock.json

Lines changed: 0 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
"node-stream-zip": "^1.15.0",
9797
"react": "^18.1.0",
9898
"react-dom": "^18.1.0",
99-
"socksv5": "0.0.6",
10099
"ssim.js": "^3.5.0",
101100
"typescript": "^5.5.3",
102101
"vite": "^5.0.13",

packages/playwright-core/src/common/socksProxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ export class SocksProxy extends EventEmitter implements SocksConnectionClient {
431431
return this._port;
432432
}
433433

434-
async listen(port: number): Promise<number> {
434+
async listen(port: number, hostname?: string): Promise<number> {
435435
return new Promise(f => {
436-
this._server.listen(port, () => {
436+
this._server.listen(port, hostname, () => {
437437
const port = (this._server.address() as AddressInfo).port;
438438
this._port = port;
439439
f(port);

tests/config/serverFixtures.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
import type { Fixtures } from '@playwright/test';
1818
import path from 'path';
19-
import socks from 'socksv5';
2019
import { TestServer } from './testserver';
2120
import { TestProxy } from './proxy';
21+
import type { SocksSocketRequestedPayload } from '../../packages/playwright-core/src/common/socksProxy';
22+
23+
import { SocksProxy } from '../../packages/playwright-core/lib/common/socksProxy';
2224

2325
export type ServerWorkerOptions = {
2426
loopback?: string;
25-
__servers: ServerFixtures & { socksServer: socks.SocksServer };
27+
__servers: ServerFixtures;
2628
};
2729

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

50-
const socksServer = socks.createServer((info, accept, deny) => {
51-
const socket = accept(true);
52-
if (!socket)
53-
return;
54-
socket.on('data', data => {
55-
if (!data.toString().includes('\r\n\r\n'))
56-
return;
57-
const body = '<html><title>Served by the SOCKS proxy</title></html>';
58-
socket.end([
59-
'HTTP/1.1 200 OK',
60-
'Connection: close',
61-
'Content-Type: text/html',
62-
'Content-Length: ' + Buffer.byteLength(body),
63-
'',
64-
body
65-
].join('\r\n'));
66-
});
67-
// Catch and ignore ECONNRESET errors.
68-
socket.on('error', () => {});
69-
setTimeout(() => socket.end(), 1000);
70-
});
52+
const socksServer = new MockSocksServer();
7153
const socksPort = port + 2;
72-
socksServer.listen(socksPort, 'localhost');
73-
socksServer.useAuth(socks.auth.None());
54+
await socksServer.listen(socksPort, 'localhost');
7455

7556
const proxyPort = port + 3;
7657
const proxyServer = await TestProxy.create(proxyPort);
@@ -81,7 +62,6 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
8162
httpsServer,
8263
socksPort,
8364
proxyServer,
84-
socksServer,
8565
});
8666

8767
await Promise.all([
@@ -116,3 +96,37 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
11696
},
11797
};
11898

99+
export class MockSocksServer {
100+
private _socksProxy: SocksProxy;
101+
102+
constructor() {
103+
this._socksProxy = new SocksProxy();
104+
this._socksProxy.setPattern('*');
105+
this._socksProxy.addListener(SocksProxy.Events.SocksRequested, async (payload: SocksSocketRequestedPayload) => {
106+
this._socksProxy.socketConnected({
107+
uid: payload.uid,
108+
host: '127.0.0.1',
109+
port: 0,
110+
});
111+
const body = '<html><title>Served by the SOCKS proxy</title></html>';
112+
const data = Buffer.from([
113+
'HTTP/1.1 200 OK',
114+
'Connection: close',
115+
'Content-Type: text/html',
116+
'Content-Length: ' + Buffer.byteLength(body),
117+
'',
118+
body
119+
].join('\r\n'));
120+
this._socksProxy.sendSocketData({ uid: payload.uid, data });
121+
this._socksProxy.sendSocketEnd({ uid: payload.uid });
122+
});
123+
}
124+
125+
async listen(port: number, hostname: string) {
126+
await this._socksProxy.listen(port, hostname);
127+
}
128+
129+
async close() {
130+
await this._socksProxy.close();
131+
}
132+
}

tests/index.d.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)