16
16
17
17
import type { Fixtures } from '@playwright/test' ;
18
18
import path from 'path' ;
19
- import socks from 'socksv5' ;
20
19
import { TestServer } from './testserver' ;
21
20
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' ;
22
24
23
25
export type ServerWorkerOptions = {
24
26
loopback ?: string ;
25
- __servers : ServerFixtures & { socksServer : socks . SocksServer } ;
27
+ __servers : ServerFixtures ;
26
28
} ;
27
29
28
30
export type ServerFixtures = {
@@ -47,30 +49,9 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
47
49
const httpsServer = await TestServer . createHTTPS ( assetsPath , httpsPort , loopback ) ;
48
50
httpsServer . enableHTTPCache ( cachedPath ) ;
49
51
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 ( ) ;
71
53
const socksPort = port + 2 ;
72
- socksServer . listen ( socksPort , 'localhost' ) ;
73
- socksServer . useAuth ( socks . auth . None ( ) ) ;
54
+ await socksServer . listen ( socksPort , 'localhost' ) ;
74
55
75
56
const proxyPort = port + 3 ;
76
57
const proxyServer = await TestProxy . create ( proxyPort ) ;
@@ -81,7 +62,6 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
81
62
httpsServer,
82
63
socksPort,
83
64
proxyServer,
84
- socksServer,
85
65
} ) ;
86
66
87
67
await Promise . all ( [
@@ -116,3 +96,37 @@ export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
116
96
} ,
117
97
} ;
118
98
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
+ }
0 commit comments