Skip to content

Instantly share code, notes, and snippets.

@roblabla
Created March 3, 2015 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblabla/384e5795dee06ddbd6a3 to your computer and use it in GitHub Desktop.
Save roblabla/384e5795dee06ddbd6a3 to your computer and use it in GitHub Desktop.
var mc = require('../');
var states = mc.protocol.states;
function print_help() {
console.log("usage: node proxy.js <target_srv> <user> [<password>]");
}
if (process.argv.length < 4) {
console.log("Too few arguments!");
print_help();
process.exit(1);
}
process.argv.forEach(function(val, index, array) {
if (val == "-h") {
print_help();
process.exit(0);
}
});
var host = process.argv[2];
var port = 25565;
var user = process.argv[3];
var passwd = process.argv[4];
if (host.indexOf(':') != -1) {
port = host.substring(host.indexOf(':')+1);
host = host.substring(0, host.indexOf(':'));
}
var srv = mc.createServer({
'online-mode': false,
port: 25566
});
srv.on('login', function (client) {
var addr = client.socket.remoteAddress;
console.log('Incoming connection', '('+addr+')');
var endedClient = false;
var endedTargetClient = false;
client.on('end', function() {
endedClient = true;
console.log('Connection closed by client', '('+addr+')');
if (!endedTargetClient)
targetClient.end("End");
});
client.on('error', function() {
endedClient = true;
console.log('Connection error by client', '('+addr+')');
if (!endedTargetClient)
targetClient.end("Error");
});
var targetClient = mc.createClient({
host: host,
port: port,
username: user,
password: passwd,
'online-mode': passwd != null ? true : false
});
var handledClientboundPacket = [/*0x04, 0x2f, 0x30*/];
var handledServerboundPacket = [0x02];
client.on('chat', function(packet) {
if (packet.message == "/dostuff")
console.log("DOSTUFF");
else
targetClient.write(packet);
})
targetClient.on('raw', function(buffer, state) {
if (client.state != states.PLAY || state != states.PLAY)
return;
var packetId = mc.protocol.types.varint[0](buffer, 0);
if (handledClientboundPackets.indexOf(packetId.value) !== -1)
{
console.log("client<-server: raw packet");
console.log(packetData);
if (!endedClient)
client.writeRaw(buffer);
}
});
client.on('raw', function(buffer, state) {
if (state != states.PLAY || targetClient.state != states.PLAY)
return;
var packetId = mc.protocol.types.varint[0](buffer, 0);
if (handledServerboundPackets.indexOf(packetId.value) !== -1)
{
console.log("client<-server: raw packet");
console.log(packetData);
if (!endedClient)
client.writeRaw(buffer);
}
});
targetClient.on('end', function() {
endedTargetClient = true;
console.log('Connection closed by server', '('+addr+')');
if (!endedClient)
client.end("End");
});
targetClient.on('error', function() {
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