2026-03-01 22:18:06 +07:00
|
|
|
/**
|
|
|
|
|
* proxy-patch.cjs
|
|
|
|
|
*
|
2026-03-01 22:33:45 +07:00
|
|
|
* Simple proxy patch using https-proxy-agent.
|
|
|
|
|
* Only routes Polymarket domains through proxy.
|
2026-03-01 22:18:06 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const PROXY_URL = process.env.PROXY_URL || '';
|
|
|
|
|
|
|
|
|
|
if (PROXY_URL) {
|
2026-03-01 22:33:45 +07:00
|
|
|
const { HttpsProxyAgent } = require('https-proxy-agent');
|
2026-03-01 22:22:20 +07:00
|
|
|
const https = require('https');
|
2026-03-01 22:27:18 +07:00
|
|
|
const http = require('http');
|
2026-03-01 22:18:06 +07:00
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
const agent = new HttpsProxyAgent(PROXY_URL);
|
2026-03-01 22:22:20 +07:00
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Polymarket domains
|
|
|
|
|
const POLY_DOMAINS = [
|
2026-03-01 22:18:06 +07:00
|
|
|
'polymarket.com',
|
|
|
|
|
'clob.polymarket.com',
|
|
|
|
|
'gamma-api.polymarket.com',
|
|
|
|
|
'data-api.polymarket.com',
|
2026-03-01 22:33:45 +07:00
|
|
|
];
|
2026-03-01 22:18:06 +07:00
|
|
|
|
2026-03-01 22:27:18 +07:00
|
|
|
function shouldProxy(hostname) {
|
2026-03-01 22:33:45 +07:00
|
|
|
if (!hostname) return false;
|
|
|
|
|
for (const domain of POLY_DOMAINS) {
|
|
|
|
|
if (hostname === domain || hostname.endsWith('.' + domain)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-03-01 22:18:06 +07:00
|
|
|
}
|
2026-03-01 22:27:18 +07:00
|
|
|
return false;
|
2026-03-01 22:18:06 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Store originals
|
2026-03-01 22:27:18 +07:00
|
|
|
const originalHttpsRequest = https.request;
|
|
|
|
|
const originalHttpRequest = http.request;
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Patch https.request
|
2026-03-01 22:22:20 +07:00
|
|
|
https.request = function(options, callback) {
|
2026-03-01 22:27:18 +07:00
|
|
|
let hostname;
|
|
|
|
|
|
2026-03-01 22:22:20 +07:00
|
|
|
if (typeof options === 'string') {
|
2026-03-01 22:27:18 +07:00
|
|
|
try {
|
2026-03-01 22:33:45 +07:00
|
|
|
const url = new URL(options);
|
|
|
|
|
hostname = url.hostname;
|
2026-03-01 22:27:18 +07:00
|
|
|
} catch {
|
|
|
|
|
return originalHttpsRequest.apply(https, arguments);
|
2026-03-01 22:22:20 +07:00
|
|
|
}
|
2026-03-01 22:33:45 +07:00
|
|
|
} else {
|
|
|
|
|
hostname = options.hostname || options.host;
|
|
|
|
|
if (typeof hostname === 'string' && hostname.includes(':')) {
|
|
|
|
|
hostname = hostname.split(':')[0];
|
|
|
|
|
}
|
2026-03-01 22:22:20 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Only proxy Polymarket
|
|
|
|
|
if (!shouldProxy(hostname)) {
|
2026-03-01 22:27:18 +07:00
|
|
|
return originalHttpsRequest.apply(https, arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// 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,
|
2026-03-01 22:27:18 +07:00
|
|
|
};
|
2026-03-01 22:33:45 +07:00
|
|
|
if (callback) {
|
|
|
|
|
return originalHttpsRequest.call(https, newOptions, callback);
|
2026-03-01 22:27:18 +07:00
|
|
|
}
|
2026-03-01 22:33:45 +07:00
|
|
|
return originalHttpsRequest.call(https, newOptions);
|
|
|
|
|
} else {
|
|
|
|
|
// Options object - just add agent
|
|
|
|
|
options.agent = agent;
|
|
|
|
|
return originalHttpsRequest.apply(https, arguments);
|
|
|
|
|
}
|
2026-03-01 22:18:06 +07:00
|
|
|
};
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Patch http.request (for HTTP proxy connections)
|
2026-03-01 22:27:18 +07:00
|
|
|
http.request = function(options, callback) {
|
|
|
|
|
let hostname;
|
|
|
|
|
|
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
|
try {
|
2026-03-01 22:33:45 +07:00
|
|
|
const url = new URL(options);
|
|
|
|
|
hostname = url.hostname;
|
2026-03-01 22:27:18 +07:00
|
|
|
} catch {
|
|
|
|
|
return originalHttpRequest.apply(http, arguments);
|
|
|
|
|
}
|
2026-03-01 22:33:45 +07:00
|
|
|
} else {
|
|
|
|
|
hostname = options.hostname || options.host;
|
2026-03-01 22:27:18 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// Only proxy Polymarket
|
|
|
|
|
if (!shouldProxy(hostname)) {
|
2026-03-01 22:27:18 +07:00
|
|
|
return originalHttpRequest.apply(http, arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
// 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);
|
2026-03-01 22:27:18 +07:00
|
|
|
}
|
2026-03-01 22:22:20 +07:00
|
|
|
};
|
|
|
|
|
|
2026-03-01 22:33:45 +07:00
|
|
|
console.log('[proxy-patch] Proxy active for Polymarket domains');
|
2026-03-01 22:18:06 +07:00
|
|
|
}
|