Below are two Node.js example scripts for testing HTTP proxies using either static IP authentication or username/password authentication.
These scripts are useful for quickly verifying your proxy setup and connectivity.
Each script:
- Fetches a random desktop user agent from Trusted Proxies’ user agent list.
- Sends a request to a target URL through your proxy.
- Prints the HTTP status code and status message, or an error if the request fails.
Instructions:
- Install dependencies with:
npm install axios https-proxy-agent node-fetch@2
Replace the placeholder values ( your_proxy_host, your_proxy_port, your_username, your_password, and https://example.com) with your actual proxy and target details.
- Save each script as a separate .js file and run with node yourscript.js.
These scripts help you confirm that your proxy is working as expected and that your requests are being routed through the proxy server.
Static IP Authentication (Node.js, CommonJS, compatible with latest packages):
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const fetch = require('node-fetch'); // Make sure you have node-fetch v2 installed
// Proxy and test details
const proxyHost = "your_proxy_host";
const proxyPort = "your_proxy_port";
const testUrl = "https://example.com"; // Set your test URL here
const uaUrl = "https://customers.trustedproxies.com/downloads/desktop_useragents.txt";
async function getRandomUserAgent() {
try {
const res = await fetch(uaUrl, {timeout: 10000});
const text = await res.text();
const userAgents = text.split('\n').map(ua => ua.trim()).filter(Boolean);
return userAgents[Math.floor(Math.random() * userAgents.length)];
} catch (err) {
throw new Error("Failed to fetch user agents: " + err);
}
}
async function main() {
const userAgent = await getRandomUserAgent();
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
const agent = new HttpsProxyAgent(proxyUrl);
try {
const response = await axios.get(testUrl, {
httpsAgent: agent,
httpAgent: agent,
headers: { 'User-Agent': userAgent },
timeout: 10000,
validateStatus: () => true
});
console.log("Testing proxy with static IP authentication...");
console.log("Status Code:", response.status);
console.log("Status:", response.statusText);
} catch (err) {
console.log("Status: Failed");
console.log("Error:", err.message);
}
}
main();
Username/Password Authentication (Node.js, CommonJS, compatible with latest packages):
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const fetch = require('node-fetch'); // Make sure you have node-fetch v2 installed
// Proxy and test details
const proxyUser = "your_username";
const proxyPass = "your_password";
const proxyHost = "your_proxy_host";
const proxyPort = "your_proxy_port";
const testUrl = "https://example.com"; // Set your test URL here
const uaUrl = "https://customers.trustedproxies.com/downloads/desktop_useragents.txt";
async function getRandomUserAgent() {
try {
const res = await fetch(uaUrl, {timeout: 10000});
const text = await res.text();
const userAgents = text.split('\n').map(ua => ua.trim()).filter(Boolean);
return userAgents[Math.floor(Math.random() * userAgents.length)];
} catch (err) {
throw new Error("Failed to fetch user agents: " + err);
}
}
async function main() {
const userAgent = await getRandomUserAgent();
const proxyUrl = `http://${proxyUser}:${proxyPass}@${proxyHost}:${proxyPort}`;
const agent = new HttpsProxyAgent(proxyUrl);
try {
const response = await axios.get(testUrl, {
httpsAgent: agent,
httpAgent: agent,
headers: { 'User-Agent': userAgent },
timeout: 10000,
validateStatus: () => true
});
console.log("Testing proxy with username/password authentication...");
console.log("Status Code:", response.status);
console.log("Status:", response.statusText);
} catch (err) {
console.log("Status: Failed");
console.log("Error:", err.message);
}
}
main();