Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
141
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('form_error', self.onError.bind(self));
  71.         self.socket.on('jderror', self.onError.bind(self));
  72.         self.socket.on('jdmsg', self.onError.bind(self));
  73.         self.socket.on('getver', self.onGetVer.bind(self));
  74.         self.socket.on('tip', self.onTip.bind(self));
  75.         self.socket.on('init', self.onInit.bind(self));
  76.         self.socket.on('info', self.onInfo.bind(self));
  77.         self.socket.on('set_hash', self.onSetHash.bind(self));
  78.         self.socket.on('balance', self.onBalance.bind(self));
  79.         self.socket.on('pong', self.onPong.bind(self));
  80.         self.socket.on('chat', self.onMsg.bind(self));
  81.         self.socket.on('reload', self.connect.bind(self));
  82.         self.socket.on('timeout', self.reconnect.bind(self));
  83.  
  84.         //socket.emit("withdraw", csrf, address.val(), amount.val(), code.val(), speech.val())
  85.         defer.setTimeout(self.heartbeat.bind(self), 5000)
  86.  
  87.     })()
  88. }
  89.  
  90. inherits(JdClient, EventEmitter);
  91.  
  92.  
  93. JdClient.prototype.reconnect = async function() {
  94.     this.socket.emit("reconnect", this.csrf);
  95. }
  96.  
  97. JdClient.prototype.connect = async function() {
  98.         this.csrf = null;
  99.         this.inits = null;
  100.         this.cookie = null;
  101.  
  102.         try {
  103.             this.cookie = await login(this.credentials, this.url)
  104.         }
  105.         catch (e) {
  106.             logger.error("jd heartbeat login error", e)
  107.             throw new Error('error connecting to just-dice')
  108.         }
  109.        
  110.         logger.info('JD - Received cookie: ' + this.cookie);
  111.         logger.info('JD - Setting up connection to %s', this.url);
  112.  
  113.         this.socket = socketio(this.url, {
  114.             multiplex: false,
  115.             agent: https.globalAgent,
  116.             extraHeaders: {
  117.                 origin: this.url,
  118.                 cookie: this.cookie
  119.             }
  120.         });
  121. }
  122.  
  123.  
  124. JdClient.prototype.heartbeat = async function() {
  125.     // if idle time hasn't been updated in 5 minutes, reconnected
  126.     if(Date.now() - this.last_idle_time > 1000 * 60 * 5 )
  127.         await this.connect()
  128.    
  129.     defer.setTimeout(this.heartbeat.bind(this), 5000)
  130. }
  131.  
  132. JdClient.prototype.onPong = function() {
  133.         this.pong_count++
  134.  
  135.         if ( this.pong_count > 7 )
  136.         {
  137.             this.last_idle_time = new Date().getTime();
  138.             this.pong_count = 0;
  139.         }
  140. };
  141.  
  142. JdClient.prototype.onBalance = function(data) {
  143.     this.last_idle_time = new Date().getTime();
  144.     this.balance = data;
  145.     logger.info('JD - onBalance: ', data);
  146.     this.emit('balance', this.balance)
  147. };
  148.  
  149. JdClient.prototype.onGetVer = function(key) {
  150.     this.last_idle_time = new Date().getTime();
  151.     this.socket.emit('version', this.csrf, key, "jdbot:" + this.version);
  152.     logger.info('JD - onGetVer: ', key);
  153. };
  154.  
  155. JdClient.prototype.onSetHash = function(hash) {
  156.     logger.info('JD - onSetHash: ', hash);
  157. };
  158.  
  159. JdClient.prototype.onTip = function(from, name, amount, comment, ignored) {
  160.     this.last_idle_time = new Date().getTime();
  161.    /*if (amount < 2) {
  162.         return this.socket.emit("chat", this.csrf, '/tip noconf ' + from + ' ' + Number(amount).toFixed(8) +
  163.             ' "Please send 2 or more clam with your withdraw address in the comment, eg: /tip 18 1.0 \'1ADDRESS\'"')
  164.     }*/
  165.     logger.info('JD - onTip: ', from, name, amount, comment, ignored);
  166. };
  167.  
  168.  
  169. JdClient.prototype.tip = function(to, amount) {
  170.     //this.socket.emit("chat", this.csrf, 'test')
  171.     let tipStr = '/tip noconf ' + to + ' ' + Number(amount) + " \"Faucet.FreeBitcoins.com payout\""
  172.     logger.info(`JD - sent ${Number(amount).toFixed(8)} CLAM to ${to}`)
  173.  
  174.     console.log(tipStr)
  175.     this.socket.emit("chat", this.csrf, tipStr)
  176.     //logger.info('JD - tipped: ', to, amount);
  177. };
  178.  
  179. JdClient.prototype.withdraw = function(to, amount) {
  180.     logger.info(`JD - sent ${Number(amount).toFixed(8)} CLAM to ${to}`)
  181.     this.socket.emit("withdraw", this.csrf, to, Number(amount), "", "", true)
  182. };
  183.  
  184.  
  185. JdClient.prototype.onInfo = function(data) {
  186.  
  187.     logger.info('JD - onInfo', data);
  188. };
  189.  
  190. JdClient.prototype.onInit = function(data) {
  191.     this.invested = data.investment;
  192.     this.uid = data.uid;
  193.     this.name = data.name;
  194.     this.username = data.username;
  195.     this.last_idle_time = new Date().getTime();
  196.  
  197.     if (!this.inits++) {
  198.         this.csrf = data.csrf;
  199.         this.balance = data.balance;
  200.         this.emit('balance', this.balance)
  201.  
  202.         logger.info('JD - connected as (' + this.uid + ') <' + this.name + '>');
  203.  
  204.     } else {
  205.         logger.info('JD - reconnected');
  206.         this.csrf = data.csrf;
  207.     }
  208. };
  209.  
  210.  
  211. JdClient.prototype.onError = function(err) {
  212.     logger.info('JD - onError: ', err);
  213. };
  214.  
  215. JdClient.prototype.onErr = function(err) {
  216.     logger.info('JD - onErr: ', err);
  217. };
  218.  
  219.  
  220. JdClient.prototype.onDisconnect = function(data) {
  221.     logger.info('JD - Client disconnected |', data, '|', typeof data);
  222.     this.emit('disconnect');
  223. };
  224.  
  225. JdClient.prototype.onMsg = function(message) {
  226.     this.last_idle_time = new Date().getTime();
  227.     let msg = {}
  228.     msg.site = 'JD'
  229.         console.log(message)
  230.     //setup message format for Instant and determine if message if private or public to
  231.     // determine how to respond to the user
  232.     /*
  233.     if (message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)) {
  234.         let isPrivateRegMatch = message.match(/\[ \((.*?)\) <(.*?)> → \(18\) <bottyMcBotFace> \] (.*)/)
  235.  
  236.         msg.type = 'private'
  237.         msg.uid = Number(isPrivateRegMatch[1])
  238.         msg.name = isPrivateRegMatch[2]
  239.         msg.message = isPrivateRegMatch[3].trim()
  240.     } else {
  241.         if (!message.match(/\((.*?)\) <(.*?)> (.*)/))
  242.             return
  243.  
  244.         let isPublicRegMatch = message.match(/\((.*?)\) <(.*?)> (.*)/)
  245.         msg.type = 'public'
  246.         msg.uid = Number(isPublicRegMatch[1])
  247.         msg.name = isPublicRegMatch[2]
  248.         msg.message = isPublicRegMatch[3].trim()
  249.     }
  250.        
  251.     //
  252.     this.emit('msg', msg);
  253.     */
  254.     if (message.match(/That is someone else's deposit address/)) {
  255.         this.emit("address-verified", true)
  256.     } else if (message.match(/Invalid CLAM address/)) {
  257.         this.emit("address-verified", false)
  258.     } else if(message.match(/That is not a current Just-Dice deposit address/)) {
  259.         this.emit("address-verified", false)
  260.     } else if(message.match(/addresses start with an/)) {
  261.         this.emit("address-verified", false)
  262.     } else {
  263.            
  264.     }
  265.  
  266.     //console.log(message)
  267. };
  268.  
  269. JdClient.prototype.say = function(msg) {
  270.     if (msg.type === "private")
  271.         this.sayPrivate(msg.uid, msg)
  272.     else
  273.         this.sayPublic(msg)
  274. };
  275.  
  276. JdClient.prototype.sayPrivate = function(uid, msg) {
  277.     this.socket.emit('chat', this.csrf, '/msg ' + uid + ' ' + msg.message);
  278. };
  279.  
  280. JdClient.prototype.sayPublic = function(msg) {
  281.     this.socket.emit("chat", this.csrf, msg.message)
  282. };
  283.  
  284. JdClient.prototype.sayText = function(msg) {
  285.     this.socket.emit("chat", this.csrf, msg)
  286. };
  287.  
  288.  
  289.  
  290. async function login(credentials, url) {
  291.     try {
  292.         let jar = request.jar();
  293.  
  294.         let req = {
  295.             url: url,
  296.             jar: jar,
  297.             form: {}
  298.         }
  299.  
  300.         if (credentials.username) req.form.username = credentials.username;
  301.         if (credentials.password) req.form.password = credentials.password;
  302.         if (credentials.code) req.form.code = credentials.code;
  303.  
  304.         let res = await request.post(req);
  305.         let cookie = jar.getCookieString(url);
  306.  
  307.         return cookie
  308.  
  309.     } catch (e) {
  310.         logger.error('[JD][login]', e)
  311.         throw new Error('error connecting to just-dice', e)
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement