fix: simplify proxy patch to use HttpsProxyAgent globally

Set https.globalAgent and override https.request to use proxy
for all Polymarket domains. Ensures @polymarket/clob-client
axios calls route through proxy.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
direkturcrypto
2026-03-01 22:22:20 +07:00
parent 368a3f8da5
commit b770f831db
+49 -15
View File
@@ -6,17 +6,21 @@
* Must be imported as very first thing in the app.
* In ES modules: import './proxy-patch.cjs'
*/
const https = require('https');
const http = require('http');
const { HttpsProxyAgent } = require('https-proxy-agent');
const PROXY_URL = process.env.PROXY_URL || '';
if (PROXY_URL) {
const agent = new HttpsProxyAgent(PROXY_URL);
const originalHttpsRequest = https.request.bind(https);
// Load https-proxy-agent which patches https.request properly
const { HttpsProxyAgent } = require('https-proxy-agent');
const https = require('https');
// Polymarket domains that must go through proxy
// Create agent that routes everything through proxy
const agent = new HttpsProxyAgent(PROXY_URL);
// Store original request
const originalRequest = https.request;
// Polymarket domains
const POLY_DOMAINS = [
'polymarket.com',
'clob.polymarket.com',
@@ -33,16 +37,46 @@ if (PROXY_URL) {
}
}
// Patch https.request
https.request = function(...args) {
const url = args[0];
if (typeof url === 'string' && shouldProxy(url)) {
const options = args[1] || {};
options.agent = agent;
return originalHttpsRequest(url, options);
// Override https.request globally
https.request = function(options, callback) {
// Handle both string URL and options object
let url;
if (typeof options === 'string') {
url = options;
} else if (options.hostname || options.host) {
const protocol = options.protocol || 'https:';
const hostname = options.hostname || options.host;
const port = options.port ? `:${options.port}` : '';
const path = options.path || '/';
url = `${protocol}//${hostname}${port}${path}`;
}
return originalHttpsRequest(...args);
// If it's a Polymarket domain, force agent
if (url && shouldProxy(url)) {
if (typeof options === 'string') {
// Convert to options object with agent
options = {
agent: agent,
};
} else {
options.agent = agent;
}
}
return originalRequest.call(https, options, callback);
};
console.log(`[proxy-patch] HTTPS patched for Polymarket routing via proxy`);
// Also patch https.get
https.get = function(options, callback) {
const req = https.request(options, callback);
req.end();
return req;
};
// Set global agent as fallback
https.globalAgent = agent;
console.log(`[proxy-patch] HTTPS agent patched for Polymarket routing via proxy`);
} else {
console.log('[proxy-patch] No PROXY_URL set, skipping patch');
}