diff --git a/index.js b/index.js index c9984b5..a1db98f 100644 --- a/index.js +++ b/index.js @@ -7,184 +7,226 @@ const bsplit = require("buffer-split"); // Load the settings var settings = require("./settings.json"); -// Encryption varibles -var encryption; -var rsa_key; - -// Create a client -var client = new net.Socket(); - -function string_decrypt(key, string) +function connect() { - // Convert the string to a buffer - var buff = new Buffer.from(string); + // Make an accessible global object + var g = new Object(); - // Create some new data - var decrypted = new Buffer.alloc(buff.length); + // Encryption varibles + g.encryption; + g.rsa_key; - // Iterate over the string - for(var i=0;i= key.str.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; } - // Return the encrypted data - return decrypted; -} - -function string_encrypt(key, string) -{ - // Convert the string to a buffer - var buff = new Buffer.from(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; + // Convert the string to a buffer + var buff = new Buffer.from(string); - // Add 1 to the key counter - key.at.tx += 1; + // Create some new data + var encrypted = new Buffer.alloc(buff.length); - // Is the key counter out of range - if(key.at.tx >= key.str.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; } - // 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) -{ - // Send the data encrypted with JSON - socket.write(string_encrypt(encryption, JSON.stringify(data)+"\n")); -} - -function socket_ondata(socket, callback) -{ - // Wait for a response - socket.on('data', function(data) + function make_encryption_key(string) { - // Decode the data - data = JSON.parse(data); + // Make a new key + var key = new Object(); - // Call the callback - callback(data); + // 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) + { + // Send the data encrypted with JSON + socket.write(string_encrypt(encryption, JSON.stringify(data)+"\n")); + } + + function socket_ondata(socket, callback) + { + // Wait for a response + socket.on('data', function(data) + { + // Decode the data + data = JSON.parse(data); + + // Call the callback + callback(data); + }); + } + + + // Connect to the server + client.connect(22068, '127.0.0.1', function() + { + // Create an rsa key + g.rsa_key = new node_rsa({b:settings.rsa_size}); + + // Send the public key to the server + client.write(g.rsa_key.exportKey("public")+"\n"); }); + + // Wait for data + client.on('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 = bsplit(data, Buffer.from("\n")); + + // Iterate over the values + for(var i=0;i