chat-client/index.js

233 lines
5.0 KiB
JavaScript

// Get some libraries
const net = require("net");
const node_rsa = require("node-rsa");
const bsplit = require("buffer-split");
// Load the settings
var settings = require("./settings.json");
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);
// Create some new data
var decrypted = new Buffer.alloc(buff.length);
// Iterate over the string
for(var i=0;i<string.length;i++)
{
// Convert the string item to a number
var d = buff[i]-key.str[key.at.rx];
while(d < 0) d += 256;
decrypted[i] = d;
// Add 1 to the key counter
key.at.rx += 1;
// Is the key counter out of range
if(key.at.rx >= 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 = new Buffer.from(string);
// Create some new data
var encrypted = new Buffer.alloc(buff.length);
// Iterate over the string
for(var i=0;i<string.length;i++)
{
// Convert the string item to a number
var e = buff[i]+key.str[key.at.tx];
while(e > 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)
{
// 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<data.length;i++)
{
// Done signal
if(data[i]=="done")
{
/*// Set sock new to 0
sock_new = 0;
// Set the key
encryption = make_encryption_key(rsa_key.decrypt(raw_encryption_data));
console.log("Done.");
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(g.encryption, data);
// Split the data by newlines
data = bsplit(data, Buffer.from("\n"));
// Iterate over them
for(var i=0;i<data.length-1;i++)
{
console.log(JSON.parse(data[i]));
}
}
});
client.on('close', function(data)
{
// Close the window
//window.close();
});
// Return the global varibles
return g;
}