Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Take the server address from the InetAddress, fixes incompatibility w…
Browse files Browse the repository at this point in the history
…ith ViaFabric's new address resolution
  • Loading branch information
Earthcomputer committed Aug 25, 2020
1 parent 71ad349 commit 84343e3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.client.gui.screen.DisconnectedScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.network.ServerInfo;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkState;
import net.minecraft.network.Packet;
Expand All @@ -28,26 +27,23 @@ public class ConnectionHandler {

private static final Logger LOGGER = LogManager.getLogger("multiconnect");

public static boolean preConnect() throws UnknownHostException {
public static boolean preConnect(String ip, int port) throws UnknownHostException {
String address;
ServerInfo serverInfo = MinecraftClient.getInstance().getCurrentServerEntry();
if (serverInfo != null) {
address = serverInfo.address;
} else if (ConnectionInfo.port == 25565) {
address = ConnectionInfo.ip;
if (port == 25565) {
address = ip;
} else {
address = ConnectionInfo.ip + ":" + ConnectionInfo.port;
address = ip + ":" + port;
}

// Hypixel has their own closed-source connection proxy and closed-source anti-cheat.
// Users were getting banned for odd reasons. Their maps are designed to have fair play between clients on any
// version, so we force the current protocol version here to disable any kind of bridge, in the hope that users
// don't get banned because they are using multiconnect.
String ip = normalizeAddress(address).split(":")[0].toLowerCase(Locale.ROOT);
if (ip.endsWith(".")) {
ip = ip.substring(0, ip.length() - 1);
String testIp = normalizeAddress(address).split(":")[0].toLowerCase(Locale.ROOT);
if (testIp.endsWith(".")) {
testIp = testIp.substring(0, testIp.length() - 1);
}
if (ip.equals("hypixel.net") || ip.endsWith(".hypixel.net")) {
if (testIp.equals("hypixel.net") || testIp.endsWith(".hypixel.net")) {
if (SharedConstants.getGameVersion().isStable()) {
ConnectionInfo.protocolVersion = SharedConstants.getGameVersion().getProtocolVersion();
} else {
Expand All @@ -70,12 +66,12 @@ public static boolean preConnect() throws UnknownHostException {
return true;
IConnectScreen connectScreen = (IConnectScreen) screen;

ClientConnection connection = ClientConnection.connect(InetAddress.getByName(ConnectionInfo.ip), ConnectionInfo.port, false);
ClientConnection connection = ClientConnection.connect(InetAddress.getByName(ip), port, false);
connectScreen.multiconnect_setVersionRequestConnection(connection);
GetProtocolPacketListener listener = new GetProtocolPacketListener(connection);
connection.setPacketListener(listener);

HandshakeC2SPacket handshake = new HandshakeC2SPacket(ConnectionInfo.ip, ConnectionInfo.port, NetworkState.STATUS);
HandshakeC2SPacket handshake = new HandshakeC2SPacket(ip, port, NetworkState.STATUS);
//noinspection ConstantConditions
((HandshakePacketAccessor) handshake).setProtocolVersion(-1);
connection.send(handshake);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

public class ConnectionInfo {

public static String ip;
public static int port = -1;
public static int protocolVersion = SharedConstants.getGameVersion().getProtocolVersion();
public static AbstractProtocol protocol = ProtocolRegistry.latest();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.earthcomputer.multiconnect.mixin.connect;

import net.earthcomputer.multiconnect.impl.ConnectionInfo;
import net.earthcomputer.multiconnect.connect.IConnectScreen;
import net.minecraft.client.gui.screen.ConnectScreen;
import net.minecraft.client.gui.screen.Screen;
Expand All @@ -17,13 +16,7 @@
@Mixin(ConnectScreen.class)
public abstract class MixinConnectScreen implements IConnectScreen {

@Unique private AtomicReference<ClientConnection> versionRequestConnection = new AtomicReference<>();

@Inject(method = "connect", at = @At("HEAD"))
public void onConnect(String ip, int port, CallbackInfo ci) {
ConnectionInfo.ip = ip;
ConnectionInfo.port = port;
}
@Unique private final AtomicReference<ClientConnection> versionRequestConnection = new AtomicReference<>();

@Accessor
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
import net.minecraft.network.ClientConnection;
import net.minecraft.network.Packet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SuppressWarnings("UnresolvedMixinReference")
@Mixin(targets = "net.minecraft.client.gui.screen.ConnectScreen$1")
public class MixinConnectScreen1 {

@Inject(method = "run()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetAddress;IZ)Lnet/minecraft/network/ClientConnection;"), cancellable = true)
public void beforeConnect(CallbackInfo ci) throws UnknownHostException {
if (!ConnectionHandler.preConnect()) {
@SuppressWarnings("ShadowTarget")
@Shadow
private int field_2415; // port

@Inject(method = "run()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetAddress;IZ)Lnet/minecraft/network/ClientConnection;"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
public void beforeConnect(CallbackInfo ci, InetAddress address) throws UnknownHostException {
if (!ConnectionHandler.preConnect(address.getHostName(), field_2415)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public abstract class MixinMinecraftClient {

@Inject(method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;)V", at = @At("RETURN"))
public void onDisconnect(Screen screen, CallbackInfo ci) {
ConnectionInfo.ip = null;
ConnectionInfo.port = -1;
ConnectionInfo.protocolVersion = SharedConstants.getGameVersion().getProtocolVersion();
ConnectionInfo.protocol = ProtocolRegistry.get(ConnectionInfo.protocolVersion);
ConnectionInfo.protocol.setup(false);
Expand Down

0 comments on commit 84343e3

Please sign in to comment.