Want more features on Pastebin? Sign Up, it's FREE!
Guest

NodeJS server (bits have been removed for clarity)

By: a guest on Jul 24th, 2014  |  syntax: JavaScript  |  size: 4.03 KB  |  views: 219  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. console.log("Starting server...");
  2.  
  3. //config and variables
  4. var serverPort = 8080;
  5. var connectCounter = 0;
  6. var adminSessions = [];
  7. var updater_msDelay = 1000 * 60;
  8. var updater_dailymotionID = "x1ulpje";
  9.  
  10. //libs
  11. var io = require("socket.io")(serverPort);
  12. var mysql = require("mysql");
  13. var http = require("http");
  14. var https = require("https");
  15. var memcached = require("memcached");
  16. var phpunserialize = require("php-unserialize");
  17. var cookie = require("cookie");
  18. var util = require('util');
  19.  
  20. var sessions = new memcached("localhost:11211");
  21. sessions.on('failure', function(details) {
  22.     console.log("Error while connecting to PHP sessions manager. error: " + details);
  23.     return -1;
  24. });
  25.  
  26. //events
  27. var Manager = new ClientEventsManager(io);
  28.  
  29. console.log("Server started");
  30.  
  31.  
  32.  
  33. process.on('exit', function(code) {
  34.     console.log("Server shutting down with code " + code + "...");
  35.     db.destroy();
  36.     sessions.end();
  37. });
  38.  
  39.  
  40.  
  41. /******************************************************************************/
  42. //functions
  43. function getJSON(options, onResult) {
  44.     var prot = options.port == 443 ? https : http;
  45.     var req = prot.request(options, function(res) {
  46.         var output = '';
  47.         res.setEncoding('utf8');
  48.         res.on('data', function(chunk) {
  49.             output += chunk;
  50.         });
  51.         res.on('end', function() {
  52.             try {
  53.                 var obj = JSON.parse(output);
  54.                 onResult(res.statusCode, obj);
  55.             } catch (e) {
  56.                 console.log("Invalid response returned from "+options.host+options.path+":"+options.port+":\n"+output);
  57.             }
  58.         });
  59.     });
  60.     req.on('error', function(err) {
  61.         console.log("Error in http request to " + options.host + " : " + err);
  62.     });
  63.     req.end();
  64. }
  65.  
  66. function isAuthentified(socket, Yep, Nope) {
  67.         console.log("Trying to retrieve data...");
  68.         socket.send("Your PHP session id: "+Manager.clients[socket.id].PHPSESSID+", right?");
  69.     sessions.get(Manager.clients[socket.id].PHPSESSID, function(err, data) {
  70.         if (err) {
  71.                         console.log("err");
  72.             Nope();
  73.                 }
  74.         if (data) {
  75.                         console.log("data OK");
  76.             var SessionData = PHPunserialize.unserializeSession(data);
  77.             console.log(util.inspect(SessionData, null));
  78.             if (SessionData.authentified) {
  79.                                 console.log("auth ok");
  80.                 Yep();
  81.             } else {
  82.                                 console.log("no auth");
  83.                                 Nope();
  84.                         }
  85.         } else {
  86.                         console.log("data not ok: "+data);
  87.                         Nope();
  88.                 }
  89.        
  90.     });
  91. }
  92.  
  93. /******************************************************************************/
  94. //classes
  95. function ClientEventsManager() {
  96.     var EventsManager = this;
  97.  
  98.     this.clients = [];
  99.     this.clientsCount = 0;
  100.     this.onClientConnection = function(socket) {
  101.         var PHPSESSID = cookie.parse(socket.request.headers.cookie).PHPSESSID;
  102.         socket.emit("info", PHPSESSID);
  103.         EventsManager.clientsCount++;
  104.         EventsManager.clients[socket.id] = {};
  105.         EventsManager.clients[socket.id].PHPSESSID = PHPSESSID;
  106.         isAuthentified(socket, function() {
  107.             socket.emit("authentified", true);
  108.         }, function() {
  109.             socket.emit("authentified", false);
  110.         });
  111.     };
  112.     this.onClientDisconnection = function(socket) {
  113.         delete EventsManager.clients[socket.id];
  114.         EventsManager.clientsCount--;
  115.  
  116.         //remove the client from the sendConnectedClientsOnUpdate array (if he is in)
  117.         if (EventsManager.sendConnectedClientsOnUpdate.indexOf(socket) > -1) {
  118.             EventsManager.sendConnectedClientsOnUpdate.splice(EventsManager.sendConnectedClientsOnUpdate.indexOf(socket), 1);
  119.         }
  120.     };
  121.  
  122.     this.createEventReceivers = function() {
  123.         io.sockets.on('connection', function(socket) {
  124.             EventsManager.onClientConnection(socket);
  125.             socket.on('disconnect', function() {
  126.                 EventsManager.onClientDisconnection(socket);
  127.             });
  128.  
  129.         });
  130.     };
  131.  
  132.     this.createEventReceivers();
  133. }
clone this paste RAW Paste Data