proxy-connection
Version:
Proxy client with automatic connection management, health checking, and fetch-like API
50 lines • 1.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.testProxy = void 0;
const socks_proxy_agent_1 = require("socks-proxy-agent");
const https_1 = __importDefault(require("https"));
const HEALTH_CHECK_URL = process.env.HEALTH_CHECK_URL || 'https://httpbin.org/ip';
const timeout = parseInt(process.env.TIMEOUT || '5000', 10);
const testProxy = async (proxy) => {
const agent = new socks_proxy_agent_1.SocksProxyAgent(`socks5://${proxy.user}:${proxy.pass}@${proxy.ip}:${proxy.port}`);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const startTime = Date.now();
const response = await new Promise((resolve, reject) => {
const req = https_1.default.get(HEALTH_CHECK_URL, {
agent,
timeout,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
}, (res) => {
resolve({ statusCode: res.statusCode || 500 });
});
req.on('error', reject);
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timed out'));
});
});
const latency = Date.now() - startTime;
clearTimeout(timeoutId);
return {
latency,
alive: response.statusCode >= 200 && response.statusCode < 300
};
}
catch (error) {
console.log('error', error); // TODO figure out with exception
clearTimeout(timeoutId);
return {
latency: timeout,
alive: false
};
}
};
exports.testProxy = testProxy;
//# sourceMappingURL=testProxy.js.map