Is it possible to get the source port in Puppeteer?

I’ve been trying to figure out how to get the source port of requests made with Puppeteer. The docs don’t seem to cover this kind of low-level network stuff. Does anyone know if it’s even possible?

Here’s a basic script I’m working with:

const puppeteer = require('puppeteer');

async function runTest() {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  
  page.on('request', req => {
    console.log('URL:', req.url());
    // How to get source port here?
  });

  await page.goto('https://example.com');
  await browser.close();
}

runTest();

I’d really appreciate any tips or workarounds. Thanks in advance!

hey nate, puppeteer doesn’t let u access source ports directly. try using the cdp to capture low-level network details or run a packet sniffer like wireshark as a workaround. it’s messy but may do the trick.

Having worked extensively with Puppeteer, I’ve noticed that accessing the source port directly isn’t straightforward because Puppeteer intentionally abstracts many low-level network details. One advanced option involves using the Chrome DevTools Protocol to tap into more granular network events, though that requires additional setup and familiarity with CDP. Alternatively, you could route your traffic through a proxy server. By configuring a proxy—such as one built with http-proxy—you can capture and log network data externally, thus obtaining details like the source port without relying solely on Puppeteer.

I’ve faced a similar challenge with Puppeteer before. Unfortunately, getting the source port isn’t directly possible through Puppeteer’s API. The request object doesn’t expose that level of network detail.

However, I found a workaround that might help. You can use Chrome DevTools Protocol (CDP) to access more detailed network information. Here’s a rough idea:

  1. Enable network events using CDP
  2. Listen for the ‘Network.requestWillBeSent’ event
  3. This event provides a ‘requestId’ which you can use to get connection details

It’s a bit more complex, but it allows access to lower-level network info. Keep in mind this approach is Chrome-specific and may not work in all scenarios. If you need this for a critical feature, you might want to consider using a different tool or library that provides easier access to network-level data.