// 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 = toBuffer(string); key.at = new Object(); key.at.rx = 0; key.at.tx = 0; //console.log("make_encryption_key:", key.str); // 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(profile, connection_id) { // Setup the port and hostname varibles var port = profile.port; var hostname = profile.hostname; // 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.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) { // Parse the string data = JSON.parse(toBytes(data)); // 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 })); // Set sock new g.sock_new = 0; } } else { // Decrypt the data data = string_decrypt(g.encryption, data); // Convert it from JSON data = JSON.parse(data); console.log("From server:", data); // Note to self: do something here } }); }); client.on('close', function(data) { // Close the connection delete connections[connection_id]; }); g.rsa_task.on('message', function(message) { //console.log(message) // Parse the message from JSON var data = JSON.parse(message); // Is this a rsa load response if(data['mode'] == 'load') { // Get the rsa key g.rsa_task.send(JSON.stringify({ mode: "get" })); } // Is this an rsa get response if(data['mode'] == 'get') { // Send the public key to the server client.write(send_ordered(JSON.stringify({ key: data['public'], mode: "pubkey" }))); } // Is this an rsa data decrypt response if(data['mode'] == 'decrypt') { // Set the key g.encryption = make_encryption_key(atob(data['out'])); //console.log("Sending 1:", data['out']); // Send status socket_write(client, g.encryption, 1); console.log("Trying to get login data"); // Create a variable var userdata_filename = "servers/"+hostname+":"+port.toString()+".json"; console.log(userdata_filename); // Try and get the users login data //var userdata = readFile() } }); // Return the global varibles return g; }