Created
January 4, 2012 15:32
-
-
Save jdiez17/1560558 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require("sys"), | |
net = require("net"), exec = require('child_process'); | |
var debug = 4; | |
//var client = net.createConnection(8000); | |
var __curr_process; | |
var client = net.createConnection(8000, "moodle.jdiez.me"); | |
client.setEncoding("UTF8"); | |
function get_payload(data) { | |
data = data.substring(data.indexOf("::") + 2); | |
length = data.substring(0, data.indexOf("::")); | |
data = data.substring((length + "::").length); | |
payload = data.substring(0, length); | |
recursion = (data.length == length) ? false : data.substring(length); | |
return {'payload': payload, 'recursion': recursion}; | |
} | |
var protocol = { | |
__send: function(stream, string) { | |
if(debug > 4) sys.puts("Sent to server: " + string); | |
stream.write(string); | |
}, | |
parse: function(data) { | |
if(data.indexOf("req_auth") == 0) { | |
if(debug > 3) sys.puts("Authentication request received."); | |
this.__send(client, "auth::"); | |
} | |
if(data.indexOf("spawn::") == 0) { | |
if(debug > 3) sys.puts("Spawn message."); | |
payload = get_payload(data); | |
__curr_process = payload.payload; | |
if(payload.recursion) protocol.parse(payload.recursion); | |
} | |
if(data.indexOf("args::") == 0) { | |
if(debug > 3) sys.puts("Args message."); | |
payload = get_payload(data); | |
var args = eval(payload.payload); | |
__curr_process = exec.spawn(__curr_process, args); | |
if(payload.recursion) protocol.parse(payload.recursion); | |
} | |
if(data.indexOf("stdin::") == 0 && __curr_process) { | |
if(debug > 3) sys.puts("Stdin event."); | |
payload = get_payload(data); | |
try { | |
__curr_process.stdin.write(payload.payload); | |
} catch(e) {} | |
if(payload.recursion) protocol.parse(payload.recursion); | |
} | |
} | |
}; | |
client.addListener("connect", function() { | |
sys.puts("Client connected."); | |
}); | |
client.addListener("data", function(data) { | |
if(debug > 4) sys.puts("Response from server: " + data); | |
protocol.parse(data); | |
// do shit with data | |
}); | |
client.addListener("close", function(data) { | |
sys.puts("Disconnected from server"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'), net = require('net'); | |
var debug = 3; | |
var client = net.createConnection(8001); | |
client.setEncoding("utf-8"); | |
var protocol = { | |
__send: function(stream, string) { | |
if(debug > 4) sys.puts("Sent to server: " + string); | |
stream.write(string); | |
}, | |
parse: function(data) { | |
if(data.indexOf("req_auth") == 0) { | |
if(debug > 4) sys.puts("Authentication request received."); | |
this.__send(client, "auth::"); | |
} | |
}, | |
spawn: function(data) { | |
if(!data) data = ""; | |
this.__send(client, "spawn::" + data.length + "::" + data); | |
}, | |
stdin: function(data) { | |
if(!data) data = ""; | |
this.__send(client, "stdin::" + data.length + "::" + data); | |
}, | |
args: function(data) { | |
if(!data) data = ""; | |
this.__send(client, "args::" + data.length + "::" + data); | |
} | |
}; | |
client.addListener("connect", function() { | |
sys.puts("Remote connected."); | |
}); | |
client.addListener("data", function(data) { | |
if(debug > 4) sys.puts("Response from server: " + data); | |
protocol.parse(data); | |
}); | |
client.addListener("close", function(data) { | |
sys.puts("Disconnected from server"); | |
}); | |
// mandar cosas: | |
process.argv.splice(0, 2); // "node remote.js" => 2, "remote" => 1 | |
protocol.spawn(process.argv[0]); | |
process.argv.splice(0, 1); | |
protocol.args(sys.inspect(process.argv)); // pseudo-serialization. good enough. | |
process.openStdin().on('data', function(data) { protocol.stdin(data); }); | |
setTimeout(function() { process.exit(); }, 500); | |
//process.exit(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'), net = require('net'); | |
var streams = []; | |
var mqueue = []; | |
var debug = 3; | |
var i = 0; | |
function cleanStreams() { | |
if(debug > 4) sys.puts("streams.length: " + streams.length); | |
for(var x = 0; x < streams.length; x++) { | |
try { | |
if(!streams[x].connection.writable) | |
streams.splice(x, 1); | |
} catch(e) { | |
streams.splice(x, 1); | |
} | |
} | |
} | |
var protocol = { | |
__send: function(x, stream, string) { | |
__sent = false; | |
if(debug > 4) sys.puts("Sent to client: " + string); | |
try { | |
stream.write(string); | |
__sent = true; | |
} catch(e) { | |
stream.end(); | |
streams.splice(x, 1); | |
} | |
if(!__sent) { | |
mqueue.push(string); | |
if(debug > 4) sys.puts("Added to mqueue."); | |
} | |
}, | |
__broadcast: function(string) { | |
cleanStreams(); | |
streams.forEach(function(stream) { | |
protocol.__send(stream.i, stream.connection, string); | |
}); | |
if(!streams.length) { | |
mqueue.push(string); | |
if(debug > 4) sys.puts("Added to mqueue."); | |
} | |
}, | |
req_auth: function(i) { | |
this.__send(i, streams[i].connection, "req_auth"); | |
}, | |
parse: function(i, data) { | |
if(data.indexOf("auth::") == 0) { | |
if(debug > 4) sys.puts("Authentication received."); | |
if(true /* auth */) { | |
this.__send(i, streams[i].connection, "auth_ok"); | |
if(mqueue.length) { | |
mqueue.forEach(function(message) { | |
if(debug > 4) sys.puts("Sending queued message."); | |
protocol.__broadcast(message); | |
}); | |
mqueue = []; | |
} | |
} else { | |
this.__send(i, streams[i].connection, "auth_ko"); | |
} | |
} | |
} | |
}; | |
var server = net.createServer(function(stream) { | |
var loc_i = i; // ñapa | |
streams[i] = ({'connection': stream, 'authenticated': false, 'i': i}); | |
i++; | |
stream.setEncoding("utf-8"); | |
stream.addListener("connect", function() { | |
sys.puts("Client connected."); | |
protocol.req_auth(loc_i); | |
}); | |
stream.addListener("data", function(data) { | |
if(debug > 4) sys.puts("Response from client: " + data); | |
protocol.parse(loc_i, data); | |
}); | |
stream.addListener("end", function() { | |
sys.puts("Client disconnected."); | |
stream.end(); | |
streams.splice(loc_i, 1); | |
}); | |
}); | |
var proxy = net.createServer(function(stream) { | |
stream.setEncoding("utf-8"); | |
stream.addListener("data", function(data) { | |
protocol.__broadcast(data); | |
}); | |
}); | |
// server.listen(8000, "localhost"); | |
server.listen(8000, "0.0.0.0"); | |
proxy.listen(8001, "localhost"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment