Sunday, 18 December 2022

Connect to a minecraft server via localhost

I tried making a proxy server for a minecraft server which you connect to, so you can modify the Minecraft protocol and / or other stuff.

I tried doing a TCP proxy server like this which redirects sockets to the given address:

var net = require("net");

process.on("uncaughtException", function(error) {
  console.error(error);
});

if (process.argv.length != 5) {
  console.log("usage: %s <localport> <remotehost> <remoteport>", process.argv[1]);
  process.exit();
}

var localport = process.argv[2];
var remotehost = process.argv[3];
var remoteport = process.argv[4];

var server = net.createServer(function (localsocket) {
  var remotesocket = new net.Socket();

  remotesocket.connect(remoteport, remotehost);

  localsocket.on('connect', function (data) {
    console.log(">>> connection #%d from %s:%d",
      server.connections,
      localsocket.remoteAddress,
      localsocket.remotePort
    );
  });

  localsocket.on('data', function (data) {
    console.log("%s:%d - writing data to remote",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    var flushed = remotesocket.write(data);
    if (!flushed) {
      console.log("  remote not flushed; pausing local");
      localsocket.pause();
    }
  });

  remotesocket.on('data', function(data) {
    console.log("%s:%d - writing data to local",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    var flushed = localsocket.write(data);
    if (!flushed) {
      console.log("  local not flushed; pausing remote");
      remotesocket.pause();
    }
  });

  localsocket.on('drain', function() {
    console.log("%s:%d - resuming remote",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    remotesocket.resume();
  });

  remotesocket.on('drain', function() {
    console.log("%s:%d - resuming local",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    localsocket.resume();
  });

  localsocket.on('close', function(had_error) {
    console.log("%s:%d - closing remote",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    remotesocket.end();
  });

  remotesocket.on('close', function(had_error) {
    console.log("%s:%d - closing local",
      localsocket.remoteAddress,
      localsocket.remotePort
    );
    localsocket.end();
  });

});

server.listen(localport);

console.log("redirecting connections from 127.0.0.1:%d to %s:%d", localport, remotehost, remoteport);

Unfortunately, this is redirecting to a wrong adress? However this method seems to be working fine on some cracked minecraft servers, but on hypixel for example it doesnt.

enter image description here Those are the logs that I receive when trying to enter 127.0.0.1:25565 (localhost:25565)

redirecting connections from 127.0.0.1:25565 to mc.hypixel.net:25565
::ffff:127.0.0.1:50233 - writing data to remote
::ffff:127.0.0.1:50233 - writing data to local
::ffff:127.0.0.1:50233 - writing data to remote
::ffff:127.0.0.1:50233 - writing data to local
::ffff:127.0.0.1:50233 - closing local
::ffff:127.0.0.1:50233 - closing remote

Furthermore, I've tried using https://github.com/darcros/node-minecraft-proxy, but I don't get any errors, I just time out when trying to connect.

I think that my client is trying to connect to the ip and it puts the handshake and the port into there. The server receives the packet and unpacks it. Since the origin is localhost and not the original server ip and port of the server, I'm getting a security error.

Now furthermorere, I have also tried using a mineflayer bot to connect to the server, which eventually leads my client to being timed out again before being able to do anything.

const mineflayer = require('mineflayer');
const net = require('net');

const server = net.createServer((socket) => {
  console.log('New connection from local server');
  const bot = mineflayer.createBot({
    host: 'mc.hypixel.net',
    port: 25565,
  });

  // Handle incoming packets from the local server
  socket.on('data', (packet) => {
    console.log('Received packet from local server');
    bot._client.write(packet);
  });

  // Handle incoming packets from the target server
  bot.on('packet', (packet) => {
    console.log('Received packet from target server');
    server.send(packet, 0, packet.length, 25565, 'localhost');
  });
});
server.listen(25565);

Possibly something wrong with sending the packet that the local server receives with redirecting it to the target server, since when I tried logging all the packets, the bot does not receive packets, however the local server does receive a packet which begins with 0f 00 2f.

I'm not an expert to what this means, but could it be a verify packet?



from Connect to a minecraft server via localhost

No comments:

Post a Comment