-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpreload.cjs
More file actions
59 lines (50 loc) · 1.49 KB
/
preload.cjs
File metadata and controls
59 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Claude proxy preload module - CommonJS for Node.js preload
const http = require('http');
const https = require('https');
const dns = require('dns');
const PROXY_HOST = '127.0.0.1';
const PROXY_PORT = 3333;
const REDIRECT_HOSTS = [
'api.anthropic.com',
'160.79.104.10',
'34.36.57.103'
];
function shouldRedirect(host) {
if (!host) return false;
const h = host.split(':')[0];
return REDIRECT_HOSTS.some(r => h === r || h.endsWith(r));
}
// Store originals
const origHttpRequest = http.request;
const origHttpsRequest = https.request;
const origDnsLookup = dns.lookup;
// Intercept HTTP
http.request = function(opts, cb) {
const host = opts.hostname || opts.host;
if (shouldRedirect(host)) {
opts.hostname = PROXY_HOST;
opts.port = PROXY_PORT;
opts.headers = opts.headers || {};
opts.headers['X-Original-Host'] = host;
}
return origHttpRequest.apply(this, arguments);
};
// Intercept HTTPS
https.request = function(opts, cb) {
const host = opts.hostname || opts.host;
if (shouldRedirect(host)) {
opts.hostname = PROXY_HOST;
opts.port = PROXY_PORT;
opts.headers = opts.headers || {};
opts.headers['X-Original-Host'] = host;
}
return origHttpsRequest.apply(this, arguments);
};
// Intercept DNS
dns.lookup = function(hostname, opts, cb) {
if (shouldRedirect(hostname)) {
return origDnsLookup('localhost', opts, cb);
}
return origDnsLookup.apply(this, arguments);
};
console.error('[PRELOAD] Proxy redirect active for:', REDIRECT_HOSTS.join(', '));