Loading KAIROXO page正在加载 KAIROXO 页面Đang tải trang KAIROXO

KAIROXO
Tutorial

Puppeteer Proxy Authentication and Exit Verification

Configure browser routing, supply HTTP proxy credentials before navigation and distinguish a verified browser exit from a successful Node.js request.

KAIROXO

A developer works at a desk with a laptop and camera.

A Puppeteer proxy belongs to the browser's network configuration, not automatically to every request made by the Node.js process controlling it. This distinction explains a common integration mistake: a script verifies an IP with a separate HTTP client, then assumes the browser uses that same route. The verification needs to happen inside the browser that will perform the task.

For an initial pilot, use a single browser, one isolated context and one page. Configure an HTTP(S) gateway, authenticate before navigation and inspect a harmless diagnostic response. Only after that should you introduce target sessions, multiple pages or production scheduling. Keep the task limited to content you are authorized to inspect.

Separate three kinds of state

Browser routing, proxy-session policy and website storage are independent. Browser routing determines the gateway Chrome contacts. A provider's session rules determine how an exit may persist. A browser context isolates cookies and storage. A fresh context therefore does not necessarily create a different proxy exit, while a new exit does not erase a website login.

Write down which property must remain stable. A localization check may need repeatable network and language settings without any login. A long-running authorized account workflow may need both persistent website state and a stable network identity. Decide those requirements before choosing a KAIROXO network product or sharing a browser across jobs.

Implementation steps

  1. Launch with an explicit gateway

    Install Puppeteer in a small Node.js project and use a supported browser installation that matches the package. Set PROXY_URI to the actual HTTP(S) gateway without embedded credentials. The launch argument carries only its origin. Keeping the password out of process arguments reduces accidental exposure in diagnostic tooling and task listings.

    Keep Chromium's default security and certificate checks. Disabling certificate validation can make a routing test appear successful while hiding a wrong endpoint or interception problem. Likewise, a sandbox workaround for an improperly configured container should not become a general recommendation for every deployment.

    Codejavascript
    import puppeteer from 'puppeteer'const required = name => {  const value = process.env[name]  if (!value) throw new Error('Missing ' + name)  return value}const endpoint = new URL(required('PROXY_URI'))if (!['http:', 'https:'].includes(endpoint.protocol) || endpoint.username || endpoint.password)  throw new Error('Use an HTTP(S) endpoint without embedded credentials')const browser = await puppeteer.launch({  headless:true,args:['--proxy-server=' + endpoint.origin]})try {  const context = await browser.createBrowserContext()  const page = await context.newPage()  await page.authenticate({    username:required('PROXY_USERNAME'),password:required('PROXY_PASSWORD')  })  const response = await page.goto('https://httpbin.org/ip',{    waitUntil:'domcontentloaded',timeout:30000  })  if (!response?.ok()) throw new Error('Navigation failed')  const text = await page.$eval('body',element => element.textContent || '')  if (typeof JSON.parse(text).origin !== 'string') throw new Error('Unexpected diagnostic response')  // Inspect origin locally. Never print page content, cookies or credentials.  console.log({status:response.status(),diagnosticShapeValid:true})} finally {  await browser.close()}
  2. Authenticate before the first navigation

    Set the username and password through page.authenticate before the page navigates. These are the proxy credentials, not a target site's login form. Puppeteer's authentication handling responds to HTTP authentication challenges, so test the behavior on known diagnostic or authorized destinations and avoid mixing unrelated origin credentials into the same pilot.

    Chromium's SOCKS support does not provide the usual username/password proxy-authentication path used by this HTTP example. A SOCKS URL that works with a Node.js agent is therefore not automatically a valid authenticated browser configuration. If the selected access product requires a different method, use the method it actually supports and verify the browser-specific behavior separately.

  3. Inspect the browser's actual response

    Navigate to the diagnostic endpoint inside the page and parse its visible JSON. The sample prints only the status and a response-shape check. Inspect the origin locally and compare it with the expected exit behavior; the existence of an origin field alone does not establish geography, carrier or session continuity.

    Then open one authorized target in the same browser context. Confirm that the required content is present, and record any redirect or challenge page as an observation rather than a successful extraction. A 200 status cannot tell you whether the page contains the product, search results or account area the task expected.

  4. Isolate jobs and close resources

    Use a separate context for independent website sessions, and close contexts when their work completes. The sample closes the whole browser in a finally block, including when JSON parsing or navigation fails. A persistent browser service needs equivalent cleanup around each job and a policy for recycling failed browser instances.

    Set page-navigation deadlines and an overall job deadline. Waiting for network idle can be unreliable on pages that continuously send analytics or streaming requests, so choose readiness from the actual content needed. The diagnostic uses DOMContentLoaded because its small JSON page does not require a complex application lifecycle.

Keep the test environment comparable

An IP address is only one part of a browser environment. Locale, time zone, viewport, device emulation, cached state and geolocation permissions can influence what a target displays. Set relevant properties deliberately, and record them with a sanitized route-profile name. Emulating a phone viewport does not turn a datacenter route into a mobile-carrier connection.

Limit concurrency while measuring memory, elapsed time and usable results. Browser workers spend resources on scripts, subresources and page state as well as the main request. Block unnecessary assets only after confirming that the test's purpose permits it: suppressing an ad script during an ad-placement check would invalidate the observation.

Questions before expanding the browser pool

Sources and further reading

Related KAIROXO guide

Loading KAIROXO page正在加载 KAIROXO 页面Đang tải trang KAIROXO