Move docker proxy into its own file

This commit is contained in:
Johannes Zellner
2018-08-13 21:10:53 +02:00
parent fb02e8768c
commit 441fdb81f8
4 changed files with 61 additions and 31 deletions

53
src/dockerproxy.js Normal file
View File

@@ -0,0 +1,53 @@
'use strict';
exports = module.exports = {
start: start,
stop: stop
};
var assert = require('assert'),
config = require('./config.js'),
debug = require('debug')('box:ldap'),
http = require('http'),
_ = require('underscore');
var gServer = null;
function start(callback) {
assert.strictEqual(typeof callback, 'function');
function interceptor(req, res) {
debug(`dockerInterceptor: ${req.method} ${req.url}`);
return false;
}
debug(`startDockerProxy: starting proxy on port ${config.get('dockerProxyPort')}`);
gServer = http.createServer(function (req, res) {
if (interceptor(req, res)) return;
// rejectUnauthorized should not be required but it doesn't work without it
var options = _.extend({ }, { socketPath: '/var/run/docker.sock' }, { method: req.method, path: req.url, headers: req.headers, rejectUnauthorized: false });
var dockerRequest = http.request(options, function (dockerResponse) {
res.writeHead(dockerResponse.statusCode, dockerResponse.headers);
dockerResponse.on('error', console.error);
dockerResponse.pipe(res, { end: true });
});
req.on('error', console.error);
if (!req.readable) {
dockerRequest.end();
} else {
req.pipe(dockerRequest, { end: true });
}
}).listen(config.get('dockerProxyPort'), callback);
}
function stop(callback) {
assert.strictEqual(typeof callback, 'function');
if (gServer) gServer.close();
callback();
}