2018-08-13 21:10:53 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
start: start,
|
|
|
|
|
stop: stop
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
config = require('./config.js'),
|
2018-08-13 22:06:28 +02:00
|
|
|
debug = require('debug')('box:dockerproxy'),
|
2018-08-13 22:01:51 +02:00
|
|
|
http = require('http');
|
2018-08-13 21:10:53 +02:00
|
|
|
|
|
|
|
|
var gServer = null;
|
|
|
|
|
|
|
|
|
|
function start(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
function interceptor(req, res) {
|
2018-08-13 22:06:28 +02:00
|
|
|
debug(`request: ${req.method} ${req.url}`);
|
2018-08-13 21:10:53 +02:00
|
|
|
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
|
2018-08-13 22:01:51 +02:00
|
|
|
var options = {
|
|
|
|
|
socketPath: '/var/run/docker.sock',
|
|
|
|
|
method: req.method,
|
|
|
|
|
path: req.url,
|
|
|
|
|
headers: req.headers,
|
|
|
|
|
rejectUnauthorized: false
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-13 21:10:53 +02:00
|
|
|
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();
|
|
|
|
|
}
|