From dd023b59ee6bcfa6fe4e24f81668bbe07171f3b2 Mon Sep 17 00:00:00 2001 From: Martijn Walraven Date: Sun, 9 Aug 2015 15:43:00 +0200 Subject: [PATCH] Remove unused HttpProxy and related config options --- .eslintignore | 1 - tools/cli/commands.js | 19 --- tools/cordova/build.js | 1 - tools/runners/run-all.js | 18 --- tools/runners/run-httpproxy.js | 278 --------------------------------- 5 files changed, 317 deletions(-) delete mode 100644 tools/runners/run-httpproxy.js diff --git a/.eslintignore b/.eslintignore index 17f6f08904..db64bfa72b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -64,7 +64,6 @@ tools/tool-env/profile.js tools/runners/run-all.js tools/runners/run-app.js -tools/runners/run-httpproxy.js tools/runners/run-mongo.js tools/runners/run-proxy.js tools/runners/run-selenium.js diff --git a/tools/cli/commands.js b/tools/cli/commands.js index 21b44cc8e8..5c3a41ffcb 100644 --- a/tools/cli/commands.js +++ b/tools/cli/commands.js @@ -261,7 +261,6 @@ var runCommandOptions = { // XXX COMPAT WITH 0.9.2.2 'mobile-port': { type: String }, 'app-port': { type: String }, - 'http-proxy-port': { type: String }, 'debug-port': { type: String }, production: { type: Boolean }, 'raw-logs': { type: Boolean }, @@ -297,8 +296,6 @@ function doRunCommand(options) { const { serverUrl, mobileServerUrl } = parseServerOptionsForRunCommand(options); - options.httpProxyPort = options['http-proxy-port']; - var projectContext = new projectContextModule.ProjectContext({ projectDir: options.appDir, allowIncompatibleUpdate: options['allow-incompatible-update'], @@ -334,12 +331,6 @@ function doRunCommand(options) { options.once = true; } - // For this release; we won't force-enable the httpProxy - if (false) { //!options.httpProxyPort) { - console.log('Forcing http proxy on port 3002 for mobile'); - options.httpProxyPort = '3002'; - } - Console.debug('Will compile mobile builds'); // Run the constraint solver and build local packages. // XXX This code should be part of the main runner loop so that we can @@ -413,7 +404,6 @@ function doRunCommand(options) { projectContext: projectContext, proxyPort: serverUrl.port, proxyHost: serverUrl.host, - httpProxyPort: options.httpProxyPort, appPort: appPort, appHost: appHost, debugPort: options['debug-port'], @@ -1404,7 +1394,6 @@ main.registerCommand({ maxArgs: Infinity, options: { port: { type: String, short: "p", default: DEFAULT_PORT }, - 'http-proxy-port': { type: String }, 'mobile-server': { type: String }, // XXX COMPAT WITH 0.9.2.2 'mobile-port': { type: String }, @@ -1457,8 +1446,6 @@ main.registerCommand({ const { serverUrl, mobileServerUrl } = parseServerOptionsForRunCommand(options); - options.httpProxyPort = options['http-proxy-port']; - // Find any packages mentioned by a path instead of a package name. We will // load them explicitly into the catalog. var packagesByPath = _.filter(options.args, function (p) { @@ -1536,11 +1523,6 @@ main.registerCommand({ if (! _.isEmpty(mobileTargets)) { var runners = []; - // For this release; we won't force-enable the httpProxy - if (false) { //!options.httpProxyPort) { - console.log('Forcing http proxy on port 3002 for mobile'); - options.httpProxyPort = '3002'; - } var platforms = platformsForTargets(mobileTargets); projectContext.platformList.write(platforms); @@ -1669,7 +1651,6 @@ var runTestAppForPackages = function (projectContext, options) { return runAll.run({ projectContext: projectContext, proxyPort: options.port, - httpProxyPort: options.httpProxyPort, debugPort: options['debug-port'], disableOplog: options['disable-oplog'], settingsFile: options.settings, diff --git a/tools/cordova/build.js b/tools/cordova/build.js index 458cd280d1..1ea75a148d 100644 --- a/tools/cordova/build.js +++ b/tools/cordova/build.js @@ -175,7 +175,6 @@ function generateCordovaBoilerplate(projectContext, clientDir, options) { DDP_DEFAULT_CONNECTION_URL: mobileServer, autoupdateVersionCordova: version, cleanCache: options.clean, - httpProxyPort: options.httpProxyPort, appId: projectContext.appIdentifier }; diff --git a/tools/runners/run-all.js b/tools/runners/run-all.js index 86a1026841..3bca359102 100644 --- a/tools/runners/run-all.js +++ b/tools/runners/run-all.js @@ -11,7 +11,6 @@ const Console = require('../console/console.js').Console; const Proxy = require('./run-proxy.js').Proxy; const Selenium = require('./run-selenium.js').Selenium; -const HttpProxy = require('./run-httpproxy.js').HttpProxy; const AppRunner = require('./run-app.js').AppRunner; const MongoRunner = require('./run-mongo.js').MongoRunner; const Updater = require('./run-updater.js').Updater; @@ -23,7 +22,6 @@ class Runner { banner, disableOplog, extraRunners, - httpProxyPort, mongoUrl, onFailure, oplogUrl, @@ -72,13 +70,6 @@ class Runner { onFailure }); - self.httpProxy = null; - if (httpProxyPort) { - self.httpProxy = new HttpProxy({ - listenPort: httpProxyPort - }); - } - self.mongoRunner = null; if (mongoUrl) { oplogUrl = disableOplog ? null : oplogUrl; @@ -142,14 +133,6 @@ class Runner { self.updater.start(); } - // print the banner only once we've successfully bound the port - if (! self.stopped && self.httpProxy) { - self.httpProxy.start(); - if (! self.quiet) { - runLog.log("Started http proxy.", { arrow: true }); - } - } - _.forEach(self.extraRunners, function (extraRunner) { if (! self.stopped) { const title = extraRunner.title; @@ -219,7 +202,6 @@ class Runner { self.stopped = true; self.proxy.stop(); - self.httpProxy && self.httpProxy.stop(); self.updater.stop(); self.mongoRunner && self.mongoRunner.stop(); _.forEach(self.extraRunners, function (extraRunner) { diff --git a/tools/runners/run-httpproxy.js b/tools/runners/run-httpproxy.js deleted file mode 100644 index 8646415804..0000000000 --- a/tools/runners/run-httpproxy.js +++ /dev/null @@ -1,278 +0,0 @@ -// The HTTP proxy is primarily so we can use localhost:3000 with OAuth, -// on devices which don't run a webserver e.g. Android / iOS -// This is a generic HTTP proxy, like a mini-Squid -// (whereas run-proxy.js is just for our app) -var _ = require('underscore'); -var Future = require('fibers/future'); -var runLog = require('./run-log.js'); - -// options: listenPort, listenHost, onFailure -var HttpProxy = function (options) { - var self = this; - - self.listenPort = options.listenPort; - self.listenHost = options.listenHost; - - self.onFailure = options.onFailure || function () {}; - - self.mode = "proxy"; - self.httpQueue = []; // keys: req, res - self.websocketQueue = []; // keys: req, socket, head - self.connectQueue = []; // keys: req, socket, head - - self.proxy = null; - self.server = null; -}; - -_.extend(HttpProxy.prototype, { - // Start the proxy server, block (yield) until it is ready to go - // (actively listening on outer and proxying to inner), and then - // return. - start: function () { - var self = this; - - if (self.server) - throw new Error("already running?"); - - self.started = false; - - var http = require('http'); - var net = require('net'); - var httpProxy = require('http-proxy'); - - self.proxy = httpProxy.createProxyServer({ - // agent is required to handle keep-alive, and http-proxy 1.0 is a little - // buggy without it: https://github.com/nodejitsu/node-http-proxy/pull/488 - agent: new http.Agent({ maxSockets: 1000 }), - xfwd: false //true - }); - - var server = self.server = http.createServer(function (req, res) { - // Normal HTTP request - self.httpQueue.push({ req: req, res: res }); - self._tryHandleConnections(); - }); - - self.server.on('connect', function (req, socket, head) { - self.connectQueue.push({ req: req, socket: socket, head: head }); - self._tryHandleConnections(); - }); - - self.server.on('upgrade', function (req, socket, head) { - // Websocket connection - self.websocketQueue.push({ req: req, socket: socket, head: head }); - self._tryHandleConnections(); - }); - - var fut = new Future; - self.server.on('error', function (err) { - if (err.code === 'EADDRINUSE') { - var port = self.listenPort; - runLog.log( - "HTTP proxy server can't listen on port " + port + ". \n" + - "If something else is using port " + port + ", you can\n" + - "specify an alternative port with --http-proxy-port ."); - } else if (self.listenHost && - (err.code === 'ENOTFOUND' || err.code === 'EADDRNOTAVAIL')) { - // This handles the case of "entered a DNS name that's unknown" - // (ENOTFOUND from getaddrinfo) and "entered some random IP that we - // can't bind to" (EADDRNOTAVAIL from listen). - runLog.log( - "Can't listen on host " + self.listenHost + - " (" + err.code + " from " + err.syscall + ")."); - } else { - runLog.log('' + err); - } - self.onFailure(); - // Allow start() to return. - fut.isResolved() || fut['return'](); - }); - - // Don't crash if the app doesn't respond; instead return an error - // immediately. - self.proxy.on('error', function (err, req, resOrSocket) { - if (resOrSocket instanceof http.ServerResponse) { - resOrSocket.writeHead(503, { - 'Content-Type': 'text/plain' - }); - resOrSocket.end('Unexpected error.'); - } else if (resOrSocket instanceof net.Socket) { - resOrSocket.end(); - } - }); - - self.server.listen(self.listenPort, self.listenHost || '0.0.0.0', function () { - if (self.server) { - self.started = true; - } else { - // stop() got called while we were invoking listen! Close the server (we - // still have the var server). The rest of the cleanup shouldn't be - // necessary. - server.close(); - } - fut.isResolved() || fut['return'](); - }); - - fut.wait(); - }, - - // Idempotent. - stop: function () { - var self = this; - - if (! self.server) - return; - - if (! self.started) { - // This probably means that we failed to listen. However, there could be a - // race condition and we could be in the middle of starting to listen! In - // that case, the listen callback will notice that we nulled out server - // here. - self.server = null; - return; - } - - // This stops listening but allows existing connections to - // complete gracefully. - self.server.close(); - self.server = null; - - // It doesn't seem to be necessary to do anything special to - // destroy an httpProxy proxyserver object. - self.proxy = null; - - // Drop any held connections. - _.each(self.httpQueue, function (c) { - c.res.statusCode = 500; - c.res.end(); - }); - self.httpQueue = []; - - _.each(self.websocketQueue, function (c) { - c.socket.destroy(); - }); - self.websocketQueue = []; - - _.each(self.connectQueue, function (c) { - c.socket.destroy(); - }); - self.connectQueue = []; - - self.mode = "hold"; - }, - - _tryHandleConnections: function () { - var self = this; - - while (self.httpQueue.length) { - if (self.mode !== "proxy") - break; - - var c = self.httpQueue.shift(); - var req = c.req; - var targetUrl = req.url; - runLog.log("Proxy request: " + req.method + " " +req.url); - self.proxy.web(c.req, c.res, { - target: targetUrl - }); - } - - while (self.websocketQueue.length) { - if (self.mode !== "proxy") - break; - - var c = self.websocketQueue.shift(); - var req = c.req; - var targetUrl = req.url; - runLog.log("Proxy request (websocket): " + req.method + " " +req.url); - self.proxy.ws(c.req, c.socket, c.head, { - target: targetUrl - }); - } - - while (self.connectQueue.length) { - if (self.mode !== "proxy") - break; - - var c = self.connectQueue.shift(); - runLog.log("Proxy request (connect): " + c.req.method + " " + c.req.url); - proxyConnectMethod(c.req, c.socket, c.head); - } - }, - - // The proxy can be in one of three modes: - // - "proxy": connections are proxied - // - // The initial mode is "proxy". - setMode: function (mode) { - var self = this; - self.mode = mode; - self._tryHandleConnections(); - } -}); - - - -// This is what http-proxy does -// XXX: We should submit connect support upstream -var setupSocket = function(socket) { - socket.setTimeout(0); - socket.setNoDelay(true); - - socket.setKeepAlive(true, 0); - - return socket; -}; - - -var proxyConnectMethod = function (req, socket, options, head, server, clb) { - if (req.method !== 'CONNECT') { - socket.destroy(); - return true; - } - - var tokens = req.url.split(':'); - - if (tokens.length != 2) { - runLog.log("Bad request: " + req.url); - socket.destroy(); - return true; - } - - var host = tokens[0]; - var port = tokens[1]; - - if (port != 443) { - runLog.log("Blocking request to non-443 port: " + req.url); - socket.destroy(); - return true; - } - - setupSocket(socket); - - // XXX: Needed? - // if (head && head.length) socket.unshift(head); - - var net = require('net'); - var proxySocket = net.createConnection(port, host); - setupSocket(proxySocket); - - socket.on('error', function (err) { - runLog.log("Error on socket: " + err); - proxySocket.end(); - }); - proxySocket.on('error', function (err) { - runLog.log("Error on proxySocket: " + err); - socket.end(); - }); - - proxySocket.on('connect', function(connect) { - runLog.log("Connection established to " + host + ":" + port); - socket.write("HTTP/1.0 200 Connection established\n\n"); - socket.pipe(proxySocket); - proxySocket.pipe(socket); - }); -}; - - -exports.HttpProxy = HttpProxy;