2 Commits
Author SHA1 Message Date
harish2704 57bf637364 V1.0.1 2020-04-03 03:21:55 +05:30
harish2704 b39ae2ad39 Fix plumbing 2020-04-03 03:21:27 +05:30
2 changed files with 11 additions and 7 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "proxy-kutti", "name": "proxy-kutti",
"version": "1.0.0", "version": "1.0.1",
"description": "A Simple and transparent caching forward proxy with minimal dependencies. Cached files are stored in filesystem by preserving the structure. Cache never expires. Useful for caching various package manager files like rpm, deb etc with in local network, especially while building containers", "description": "A Simple and transparent caching forward proxy with minimal dependencies. Cached files are stored in filesystem by preserving the structure. Cache never expires. Useful for caching various package manager files like rpm, deb etc with in local network, especially while building containers",
"main": "proxy.js", "main": "proxy.js",
"repository": "https://github.com/harish2704/node-proxy-kutti", "repository": "https://github.com/harish2704/node-proxy-kutti",
+10 -6
View File
@@ -19,7 +19,10 @@ const tls = require('tls');
const generateKeyPair = util.promisify(require('crypto').generateKeyPair); const generateKeyPair = util.promisify(require('crypto').generateKeyPair);
const net = require('net'); const net = require('net');
const os = require('os'); const os = require('os');
const { Readable } = require('stream')
const log = console.log.bind(console);
const httpsPort = os.tmpdir() + `/proxy-kutti-${Date.now()}.sock` const httpsPort = os.tmpdir() + `/proxy-kutti-${Date.now()}.sock`
const configHome = os.homedir() + '/.config/proxy-kutti'; const configHome = os.homedir() + '/.config/proxy-kutti';
const configFile = process.env.PROXY_KUTTI_CONFIG || configHome + '/config'; const configFile = process.env.PROXY_KUTTI_CONFIG || configHome + '/config';
@@ -29,7 +32,7 @@ const config = {
cache_dir: os.homedir() + '/.cache/proxy-kutti', cache_dir: os.homedir() + '/.cache/proxy-kutti',
root_ca_key: configHome + '/rootCA.key', root_ca_key: configHome + '/rootCA.key',
root_ca_cert: configHome + '/rootCA.pem', root_ca_cert: configHome + '/rootCA.pem',
url_rewrites: '#http[s]://(.*)/7.7.1908/#http://mirrors.centos/7.7.1908/# #http[s]://(.*)epel/7/x86_64/#http://mirror.epel/7/x86_64/#', url_rewrites: '#https?://(.*)/7.7.1908/#http://mirrors.centos/7.7.1908/# #https?://(.*)epel/7/x86_64/#http://mirror.epel/7/x86_64/#',
}; };
try { try {
@@ -96,7 +99,7 @@ async function getContent(httpModule, origReq, origRes) {
await untillRequestFinished( cachedFile ); await untillRequestFinished( cachedFile );
} }
if (false !== (await fsPromise.access(cachedFileMeta).catch(() => false))) { if (false !== (await fsPromise.access(cachedFileMeta).catch(() => false))) {
proxyRes = fs.createReadStream(cachedFile); proxyRes = method === 'HEAD' ? Readable.from('') : fs.createReadStream(cachedFile);
Object.assign( proxyRes, JSON.parse(await fsPromise.readFile(cachedFileMeta)) ); Object.assign( proxyRes, JSON.parse(await fsPromise.readFile(cachedFileMeta)) );
isHit = true; isHit = true;
} else { } else {
@@ -115,7 +118,7 @@ async function getContent(httpModule, origReq, origRes) {
}); });
await fsPromise.mkdir(dirname(cachedFile), { recursive: true }); await fsPromise.mkdir(dirname(cachedFile), { recursive: true });
/** /**
* write metda data only if the request completed successfully * write metadata only if the request completed successfully
* Otherwise, partial & invalid cached content will be served next time * Otherwise, partial & invalid cached content will be served next time
*/ */
origRes.on('finish', () => fsPromise.writeFile(cachedFileMeta, JSON.stringify({ headers: proxyRes.headers, statusCode: proxyRes.statusCode } )) ) origRes.on('finish', () => fsPromise.writeFile(cachedFileMeta, JSON.stringify({ headers: proxyRes.headers, statusCode: proxyRes.statusCode } )) )
@@ -246,13 +249,14 @@ function main() {
? ['127.0.0.1', httpsPort] ? ['127.0.0.1', httpsPort]
: req.url.split(':'); : req.url.split(':');
var httpsProxyConnection = net.createConnection(port, host); var httpsProxyConnection = net.createConnection(port, host);
res.on('close', () => httpsProxyConnection.destroy()); res.on('close', () => res.unpipe( httpsProxyConnection ));
res.on('error', () => res.unpipe( httpsProxyConnection ));
res.pipe(httpsProxyConnection); res.pipe(httpsProxyConnection);
httpsProxyConnection.pipe(res); httpsProxyConnection.pipe(res);
}); });
httpProxy.listen(config.port, config.host, function() { httpProxy.listen(config.port, config.host, function() {
console.log(`Proxy-kutti is running... log(`Proxy-kutti is running...
Using env variables Using env variables
PROXY_KUTTI_CONFIG=${configFile} PROXY_KUTTI_CONFIG=${configFile}
@@ -272,6 +276,6 @@ Run the following command shell to start using this proxy
if (require.main === module) { if (require.main === module) {
main(); main();
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {
console.log(err); log(err);
}) })
} }