2017-05-29 01:46:00 +03:00
/ * *
* @ project iii - client
* @ author Valentin Popov < info @ valentineus . link >
* @ license See LICENSE . md file included in this distribution .
* /
var http = require ( 'http' ) ;
2017-05-29 07:02:09 +03:00
exports . connect = connect ;
2017-05-29 01:46:00 +03:00
/ * *
* Connects to the server and returns the connection data .
* @ param { String } uuid - The bot UUID .
* @ param { requestCallback } callback - The callback that handles the response .
* /
2017-05-29 07:02:09 +03:00
function connect ( uuid , callback ) {
2017-05-29 01:46:00 +03:00
uuid = uuid || '' ;
2017-05-30 16:03:39 +03:00
if ( ! isVerification ( uuid ) ) {
2017-05-29 01:46:00 +03:00
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' ,
2017-05-29 07:02:09 +03:00
} ;
2017-05-29 01:46:00 +03:00
const request = http . request ( query , function ( response ) {
var json = '' ;
2017-05-30 16:03:39 +03:00
response . on ( 'data' , ( raw ) => json = decryptJSON ( raw ) ) ;
2017-05-29 01:46:00 +03:00
response . on ( 'end' , ( ) => callback ( json . result ) ) ;
} ) ;
2017-05-29 07:02:09 +03:00
request . on ( 'error' , ( error ) => Error ( error . message ) ) ;
2017-05-29 01:46:00 +03:00
request . end ( ) ;
}
2017-05-29 07:02:09 +03:00
exports . send = send ;
2017-05-29 01:46:00 +03:00
/ * *
* 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 .
* /
2017-05-29 07:02:09 +03:00
function send ( raw , callback ) {
2017-05-29 01:46:00 +03:00
raw = raw || { } ;
const query = {
port : 80 ,
hostname : 'iii.ru' ,
path : '/api/2.0/json/Chat.request' ,
method : 'POST' ,
2017-05-29 07:02:09 +03:00
} ;
2017-05-29 01:46:00 +03:00
2017-05-30 16:03:39 +03:00
const data = createPackage ( raw ) ;
2017-05-29 01:46:00 +03:00
const request = http . request ( query , function ( response ) {
var json = '' ;
2017-05-30 16:03:39 +03:00
response . on ( 'data' , ( raw ) => json = decryptJSON ( raw ) ) ;
response . on ( 'end' , ( ) => callback ( json . result ) ) ;
2017-05-29 01:46:00 +03:00
} ) ;
2017-05-29 07:02:09 +03:00
request . on ( 'error' , ( error ) => Error ( error ) ) ;
2017-05-29 01:46:00 +03:00
request . write ( data ) ;
request . end ( ) ;
}
2017-05-30 16:03:39 +03:00
exports . encrypt = encrypt ;
2017-05-29 01:46:00 +03:00
/ * *
* Encrypts the incoming data .
* @ param { String } raw - Decrypted data .
* @ returns { String } - Encrypted string .
* /
2017-05-30 16:03:39 +03:00
function encrypt ( raw ) {
2017-05-29 01:46:00 +03:00
raw = raw || '' ;
var base64 = Buffer . from ( raw ) . toString ( 'base64' ) ;
var string = Buffer . from ( base64 ) ;
2017-05-30 16:03:39 +03:00
return mergerString ( string ) . toString ( 'base64' ) ;
2017-05-29 01:46:00 +03:00
}
2017-05-30 16:03:39 +03:00
exports . decrypt = decrypt ;
2017-05-29 01:46:00 +03:00
/ * *
* Decrypts the incoming data .
* @ param { String } raw - Encrypted data .
* @ returns { String } - Decrypted string .
* /
2017-05-30 16:03:39 +03:00
function decrypt ( raw ) {
2017-05-29 01:46:00 +03:00
raw = raw || '' ;
var string = Buffer . from ( raw , 'base64' ) ;
2017-05-30 16:03:39 +03:00
var decrypted = mergerString ( string ) . toString ( ) ;
2017-05-29 01:46:00 +03:00
return Buffer . from ( decrypted , 'base64' ) ;
}
2017-05-30 16:03:39 +03:00
exports . decryptJSON = decryptJSON ;
2017-05-29 01:46:00 +03:00
/ * *
* Decrypts an encrypted JSON object .
* @ param { String } raw - Encrypted data .
* @ returns { Object } - Decrypted JSON .
* /
2017-05-30 16:03:39 +03:00
function decryptJSON ( raw ) {
2017-05-29 01:46:00 +03:00
raw = raw || '' ;
var string = raw . toString ( 'ascii' ) ;
2017-05-30 16:03:39 +03:00
var data = decrypt ( string ) ;
2017-05-29 01:46:00 +03:00
return JSON . parse ( data ) ;
}
2017-05-30 16:03:39 +03:00
exports . mergerString = mergerString ;
2017-05-29 01:46:00 +03:00
/ * *
* Merge and convert a string .
* @ param { String } raw - The string to convert .
* @ returns { String } - The converted string .
* /
2017-05-30 16:03:39 +03:00
function mergerString ( data ) {
2017-05-29 01:46:00 +03:00
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 ;
}
2017-05-30 16:03:39 +03:00
exports . createPackage = createPackage ;
2017-05-29 01:46:00 +03:00
/ * *
* 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 .
* /
2017-05-30 16:03:39 +03:00
function createPackage ( raw ) {
2017-05-29 01:46:00 +03:00
raw = raw || { } ;
if ( ! raw . text ) {
throw new Error ( 'There is no data to send!' ) ;
}
2017-05-30 16:03:39 +03:00
if ( ! isVerification ( raw . cuid ) ) {
2017-05-29 01:46:00 +03:00
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 ) ;
2017-05-30 16:03:39 +03:00
return encrypt ( json ) ;
2017-05-29 01:46:00 +03:00
}
2017-05-30 16:03:39 +03:00
exports . isVerification = isVerification ;
2017-05-29 01:46:00 +03:00
/ * *
* Validation UUID format string .
* @ param { String } data - The string to check .
* @ returns { Boolean }
* /
2017-05-30 16:03:39 +03:00
function isVerification ( data ) {
2017-05-29 01:46:00 +03:00
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 ) ;
}