Skip to content
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

Unable to proxy connections with net.connect followed by http.request #16367

Open
mattfysh opened this issue Jan 13, 2025 · 0 comments
Open

Unable to proxy connections with net.connect followed by http.request #16367

mattfysh opened this issue Jan 13, 2025 · 0 comments
Labels
bug Something isn't working node:http node:net

Comments

@mattfysh
Copy link

What version of Bun is running?

1.1.43+76800b049

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

import net from 'node:net'
import http from 'node:http'

const url = new URL(process.env.URL)
const proxy = new URL(process.env.PROXY_CONN)
const auth = `${proxy.username}:${proxy.password}`

const socket = net.connect({
  host: proxy.hostname,
  port: Number.parseInt(proxy.port ?? (proxy.protocol === 'http:' ? 80 : 443)),
})

await new Promise((resolve, reject) => {
  socket.on('error', reject)
  socket.on('timeout', reject)
  socket.on('connect', resolve)
})
console.log("Socket connected")

const req = http.request({
  path: `${url.origin}${url.pathname}`,
  headers: {
    "host": url.hostname,
    "proxy-authorization": `Basic ${Buffer.from(auth).toString('base64')}`,
  },
  createConnection: () => socket,
})

const res = await new Promise((resolve, reject) => {
  req.on('response', async res => {
    res.on('error', reject)
    resolve(res)
  })
  req.on('error', reject)
  req.on('timeout', reject)
  req.end()
})

const body = await new Response(res).text()
console.log(body)

What is the expected behavior?

The http request should be proxied through the proxy socket, and return the remote response.

What do you see instead?

Socket connected
error: Unable to connect. Is the computer able to access the url?
  path: "http://lumtest.com/echo.json",
 errno: 0,
  code: "ConnectionRefused",

Additional information

Manually proxying requests via net.connect works without issue in node.js

@mattfysh mattfysh added bug Something isn't working needs triage labels Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node:http node:net
Projects
None yet
Development

No branches or pull requests

2 participants