From f302bfc8f81156ddfe7bac8d4af6f5e3f3d9be8b Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 17 Feb 2014 16:01:09 -0800 Subject: [PATCH] Break Aphlict's flash policy server into a separate class Summary: Ref T4324. One of the server we start just sends pre-canned XML responses. Separate it out of the main file and hand it all the objects it interacts with in structured, reasonable ways. Test Plan: Hit "Send Test Notification", saw notification, saw flash policy info in the log. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4324 Differential Revision: https://secure.phabricator.com/D8257 --- support/aphlict/server/aphlict_server.js | 29 ++------ .../server/lib/AphlictFlashPolicyServer.js | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 support/aphlict/server/lib/AphlictFlashPolicyServer.js diff --git a/support/aphlict/server/aphlict_server.js b/support/aphlict/server/aphlict_server.js index 6635a230c0..8d1bd61a03 100644 --- a/support/aphlict/server/aphlict_server.js +++ b/support/aphlict/server/aphlict_server.js @@ -8,6 +8,7 @@ var JX = require('./lib/javelin').JX; +JX.require('lib/AphlictFlashPolicyServer', __dirname); JX.require('lib/AphlictListenerList', __dirname); JX.require('lib/AphlictLog', __dirname); @@ -64,33 +65,15 @@ var http = require('http'); var url = require('url'); var querystring = require('querystring'); - process.on('uncaughtException', function (err) { - log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err); + debug.log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err); process.exit(1); }); -function getFlashPolicy() { - return [ - '', - '', - '', - '', - '' - ].join('\n'); -} - -net.createServer(function(socket) { - socket.write(getFlashPolicy() + '\0'); - socket.end(); - - debug.log('[' + socket.remoteAddress + '] Sent Flash Policy'); - - socket.on('error', function (e) { - debug.log('Error in policy server: ' + e); - }); -}).listen(843); +var flash_server = new JX.AphlictFlashPolicyServer() + .setDebugLog(debug) + .setAccessPort(config.port) + .start(); var send_server = net.createServer(function(socket) { diff --git a/support/aphlict/server/lib/AphlictFlashPolicyServer.js b/support/aphlict/server/lib/AphlictFlashPolicyServer.js new file mode 100644 index 0000000000..77cb6311f8 --- /dev/null +++ b/support/aphlict/server/lib/AphlictFlashPolicyServer.js @@ -0,0 +1,68 @@ +var JX = require('javelin').JX; + +var net = require('net'); + +/** + * Server which handles cross-domain policy requests for Flash. + * + * var server = new AphlictFlashPolicyServer() + * .setAccessPort(9999) + * .start(); + */ +JX.install('AphlictFlashPolicyServer', { + + members: { + _server: null, + _port: 843, + _accessPort: null, + _debug: null, + + setDebugLog : function(log) { + this._debug = log; + return this; + }, + + setAccessPort : function(port) { + this._accessPort = port; + return this; + }, + + start: function() { + this._server = net.createServer(JX.bind(this, this._didConnect)); + this._server.listen(this._port); + return this; + }, + + _didConnect: function(socket) { + this._log(' Policy Request From %s', socket.remoteAddress); + + socket.on('error', JX.bind(this, this._didSocketError, socket)); + + socket.write(this._getFlashPolicyResponse()); + socket.end(); + }, + + _didSocketError: function(socket, error) { + this._log(' Socket Error: %s', error); + }, + + _log: function(pattern) { + this._debug && this._debug.log.apply(this._debug, arguments); + }, + + _getFlashPolicyResponse: function() { + var policy = [ + '', + '', + '', + '', + '' + ]; + + return policy.join("\n") + "\0"; + } + + } + +});