mirror of
				https://github.com/valentineus/iii-client.git
				synced 2025-11-04 03:49:44 +03:00 
			
		
		
		
	
		
			
	
	
		
			233 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
		
		
			
		
	
	
			233 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| 
								 | 
							
								<!DOCTYPE html>
							 | 
						||
| 
								 | 
							
								<html lang="en">
							 | 
						||
| 
								 | 
							
								<head>
							 | 
						||
| 
								 | 
							
								    <meta charset="utf-8">
							 | 
						||
| 
								 | 
							
								    <title>JSDoc: Source: index.js</title>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    <script src="scripts/prettify/prettify.js"> </script>
							 | 
						||
| 
								 | 
							
								    <script src="scripts/prettify/lang-css.js"> </script>
							 | 
						||
| 
								 | 
							
								    <!--[if lt IE 9]>
							 | 
						||
| 
								 | 
							
								      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
							 | 
						||
| 
								 | 
							
								    <![endif]-->
							 | 
						||
| 
								 | 
							
								    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
							 | 
						||
| 
								 | 
							
								    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
							 | 
						||
| 
								 | 
							
								</head>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<body>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<div id="main">
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    <h1 class="page-title">Source: index.js</h1>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    <section>
							 | 
						||
| 
								 | 
							
								        <article>
							 | 
						||
