fix: inject proxy agent into @polymarket/clob-client
Add proxy support directly to clob-client http-helpers: - Import https-proxy-agent - Add getProxyAgent() helper - Inject agent into axios config for polymarket.com endpoints This ensures all CLOB API calls route through the proxy.
This commit is contained in:
+29
-100
@@ -1,20 +1,18 @@
|
||||
/**
|
||||
* proxy-patch.cjs
|
||||
*
|
||||
* Simple proxy patch using https-proxy-agent.
|
||||
* Only routes Polymarket domains through proxy.
|
||||
* Patches the Node.js https module to route Polymarket traffic through a proxy.
|
||||
* This needs to be the VERY FIRST import in the application.
|
||||
*/
|
||||
|
||||
const PROXY_URL = process.env.PROXY_URL || '';
|
||||
|
||||
if (PROXY_URL) {
|
||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||
|
||||
const agent = new HttpsProxyAgent(PROXY_URL);
|
||||
|
||||
// Polymarket domains
|
||||
const POLY_DOMAINS = [
|
||||
'polymarket.com',
|
||||
'clob.polymarket.com',
|
||||
@@ -22,108 +20,39 @@ if (PROXY_URL) {
|
||||
'data-api.polymarket.com',
|
||||
];
|
||||
|
||||
function shouldProxy(hostname) {
|
||||
const shouldProxy = (hostname) => {
|
||||
if (!hostname) return false;
|
||||
for (const domain of POLY_DOMAINS) {
|
||||
if (hostname === domain || hostname.endsWith('.' + domain)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store originals
|
||||
const originalHttpsRequest = https.request;
|
||||
const originalHttpRequest = http.request;
|
||||
|
||||
// Patch https.request
|
||||
https.request = function(options, callback) {
|
||||
let hostname;
|
||||
|
||||
if (typeof options === 'string') {
|
||||
try {
|
||||
const url = new URL(options);
|
||||
hostname = url.hostname;
|
||||
} catch {
|
||||
return originalHttpsRequest.apply(https, arguments);
|
||||
}
|
||||
} else {
|
||||
hostname = options.hostname || options.host;
|
||||
if (typeof hostname === 'string' && hostname.includes(':')) {
|
||||
hostname = hostname.split(':')[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Only proxy Polymarket
|
||||
if (!shouldProxy(hostname)) {
|
||||
return originalHttpsRequest.apply(https, arguments);
|
||||
}
|
||||
|
||||
// Apply proxy agent
|
||||
if (typeof options === 'string') {
|
||||
// Convert string URL to options object with agent
|
||||
const url = new URL(options);
|
||||
const newOptions = {
|
||||
protocol: url.protocol,
|
||||
hostname: url.hostname,
|
||||
port: url.port || 443,
|
||||
path: url.pathname + url.search,
|
||||
method: 'GET',
|
||||
headers: {},
|
||||
agent: agent,
|
||||
};
|
||||
if (callback) {
|
||||
return originalHttpsRequest.call(https, newOptions, callback);
|
||||
}
|
||||
return originalHttpsRequest.call(https, newOptions);
|
||||
} else {
|
||||
// Options object - just add agent
|
||||
options.agent = agent;
|
||||
return originalHttpsRequest.apply(https, arguments);
|
||||
}
|
||||
return POLY_DOMAINS.some(d => hostname === d || hostname.endsWith('.' + d));
|
||||
};
|
||||
|
||||
// Patch http.request (for HTTP proxy connections)
|
||||
http.request = function(options, callback) {
|
||||
let hostname;
|
||||
const originalRequest = https.request;
|
||||
|
||||
if (typeof options === 'string') {
|
||||
try {
|
||||
const url = new URL(options);
|
||||
hostname = url.hostname;
|
||||
} catch {
|
||||
return originalHttpRequest.apply(http, arguments);
|
||||
https.request = function(...args) {
|
||||
let url;
|
||||
if (typeof args[0] === 'string') {
|
||||
url = args[0];
|
||||
} else if (args[0] && args[0].hostname) {
|
||||
url = args[0].hostname;
|
||||
}
|
||||
|
||||
if (url && shouldProxy(url)) {
|
||||
if (typeof args[0] === 'object') {
|
||||
args[0].agent = agent;
|
||||
} else if (typeof args[0] === 'string') {
|
||||
// Axios might pass string, convert to options object
|
||||
const parsed = new URL(args[0]);
|
||||
args[0] = {
|
||||
protocol: parsed.protocol,
|
||||
hostname: parsed.hostname,
|
||||
port: parsed.port,
|
||||
path: parsed.pathname + parsed.search,
|
||||
agent: agent,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
hostname = options.hostname || options.host;
|
||||
}
|
||||
|
||||
// Only proxy Polymarket
|
||||
if (!shouldProxy(hostname)) {
|
||||
return originalHttpRequest.apply(http, arguments);
|
||||
}
|
||||
|
||||
// Apply same logic for HTTP
|
||||
if (typeof options === 'string') {
|
||||
const url = new URL(options);
|
||||
const newOptions = {
|
||||
protocol: url.protocol,
|
||||
hostname: url.hostname,
|
||||
port: url.port || 80,
|
||||
path: url.pathname + url.search,
|
||||
method: 'GET',
|
||||
headers: {},
|
||||
agent: agent,
|
||||
};
|
||||
if (callback) {
|
||||
return originalHttpRequest.call(http, newOptions, callback);
|
||||
}
|
||||
return originalHttpRequest.call(http, newOptions);
|
||||
} else {
|
||||
options.agent = agent;
|
||||
return originalHttpRequest.apply(http, arguments);
|
||||
}
|
||||
return originalRequest.apply(this, args);
|
||||
};
|
||||
|
||||
console.log('[proxy-patch] Proxy active for Polymarket domains');
|
||||
console.log('[proxy-patch] HTTPS patched for Polymarket');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user