aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/socket.io/node_modules/policyfile/lib/server.js
blob: a525772bebb2eae7864c130c674452e96f8c7f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * Module dependencies and cached references.
 */

var slice = Array.prototype.slice
  , net = require('net');

/**
 * The server that does the Policy File severing
 *
 * Options:
 *   - `log`  false or a function that can output log information, defaults to console.log?
 *
 * @param {Object} options Options to customize the servers functionality.
 * @param {Array} origins The origins that are allowed on this server, defaults to `*:*`.
 * @api public
 */

function Server (options, origins) {
  var me = this;

  this.origins = origins || ['*:*'];
  this.port = 843;
  this.log = console.log;

  // merge `this` with the options
  Object.keys(options).forEach(function (key) {
    me[key] && (me[key] = options[key])
  });

  // create the net server
  this.socket = net.createServer(function createServer (socket) {
    socket.on('error', function socketError () { 
      me.responder.call(me, socket);
    });

    me.responder.call(me, socket);
  });

  // Listen for errors as the port might be blocked because we do not have root priv.
  this.socket.on('error', function serverError (err) {
    // Special and common case error handling
    if (err.errno == 13) {
      me.log && me.log(
        'Unable to listen to port `' + me.port + '` as your Node.js instance does not have root privileges. ' +
        (
          me.server
          ? 'The Flash Policy File requests will only be served inline over the supplied HTTP server. Inline serving is slower than a dedicated server instance.'
          : 'No fallback server supplied, we will be unable to answer Flash Policy File requests.'
        )
      );

      me.emit('connect_failed', err);
      me.socket.removeAllListeners();
      delete me.socket;
    } else {
      me.log && me.log('FlashPolicyFileServer received an error event:\n' + (err.message ? err.message : err));
    }
  });

  this.socket.on('timeout', function serverTimeout () {});
  this.socket.on('close', function serverClosed (err) {
    err && me.log && me.log('Server closing due to an error: \n' + (err.message ? err.message : err));

    if (me.server) {
      // Remove the inline policy listener if we close down
      // but only when the server was `online` (see listen prototype)
      if (me.server['@'] && me.server.online) {
        me.server.removeListener('connection', me.server['@']);
      }

      // not online anymore
      delete me.server.online;
    }
  });

  // Compile the initial `buffer`
  this.compile();
}

/**
 * Start listening for requests
 *
 * @param {Number} port The port number it should be listening to.
 * @param {Server} server A HTTP server instance, this will be used to listen for inline requests
 * @param {Function} cb The callback needs to be called once server is ready
 * @api public
 */

Server.prototype.listen = function listen (port, server, cb){
  var me = this
    , args = slice.call(arguments, 0)
    , callback;
 
  // assign the correct vars, for flexible arguments
  args.forEach(function args (arg){
    var type = typeof arg;

    if (type === 'number') me.port = arg;
    if (type === 'function') callback = arg;
    if (type === 'object') me.server = arg;
  });

  if (this.server) {

    // no one in their right mind would ever create a `@` prototype, so Im just gonna store
    // my function on the server, so I can remove it later again once the server(s) closes
    this.server['@'] = function connection (socket) {
      socket.once('data', function requestData (data) {
        // if it's a Flash policy request, and we can write to the 
        if (
             data
          && data[0] === 60
          && data.toString() === '<policy-file-request/>\0'
          && socket
          && (socket.readyState === 'open' || socket.readyState === 'writeOnly')
        ){
          // send the buffer
          try {
            socket.end(me.buffer);
          } catch (e) {}
        }
      });
    };

    // attach it
    this.server.on('connection', this.server['@']);
  }

  // We add a callback method, so we can set a flag for when the server is `enabled` or `online`.
  // this flag is needed because if a error occurs and the we cannot boot up the server the
  // fallback functionality should not be removed during the `close` event
  this.port >= 0 && this.socket.listen(this.port, function serverListening () {
    me.socket.online = true;
    if (callback) {
      callback.call(me);
      callback = undefined;
    }
  });

  return this;
};

/**
 * Responds to socket connects and writes the compile policy file.
 *
 * @param {net.Socket} socket The socket that needs to receive the message
 * @api private
 */

Server.prototype.responder = function responder (socket){
  if (socket && socket.readyState == 'open' && socket.end) {
    try {
      socket.end(this.buffer);
    } catch (e) {}
  }
};

/**
 * Compiles the supplied origins to a Flash Policy File format and stores it in a Node.js Buffer
 * this way it can be send over the wire without any performance loss.
 *
 * @api private
 */

Server.prototype.compile = function compile (){
  var xml = [
        '<?xml version="1.0"?>'
      , '<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">'
      , '<cross-domain-policy>'
    ];

  // add the allow access element
  this.origins.forEach(function origin (origin){
    var parts = origin.split(':');
    xml.push('<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>');
  });

  xml.push('</cross-domain-policy>');

  // store the result in a buffer so we don't have to re-generate it all the time
  this.buffer = new Buffer(xml.join(''), 'utf8');

  return this;
};

/**
 * Adds a new origin to the Flash Policy File.
 *
 * @param {Arguments} The origins that need to be added.
 * @api public
 */

Server.prototype.add = function add(){
  var args = slice.call(arguments, 0)
    , i = args.length;

  // flag duplicates
  while (i--) {
    if (this.origins.indexOf(args[i]) >= 0){
      args[i] = null;
    }
  }

  // Add all the arguments to the array
  // but first we want to remove all `falsy` values from the args
  Array.prototype.push.apply(
    this.origins
  , args.filter(function filter (value) {
      return !!value;
    })
  );

  this.compile();
  return this;
};

/**
 * Removes a origin from the Flash Policy File.
 *
 * @param {String} origin The origin that needs to be removed from the server
 * @api public
 */

Server.prototype.remove = function remove (origin){
  var position = this.origins.indexOf(origin);

  // only remove and recompile if we have a match
  if (position > 0) {
    this.origins.splice(position,1);
    this.compile();
  }

  return this;
};

/**
 * Closes and cleans up the server
 *
 * @api public
 */

Server.prototype.close = function close () {
  this.socket.removeAllListeners();
  this.socket.close();

  return this;
};

/**
 * Proxy the event listener requests to the created Net server
 */

Object.keys(process.EventEmitter.prototype).forEach(function proxy (key){
  Server.prototype[key] = Server.prototype[key] || function () {
    if (this.socket) {
      this.socket[key].apply(this.socket, arguments);
    }

    return this;
  };
});

/**
 * Creates a new server instance.
 *
 * @param {Object} options A options object to override the default config
 * @param {Array} origins The origins that should be allowed by the server
 * @api public
 */

exports.createServer = function createServer(options, origins){
  origins = Array.isArray(origins) ? origins : (Array.isArray(options) ? options : false);
  options = !Array.isArray(options) && options ? options : {};

  return new Server(options, origins);
};

/**
 * Provide a hook to the original server, so it can be extended if needed.
 */

exports.Server = Server;

/**
 * Module version
 */

exports.version = '0.0.4';