| 1 | package net.minecraft.client.network;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import com.mojang.authlib.exceptions.AuthenticationException;
|
| 5 | import com.mojang.authlib.exceptions.AuthenticationUnavailableException;
|
| 6 | import com.mojang.authlib.exceptions.InvalidCredentialsException;
|
| 7 | import com.mojang.authlib.minecraft.MinecraftSessionService;
|
| 8 | import io.netty.util.concurrent.Future;
|
| 9 | import io.netty.util.concurrent.GenericFutureListener;
|
| 10 | import java.math.BigInteger;
|
| 11 | import java.security.PublicKey;
|
| 12 | import javax.crypto.SecretKey;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.GuiDisconnected;
|
| 15 | import net.minecraft.client.gui.GuiScreen;
|
| 16 | import net.minecraft.network.EnumConnectionState;
|
| 17 | import net.minecraft.network.NetworkManager;
|
| 18 | import net.minecraft.network.login.INetHandlerLoginClient;
|
| 19 | import net.minecraft.network.login.client.C01PacketEncryptionResponse;
|
| 20 | import net.minecraft.network.login.server.S00PacketDisconnect;
|
| 21 | import net.minecraft.network.login.server.S01PacketEncryptionRequest;
|
| 22 | import net.minecraft.network.login.server.S02PacketLoginSuccess;
|
| 23 | import net.minecraft.network.login.server.S03PacketEnableCompression;
|
| 24 | import net.minecraft.util.ChatComponentTranslation;
|
| 25 | import net.minecraft.util.CryptManager;
|
| 26 | import net.minecraft.util.IChatComponent;
|
| 27 | import org.apache.logging.log4j.LogManager;
|
| 28 | import org.apache.logging.log4j.Logger;
|
| 29 |
|
| 30 | public class NetHandlerLoginClient implements INetHandlerLoginClient
|
| 31 | {
|
| 32 | private static final Logger logger = LogManager.getLogger();
|
| 33 | private final Minecraft mc;
|
| 34 | private final GuiScreen previousGuiScreen;
|
| 35 | private final NetworkManager networkManager;
|
| 36 | private GameProfile gameProfile;
|
| 37 |
|
| 38 | public NetHandlerLoginClient(NetworkManager networkManagerIn, Minecraft mcIn, GuiScreen p_i45059_3_)
|
| 39 | {
|
| 40 | this.networkManager = networkManagerIn;
|
| 41 | this.mc = mcIn;
|
| 42 | this.previousGuiScreen = p_i45059_3_;
|
| 43 | }
|
| 44 |
|
| 45 | public void handleEncryptionRequest(S01PacketEncryptionRequest packetIn)
|
| 46 | {
|
| 47 | final SecretKey secretkey = CryptManager.createNewSharedKey();
|
| 48 | String s = packetIn.getServerId();
|
| 49 | PublicKey publickey = packetIn.getPublicKey();
|
| 50 | String s1 = (new BigInteger(CryptManager.getServerIdHash(s, publickey, secretkey))).toString(16);
|
| 51 |
|
| 52 | if (this.mc.getCurrentServerData() != null && this.mc.getCurrentServerData().isOnLAN())
|
| 53 | {
|
| 54 | try
|
| 55 | {
|
| 56 | this.getSessionService().joinServer(this.mc.getSession().getProfile(), this.mc.getSession().getToken(), s1);
|
| 57 | }
|
| 58 | catch (AuthenticationException var10)
|
| 59 | {
|
| 60 | logger.warn("Couldn\'t connect to auth servers but will continue to join LAN");
|
| 61 | }
|
| 62 | }
|
| 63 | else
|
| 64 | {
|
| 65 | try
|
| 66 | {
|
| 67 | this.getSessionService().joinServer(this.mc.getSession().getProfile(), this.mc.getSession().getToken(), s1);
|
| 68 | }
|
| 69 | catch (AuthenticationUnavailableException var7)
|
| 70 | {
|
| 71 | this.networkManager.closeChannel(new ChatComponentTranslation("disconnect.loginFailedInfo", new Object[] {new ChatComponentTranslation("disconnect.loginFailedInfo.serversUnavailable", new Object[0])}));
|
| 72 | return;
|
| 73 | }
|
| 74 | catch (InvalidCredentialsException var8)
|
| 75 | {
|
| 76 | this.networkManager.closeChannel(new ChatComponentTranslation("disconnect.loginFailedInfo", new Object[] {new ChatComponentTranslation("disconnect.loginFailedInfo.invalidSession", new Object[0])}));
|
| 77 | return;
|
| 78 | }
|
| 79 | catch (AuthenticationException authenticationexception)
|
| 80 | {
|
| 81 | this.networkManager.closeChannel(new ChatComponentTranslation("disconnect.loginFailedInfo", new Object[] {authenticationexception.getMessage()}));
|
| 82 | return;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | this.networkManager.sendPacket(new C01PacketEncryptionResponse(secretkey, publickey, packetIn.getVerifyToken()), new GenericFutureListener < Future <? super Void >> ()
|
| 87 | {
|
| 88 | public void operationComplete(Future <? super Void > p_operationComplete_1_) throws Exception
|
| 89 | {
|
| 90 | NetHandlerLoginClient.this.networkManager.enableEncryption(secretkey);
|
| 91 | }
|
| 92 | }, new GenericFutureListener[0]);
|
| 93 | }
|
| 94 |
|
| 95 | private MinecraftSessionService getSessionService()
|
| 96 | {
|
| 97 | return this.mc.getSessionService();
|
| 98 | }
|
| 99 |
|
| 100 | public void handleLoginSuccess(S02PacketLoginSuccess packetIn)
|
| 101 | {
|
| 102 | this.gameProfile = packetIn.getProfile();
|
| 103 | this.networkManager.setConnectionState(EnumConnectionState.PLAY);
|
| 104 | this.networkManager.setNetHandler(new NetHandlerPlayClient(this.mc, this.previousGuiScreen, this.networkManager, this.gameProfile));
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Invoked when disconnecting, the parameter is a ChatComponent describing the reason for termination
|
| 109 | */
|
| 110 | public void onDisconnect(IChatComponent reason)
|
| 111 | {
|
| 112 | this.mc.displayGuiScreen(new GuiDisconnected(this.previousGuiScreen, "connect.failed", reason));
|
| 113 | }
|
| 114 |
|
| 115 | public void handleDisconnect(S00PacketDisconnect packetIn)
|
| 116 | {
|
| 117 | this.networkManager.closeChannel(packetIn.func_149603_c());
|
| 118 | }
|
| 119 |
|
| 120 | public void handleEnableCompression(S03PacketEnableCompression packetIn)
|
| 121 | {
|
| 122 | if (!this.networkManager.isLocalChannel())
|
| 123 | {
|
| 124 | this.networkManager.setCompressionTreshold(packetIn.getCompressionTreshold());
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|