chat-server/main.js

206 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-03-24 17:57:54 +11:00
#!/usr/bin/node
// Get some libraries
const random_bytes = require("random-bytes");
const bsplit = require("buffer-split");
const node_rsa = require("node-rsa");
2019-03-24 17:57:54 +11:00
const net = require("net");
const fs = require("fs");
// Load the settings
var settings = require("./settings.json");
console.log("Ready.");
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, data)
{
// Send the data encrypted with JSON
socket.sock.write(string_encrypt(socket.key, 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);
});
}
function socket_init(socket, callback, ondata)
{
// Set the socket
var sock = new Object();
sock.sock = socket;
sock.new = true;
console.log("Connection from "+socket.localAdress);
// Wait for data
sock.sock.on('data', function(data)
{
// Is the socket new
if(sock.new)
{
// Set sock new
sock.new = false;
// Load the key
var key = new node_rsa();
key.importKey(data.toString(), 'public');
console.log("Loaded the RSA key");
// Get some random bytes
random_bytes(settings.encryption_key_size, function(error,string)
{
// Throw an error if there is one
if(error) throw error;
// Make the key
sock.key = make_encryption_key(string);
console.log("Created an encryption key")
// Encrypt the key with RSA
var key_encrypted = key.encrypt(sock.key.str, 'base64');
console.log("Encrypted the key");
// Send the size of the key to the client
sock.sock.write(key_encrypted.length.toString()+"\n");
console.log("Sent the size of the key:", key_encrypted.length)
// Send the key to the client
sock.sock.write(key_encrypted.toString()+"\n");
console.log("Sent the key to the client");
// Send the done signal
//sock.sock.write("done"+"\n");
// Call the callback
setTimeout(callback, 1000, sock);
});
}
else
{
// Decrypt the data
data = string_decrypt(sock.key, data);
// Split the data by newlines
data = bsplit(data, Buffer("\n"));
// Iterate over them
for(var i=0;i<data.length-1;i++)
{
// Send the parsed data to the callback
console.log(data[i]);
ondata(JSON.parse(data[i]));
}
}
});
// Return the socket
return sock;
}
2019-03-24 17:57:54 +11:00
// Create a server
var server = net.createServer(function(socket)
{
var sock;
2019-03-24 17:57:54 +11:00
// Initialise the socket and wait for data
socket_init(socket, function(socket)
{
socket_write(socket, {message: "From the server! :D", array: [1,2,3,4,6,5,"xD",12]});
sock = socket;
},
function(data)
2019-03-24 17:57:54 +11:00
{
console.log(data);
socket_write(sock, {echo: data});
2019-03-24 17:57:54 +11:00
});
});
// Listen for data
server.listen(22068, '');