Guest User

Untitled

a guest
Jul 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. var redisClient = require("redis-client"),
  2. dealer = require("../lib"),
  3. sys = require('sys'),
  4. url = require('url')
  5.  
  6. var port = process.env['PORT'] || 3840
  7. var redisConf = process.env['REDIS_URL'] || "redis://127.0.0.1:6379/0"
  8.  
  9. conn = dealer.create()
  10. conn.server.listen(port)
  11.  
  12. sys.puts("listening on " + port)
  13.  
  14. conn.addListener('connect', function(client) {
  15. sys.puts('connected to ' + sys.inspect(client.channels) + ': '+ client.id)
  16. })
  17.  
  18. // Injecting the credentials checking into the socket lifecycle
  19. conn.addListener('connect', function(client) {
  20. sys.puts('Checking auth for' + sys.inspect(client.channels) + ': '+ client.id)
  21. checkAuth(client);
  22. })
  23.  
  24. conn.addListener('disconnect', function(client) {
  25. sys.puts('disconnected: ' + client.id)
  26. })
  27.  
  28. conn.addListener('receive', function(client, data) {
  29. sys.puts('MSG from ' + client.id + sys.inspect(client.channels) + ': ' + data)
  30. conn.sendToChannel(client.channels.shift(), data)
  31. })
  32.  
  33. redisConf = url.parse(redisConf)
  34. redisDb = redisConf.pathname.substr(1)
  35. redis = redisClient.createClient(redisConf.port, redisConf.hostname);
  36.  
  37. // We'll need a second client to perform non pubsub operations
  38. redis2 = redisClient.createClient(redisConf.port, redisConf.hostname);
  39.  
  40.  
  41. // We tweak a little bit the dealer.js redis pubsub bit to allow for sending non-chat/administrative messages to the chat channels
  42. redis.stream.on("connect", function () {
  43. redis.select(redisDb, function(a) {
  44. redis.subscribeTo("commchannels:*:announcements", function (channel, message) {
  45. sys.puts("ANN to " + channel + ": " + message)
  46. var channel_str = String(channel)
  47. var channel_arr = channel_str.split(':')
  48. var channel_id = channel_arr[1]
  49. conn.sendToChannel(channel_id, "ANN: "+message)
  50. })
  51. })
  52. })
  53.  
  54.  
  55. // A redis set as a whitelist for authorized user ids, so we can SADD to the user list from a web interface, restful API calls, etc.
  56.  
  57. function checkAuth(client) {
  58.  
  59. var debug = false
  60.  
  61. var channel_id = client.channels[client.channels.length-1]
  62. var channel_key = 'commchannels:'+channel_id+':members'
  63. var client_id = parseInt(client.id)
  64.  
  65. redis2.select(redisDb, function(a) {
  66. redis2.sismember(channel_key, client_id, function (err, result) {
  67. sys.puts(client_id+ " is member of "+channel_key+": " + result)
  68.  
  69. if (err != null) {
  70. sys.puts(sys.inspect(err))
  71. }
  72.  
  73. if (result == 0) {
  74. client.send("You are not entitled to this conversation. Bye!")
  75. conn.disconnect(client)
  76. }
  77. else if (result == 1){
  78. client.send("Please, be welcome.")
  79. return client
  80. }
  81.  
  82.  
  83. })
  84.  
  85. if (debug) {
  86. redis2.smembers(channel_key,function(err,members){
  87. if (err != null) {
  88. sys.puts(sys.inspect(err))
  89. }
  90. sys.puts("Members: "+members)
  91. })
  92. }
  93.  
  94. })
  95. }
Add Comment
Please, Sign In to add comment