diff --git a/box.js b/box.js index 26e952258..71ba1841b 100755 --- a/box.js +++ b/box.js @@ -13,6 +13,7 @@ var appHealthMonitor = require('./src/apphealthmonitor.js'), async = require('async'), config = require('./src/config.js'), ldap = require('./src/ldap.js'), + dockerProxy = require('./src/dockerproxy.js'), server = require('./src/server.js'); console.log(); @@ -25,6 +26,9 @@ console.log(' Version: ', config.version()); console.log(' Admin Origin: ', config.adminOrigin()); console.log(' Appstore API server origin: ', config.apiServerOrigin()); console.log(' Appstore Web server origin: ', config.webServerOrigin()); +console.log(' SysAdmin Port: ', config.get('sysadminPort')); +console.log(' LDAP Server Port: ', config.get('ldapPort')); +console.log(' Docker Proxy Port: ', config.get('dockerProxyPort')); console.log(); console.log('=========================================='); console.log(); @@ -32,6 +36,7 @@ console.log(); async.series([ server.start, ldap.start, + dockerProxy.start, appHealthMonitor.start, ], function (error) { if (error) { @@ -46,11 +51,13 @@ var NOOP_CALLBACK = function () { }; process.on('SIGINT', function () { server.stop(NOOP_CALLBACK); ldap.stop(NOOP_CALLBACK); + dockerProxy.stop(NOOP_CALLBACK); setTimeout(process.exit.bind(process), 3000); }); process.on('SIGTERM', function () { server.stop(NOOP_CALLBACK); ldap.stop(NOOP_CALLBACK); + dockerProxy.stop(NOOP_CALLBACK); setTimeout(process.exit.bind(process), 3000); }); diff --git a/src/config.js b/src/config.js index 9ea0902b7..4210e055d 100644 --- a/src/config.js +++ b/src/config.js @@ -103,7 +103,7 @@ function initConfig() { data.smtpPort = 2525; // this value comes from mail container data.sysadminPort = 3001; data.ldapPort = 3002; - data.dockerProxyPort = 5687; + data.dockerProxyPort = 3003; // keep in sync with start.sh data.database = { diff --git a/src/dockerproxy.js b/src/dockerproxy.js new file mode 100644 index 000000000..245380cdd --- /dev/null +++ b/src/dockerproxy.js @@ -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(); +} \ No newline at end of file diff --git a/src/platform.js b/src/platform.js index e7270e539..8c92929df 100644 --- a/src/platform.js +++ b/src/platform.js @@ -14,7 +14,6 @@ var apps = require('./apps.js'), debug = require('debug')('box:platform'), fs = require('fs'), hat = require('./hat.js'), - http = require('http'), infra = require('./infra_version.js'), locker = require('./locker.js'), mail = require('./mail.js'), @@ -316,35 +315,6 @@ function startApps(existingInfra, callback) { } } -function startDockerProxy(callback) { - assert.strictEqual(typeof callback, 'function'); - - function interceptor(req, res) { - debug(`dockerInterceptor: ${req.method} ${req.url}`); - return false; - } - - return 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 handleCertChanged(cn) { assert.strictEqual(typeof cn, 'string');