// Get some libraries const fs = require("fs"); const net = require("net"); const path = require("path"); const node_rsa = require("node-rsa"); const bsplit = require("buffer-split"); const child_process = require('child_process'); // Load the settings var settings = require("./settings.json"); // Create an rsa key var rsa_key; function toBuffer(bytes) { // String if(typeof(bytes) == 'string') { // Create a buffer var buffer = new Buffer.alloc(bytes.length); // Loop over the bytes for(var i=0;i= 4) { // Get the number recieve_size = recieve_buffer.readUInt32BE(0); // Reset the buffer recieve_buffer = new Buffer.alloc(0); // Set the get data mode to true recieve_get = true; } } else { // Is the recieve buffer as big as the size if(recieve_buffer.length == recieve_size) { // Call the callback callback(recieve_buffer); // Reset the buffer and the size recieve_buffer = new Buffer.alloc(0); recieve_size = 0; // Set the get data mode to false recieve_get = false; } } } } function send_ordered(data) { // Convert the data into a buffer data = toBuffer(data); // Get the size of the data var size = data.length; // Turn it into a 32 bit buffer var bytes = new Buffer.alloc(4); bytes.writeUInt32BE(size, 0); // Return the buffer and the data return new Buffer.concat([bytes, data]); } function string_decrypt(key, string) { // Convert the string to a buffer var buff = toBuffer(string); // Create some new data var decrypted = new Buffer.alloc(buff.length); // Iterate over the string for(var i=0;i= key.str.length) { // Set to zero key.at.rx = 0; } } // Return the encrypted data return decrypted; } function string_encrypt(key, string) { // Convert the string to a buffer var buff = toBuffer(string); // Create some new data var encrypted = new Buffer.alloc(buff.length); // Iterate over the string for(var i=0;i 255) e -= 256; encrypted[i] = e; // Add 1 to the key counter key.at.tx += 1; // Is the key counter out of range if(key.at.tx >= key.str.length) { // Set to zero key.at.tx = 0; } } // Return the encrypted data return encrypted; } function make_encryption_key(string) { // Make a new key var key = new Object(); // Set the varibles key.str = string; key.at = new Object(); key.at.rx = 0; key.at.tx = 0; // Return the key return key; } function socket_write(socket, encryption, data) { console.log("socket_write() b4:", data); // Convert the data to JSON data = JSON.stringify(data); console.log("socket_write() stringify:", data); // Encrypt the data data = string_encrypt(encryption, data); console.log("socket_write() encrypt:", data); // Order the request data = send_ordered(data); console.log("socket_write() ordered:", data); // Send the data socket.write(data); } function connect(hostname, port=22068) { // Make an accessible global object var g = new Object(); // RSA child task g.rsa_task = child_process.fork( path.resolve('rsa.js'), [], { stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ], silent: false } ); g.rsa_task.unref(); g.rsa_task.stdout.on('data', function(data) { console.log("child stdout:", data.toString()); }); g.rsa_task.stderr.on('data', function(data) { console.log("child stderr:", data.toString()); }); g.rsa_task.on('error', function(error) { console.log("Error on child process:", error); }) g.rsa_task.on('exit', function(code, signal) { console.log("Child process closed:", code, signal); }); // Encryption varibles g.encryption; // Set new g.sock_new = 2; // Set a temporary encryption varible g.raw_encryption_data = ""; g.raw_encryption_data_upto = 0; g.raw_encryption_data_size = 0; // Create a client var client = new net.Socket(); function rsa_task_send(data) { console.log("Send to child:", JSON.stringify(data)); g.rsa_task.send(JSON.stringify(data)); } // Connect to the server client.connect(port, hostname, function() { // Load the RSA key rsa_task_send({ mode: "load" }); }); // Wait for data client.on('data', function(data) { // Recieve data in order recieve_ordered(data, function(data) { console.log("RAW: ", { input: data, sock_new:g.sock_new, raw_encryption_data_upto: g.raw_encryption_data_upto, bsplit_length: bsplit(data, Buffer.from("\n")).length }); if(g.sock_new != 0) { // Split the data by newlines data = JSON.parse(data.toString()); // Is this the key if(data.mode == "encryption_key") { // Send the key to be decrypted g.rsa_task.send(JSON.stringify({ mode: "decrypt", data: data.key })); } } else { // Decrypt the data data = string_decrypt(g.encryption, data); // Split the data by newlines data = bsplit(data, Buffer.from("\n")); // Iterate over them for(var i=0;i