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:
co-authored by
Claude Opus 4.6
parent
368a3f8da5
commit
b770f831db
+49
-15
@@ -6,17 +6,21 @@
|
|||||||
* Must be imported as very first thing in the app.
|
* Must be imported as very first thing in the app.
|
||||||
* In ES modules: import './proxy-patch.cjs'
|
* 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 || '';
|
const PROXY_URL = process.env.PROXY_URL || '';
|
||||||
|
|
||||||
if (PROXY_URL) {
|
if (PROXY_URL) {
|
||||||
const agent = new HttpsProxyAgent(PROXY_URL);
|
// Load https-proxy-agent which patches https.request properly
|
||||||
const originalHttpsRequest = https.request.bind(https);
|
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 = [
|
const POLY_DOMAINS = [
|
||||||
'polymarket.com',
|
'polymarket.com',
|
||||||
'clob.polymarket.com',
|
'clob.polymarket.com',
|
||||||
@@ -33,16 +37,46 @@ if (PROXY_URL) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch https.request
|
// Override https.request globally
|
||||||
https.request = function(...args) {
|
https.request = function(options, callback) {
|
||||||
const url = args[0];
|
// Handle both string URL and options object
|
||||||
if (typeof url === 'string' && shouldProxy(url)) {
|
let url;
|
||||||
const options = args[1] || {};
|
if (typeof options === 'string') {
|
||||||
options.agent = agent;
|
url = options;
|
||||||
return originalHttpsRequest(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');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user