aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/server.js
blob: 00f915ba57e9a3522a0b3ecd1ab198eb66d6c14c (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
var http = require('http')
  , https = require('https')
  , sio = require('socket.io')  
  , fs = require('fs')
  //, RedisStore = sio.RedisStore;

//
// Load configuration file
var config = JSON.parse(
  fs.readFileSync(__dirname + "/config.json").toString().replace(
    new RegExp("\\/\\*(.|\\r|\\n)*?\\*\\/", "g"),
    "" // strip out comments
  )
);

//
// Create server instance
var server = (config.ssl && config.ssl.enabled ?
  // HTTPS
  https.createServer({
      key: fs.readFileSync(config.ssl.key)
    , cert: fs.readFileSync(config.ssl.cert)
  }) :
  // HTTP
  http.createServer())  
    .listen(config.port, '127.0.0.1', function() {
      var addr = server.address();
      console.log('Symple server listening on ' + (config.ssl && config.ssl.enabled ? 'https' : 'http')  + '://' + addr.address + ':' + addr.port);
    });


var io = sio.listen(server);
io.set('log level', 1);

//
// Socket.IO Configuration
io.configure(function () {

  if (config.redis) {
  
    // Initialize the redis store
    var store = new sio.RedisStore({
      nodeID: config.nodeId     || 1,
      redisPub: config.redis    || {},
      redisSub: config.redis    || {},
      redisClient: config.redis || {}
    });
      
    // Authenticate redis connections if required
    if (config.redis && config.redis.password) {
      store.pub.auth(config.redis.password)
      store.sub.auth(config.redis.password)
      store.cmd.auth(config.redis.password)
    }
    
    io.set('store', store); 
  }
});


//
// Globals
//

function isfunc(obj) {
  return !!(obj && obj.constructor && obj.call && obj.apply);
}

function respond(ack, status, message, data) {
  if (ack && isfunc(ack)) {
    res = {}
    res.type = 'response';
    res.status = status;
    res.message = message;
    if (data)
      res.data = data.data ? data.data : data;
    console.log('Responding: ', res);    
    ack(res);
  }
}

// Parses a Symple endpoint address with the following 
// format: user@group/id
function parseAddress(str) {
    var addr = {}, base,
        arr = str.split("/")
        
    if (arr.length < 2) // no id
        base = str;        
    else { // has id
        addr.id = arr[1];   
        base = arr[0];   
    }
    
    arr = base.split("@")
    if (arr.length < 2) // group only
        addr.group = base;         
    else { // group and user
        addr.user = arr[0];
        addr.group  = arr[1];
    }
        
    return addr;
}

function buildAddress(peer) {
    return peer.user + "@" + peer.group + "/" + peer.id;
}


//
// Socket.IO Socket extensions
//

sio.Socket.prototype.authorize = function(req, fn) {  
    
  var client = this;
  
  // Authenticated Access
  if (!config.anonymous) {
    if (!req.user || !req.token)
      return fn(400, 'Bad request');
    
    // Retreive the session from Redis
    client.token = req.token;                 // Remote session token
    client.getSession(function(err, session) {
      //console.log('Authenticating: ', req.token, ':', session);
      if (err || typeof session !== 'object' || typeof session.user !== 'object') {
        //console.log('Authentication error: ', req.token, ':', err);
        return fn(401, 'Authentication failed');
      }
      else {
        //console.log('Authentication success: ', req);        
        client.session = session;             // Remote session object
        client.group = session.user.group;    // The client's parent group
        client.access = session.user.access;  // The client access level [1 - 10]
        client.user = session.user.user;      // The client login name
        client.user_id = session.user.user_id;// The client login user ID
        client.onAuthorize(req);
        return fn(200, 'Welcome ' + client.name);
      }
    });
  }
  
  // Anonymous Access
  else {
    if (!req.user)
      return fn(400, 'Bad request');
      
    client.access = -1;
    client.name = req.name;
    client.group = req.group;
    client.user = req.user;
    client.user_id = req.user_id;
    client.onAuthorize(req);
    return fn(200, 'Welcome ' + client.name);
  }
}


sio.Socket.prototype.onAuthorize = function(req) {
  console.log(this.id, 'on authorize: ', req);
  this.online = true;
  this.name = req.name ?                // The client display name
      req.name : this.user;
  this.type = req.type;                 // The client type
  this.join('user-' + this.user);       // join user channel
  this.join('group-' + this.group);     // join group channel
}


sio.Socket.prototype.toPresence = function(p) {
  if (!p || typeof p !== 'object')
    p = {};
  p.type = 'presence';
  p.data = this.toPeer(p.data);  
  if (!p.from)
    p.from = this.toAddress();
  //if (!p.from || typeof p.from !== 'object') {
  //  p.from = {};
  //  p.from.name = this.name;
  //}
  return p;
}


sio.Socket.prototype.toPeer = function(p) {
  if (!p || typeof p !== 'object')
    p = {};
  p.id = this.id; //sympleID;
  p.type = this.type;
  p.node = this.node;
  p.user = this.user;
  p.user_id = this.user_id;
  p.group = this.group;
  p.access = this.access;
  p.online = this.online;
  p.host = this.handshake.headers['x-real-ip'] 
    || this.handshake.headers['x-forwarded-for'] 
    || this.handshake.address.address; //this.handshake ?  : '';

  // allow client to change name
  if (typeof p.name === 'string')
    this.name = p.name;
  else
    p.name = this.name;

  return p;
}


sio.Socket.prototype.toAddress = function() {
  return this.user + "@" + this.group + "/" + this.id;
}


sio.Socket.prototype.getSessionKey = function(fn) {
  // token must be set
  io.store.cmd.keys("symple:*:" + this.token, function(err, keys) {
    fn(err, keys.length ? keys[0] : null)
  });  
}


sio.Socket.prototype.getSession = function(fn) {
  this.getSessionKey(function(err, key) {
    if (key) {
      io.store.cmd.get(key, function(err, session) {
        fn(err, JSON.parse(session));
      });
    }
    else fn("No session", null);
  });
}


sio.Socket.prototype.touchSession = function(fn) { 
  this.getSessionKey(function(err, key) {
    if (key) {
      // expire in 15 mins
      io.store.cmd.expire(key, 15 * 60, fn);
    }
    else fn("No session", null);
  });
}

sio.Socket.prototype.getDestinationAddress = function(message) {
  switch(typeof message.to) {
    case 'object': 
      return message.to;  
    case 'string':
      return parseAddress(message.to);     
    case 'undefined': 
      return { group: this.group };
  }
}


sio.Socket.prototype.broadcastMessage = function(message) {
  if (!message || typeof message !== 'object' || !message.from) {
    console.error(this.id, 'dropping invalid message:', message);
    return;
  }

  // Replace from address with server-side peer data for security.
  //message.from.id = this.id;
  //message.from.type = this.type;
  //message.from.group = this.group;
  //message.from.access = this.access;
  //message.from.user = this.user;
  //message.from.user_id = this.user_id;
  
  // Get an destination address object for routing  
  var to = this.getDestinationAddress(message);
    
  // Make sure we have a valid destination address
  if (typeof to !== 'object' || typeof to.group === 'undefined') {
    console.error(this.id, 'dropping invalid message without destination:', to, ':', message);
    return;
  }
  
  // If a session id was given we send a directed message to that session id.  
  if (typeof to.id === 'string' && to.id.length) {
    this.namespace/*.except(this.unauthorizedIDs())*/.socket(to.id).json.send(message);
  }
  
  // If a user was given (but no session id) we broadcast a message to user scope.
  // TODO: Ensure group membership
  else if (to.user && typeof to.user === 'string') {
    this.broadcast.to('user-' + to.user/*, this.unauthorizedIDs()*/).json.send(message);
  }
  
  // If a group was given (but no session id or user) we broadcast to group scope.
  else if (to.group && typeof to.group === 'string') {
    this.broadcast.to('group-' + to.group/*, this.unauthorizedIDs()*/).json.send(message);
  }
  
  else {
    console.error(this.id, 'cannot route invalid message:', message);
  }
}


//
// Socket.IO connection handler
//

io.sockets.on('connection', function(client) {    

  // 5 seconds to Announce or get booted
  var interval = setInterval(function () {
      console.log(client.id, 'failed to announce'); 
      client.disconnect();
  }, 5000);

  // Announce
  client.on('announce', function(req, ack) {    
    console.log(client.id, 'announcing:', req);

    try {

      // Authorization
      client.authorize(req, function(status, message) {
        // console.log(client.id, 'announce result:', status);
        clearInterval(interval);
        if (status == 200)
          respond(ack, status, message, client.toPeer());
        else {
          respond(ack, status, message);
          client.disconnect();
          return;
        }

        // Message
        client.on('message', function(m, ack) {
          if (m) {
            if (m.type == 'presence')
              this.toPresence(m);
            client.broadcastMessage(m);
            respond(ack, 200, 'Message received');
          }
        });

        // Peers
        client.on('peers', function(ack) {
          respond(ack, 200, '', this.peers(false));
        });

        // Timer
        if (config.redis) {
          // Keep sessions from expiring while connected
          interval = setInterval(function () {
            // Touch the client session event 10
            // minutes to prevent it from expiring.
            client.touchSession(function(err, res) {
              console.log(client.id, 'touching session:', !!res);
            });
          }, 10 * 60000);
        }

      });
    }
    catch (e) {
        console.log(client.id, 'internal error: ', e);
        client.disconnect();
    }
  }); 

  //
  // Disconnection
  client.on('disconnect', function() {
    console.log(client.id, 'is disconnecting');
    clearInterval(interval);
    if (client.online) {
      client.online = false;
      var p = client.toPresence();
      //console.log('Disconnecting', p);
      client.broadcastMessage(p);
    }
    client.leave('user-' + client.user);    // leave user channel
    client.leave('group-' + client.group);  // leave group channel
  });
});


//
// Socket.IO Manager extensions
//

//sio.Socket.prototype.authorizedClients = function() {
//  var res = [];
//  var clients = io.sockets.clients(this.group);
//  for (i = 0; i < clients.length; i++) {
//    if (clients[i].access >= this.access)
//      res.push(clients[i]);
//  }
//  return res;
//}


// Returns an array of authorized peers belonging to the currect
// client socket group.
//sio.Socket.prototype.peers = function(includeSelf) {
//  res = []
//  //var clients = this.authorizedClients();
//  var clients = io.sockets.clients('group-' + this.group);
//  for (i = 0; i < clients.length; i++) {
//    if ((!includeSelf && clients[i] == this) ||
//            clients[i].access > this.access)
//      continue;
//    res.push(clients[i].toPeer());
//  }
//  return res;
//}


// Returns an array of group peer IDs that dont have permission
// to receive messages broadcast by the current peer ie. access
// is lower than the current peer.
//sio.Socket.prototype.unauthorizedIDs = function() {
//  var res = [];
//  var clients = io.sockets.clients('group-' + this.group);
//  for (i = 0; i < clients.length; i++) {
//    if (clients[i].access < this.access)
//      res.push(clients[i].id);
//  }
//  console.log('Unauthorized IDs:', this.name, ':', this.access, ':', res);
//  return res;
//}

//function packetSender(packet) {
//  var res = packet.match(/\"from\"[ :]+[ {]+[^}]*\"id\"[ :]+\"(.*?)\"/);
//  return res ? io.sockets.sockets[res[1]] : null;
//}

//onDispatchOriginal = sio.Manager.prototype.onDispatch;
//sio.Manager.prototype.onDispatch = function(room, packet, volatile, exceptions) {
//
//  // Authorise outgoing messages via the onDispatch method so unprotected
//  // data can not be published directly from Redis.
//  var sender = packetSender(packet);
//  if (sender) {
//    if (!exceptions)
//      exceptions = [sender.id]; // dont send to self
//    exceptions = exceptions.concat(sender.unauthorizedIDs());
//    //console.log("Sending a message excluding: ", exceptions, ': ', sender.unauthorizedIDs());
//    onDispatchOriginal.call(this, room, packet, volatile, exceptions)
//  }
//}

//onClientDispatchOriginal = sio.Manager.prototype.onClientDispatch;
//sio.Manager.prototype.onClientDispatch = function (id, packet) {
//    
//  // Ensure the recipient has sufficient permission to recieve the message
//  var sender = packetSender(packet);
//  var recipient = io.sockets.sockets[id];
//  if (sender && recipient && recipient.access >= sender) {
//      onClientDispatchOriginal.call(this, id, packet);
//  }
//}