-
Notifications
You must be signed in to change notification settings - Fork 5.9k
fix(ext/node): emit "connect" event on http.Server for CONNECT requests #32599
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
Merged
+264
−9
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a5e2e6
fix(ext/node): initialize debuglog testEnabled with safe default
bartlomieju 967bfa2
fix(ext/node): emit "connect" event on http.Server for CONNECT requests
bartlomieju 8173cd5
fix: extract head bytes for CONNECT event
bartlomieju 0dbb22f
Merge branch 'main' into fix/http-server-connect-event
bartlomieju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "args": "run -A main.ts", | ||
| "output": "main.out" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| CONNECT event received: CONNECT 127.0.0.1:[WILDCARD] | ||
| Client received status: HTTP/1.1 200 OK | ||
| Tunnel data received successfully |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import * as http from "node:http"; | ||
| import * as net from "node:net"; | ||
|
|
||
| // Test that http.Server emits the "connect" event for CONNECT requests. | ||
| // This is essential for HTTP proxy servers (e.g., proxy-chain used by Crawlee). | ||
|
|
||
| // Start a simple TCP echo server to act as the "target" | ||
| const target = net.createServer((socket) => { | ||
| socket.write("hello from target"); | ||
| socket.end(); | ||
| }); | ||
|
|
||
| target.listen(0, () => { | ||
| const targetPort = (target.address() as net.AddressInfo).port; | ||
|
|
||
| const server = http.createServer((_req, res) => { | ||
| res.writeHead(200); | ||
| res.end("ok"); | ||
| }); | ||
|
|
||
| server.on("connect", (req, clientSocket, _head) => { | ||
| console.log(`CONNECT event received: ${req.method} ${req.url}`); | ||
|
|
||
| // Connect to the target | ||
| const [hostname, port] = req.url!.split(":"); | ||
| const targetSocket = net.connect(Number(port), hostname, () => { | ||
| clientSocket.write( | ||
| "HTTP/1.1 200 Connection Established\r\n\r\n", | ||
| ); | ||
| targetSocket.pipe(clientSocket); | ||
| clientSocket.pipe(targetSocket); | ||
| }); | ||
|
|
||
| targetSocket.on("error", (err) => { | ||
| clientSocket.end(`HTTP/1.1 502 Bad Gateway\r\n\r\n`); | ||
| }); | ||
| }); | ||
|
|
||
| server.listen(0, () => { | ||
| const proxyPort = (server.address() as net.AddressInfo).port; | ||
|
|
||
| // Send a CONNECT request to the proxy | ||
| const client = net.connect(proxyPort, "127.0.0.1", () => { | ||
| client.write( | ||
| `CONNECT 127.0.0.1:${targetPort} HTTP/1.1\r\nHost: 127.0.0.1:${targetPort}\r\n\r\n`, | ||
| ); | ||
| }); | ||
|
|
||
| let data = ""; | ||
| client.on("data", (chunk) => { | ||
| data += chunk.toString(); | ||
| }); | ||
|
|
||
| client.on("end", () => { | ||
| // Should have received the 200 Connection Established + target data | ||
| const lines = data.split("\r\n"); | ||
| console.log(`Client received status: ${lines[0]}`); | ||
| if (data.includes("hello from target")) { | ||
| console.log("Tunnel data received successfully"); | ||
| } | ||
| client.end(); | ||
| server.close(); | ||
| target.close(); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.