-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
281 lines (244 loc) · 8.13 KB
/
proxy.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env node
/*
* ഓം ബ്രഹ്മാർപ്പണം
* proxy.js
* Created: Sat Mar 21 2020 02:04:37 GMT+0530 (GMT+05:30)
* Copyright 2020 Harish Karumuthil<harish2704@gmail.com>
*/
const http = require('http');
const https = require('https');
const url = require('url');
const fs = require('fs');
const util = require('util');
const fsPromise = fs.promises;
const { dirname } = require('path');
const forge = require('node-forge');
const pki = forge.pki;
const tls = require('tls');
const generateKeyPair = util.promisify(require('crypto').generateKeyPair);
const net = require('net');
const os = require('os');
const { Readable } = require('stream')
const log = console.log.bind(console);
const httpsPort = os.tmpdir() + `/proxy-kutti-${Date.now()}.sock`
const configHome = os.homedir() + '/.config/proxy-kutti';
const configFile = process.env.PROXY_KUTTI_CONFIG || configHome + '/config';
const config = {
port: 8080,
host: '127.0.0.1',
cache_dir: os.homedir() + '/.cache/proxy-kutti',
root_ca_key: configHome + '/rootCA.key',
root_ca_cert: configHome + '/rootCA.pem',
url_rewrites: '#https?://(.*)/7.7.1908/#http://mirrors.centos/7.7.1908/# #https?://(.*)epel/7/x86_64/#http://mirror.epel/7/x86_64/#',
};
try {
Object.assign(config, require(configFile));
} catch (e) {}
Object.keys(config).forEach(function(k) {
config[k] = process.env['PROXY_KUTTI_' + k] || config[k];
});
const urlMappings = config.url_rewrites.split(' ').map(function(pattern) {
debugger;
if (pattern[0] === pattern.slice(-1)) {
const [search, replace] = pattern.slice(1, -1).split(pattern[0]);
return { search: new RegExp(search), replace };
}
throw new Error(`Invalid url_rewrite "${pattern}"`);
});
function mapUrl(origUrl) {
let out = origUrl;
let i = 0,
l = urlMappings.length,
mapping;
while (i < l) {
urlMap = urlMappings[i];
out = out.replace(urlMap.search, urlMap.replace);
i++;
}
return out;
}
const runnninRequests = {};
function untillRequestFinished( cachedFile ){
return new Promise(res => runnninRequests[ cachedFile ].on('close', res ));
}
function startNewRequest( cachedFile ){
const stream = fs.createWriteStream( cachedFile );
runnninRequests[cachedFile] = stream;
stream.on('close', () => delete runnninRequests[cachedFile] );
return stream;
}
async function getContent(httpModule, origReq, origRes) {
const origUrl = url.parse(origReq.url);
const mappedUrlStr = mapUrl(origReq.url);
const mappedUrl = url.parse(mappedUrlStr);
const mappedPort = mappedUrl.port ? ':' + mappedUrl.port : '';
const method = origReq.method;
const proto = httpModule === http ? 'http':'https';
let cachedFile = `${config.cache_dir}/${proto}/${mappedUrl.host}${mappedPort}/${method}${mappedUrl.path}`;
if( mappedUrl.path.slice(-1) === '/' ){
cachedFile += '#index.data';
} else {
cachedFile += '.data';
}
const cachedFileMeta = `${cachedFile}.meta`;
let proxyRes;
let isHit = false;
if( cachedFile in runnninRequests ){
await untillRequestFinished( cachedFile );
}
if (false !== (await fsPromise.access(cachedFileMeta).catch(() => false))) {
proxyRes = method === 'HEAD' ? Readable.from('') : fs.createReadStream(cachedFile);
Object.assign( proxyRes, JSON.parse(await fsPromise.readFile(cachedFileMeta)) );
isHit = true;
} else {
proxyRes = await new Promise(res => {
const proxyReq = httpModule.request(
{
host: origUrl.host,
port: origUrl.port,
path: origUrl.path,
method,
headers: origReq.headers,
},
res
);
origReq.pipe( proxyReq );
});
await fsPromise.mkdir(dirname(cachedFile), { recursive: true });
/**
* write metadata only if the request completed successfully
* Otherwise, partial & invalid cached content will be served next time
*/
origRes.on('finish', () => fsPromise.writeFile(cachedFileMeta, JSON.stringify({ headers: proxyRes.headers, statusCode: proxyRes.statusCode } )) )
proxyRes.pipe( startNewRequest(cachedFile));
}
console.log(`${new Date().toISOString()} ${isHit ? 'Hit!' : 'Miss'} ${method} ${origReq.url} => ${cachedFile}`);
origRes.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(origRes);
/**
* Don't let download to continue if client closes the connection before it is finished
*/
origRes.on('close', () => proxyRes.destroy() )
return proxyRes;
}
function createFakeCertificateByDomain(caKey, caCert, domain) {
const keys = pki.rsa.generateKeyPair(2048);
const cert = pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = new Date().getTime() + '';
cert.validity.notBefore = new Date();
cert.validity.notBefore.setFullYear(
cert.validity.notBefore.getFullYear() - 1
);
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 1);
var attrs = [
{
name: 'commonName',
value: domain,
},
{
name: 'organizationName',
value: 'Proxy-kutti',
},
];
cert.setSubject(attrs);
cert.setIssuer(caCert.subject.attributes);
cert.setExtensions([
{
name: 'subjectAltName',
altNames: [
{
type: 2,
value: domain,
},
],
},
{
name: 'extKeyUsage',
serverAuth: true,
},
]);
cert.sign(caKey, forge.md.sha256.create());
return {
key: forge.pki.privateKeyToPem(keys.privateKey),
cert: forge.pki.certificateToPem(cert),
};
}
function initHttpsMitmProxy() {
const caCertPath = config.root_ca_cert;
const caKeyPath = config.root_ca_key;
const caCertPem = fs.readFileSync(caCertPath);
const caKeyPem = fs.readFileSync(caKeyPath);
const caCert = forge.pki.certificateFromPem(caCertPem);
const caKey = forge.pki.decryptRsaPrivateKey(caKeyPem, 'secret');
const fakeCertObj = createFakeCertificateByDomain(caKey, caCert, 'localhost');
debugger;
const https_opts = {
key: fakeCertObj.key,
cert: fakeCertObj.cert,
SNICallback: (hostname, done) => {
let certObj = createFakeCertificateByDomain(caKey, caCert, hostname);
done(
null,
tls.createSecureContext({
key: certObj.key,
cert: certObj.cert,
})
);
},
};
const httpsProxy = https.createServer(https_opts, (req, res) => {
req.url = `https://${req.headers.host}${req.url}`;
getContent(https, req, res);
});
httpsProxy.listen( httpsPort, '127.0.0.1');
}
function main() {
const httpProxy = http.createServer(getContent.bind(null, http));
const isHttpMitmEnabled =
fs.existsSync(config.root_ca_cert) && fs.existsSync(config.root_ca_key);
let httpsMsg = '';
if (isHttpMitmEnabled === false) {
httpsMsg = `https requests are not cached since it is not configured.
Make sure that the files
${config.root_ca_cert}
${config.root_ca_key}
exists and accessible to the process.
Refer documentation more details.\n`;
} else {
initHttpsMitmProxy();
}
const util = require('util');
httpProxy.on('connect', function(req, res) {
res.write(
'HTTP/1.0 200 Connection established\r\nProxy-agent: proxy-kutti\r\n\r\n'
);
const [host, port] = isHttpMitmEnabled
? ['127.0.0.1', httpsPort]
: req.url.split(':');
var httpsProxyConnection = net.createConnection(port, host);
res.on('close', () => res.unpipe( httpsProxyConnection ));
res.on('error', () => res.unpipe( httpsProxyConnection ));
res.pipe(httpsProxyConnection);
httpsProxyConnection.pipe(res);
});
httpProxy.listen(config.port, config.host, function() {
log(`Proxy-kutti is running...
Using env variables
PROXY_KUTTI_CONFIG=${configFile}
Current Configuration ( edit ${configFile}.(json|js) or set env variable PROXY_KUTTI_<config-key>=<value> to change )
${JSON.stringify(config, null, 2).slice(2, -2)}
${httpsMsg}
Run the following command shell to start using this proxy
export http_proxy=http://${config.host}:${config.port}
${isHttpMitmEnabled? 'export https_proxy=http://'+config.host+':'+config.port: ''}
`);
});
}
if (require.main === module) {
main();
process.on('uncaughtException', function (err) {
log(err);
})
}