Fixed memory leak issues

This commit is contained in:
jsrobson10 2019-04-29 09:04:32 +10:00
parent 1d159570fc
commit bc797a9e12
2 changed files with 43 additions and 23 deletions

View File

@ -350,13 +350,15 @@ function connect(profile, connection_id)
g.users = {}; g.users = {};
// RSA child task // RSA child task
g.rsa_task = child_process.fork( if(!g.rsa_task) {
path.resolve('rsa.js'), [], g.rsa_task = child_process.fork(
{ path.resolve('rsa.js'), [],
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ], {
silent: false stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ],
} silent: false
); }
);
}
g.rsa_task.stdout.on('data', function(data) g.rsa_task.stdout.on('data', function(data)
{ {
@ -389,24 +391,20 @@ function connect(profile, connection_id)
g.raw_encryption_data_upto = 0; g.raw_encryption_data_upto = 0;
g.raw_encryption_data_size = 0; g.raw_encryption_data_size = 0;
// Create a client
var client;
// Is this direct mode // Is this direct mode
if(profile.mode == "direct") if(profile.mode == "direct")
{ {
// Set the client to a direct connection // Set the client to a direct connection
client = new net.Socket(); g.client = new net.Socket();
g.client = client;
} }
// Is this socks5 mode // Is this socks5 mode
if(profile.mode == "socks5") if(profile.mode == "socks5")
{ {
// Set the client as a socks proxy // Set the client as a socks proxy
client = socks_new(profile.proxy.hostname, profile.proxy.port); if(!g.client) {
console.log("Connecting with socks5", profile.proxy.hostname, profile.proxy.port) g.client = socks_new(profile.proxy.hostname, profile.proxy.port);
g.client = client; }
} }
function rsa_task_send(data) function rsa_task_send(data)
@ -416,7 +414,7 @@ function connect(profile, connection_id)
} }
// Connect to the server // Connect to the server
client.connect(port, hostname, function() g.client.connect(port, hostname, function()
{ {
console.log("Connected", profile.mode); console.log("Connected", profile.mode);
// Load the RSA key // Load the RSA key
@ -426,7 +424,7 @@ function connect(profile, connection_id)
}); });
// Catch errors // Catch errors
client.on("error", function(e) g.client.on("error", function(e)
{ {
// Tell the user // Tell the user
console.log("Connection failed with error ", e); console.log("Connection failed with error ", e);
@ -440,7 +438,7 @@ function connect(profile, connection_id)
} }
// Wait for data // Wait for data
client.on('data', function(data) g.client.on('data', function(data)
{ {
// Recieve data in order // Recieve data in order
recieve_ordered(data, recieve_ordered_memory, function(data) recieve_ordered(data, recieve_ordered_memory, function(data)
@ -486,7 +484,7 @@ function connect(profile, connection_id)
if(data.error == "auth") if(data.error == "auth")
{ {
// Destroy the connection // Destroy the connection
client.destroy(); g.client.destroy();
// Delete the connections // Delete the connections
delete connections[connection_id]; delete connections[connection_id];
@ -576,13 +574,16 @@ function connect(profile, connection_id)
}); });
}); });
client.on('close', function(data) g.client.on('close', function(data)
{ {
console.log("Connection closed:", connection_id) console.log("Connection closed:", connection_id)
// Close the connection // Close the connection
delete connections[connection_id]; delete connections[connection_id];
// Terminate the child processes
g.rsa_task.kill();
// Attempt to restart the connection in 10 seconds // Attempt to restart the connection in 10 seconds
console.log("Attempting reconnection...") console.log("Attempting reconnection...")
setTimeout(function() setTimeout(function()
@ -599,7 +600,7 @@ function connect(profile, connection_id)
console.log("Caught error", e); console.log("Caught error", e);
} }
}, },
1000); 10000);
}); });
g.rsa_task.on('message', function(message) g.rsa_task.on('message', function(message)
@ -622,7 +623,7 @@ function connect(profile, connection_id)
if(data['mode'] == 'get') if(data['mode'] == 'get')
{ {
// Send the public key to the server // Send the public key to the server
client.write(send_ordered(JSON.stringify({ g.client.write(send_ordered(JSON.stringify({
key: data['public'], key: data['public'],
mode: "pubkey" mode: "pubkey"
}))); })));
@ -638,7 +639,7 @@ function connect(profile, connection_id)
// Send login // Send login
console.log("Sending login") console.log("Sending login")
socket_write(client, g.encryption, { socket_write(g.client, g.encryption, {
mode: "login", mode: "login",
username: profile.username, username: profile.username,
password: profile.password password: profile.password

View File

@ -91,8 +91,27 @@ function profile_connect(id)
// Is the profile set // Is the profile set
if(profiles[id]) if(profiles[id])
{ {
// Allocate some varibles for reusing from an old connection
/*var client;
var rsa_task;
// Is there a connection here
if(connections[id])
{
// Reuse some varibles
var client = connections[id].client;
var rsa_task = connections[id].rsa_task;
console.log("rescued");
}
console.log("clirsa", client, rsa_task)*/
// Reconnect to the server // Reconnect to the server
connections[id] = connect(profiles[id], id); connections[id] = connect(profiles[id], id);
// Set the old varibles
/*connections[id].client = client;
connections[id].rsa_task = rsa_task;*/
} }
else else