| 
								 | 
							
								            <pre class="prettyprint source linenums"><code>/**
							 | 
						||
| 
								 | 
							
								 * @project iii-client
							 | 
						||
| 
								 | 
							
								 * @author Valentin Popov <info@valentineus.link>
							 | 
						||
| 
								 | 
							
								 * @license See LICENSE.md file included in this distribution.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var http = require('http');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Connects to the server and returns the connection data.
							 | 
						||
| 
								 | 
							
								 * @param {String} uuid - The bot UUID.
							 | 
						||
| 
								 | 
							
								 * @param {requestCallback} callback - The callback that handles the response.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var connect = function(uuid, callback) {
							 | 
						||
| 
								 | 
							
								    uuid = uuid || '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (!_verification(uuid)) {
							 | 
						||
| 
								 | 
							
								        throw new Error('The UUID is not a valid value!');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const query = {
							 | 
						||
| 
								 | 
							
								        port: 80,
							 | 
						||
| 
								 | 
							
								        hostname: 'iii.ru',
							 | 
						||
| 
								 | 
							
								        path: '/api/2.0/json/Chat.init/' + uuid + '/',
							 | 
						||
| 
								 | 
							
								        method: 'GET',
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const request = http.request(query, function(response) {
							 | 
						||
| 
								 | 
							
								        var json = '';
							 | 
						||
| 
								 | 
							
								        response.on('data', (raw) => json = _decryptJSON(raw));
							 | 
						||
| 
								 | 
							
								        response.on('end', () => callback(json.result));
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    request.on('error', function(error) {
							 | 
						||
| 
								 | 
							
								        console.error('Error connecting to the server!\n', error.message);
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    request.end();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports.connect = connect;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Send a message to the server and return a response.
							 | 
						||
| 
								 | 
							
								 * @param {Object} raw - The data to send.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw.cuid - Session identifier.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw.text - Message text.
							 | 
						||
| 
								 | 
							
								 * @param {requestCallback} callback - The callback that handles the response.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var send = function(raw, callback) {
							 | 
						||
| 
								 | 
							
								    raw = raw || {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const query = {
							 | 
						||
| 
								 | 
							
								        port: 80,
							 | 
						||
| 
								 | 
							
								        hostname: 'iii.ru',
							 | 
						||
| 
								 | 
							
								        path: '/api/2.0/json/Chat.request',
							 | 
						||
| 
								 | 
							
								        method: 'POST',
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const data = _createPackage(raw);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const request = http.request(query, function(response) {
							 | 
						||
| 
								 | 
							
								        var json = '';
							 | 
						||
| 
								 | 
							
								        response.on('data', (raw) => json = _decryptJSON(raw));
							 | 
						||
| 
								 | 
							
								        response.on('end', () => callback(json));
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    request.on('error', function(error) {
							 | 
						||
| 
								 | 
							
								        console.error('Error sending the package!\n', error.message);
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    request.write(data);
							 | 
						||
| 
								 | 
							
								    request.end();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports.send = send;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Encrypts the incoming data.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw - Decrypted data.
							 | 
						||
| 
								 | 
							
								 * @returns {String} - Encrypted string.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _encrypt = function(raw) {
							 | 
						||
| 
								 | 
							
								    raw = raw || '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    var base64 = Buffer.from(raw).toString('base64');
							 | 
						||
| 
								 | 
							
								    var string = Buffer.from(base64);
							 | 
						||
| 
								 | 
							
								    return _merger(string).toString('base64');
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._encrypt = _encrypt;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Decrypts the incoming data.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw - Encrypted data.
							 | 
						||
| 
								 | 
							
								 * @returns {String} - Decrypted string.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _decrypt = function(raw) {
							 | 
						||
| 
								 | 
							
								    raw = raw || '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    var string = Buffer.from(raw, 'base64');
							 | 
						||
| 
								 | 
							
								    var decrypted = _merger(string).toString();
							 | 
						||
| 
								 | 
							
								    return Buffer.from(decrypted, 'base64');
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._decrypt = _decrypt;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Decrypts an encrypted JSON object.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw - Encrypted data.
							 | 
						||
| 
								 | 
							
								 * @returns {Object} - Decrypted JSON.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _decryptJSON = function(raw) {
							 | 
						||
| 
								 | 
							
								    raw = raw || '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    var string = raw.toString('ascii');
							 | 
						||
| 
								 | 
							
								    var data = _decrypt(string);
							 | 
						||
| 
								 | 
							
								    return JSON.parse(data);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._decryptJSON = _decryptJSON;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Merge and convert a string.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw - The string to convert.
							 | 
						||
| 
								 | 
							
								 * @returns {String} - The converted string.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _merger = function(data) {
							 | 
						||
| 
								 | 
							
								    data = data || '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const salt = Buffer.from('some very-very long string without any non-latin characters due to different string representations inside of variable programming languages');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for (var i = 0; i < data.length; i++) {
							 | 
						||
| 
								 | 
							
								        data[i] = data[i] ^ salt[i % salt.length];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return data;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._merger = _merger;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Creates an encrypted package to send.
							 | 
						||
| 
								 | 
							
								 * @param {Object} raw - The data to send.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw.cuid - Session identifier.
							 | 
						||
| 
								 | 
							
								 * @param {String} raw.text - Message text.
							 | 
						||
| 
								 | 
							
								 * @returns {String} - Encrypted string.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _createPackage = function(raw) {
							 | 
						||
| 
								 | 
							
								    raw = raw || {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (!raw.text) {
							 | 
						||
| 
								 | 
							
								        throw new Error('There is no data to send!');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (!_verification(raw.cuid)) {
							 | 
						||
| 
								 | 
							
								        throw new Error('Parameter \'CUID\' is not a valid UUID value!');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    var data = [];
							 | 
						||
| 
								 | 
							
								    data.push(raw.cuid);
							 | 
						||
| 
								 | 
							
								    data.push(raw.text.toString());
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    var json = JSON.stringify(data);
							 | 
						||
| 
								 | 
							
								    return _encrypt(json);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._createPackage = _createPackage;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Validation UUID format string.
							 | 
						||
| 
								 | 
							
								 * @param {String} data - The string to check.
							 | 
						||
| 
								 | 
							
								 * @returns {Boolean}
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var _verification = function(data) {
							 | 
						||
| 
								 | 
							
								    data = data || '';
							 | 
						||
| 
								 | 
							
								    const regexp = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', 'i');
							 | 
						||
| 
								 | 
							
								    return regexp.test(data);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exports._verification = _verification;
							 | 
						||
| 
								 | 
							
								</code></pre>
							 | 
						||
| 
								 | 
							
								        </article>
							 | 
						||
| 
								 | 
							
								    </section>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								</div>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<nav>
							 | 
						||
| 
								 | 
							
								    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#_createPackage">_createPackage</a></li><li><a href="global.html#_decrypt">_decrypt</a></li><li><a href="global.html#_decryptJSON">_decryptJSON</a></li><li><a href="global.html#_encrypt">_encrypt</a></li><li><a href="global.html#_merger">_merger</a></li><li><a href="global.html#_verification">_verification</a></li><li><a href="global.html#connect">connect</a></li><li><a href="global.html#http">http</a></li><li><a href="global.html#send">send</a></li></ul>
							 | 
						||
| 
								 | 
							
								</nav>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<br class="clear">
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<footer>
							 | 
						||
| 
								 | 
							
								    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon May 29 2017 01:45:41 GMT+0300 (MSK)
							 | 
						||
| 
								 | 
							
								</footer>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<script> prettyPrint(); </script>
							 | 
						||
| 
								 | 
							
								<script src="scripts/linenumber.js"> </script>
							 | 
						||
| 
								 | 
							
								</body>
							 | 
						||
| 
								 | 
							
								</html>
							 |