-
Notifications
You must be signed in to change notification settings - Fork 4.2k
bug(network): Use proxySocket to determine alpnProtocol #35990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1d85f5b
a4dad1a
afb8d3d
fd41a56
bd2cef9
bdc21b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -42,7 +42,7 @@ function loadDummyServerCertsIfNeeded() { | |||||||||||
class ALPNCache { | ||||||||||||
private _cache = new Map<string, ManualPromise<string>>(); | ||||||||||||
|
||||||||||||
get(host: string, port: number, success: (protocol: string) => void) { | ||||||||||||
get(host: string, port: number, secureContext: tls.SecureContext | undefined, proxySocket: stream.Duplex | undefined, success: (protocol: string) => void) { | ||||||||||||
const cacheKey = `${host}:${port}`; | ||||||||||||
{ | ||||||||||||
const result = this._cache.get(cacheKey); | ||||||||||||
|
@@ -55,11 +55,13 @@ class ALPNCache { | |||||||||||
this._cache.set(cacheKey, result); | ||||||||||||
result.then(success); | ||||||||||||
createTLSSocket({ | ||||||||||||
socket: proxySocket, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this mutually exclusive with (host, port), if so can we provide either one or the other? Or if they are required, let's comment why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Internally, this method calls playwright/packages/playwright-core/src/server/utils/happyEyeballs.ts Lines 131 to 135 in afb8d3d
yes, Instead we can add a comment specifying the use of these params and its use cases. |
||||||||||||
host, | ||||||||||||
port, | ||||||||||||
servername: net.isIP(host) ? undefined : host, | ||||||||||||
ALPNProtocols: ['h2', 'http/1.1'], | ||||||||||||
rejectUnauthorized: false, | ||||||||||||
secureContext, | ||||||||||||
}).then(socket => { | ||||||||||||
// The server may not respond with ALPN, in which case we default to http/1.1. | ||||||||||||
result.resolve(socket.alpnProtocol || 'http/1.1'); | ||||||||||||
|
@@ -99,11 +101,7 @@ class SocksProxyConnection { | |||||||||||
} | ||||||||||||
|
||||||||||||
async connect() { | ||||||||||||
if (this.socksProxy.proxyAgentFromOptions) | ||||||||||||
this.target = await this.socksProxy.proxyAgentFromOptions.callback(new EventEmitter() as any, { host: rewriteToLocalhostIfNeeded(this.host), port: this.port, secureEndpoint: false }); | ||||||||||||
else | ||||||||||||
this.target = await createSocket(rewriteToLocalhostIfNeeded(this.host), this.port); | ||||||||||||
|
||||||||||||
this.target = await this._createProxySocket() ?? await createSocket(rewriteToLocalhostIfNeeded(this.host), this.port); | ||||||||||||
this.target.once('close', this._targetCloseEventListener); | ||||||||||||
this.target.once('error', error => this.socksProxy._socksProxy.sendSocketError({ uid: this.uid, error: error.message })); | ||||||||||||
if (this._closed) { | ||||||||||||
|
@@ -142,15 +140,23 @@ class SocksProxyConnection { | |||||||||||
this.target.write(data); | ||||||||||||
} | ||||||||||||
|
||||||||||||
private _attachTLSListeners() { | ||||||||||||
private async _createProxySocket() { | ||||||||||||
if (this.socksProxy.proxyAgentFromOptions) | ||||||||||||
return await this.socksProxy.proxyAgentFromOptions.callback(new EventEmitter() as any, { host: rewriteToLocalhostIfNeeded(this.host), port: this.port, secureEndpoint: false }); | ||||||||||||
} | ||||||||||||
|
||||||||||||
private async _attachTLSListeners() { | ||||||||||||
this.internal = new stream.Duplex({ | ||||||||||||
read: () => {}, | ||||||||||||
read: () => { }, | ||||||||||||
write: (data, encoding, callback) => { | ||||||||||||
this.socksProxy._socksProxy.sendSocketData({ uid: this.uid, data }); | ||||||||||||
callback(); | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
this.socksProxy.alpnCache.get(rewriteToLocalhostIfNeeded(this.host), this.port, alpnProtocolChosenByServer => { | ||||||||||||
const secureContext = this.socksProxy.secureContextMap.get(new URL(`https://${this.host}:${this.port}`).origin); | ||||||||||||
const proxySocket = await this._createProxySocket(); | ||||||||||||
this.socksProxy.alpnCache.get(rewriteToLocalhostIfNeeded(this.host), this.port, secureContext, proxySocket, alpnProtocolChosenByServer => { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this sent to the proxy and now the negotiation happens between the proxy server and the client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the job of proxySocket is just to establish the connection. The negotiation still happens between client and destination server. |
||||||||||||
proxySocket?.destroy(); | ||||||||||||
debugLogger.log('client-certificates', `Proxy->Target ${this.host}:${this.port} chooses ALPN ${alpnProtocolChosenByServer}`); | ||||||||||||
if (this._closed) | ||||||||||||
return; | ||||||||||||
|
@@ -221,7 +227,7 @@ class SocksProxyConnection { | |||||||||||
rejectUnauthorized: !this.socksProxy.ignoreHTTPSErrors, | ||||||||||||
ALPNProtocols: [internalTLS.alpnProtocol || 'http/1.1'], | ||||||||||||
servername: !net.isIP(this.host) ? this.host : undefined, | ||||||||||||
secureContext: this.socksProxy.secureContextMap.get(new URL(`https://${this.host}:${this.port}`).origin), | ||||||||||||
secureContext, | ||||||||||||
}); | ||||||||||||
|
||||||||||||
targetTLS.once('secureConnect', () => { | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @mxschmitt , Sharing some insights while developing this PR. I tried re-using the same function with socket, but it times out. Reason being, we dont have a
secureConnect
listener here:playwright/packages/playwright-core/src/server/utils/happyEyeballs.ts
Lines 143 to 162 in 809174b
playwright/packages/playwright-core/src/server/utils/happyEyeballs.ts
Lines 131 to 135 in 809174b
secureConnect
event which isn't present. Instead, we rely on listeningsecureConnect
event here:playwright/packages/playwright-core/src/server/utils/happyEyeballs.ts
Lines 87 to 88 in 809174b
This required a bit of refactoring, hence avoided that change.
Looking at the recent changes you made, looks awesome. Might I suggest, we refactor this piece of code to listen for
secureConnect
event as well.Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, Let me know if this makes sense. Or if you have an approach in mind I can follow to fix this issue?