UNPKG

card-sharing

Version:

card sharing config generator by parsing online sources

369 lines (296 loc) 11.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _cryptographicBlock = require('./cryptographic-block'); var _cryptographicBlock2 = _interopRequireDefault(_cryptographicBlock); var _net = require('net'); var _net2 = _interopRequireDefault(_net); var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ClineTester = function () { function ClineTester(host, port, username, pass) { var timeout = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 30000; _classCallCheck(this, ClineTester); this.recvblock = new _cryptographicBlock2.default(); this.sendblock = new _cryptographicBlock2.default(); this.timeout = timeout; this.host = host; this.port = port; this.username = username; this.password = pass; var socket = this.socket = new _net2.default.Socket(); socket.setNoDelay(); socket.setTimeout(timeout); } _createClass(ClineTester, [{ key: 'test', value: function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return this.connect(); case 3: _context.next = 5; return this.handshake(); case 5: _context.next = 7; return this.login(); case 7: _context.next = 13; break; case 9: _context.prev = 9; _context.t0 = _context['catch'](0); this.socket.destroy(); throw _context.t0; case 13: this.socket.destroy(); case 14: case 'end': return _context.stop(); } } }, _callee, this, [[0, 9]]); })); function test() { return _ref.apply(this, arguments); } return test; }() }, { key: 'isValid', value: function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return this.test(); case 3: _context2.next = 8; break; case 5: _context2.prev = 5; _context2.t0 = _context2['catch'](0); return _context2.abrupt('return', false); case 8: return _context2.abrupt('return', true); case 9: case 'end': return _context2.stop(); } } }, _callee2, this, [[0, 5]]); })); function isValid() { return _ref2.apply(this, arguments); } return isValid; }() }, { key: 'isInvalid', value: function () { var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.isValid(); case 2: return _context3.abrupt('return', !_context3.sent); case 3: case 'end': return _context3.stop(); } } }, _callee3, this); })); function isInvalid() { return _ref3.apply(this, arguments); } return isInvalid; }() }, { key: 'connect', value: function connect() { var host = this.host, port = this.port, socket = this.socket, timeout = this.timeout; return new Promise(function (ful, rej) { socket.connect({ host: host, port: port }, ful); socket.once('timeout', rej); socket.once('error', rej); setTimeout(function () { return socket.end(); }, timeout); }); } }, { key: 'handshake', value: function () { var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { var recvblock, sendblock, hello, sha1hash; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: recvblock = this.recvblock, sendblock = this.sendblock; _context4.next = 3; return this.read(); case 3: hello = _context4.sent; xor(hello); sha1hash = _crypto2.default.createHash('sha1').update(hello).digest(); recvblock.init(sha1hash); recvblock.decrypt(hello); sendblock.init(hello); sendblock.decrypt(sha1hash); _context4.next = 12; return this.write(sha1hash); case 12: case 'end': return _context4.stop(); } } }, _callee4, this); })); function handshake() { return _ref4.apply(this, arguments); } return handshake; }() }, { key: 'login', value: function () { var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { var username, password, recvblock, sendblock, greeting, ack; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: username = this.username, password = this.password, recvblock = this.recvblock, sendblock = this.sendblock; _context5.next = 3; return this.write(padBuffer(username, 20)); case 3: sendblock.encrypt(padBuffer(password)); greeting = padBuffer('CCcam', 6); _context5.next = 7; return this.write(greeting); case 7: _context5.next = 9; return this.read(); case 9: ack = _context5.sent; recvblock.decrypt(ack); if (ack.includes('CCcam')) { _context5.next = 13; break; } throw new Error('Invalid username or password'); case 13: case 'end': return _context5.stop(); } } }, _callee5, this); })); function login() { return _ref5.apply(this, arguments); } return login; }() }, { key: 'read', value: function () { var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { var socket, timeout, data; return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: socket = this.socket, timeout = this.timeout; _context6.next = 3; return new Promise(function (ful, rej) { socket.once('data', ful); socket.once('error', rej); socket.once('end', rej); socket.once('timeout', rej); }); case 3: data = _context6.sent; return _context6.abrupt('return', data); case 5: case 'end': return _context6.stop(); } } }, _callee6, this); })); function read() { return _ref6.apply(this, arguments); } return read; }() }, { key: 'write', value: function () { var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(data) { var len = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : data.length; var socket, sendblock, timeout; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: socket = this.socket, sendblock = this.sendblock, timeout = this.timeout; sendblock.encrypt(data, len); _context7.next = 4; return new Promise(function (ful, rej) { socket.write(data, null, ful); socket.once('error', rej); socket.once('end', rej); socket.once('timeout', rej); }); case 4: case 'end': return _context7.stop(); } } }, _callee7, this); })); function write(_x2) { return _ref7.apply(this, arguments); } return write; }() }]); return ClineTester; }(); exports.default = ClineTester; var xor = function xor(buf) { var cccam = 'CCcam'; for (var i = 0; i < 8; i++) { buf[8 + i] = 0xff & i * buf[i]; if (i < 5) buf[i] ^= cccam[i].charCodeAt(); } return buf; }; var padBuffer = function padBuffer(input) { var len = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : input.length; if (!(input instanceof Buffer)) input = Buffer.from(input); var output = Buffer.alloc(len, 0); input.copy(output); return output; };