Made connections happen in a function instead of globally.

This commit is contained in:
jsrobson10 2019-03-30 21:28:20 +11:00
parent 727643aa79
commit e685e63c70
2 changed files with 203 additions and 161 deletions

182
index.js
View File

@ -7,15 +7,28 @@ 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()
{
// Make an accessible global object
var g = new Object();
// Encryption varibles
g.encryption;
g.rsa_key;
// 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 string_decrypt(key, string)
{
// Convert the string to a buffer
var buff = new Buffer.from(string);
@ -43,10 +56,10 @@ function string_decrypt(key, string)
// Return the encrypted data
return decrypted;
}
}
function string_encrypt(key, string)
{
function string_encrypt(key, string)
{
// Convert the string to a buffer
var buff = new Buffer.from(string);
@ -74,10 +87,10 @@ function string_encrypt(key, string)
// Return the encrypted data
return encrypted;
}
}
function make_encryption_key(string)
{
function make_encryption_key(string)
{
// Make a new key
var key = new Object();
@ -89,16 +102,16 @@ function make_encryption_key(string)
// Return the key
return key;
}
}
function socket_write(socket, encryption, data)
{
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)
{
function socket_ondata(socket, callback)
{
// Wait for a response
socket.on('data', function(data)
{
@ -108,54 +121,41 @@ function socket_ondata(socket, callback)
// Call the callback
callback(data);
});
}
// Connect to the server
client.connect(22068, '127.0.0.1', function()
{
// Create an rsa key
rsa_key = new node_rsa({b:settings.rsa_size});
// Send the public key to the server
client.write(rsa_key.exportKey("public"));
});
// Set new
var sock_new = 2;
// Set a temporary encryption varible
var raw_encryption_data = "";
var raw_encryption_data_upto = 0;
var raw_encryption_data_size = 0;
// Wait for data
client.on('data', function(data)
{
if(sock_new == 2)
{
console.log("Recieved key.");
// Set the size of the key
raw_encryption_data_size = parseInt(data);
// Set sock new to 1
sock_new = 1;
}
// Is the socket recieving the key
else if(sock_new == 1)
{
console.log(data);
// Decode the data
raw_encryption_data += data;
raw_encryption_data_upto += data.length;
console.log(data.length);
// Is this the end of the stream
if(raw_encryption_data_upto == raw_encryption_data_size)
// Connect to the server
client.connect(22068, '127.0.0.1', function()
{
// Set sock new to 0
// 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<data.length;i++)
{
// Done signal
if(data[i]=="done")
{
/*// Set sock new to 0
sock_new = 0;
// Set the key
@ -163,14 +163,52 @@ client.on('data', function(data)
console.log("Done.");
socket_write(client, encryption, {message: "hello, world!", number: 53});
socket_write(client, encryption, {message: "hello, world!", number: 53});*/
}
// Is the client recieving the key size
if(g.sock_new == 2)
{
console.log("Recieved key.");
// Set the size of the key
g.raw_encryption_data_size = parseInt(data[i]);
// Set sock new to 1
g.sock_new = 1;
}
// Is the client recieving the key
else if(g.sock_new == 1)
{
console.log(data[i]);
// Decode the data
g.raw_encryption_data += data[i];
g.raw_encryption_data_upto += data[i].length;
console.log(data[i].length);
// Is this the end of the stream
if(g.raw_encryption_data_upto == g.raw_encryption_data_size)
{
// Set sock new to 0
g.sock_new = 0;
// Set the key
g.encryption = make_encryption_key(g.rsa_key.decrypt(g.raw_encryption_data));
console.log("Done.");
socket_write(client, g.encryption, {message: "hello, world!", number: 53});
}
}
}
}
else
{
// Decrypt the data
data = string_decrypt(encryption, data);
data = string_decrypt(g.encryption, data);
// Split the data by newlines
data = bsplit(data, Buffer.from("\n"));
@ -181,10 +219,14 @@ client.on('data', function(data)
console.log(JSON.parse(data[i]));
}
}
});
});
client.on('close', function(data)
{
client.on('close', function(data)
{
// Close the window
//window.close();
});
});
// Return the global varibles
return g;
}

View File

@ -1,3 +1,3 @@
{
"rsa_size": 512
"rsa_size": 2048
}