Advertisement
Guest User

Untitled

a guest
Feb 4th, 2018
96
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. };
  143.  
  144. JdClient.prototype.onGetVer = function(key) {
  145.     this.last_idle_time = new Date().getTime();
  146.     socket.emit('version', this.csrf, key, "jdbot:" + this.version);
  147.     logger.info('JD - onGetVer: ', key);
  148. };
  149.  
  150. JdClient.prototype.onSetHash = function(hash) {
  151.     logger.info('JD - onSetHash: ', hash);
  152. };
  153.  
  154. JdClient.prototype.onTip = function(from, name, amount, comment, ignored) {
  155.     this.last_idle_time = new Date().getTime();
  156.     if (amount < 2) {
  157.         return socket.emit("chat", csrf, '/tip noconf ' + from + ' ' + Number(amount).toFixed(8) +
  158.             ' "Please send 2 or more clam with your withdraw address in the comment, eg: /tip 18 1.0 \'1ADDRESS\'"')
  159.     }
  160.     logger.info('JD - onTip: ', from, name, amount, comment, ignored);
  161. };
  162.  
  163. JdClient.prototype.onInit = function(data) {
  164.     this.invested = data.investment;
  165.     this.uid = data.uid;
  166.     this.name = data.name;
  167.     this.username = data.username;
  168.     this.last_idle_time = new Date().getTime();
  169.  
  170.     if (!this.inits++) {
  171.         this.csrf = data.csrf;
  172.         this.balance = data.balance;
  173.         this.emit('balance', this.balance)
  174.  
  175.         logger.info('JD - connected as (' + this.uid + ') <' + this.name + '>');
  176.  
  177.     } else {
  178.         logger.info('JD - reconnected');
  179.         this.csrf = data.csrf;
  180.     }
  181. };
  182.  
  183.  
  184. JdClient.prototype.onError = function(err) {
  185.     logger.info('JD - onError: ', err);
  186. };
  187.  
  188. JdClient.prototype.onErr = function(err) {
  189.     logger.info('JD - onErr: ', err);
  190. };
  191.  
  192.  
  193. JdClient.prototype.onDisconnect = function(data) {
  194.     logger.info('JD - Client disconnected |', data, '|', typeof data);
  195. };
  196.  
  197. JdClient.prototype.onMsg = function(message) {
  198.     this.last_idle_time = new Date().getTime();
  199.     let msg = {}
  200.     msg.site = 'JD'
  201.  
  202.     console.log(message)
  203.     //setup message format for Instant and determine if message if private or public to
  204.     // determine how to respond to the user
  205.     if (message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)) {
  206.         let isPrivateRegMatch = message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)
  207.  
  208.         msg.type = 'private'
  209.         msg.uid = Number(isPrivateRegMatch[1])
  210.         msg.name = isPrivateRegMatch[2]
  211.         msg.message = isPrivateRegMatch[3].trim()
  212.     } else {
  213.         if (!message.match(/\((.*?)\) <(.*?)> (.*)/))
  214.             return
  215.  
  216.         let isPublicRegMatch = message.match(/\((.*?)\) <(.*?)> (.*)/)
  217.         msg.type = 'public'
  218.         msg.uid = Number(isPublicRegMatch[1])
  219.         msg.name = isPublicRegMatch[2]
  220.         msg.message = isPublicRegMatch[3].trim()
  221.     }
  222.  
  223.     this.emit('msg', msg);
  224. };
  225.  
  226. JdClient.prototype.say = function(msg) {
  227.     if (msg.type === "private")
  228.         this.sayPrivate(msg.uid, msg)
  229.     else
  230.         this.sayPublic(msg)
  231. };
  232.  
  233. JdClient.prototype.sayPrivate = function(uid, msg) {
  234.     this.socket.emit('chat', this.csrf, '/msg ' + uid + ' ' + msg.message);
  235. };
  236.  
  237. JdClient.prototype.sayPublic = function(msg) {
  238.     this.socket.emit("chat", this.csrf, msg.message)
  239. };
  240.  
  241.  
  242. async function login(credentials, url) {
  243.     try {
  244.         let jar = request.jar();
  245.  
  246.         let req = {
  247.             url: url,
  248.             jar: jar,
  249.             form: {}
  250.         }
  251.  
  252.         if (credentials.username) req.form.username = credentials.username;
  253.         if (credentials.password) req.form.password = credentials.password;
  254.         if (credentials.code) req.form.code = credentials.code;
  255.  
  256.         let res = await request.post(req);
  257.         let cookie = jar.getCookieString(url);
  258.  
  259.         return cookie
  260.  
  261.     } catch (e) {
  262.         logger.error('[JD][login]', e)
  263.         throw new Error('error connecting to just-dice', e)
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement