chat-client/index.js

387 lines
7.7 KiB
JavaScript
Raw Normal View History

2019-03-24 17:22:44 +11:00
// Get some libraries
2019-04-09 19:18:47 +10:00
const fs = require("fs");
2019-03-24 17:22:44 +11:00
const net = require("net");
2019-04-09 19:18:47 +10:00
const path = require("path");
const node_rsa = require("node-rsa");
const bsplit = require("buffer-split");
2019-04-09 19:18:47 +10:00
const child_process = require('child_process');
// Load the settings
var settings = require("./settings.json");
2019-04-09 19:18:47 +10:00
// Create an rsa key
var rsa_key;
function toBuffer(bytes)
{
2019-04-09 19:18:47 +10:00
// String
if(typeof(bytes) == 'string')
{
// Create a buffer
var buffer = new Buffer.alloc(bytes.length);
2019-03-24 17:22:44 +11:00
2019-04-09 19:18:47 +10:00
// Loop over the bytes
for(var i=0;i<bytes.length;i++)
{
// Save to the buffer
buffer[i] = bytes[i].charCodeAt(0);
}
2019-03-24 17:22:44 +11:00
2019-04-09 19:18:47 +10:00
// Return the buffer
return buffer;
}
2019-04-09 19:18:47 +10:00
// Return if this is anything else
return bytes;
}
2019-04-09 19:18:47 +10:00
function toBytes(buffer)
{
// Make sure the buffer is a buffer
buffer = toBuffer(buffer);
// Create some bytes
var bytes = "";
2019-04-09 19:18:47 +10:00
// Loop over the buffer
for(var i=0;i<buffer.length;i++)
{
2019-04-09 19:18:47 +10:00
// Add to the bytes string
bytes += String.fromCharCode(buffer[i]);
}
2019-04-09 19:18:47 +10:00
// Return the bytes
return bytes;
}
2019-04-09 19:18:47 +10:00
// Recieve helper global varibles
var recieve_buffer = new Buffer.alloc(0);
var recieve_get = false;
var recieve_size = 0;
2019-04-09 19:18:47 +10:00
function recieve_ordered(data, callback)
{
// Convert the data into a buffer
data = toBuffer(data);
// Loop over the data
for(var i=0;i<data.length;i++)
{
// Add the data to the buffer
recieve_buffer = new Buffer.concat([recieve_buffer, Buffer.from([data[i]])]);
2019-04-09 19:18:47 +10:00
// Is the buffer getting data
if(!recieve_get)
{
// Does the buffer contain a number
if(recieve_buffer.length >= 4)
{
2019-04-09 19:18:47 +10:00
// 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;
}
}
2019-04-09 19:18:47 +10:00
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;
}
}
}
2019-04-09 19:18:47 +10:00
}
2019-04-09 19:18:47 +10:00
function send_ordered(data)
{
// Convert the data into a buffer
data = toBuffer(data);
2019-04-09 19:18:47 +10:00
// Get the size of the data
var size = data.length;
2019-04-09 19:18:47 +10:00
// Turn it into a 32 bit buffer
var bytes = new Buffer.alloc(4);
bytes.writeUInt32BE(size, 0);
2019-04-09 19:18:47 +10:00
// Return the buffer and the data
return new Buffer.concat([bytes, data]);
}
2019-04-09 19:18:47 +10:00
function string_decrypt(key, string)
{
// Convert the string to a buffer
var buff = toBuffer(string);
2019-04-09 19:18:47 +10:00
// Create some new data
var decrypted = new Buffer.alloc(buff.length);
2019-04-09 19:18:47 +10:00
// Iterate over the string
for(var i=0;i<string.length;i++)
{
2019-04-09 19:18:47 +10:00
// Convert the string item to a number
var d = buff[i]-key.str[key.at.rx];
while(d < 0) d += 256;
decrypted[i] = d;
2019-04-09 19:18:47 +10:00
// Add 1 to the key counter
key.at.rx += 1;
2019-03-24 17:22:44 +11:00
2019-04-09 19:18:47 +10:00
// Is the key counter out of range
if(key.at.rx >= key.str.length)
{
// Set to zero
key.at.rx = 0;
}
}
2019-04-09 19:18:47 +10:00
// Return the encrypted data
return decrypted;
}
2019-04-09 19:18:47 +10:00
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<string.length;i++)
{
2019-04-09 19:18:47 +10:00
// Convert the string item to a number
var e = buff[i]+key.str[key.at.tx];
while(e > 255) e -= 256;
encrypted[i] = e;
2019-04-09 19:18:47 +10:00
// 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;
}
}
2019-04-09 19:18:47 +10:00
// Return the encrypted data
return encrypted;
}
2019-04-09 19:18:47 +10:00
function make_encryption_key(string)
{
// Make a new key
var key = new Object();
2019-04-09 19:18:47 +10:00
// Set the varibles
key.str = string;
key.at = new Object();
key.at.rx = 0;
key.at.tx = 0;
2019-04-09 19:18:47 +10:00
// 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);
2019-04-09 19:18:47 +10:00
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'), [],
{
2019-04-09 19:18:47 +10:00
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ],
silent: false
}
);
2019-04-09 19:18:47 +10:00
g.rsa_task.unref();
2019-04-09 19:18:47 +10:00
g.rsa_task.stdout.on('data', function(data)
{
console.log("child stdout:", data.toString());
});
2019-04-09 19:18:47 +10:00
g.rsa_task.stderr.on('data', function(data)
{
console.log("child stderr:", data.toString());
});
2019-04-09 19:18:47 +10:00
g.rsa_task.on('error', function(error)
{
console.log("Error on child process:", error);
})
2019-04-09 19:18:47 +10:00
g.rsa_task.on('exit', function(code, signal)
{
console.log("Child process closed:", code, signal);
});
2019-04-09 19:18:47 +10:00
// Encryption varibles
g.encryption;
2019-04-09 19:18:47 +10:00
// Set new
g.sock_new = 2;
2019-04-09 19:18:47 +10:00
// Set a temporary encryption varible
g.raw_encryption_data = "";
g.raw_encryption_data_upto = 0;
g.raw_encryption_data_size = 0;
2019-04-09 19:18:47 +10:00
// Create a client
var client = new net.Socket();
2019-04-09 19:18:47 +10:00
function rsa_task_send(data)
{
console.log("Send to child:", JSON.stringify(data));
g.rsa_task.send(JSON.stringify(data));
}
2019-04-09 19:18:47 +10:00
// Connect to the server
client.connect(port, hostname, function()
{
// Load the RSA key
rsa_task_send({
mode: "load"
});
});
2019-04-09 19:18:47 +10:00
// 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());
2019-04-09 19:18:47 +10:00
// 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
}));
}
}
2019-04-09 19:18:47 +10:00
else
{
// Decrypt the data
data = string_decrypt(g.encryption, data);
2019-04-09 19:18:47 +10:00
// Split the data by newlines
data = bsplit(data, Buffer.from("\n"));
2019-04-09 19:18:47 +10:00
// Iterate over them
for(var i=0;i<data.length-1;i++)
{
console.log(JSON.parse(data[i]));
}
}
2019-04-09 19:18:47 +10:00
});
});
client.on('close', function(data)
{
// Close the window
//window.close();
});
2019-03-24 17:22:44 +11:00
2019-04-09 19:18:47 +10:00
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:",atob(data['out']));
// Send status
socket_write(client, g.encryption, 1);
console.log("Done.");
}
});
// Return the global varibles
return g;
}