From a122f0994e881862288c72a90b11a0dfb5691203 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sun, 13 Dec 2015 20:10:33 +0000 Subject: [PATCH] Stuff --- lib/Client/InternalClient.js | 20 +++++ lib/Util/TokenCacher.js | 141 +++++++++++++++++++++++++++++++++++ package.json | 1 + src/Client/InternalClient.js | 17 +++++ src/Util/TokenCacher.js | 127 +++++++++++++++++++++++++++++++ test/msgbot.js | 7 +- 6 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 lib/Util/TokenCacher.js create mode 100644 src/Util/TokenCacher.js diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 110af4eb1..494f91e1c 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -74,6 +74,10 @@ var _VoiceVoiceConnection = require("../Voice/VoiceConnection"); var _VoiceVoiceConnection2 = _interopRequireDefault(_VoiceVoiceConnection); +var _UtilTokenCacher = require("../Util/TokenCacher"); + +var _UtilTokenCacher2 = _interopRequireDefault(_UtilTokenCacher); + var zlib; var libVersion = require('../../package.json').version; @@ -162,6 +166,9 @@ var InternalClient = (function () { this.resolver = new _ResolverResolver2["default"](this); this.readyTime = null; this.messageAwaits = {}; + + this.tokenCacher = new _UtilTokenCacher2["default"](this.client); + this.tokenCacher.init(0); }; InternalClient.prototype.cleanIntervals = function cleanIntervals() { @@ -357,6 +364,18 @@ var InternalClient = (function () { var _this6 = this; var client = this.client; + + console.log(this.tokenCacher.done); + if (!this.tokenCacher.done) { + return new Promise(function (resolve, reject) { + setTimeout(function () { + _this6.login(email, password).then(resolve)["catch"](reject); + }, 20); + }); + } else { + console.log("YAA - " + this.tokenCacher.getToken(email, password)); + } + if (this.state !== _ConnectionState2["default"].DISCONNECTED && this.state !== _ConnectionState2["default"].IDLE) { return Promise.reject(new Error("already logging in/logged in/ready!")); } @@ -368,6 +387,7 @@ var InternalClient = (function () { password: password }).then(function (res) { var token = res.token; + _this6.tokenCacher.setToken(email, password, token); _this6.state = _ConnectionState2["default"].LOGGED_IN; _this6.token = token; _this6.email = email; diff --git a/lib/Util/TokenCacher.js b/lib/Util/TokenCacher.js new file mode 100644 index 000000000..97a249a70 --- /dev/null +++ b/lib/Util/TokenCacher.js @@ -0,0 +1,141 @@ +"use strict"; +/* global process */ + +exports.__esModule = true; + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var _fsExtra = require("fs-extra"); + +var _fsExtra2 = _interopRequireDefault(_fsExtra); + +var _events = require("events"); + +var _events2 = _interopRequireDefault(_events); + +var _crypto = require("crypto"); + +var _crypto2 = _interopRequireDefault(_crypto); + +var savePaths = [process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + 'Library/Preference' : '/var/local'), process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME']]; + +var algo = "aes-256-ctr"; + +var TokenCacher = (function (_EventEmitter) { + _inherits(TokenCacher, _EventEmitter); + + function TokenCacher(client, options) { + _classCallCheck(this, TokenCacher); + + _EventEmitter.call(this); + this.client = client; + this.savePath = null; + this.error = false; + this.done = false; + this.data = {}; + } + + TokenCacher.prototype.setToken = function setToken(email, password, token) { + console.log("wanting to cache", token); + token = new Buffer(token).toString("base64"); + var cipher = _crypto2["default"].createCipher(algo, password); + var crypted = cipher.update(token, 'utf8', 'base64'); + crypted += cipher.final('base64'); + this.data[email] = crypted; + this.save(); + }; + + TokenCacher.prototype.save = function save() { + _fsExtra2["default"].writeJson(this.savePath, this.data); + }; + + TokenCacher.prototype.getToken = function getToken(email, password) { + + if (this.data[email]) { + + try { + var decipher = _crypto2["default"].createDecipher(algo, password); + var dec = decipher.update(this.data[email], "base64", 'utf8'); + dec += decipher.final('utf8'); + return new Buffer(dec, "base64").toString("ascii"); + } catch (e) { + return null; + } + } else { + return null; + } + }; + + TokenCacher.prototype.init = function init(ind) { + var _this = this; + + var self = this; + var savePath = savePaths[ind]; + + _fsExtra2["default"].ensureDir(savePath, function (err) { + + if (err) { + error(err); + } else { + //good to go + + _fsExtra2["default"].ensureFile(savePath + "/.discordjs/tokens.json", function (err) { + if (err) { + error(err); + } else { + //file exists + + _fsExtra2["default"].readFile(savePath + "/.discordjs/tokens.json", function (err, data) { + + if (err) { + error(err); + } else { + // can read file, is it valid JSON? + try { + + _this.data = JSON.parse(data); + // good to go! + _this.savePath = savePath + "/.discordjs/tokens.json"; + _this.emit("ready"); + _this.done = true; + } catch (e) { + // not valid JSON, make it valid and then write + _fsExtra2["default"].writeJson(savePath + "/.discordjs/tokens.json", {}, function (err) { + if (err) { + error(err); + } else { + // good to go! + _this.savePath = savePath + "/.discordjs/tokens.json"; + _this.emit("ready"); + _this.done = true; + } + }); + } + } + }); + } + }); + } + }); + + function error(e) { + ind++; + if (!savePaths[ind]) { + self.emit("error"); + self.error = e; + self.done = true; + } else { + self.init(ind); + } + } + }; + + return TokenCacher; +})(_events2["default"]); + +exports["default"] = TokenCacher; +module.exports = exports["default"]; diff --git a/package.json b/package.json index 7456e5e47..6aa9967c9 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "homepage": "https://github.com/hydrabolt/discord.js#readme", "dependencies": { + "fs-extra": "^0.26.2", "superagent": "^1.5.0", "ws": "^0.8.1" }, diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 4ac7eac33..19a4f0535 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -20,6 +20,7 @@ import Message from "../Structures/Message"; import Role from "../Structures/Role"; import Invite from "../Structures/Invite"; import VoiceConnection from "../Voice/VoiceConnection"; +import TokenCacher from "../Util/TokenCacher"; var zlib; var libVersion = require('../../package.json').version; @@ -101,6 +102,9 @@ export default class InternalClient { this.resolver = new Resolver(this); this.readyTime = null; this.messageAwaits = {}; + + this.tokenCacher = new TokenCacher(this.client); + this.tokenCacher.init(0); } cleanIntervals(){ @@ -260,6 +264,18 @@ export default class InternalClient { // def login login(email, password) { var client = this.client; + + console.log(this.tokenCacher.done); + if(!this.tokenCacher.done){ + return new Promise((resolve, reject) => { + setTimeout(() => { + this.login(email, password).then(resolve).catch(reject); + }, 20); + }); + }else{ + console.log("Cached - " + this.tokenCacher.getToken(email, password)); + } + if(this.state !== ConnectionState.DISCONNECTED && this.state !== ConnectionState.IDLE) { return Promise.reject(new Error("already logging in/logged in/ready!")); } @@ -272,6 +288,7 @@ export default class InternalClient { }) .then(res => { var token = res.token; + this.tokenCacher.setToken(email, password, token); this.state = ConnectionState.LOGGED_IN; this.token = token; this.email = email; diff --git a/src/Util/TokenCacher.js b/src/Util/TokenCacher.js new file mode 100644 index 000000000..fe2e48398 --- /dev/null +++ b/src/Util/TokenCacher.js @@ -0,0 +1,127 @@ +"use strict"; +/* global process */ + +import fs from "fs-extra"; +import EventEmitter from "events"; +import crypto from "crypto"; + +var savePaths = [ + process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + 'Library/Preference' : '/var/local'), + process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] +]; + +var algo = "aes-256-ctr"; + +export default class TokenCacher extends EventEmitter { + + constructor(client, options) { + super(); + this.client = client; + this.savePath = null; + this.error = false; + this.done = false; + this.data = {}; + } + + setToken(email, password, token) { + console.log("wanting to cache", token); + token = new Buffer(token).toString("base64"); + var cipher = crypto.createCipher(algo,password) + var crypted = cipher.update(token,'utf8','base64') + crypted += cipher.final('base64'); + this.data[email] = crypted; + this.save(); + } + + save() { + fs.writeJson(this.savePath, this.data); + } + + getToken(email, password){ + + if (this.data[email]) { + + try { + var decipher = crypto.createDecipher(algo, password) + var dec = decipher.update(this.data[email], "base64", 'utf8') + dec += decipher.final('utf8'); + return new Buffer(dec, "base64").toString("ascii"); + } catch (e) { + return null; + } + + } else { + return null; + } + + } + + init(ind) { + + var self = this; + var savePath = savePaths[ind]; + + fs.ensureDir(savePath, err => { + + if (err) { + error(err); + } else { + //good to go + + fs.ensureFile(savePath + "/.discordjs/tokens.json", err => { + if (err) { + error(err); + } else { + //file exists + + fs.readFile(savePath + "/.discordjs/tokens.json", (err, data) => { + + if (err) { + error(err); + } else { + // can read file, is it valid JSON? + try { + + this.data = JSON.parse(data); + // good to go! + this.savePath = savePath + "/.discordjs/tokens.json"; + this.emit("ready"); + this.done = true; + + } catch (e) { + // not valid JSON, make it valid and then write + fs.writeJson(savePath + "/.discordjs/tokens.json", {}, err => { + if (err) { + error(err); + } else { + // good to go! + this.savePath = savePath + "/.discordjs/tokens.json"; + this.emit("ready"); + this.done = true; + } + }); + } + } + + }); + + } + }) + + } + + }); + + function error(e) { + ind++; + if (!savePaths[ind]) { + self.emit("error"); + self.error = e; + self.done = true; + } else { + self.init(ind); + } + } + + } +} \ No newline at end of file diff --git a/test/msgbot.js b/test/msgbot.js index 9ed45c2e6..95b3b4629 100644 --- a/test/msgbot.js +++ b/test/msgbot.js @@ -6,10 +6,7 @@ var client = new Discord.Client({revive : true}); var request = require("superagent"); client.on("ready", () => { - console.log("ready"); - for (var server in client.servers) { - console.log(server); - } + console.log("ready - " + client.internal.token); }); @@ -31,7 +28,7 @@ client.on("message", msg => { } if (msg.content === "$perms") { - + }