Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const _ = require('lodash');
  4. const EventEmitter = require('events').EventEmitter;
  5. const inherits = require('util').inherits;
  6. const request = require('co-request');
  7. const defer = require('co-defer')
  8. const socketio = require('socket.io-client');
  9. const debug = require('debug')('instant:client');
  10. const https = require('https');
  11. const logger = require('winston')
  12.  
  13. https.globalAgent.options.rejectUnauthorized = false;
  14.  
  15. module.exports = JdClient;
  16.  
  17. function JdClient(username, password) {
  18.     EventEmitter.call(this);
  19.     let self = this;
  20.     // Save configuration and stuff.
  21.  
  22.     (async () => {
  23.         self.username = null;
  24.         self.name = null;
  25.         self.balance = null;
  26.  
  27.         self.csrf = null;
  28.         self.inits = null;
  29.         self.uid = null;
  30.         self.invested = null;
  31.         self.balance = null;
  32.         self.max_profit = null;
  33.         self.cookie = null;
  34.         self.version = '0.1.5'
  35.         self.url = 'https://just-dice.com'
  36.         self.last_idle_time;
  37.         self.pong_count = 0;
  38.  
  39.         self.credentials = {
  40.             hash: '',
  41.             username: username,
  42.             password: password,
  43.             code: ''
  44.         }
  45.  
  46.         try {
  47.             self.cookie = await login(self.credentials, self.url)
  48.         }
  49.         catch (e) {
  50.             logger.error("jd login error", e)
  51.             throw new Error('error connecting to just-dice')
  52.         }
  53.        
  54.         logger.info('JD - Received cookie: ' + self.cookie);
  55.         logger.info('JD - Setting up connection to %s', self.url);
  56.  
  57.         self.socket = socketio(self.url, {
  58.             multiplex: false,
  59.             agent: https.globalAgent,
  60.             extraHeaders: {
  61.                 origin: self.url,
  62.                 cookie: self.cookie
  63.             }
  64.         });
  65.  
  66.         self.socket.on('error', self.onError.bind(self));
  67.         self.socket.on('login_error', self.onError.bind(self));
  68.         self.socket.on('invest_error', self.onError.bind(self));
  69.         self.socket.on('divest_error', self.onError.bind(self));
  70.         self.socket.on('getver', self.onGetVer.bind(self));
  71.         self.socket.on('tip', self.onTip.bind(self));
  72.         self.socket.on('init', self.onInit.bind(self));
  73.         self.socket.on('set_hash', self.onSetHash.bind(self));
  74.         self.socket.on('balance', self.onBalance.bind(self));
  75.         self.socket.on('pong', self.onPong.bind(self));
  76.         self.socket.on('chat', self.onMsg.bind(self));
  77.         self.socket.on('reload', self.connect.bind(self));
  78.         self.socket.on('timeout', self.reconnect.bind(self));
  79.  
  80.         //socket.emit("withdraw", csrf, address.val(), amount.val(), code.val(), speech.val())
  81.         defer.setTimeout(self.heartbeat.bind(self), 5000)
  82.  
  83.     })()
  84. }
  85.  
  86. inherits(JdClient, EventEmitter);
  87.  
  88.  
  89. JdClient.prototype.reconnect = async function() {
  90.     this.socket.emit("reconnect", this.csrf);
  91. }
  92.  
  93. JdClient.prototype.connect = async function() {
  94.         this.csrf = null;
  95.         this.inits = null;
  96.         this.cookie = null;
  97.  
  98.         try {
  99.             this.cookie = await login(this.credentials, this.url)
  100.         }
  101.         catch (e) {
  102.             logger.error("jd heartbeat login error", e)
  103.             throw new Error('error connecting to just-dice')
  104.         }
  105.        
  106.         logger.info('JD - Received cookie: ' + this.cookie);
  107.         logger.info('JD - Setting up connection to %s', this.url);
  108.  
  109.         this.socket = socketio(this.url, {
  110.             multiplex: false,
  111.             agent: https.globalAgent,
  112.             extraHeaders: {
  113.                 origin: this.url,
  114.                 cookie: this.cookie
  115.             }
  116.         });
  117. }
  118.  
  119.  
  120. JdClient.prototype.heartbeat = async function() {
  121.     // if idle time hasn't been updated in 5 minutes, reconnected
  122.     if(Date.now() - this.last_idle_time > 1000 * 60 * 5 )
  123.         await this.connect()
  124.    
  125.     defer.setTimeout(this.heartbeat.bind(this), 5000)
  126. }
  127.  
  128. JdClient.prototype.onPong = function() {
  129.         this.pong_count++
  130.  
  131.         if ( this.pong_count > 7 )
  132.         {
  133.             this.last_idle_time = new Date().getTime();
  134.             this.pong_count = 0;
  135.         }
  136. };
  137.  
  138. JdClient.prototype.onBalance = function(data) {
  139.     this.last_idle_time = new Date().getTime();
  140.     this.balance = data;
  141.     logger.info('JD - onBalance: ', data);
  142.     this.emit('balance', this.balance)
  143. };
  144.  
  145. JdClient.prototype.onGetVer = function(key) {
  146.     this.last_idle_time = new Date().getTime();
  147.     socket.emit('version', this.csrf, key, "jdbot:" + this.version);
  148.     logger.info('JD - onGetVer: ', key);
  149. };
  150.  
  151. JdClient.prototype.onSetHash = function(hash) {
  152.     logger.info('JD - onSetHash: ', hash);
  153. };
  154.  
  155. JdClient.prototype.onTip = function(from, name, amount, comment, ignored) {
  156.     this.last_idle_time = new Date().getTime();
  157.     if (amount < 2) {
  158.         return socket.emit("chat", csrf, '/tip noconf ' + from + ' ' + Number(amount).toFixed(8) +
  159.             ' "Please send 2 or more clam with your withdraw address in the comment, eg: /tip 18 1.0 \'1ADDRESS\'"')
  160.     }
  161.     logger.info('JD - onTip: ', from, name, amount, comment, ignored);
  162. };
  163.  
  164. JdClient.prototype.onInit = function(data) {
  165.     this.invested = data.investment;
  166.     this.uid = data.uid;
  167.     this.name = data.name;
  168.     this.username = data.username;
  169.     this.last_idle_time = new Date().getTime();
  170.  
  171.     if (!this.inits++) {
  172.         this.csrf = data.csrf;
  173.         this.balance = data.balance;
  174.         this.emit('balance', this.balance)
  175.  
  176.         logger.info('JD - connected as (' + this.uid + ') <' + this.name + '>');
  177.  
  178.     } else {
  179.         logger.info('JD - reconnected');
  180.         this.csrf = data.csrf;
  181.     }
  182. };
  183.  
  184.  
  185. JdClient.prototype.onError = function(err) {
  186.     logger.info('JD - onError: ', err);
  187. };
  188.  
  189. JdClient.prototype.onErr = function(err) {
  190.     logger.info('JD - onErr: ', err);
  191. };
  192.  
  193.  
  194. JdClient.prototype.onDisconnect = function(data) {
  195.     logger.info('JD - Client disconnected |', data, '|', typeof data);
  196.     this.emit('disconnect');
  197. };
  198.  
  199. JdClient.prototype.onMsg = function(message) {
  200.     this.last_idle_time = new Date().getTime();
  201.     let msg = {}
  202.     msg.site = 'JD'
  203.  
  204.     console.log(message)
  205.     //setup message format for Instant and determine if message if private or public to
  206.     // determine how to respond to the user
  207.     if (message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)) {
  208.         let isPrivateRegMatch = message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)
  209.  
  210.         msg.type = 'private'
  211.         msg.uid = Number(isPrivateRegMatch[1])
  212.         msg.name = isPrivateRegMatch[2]
  213.         msg.message = isPrivateRegMatch[3].trim()
  214.     } else {
  215.         if (!message.match(/\((.*?)\) <(.*?)> (.*)/))
  216.             return
  217.  
  218.         let isPublicRegMatch = message.match(/\((.*?)\) <(.*?)> (.*)/)
  219.         msg.type = 'public'
  220.         msg.uid = Number(isPublicRegMatch[1])
  221.         msg.name = isPublicRegMatch[2]
  222.         msg.message = isPublicRegMatch[3].trim()
  223.     }
  224.  
  225.     this.emit('msg', msg);
  226. };
  227.  
  228. JdClient.prototype.say = function(msg) {
  229.     if (msg.type === "private")
  230.         this.sayPrivate(msg.uid, msg)
  231.     else
  232.         this.sayPublic(msg)
  233. };
  234.  
  235. JdClient.prototype.sayPrivate = function(uid, msg) {
  236.     this.socket.emit('chat', this.csrf, '/msg ' + uid + ' ' + msg.message);
  237. };
  238.  
  239. JdClient.prototype.sayPublic = function(msg) {
  240.     this.socket.emit("chat", this.csrf, msg.message)
  241. };
  242.  
  243.  
  244. async function login(credentials, url) {
  245.     try {
  246.         let jar = request.jar();
  247.  
  248.         let req = {
  249.             url: url,
  250.             jar: jar,
  251.             form: {}
  252.         }
  253.  
  254.         if (credentials.username) req.form.username = credentials.username;
  255.         if (credentials.password) req.form.password = credentials.password;
  256.         if (credentials.code) req.form.code = credentials.code;
  257.  
  258.         let res = await request.post(req);
  259.         let cookie = jar.getCookieString(url);
  260.  
  261.         return cookie
  262.  
  263.     } catch (e) {
  264.         logger.error('[JD][login]', e)
  265.         throw new Error('error connecting to just-dice', e)
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement