2019-03-24 17:57:54 +11:00
|
|
|
#!/usr/bin/node
|
|
|
|
|
|
|
|
// Get some libraries
|
2019-03-30 18:34:19 +11:00
|
|
|
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");
|
|
|
|
|
2019-03-30 18:34:19 +11:00
|
|
|
// 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;
|
|
|
|
|
2019-03-30 21:27:36 +11:00
|
|
|
console.log("Connection from "+socket.localAdress);
|
|
|
|
|
2019-03-30 18:34:19 +11:00
|
|
|
// 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
|
2019-03-30 21:27:36 +11:00
|
|
|
sock.sock.write(key_encrypted.length.toString()+"\n");
|
2019-03-30 18:34:19 +11:00
|
|
|
console.log("Sent the size of the key:", key_encrypted.length)
|
|
|
|
|
|
|
|
// Send the key to the client
|
2019-03-30 21:27:36 +11:00
|
|
|
sock.sock.write(key_encrypted.toString()+"\n");
|
2019-03-30 18:34:19 +11:00
|
|
|
console.log("Sent the key to the client");
|
|
|
|
|
2019-03-30 21:27:36 +11:00
|
|
|
// Send the done signal
|
|
|
|
//sock.sock.write("done"+"\n");
|
|
|
|
|
2019-03-30 18:34:19 +11:00
|
|
|
// Call the callback
|
2019-03-30 21:27:36 +11:00
|
|
|
setTimeout(callback, 1000, sock);
|
2019-03-30 18:34:19 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-03-30 21:27:36 +11:00
|
|
|
console.log(data[i]);
|
2019-03-30 18:34:19 +11:00
|
|
|
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)
|
|
|
|
{
|
2019-03-30 18:34:19 +11:00
|
|
|
var sock;
|
2019-03-24 17:57:54 +11:00
|
|
|
|
2019-03-30 18:34:19 +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
|
|
|
{
|
2019-03-30 18:34:19 +11:00
|
|
|
console.log(data);
|
|
|
|
socket_write(sock, {echo: data});
|
2019-03-24 17:57:54 +11:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for data
|
|
|
|
server.listen(22068, '');
|