From b770f831dbd6653b07b31bcf848e747db356fbfc Mon Sep 17 00:00:00 2001 From: direkturcrypto Date: Sun, 1 Mar 2026 22:22:20 +0700 Subject: [PATCH] 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 --- src/utils/proxy-patch.cjs | 64 ++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/utils/proxy-patch.cjs b/src/utils/proxy-patch.cjs index 5115ddc..2cf5c5c 100644 --- a/src/utils/proxy-patch.cjs +++ b/src/utils/proxy-patch.cjs @@ -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'); }