Skip to content

Instantly share code, notes, and snippets.

@roblabla
Created October 18, 2015 11:17
Show Gist options
  • Save roblabla/2e7a460722ea8c16b01d to your computer and use it in GitHub Desktop.
Save roblabla/2e7a460722ea8c16b01d to your computer and use it in GitHub Desktop.
A raw proxy for NMP
var mc = require('minecraft-protocol') // import nmp
, states = mc.states // import states as var from nmp
, server = new mc.Server();
server.listen(25565);
server.on('connection', function(client) {
var addr = client.socket.remoteAddress; // get the ip
console.log('Incoming connection', '(' + addr + ')'); // print ip on connection
var endedClient = false;
var endedTargetClient = false;
client.on('end', function() { // end event when client disconnects
endedClient = true;
console.log('Connection closed by client', '(' + addr + ')');
if(!endedTargetClient) {
targetClient.end("End");
}
});
client.on('error', function() { // error event when something goes wrong
endedClient = true;
console.log('Connection error by client', '(' + addr + ')');
if(!endedTargetClient) {
targetClient.end("Error");
}
});
client.on('set_protocol', function(packet) {
client.state = states.LOGIN;
});
var targetClient = new mc.Client();
targetClient.connect(25566, 'localhost');
client.on('raw', function(buffer, state) { // raw event forwarding raw data to the minecraft server
targetClient.writeRaw(buffer);
});
targetClient.on('raw', function(buffer, state) { // raw event forwarding raw data to client from minecraft server
if (!endedClient) {
client.writeRaw(buffer);
}
});
targetClient.on('end', function() { // When server stops
endedTargetClient = true;
console.log('Connection closed by server', '(' + addr + ')');
if(!endedClient) {
client.end("End");
}
});
targetClient.on('error', function() { // When theres a server error
endedTargetClient = true;
console.log('Connection error by server', '(' + addr + ')');
if(!endedClient) {
client.end("Error");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment