| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.google.common.base.Charsets;
|
| 4 | import com.mojang.authlib.GameProfile;
|
| 5 | import com.mojang.authlib.exceptions.AuthenticationUnavailableException;
|
| 6 | import io.netty.channel.ChannelFuture;
|
| 7 | import io.netty.channel.ChannelFutureListener;
|
| 8 | import io.netty.util.concurrent.GenericFutureListener;
|
| 9 | import java.math.BigInteger;
|
| 10 | import java.security.PrivateKey;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.Random;
|
| 13 | import java.util.UUID;
|
| 14 | import java.util.concurrent.atomic.AtomicInteger;
|
| 15 | import javax.crypto.SecretKey;
|
| 16 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 17 | import net.minecraft.network.NetworkManager;
|
| 18 | import net.minecraft.network.login.INetHandlerLoginServer;
|
| 19 | import net.minecraft.network.login.client.C00PacketLoginStart;
|
| 20 | import net.minecraft.network.login.client.C01PacketEncryptionResponse;
|
| 21 | import net.minecraft.network.login.server.S00PacketDisconnect;
|
| 22 | import net.minecraft.network.login.server.S01PacketEncryptionRequest;
|
| 23 | import net.minecraft.network.login.server.S02PacketLoginSuccess;
|
| 24 | import net.minecraft.network.login.server.S03PacketEnableCompression;
|
| 25 | import net.minecraft.server.MinecraftServer;
|
| 26 | import net.minecraft.util.ChatComponentText;
|
| 27 | import net.minecraft.util.CryptManager;
|
| 28 | import net.minecraft.util.IChatComponent;
|
| 29 | import net.minecraft.util.ITickable;
|
| 30 | import org.apache.commons.lang3.Validate;
|
| 31 | import org.apache.logging.log4j.LogManager;
|
| 32 | import org.apache.logging.log4j.Logger;
|
| 33 |
|
| 34 | public class NetHandlerLoginServer implements INetHandlerLoginServer, ITickable
|
| 35 | {
|
| 36 | private static final AtomicInteger AUTHENTICATOR_THREAD_ID = new AtomicInteger(0);
|
| 37 | private static final Logger logger = LogManager.getLogger();
|
| 38 | private static final Random RANDOM = new Random();
|
| 39 | private final byte[] verifyToken = new byte[4];
|
| 40 | private final MinecraftServer server;
|
| 41 | public final NetworkManager networkManager;
|
| 42 | private NetHandlerLoginServer.LoginState currentLoginState = NetHandlerLoginServer.LoginState.HELLO;
|
| 43 |
|
| 44 | /** How long has player been trying to login into the server. */
|
| 45 | private int connectionTimer;
|
| 46 | private GameProfile loginGameProfile;
|
| 47 | private String serverId = "";
|
| 48 | private SecretKey secretKey;
|
| 49 | private EntityPlayerMP player;
|
| 50 |
|
| 51 | public NetHandlerLoginServer(MinecraftServer serverIn, NetworkManager networkManagerIn)
|
| 52 | {
|
| 53 | this.server = serverIn;
|
| 54 | this.networkManager = networkManagerIn;
|
| 55 | RANDOM.nextBytes(this.verifyToken);
|
| 56 | }
|
| 57 |
|
| 58 | /**
|
| 59 | * Like the old updateEntity(), except more generic.
|
| 60 | */
|
| 61 | public void update()
|
| 62 | {
|
| 63 | if (this.currentLoginState == NetHandlerLoginServer.LoginState.READY_TO_ACCEPT)
|
| 64 | {
|
| 65 | this.tryAcceptPlayer();
|
| 66 | }
|
| 67 | else if (this.currentLoginState == NetHandlerLoginServer.LoginState.DELAY_ACCEPT)
|
| 68 | {
|
| 69 | EntityPlayerMP entityplayermp = this.server.getConfigurationManager().getPlayerByUUID(this.loginGameProfile.getId());
|
| 70 |
|
| 71 | if (entityplayermp == null)
|
| 72 | {
|
| 73 | this.currentLoginState = NetHandlerLoginServer.LoginState.READY_TO_ACCEPT;
|
| 74 | this.server.getConfigurationManager().initializeConnectionToPlayer(this.networkManager, this.player);
|
| 75 | this.player = null;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | if (this.connectionTimer++ == 600)
|
| 80 | {
|
| 81 | this.closeConnection("Took too long to log in");
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public void closeConnection(String reason)
|
| 86 | {
|
| 87 | try
|
| 88 | {
|
| 89 | logger.info("Disconnecting " + this.getConnectionInfo() + ": " + reason);
|
| 90 | ChatComponentText chatcomponenttext = new ChatComponentText(reason);
|
| 91 | this.networkManager.sendPacket(new S00PacketDisconnect(chatcomponenttext));
|
| 92 | this.networkManager.closeChannel(chatcomponenttext);
|
| 93 | }
|
| 94 | catch (Exception exception)
|
| 95 | {
|
| 96 | logger.error((String)"Error whilst disconnecting player", (Throwable)exception);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public void tryAcceptPlayer()
|
| 101 | {
|
| 102 | if (!this.loginGameProfile.isComplete())
|
| 103 | {
|
| 104 | this.loginGameProfile = this.getOfflineProfile(this.loginGameProfile);
|
| 105 | }
|
| 106 |
|
| 107 | String s = this.server.getConfigurationManager().allowUserToConnect(this.networkManager.getRemoteAddress(), this.loginGameProfile);
|
| 108 |
|
| 109 | if (s != null)
|
| 110 | {
|
| 111 | this.closeConnection(s);
|
| 112 | }
|
| 113 | else
|
| 114 | {
|
| 115 | this.currentLoginState = NetHandlerLoginServer.LoginState.ACCEPTED;
|
| 116 |
|
| 117 | if (this.server.getNetworkCompressionTreshold() >= 0 && !this.networkManager.isLocalChannel())
|
| 118 | {
|
| 119 | this.networkManager.sendPacket(new S03PacketEnableCompression(this.server.getNetworkCompressionTreshold()), new ChannelFutureListener()
|
| 120 | {
|
| 121 | public void operationComplete(ChannelFuture p_operationComplete_1_) throws Exception
|
| 122 | {
|
| 123 | NetHandlerLoginServer.this.networkManager.setCompressionTreshold(NetHandlerLoginServer.this.server.getNetworkCompressionTreshold());
|
| 124 | }
|
| 125 | }, new GenericFutureListener[0]);
|
| 126 | }
|
| 127 |
|
| 128 | this.networkManager.sendPacket(new S02PacketLoginSuccess(this.loginGameProfile));
|
| 129 | EntityPlayerMP entityplayermp = this.server.getConfigurationManager().getPlayerByUUID(this.loginGameProfile.getId());
|
| 130 |
|
| 131 | if (entityplayermp != null)
|
| 132 | {
|
| 133 | this.currentLoginState = NetHandlerLoginServer.LoginState.DELAY_ACCEPT;
|
| 134 | this.player = this.server.getConfigurationManager().createPlayerForUser(this.loginGameProfile);
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | this.server.getConfigurationManager().initializeConnectionToPlayer(this.networkManager, this.server.getConfigurationManager().createPlayerForUser(this.loginGameProfile));
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | /**
|
| 144 | * Invoked when disconnecting, the parameter is a ChatComponent describing the reason for termination
|
| 145 | */
|
| 146 | public void onDisconnect(IChatComponent reason)
|
| 147 | {
|
| 148 | logger.info(this.getConnectionInfo() + " lost connection: " + reason.getUnformattedText());
|
| 149 | }
|
| 150 |
|
| 151 | public String getConnectionInfo()
|
| 152 | {
|
| 153 | return this.loginGameProfile != null ? this.loginGameProfile.toString() + " (" + this.networkManager.getRemoteAddress().toString() + ")" : String.valueOf((Object)this.networkManager.getRemoteAddress());
|
| 154 | }
|
| 155 |
|
| 156 | public void processLoginStart(C00PacketLoginStart packetIn)
|
| 157 | {
|
| 158 | Validate.validState(this.currentLoginState == NetHandlerLoginServer.LoginState.HELLO, "Unexpected hello packet", new Object[0]);
|
| 159 | this.loginGameProfile = packetIn.getProfile();
|
| 160 |
|
| 161 | if (this.server.isServerInOnlineMode() && !this.networkManager.isLocalChannel())
|
| 162 | {
|
| 163 | this.currentLoginState = NetHandlerLoginServer.LoginState.KEY;
|
| 164 | this.networkManager.sendPacket(new S01PacketEncryptionRequest(this.serverId, this.server.getKeyPair().getPublic(), this.verifyToken));
|
| 165 | }
|
| 166 | else
|
| 167 | {
|
| 168 | this.currentLoginState = NetHandlerLoginServer.LoginState.READY_TO_ACCEPT;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | public void processEncryptionResponse(C01PacketEncryptionResponse packetIn)
|
| 173 | {
|
| 174 | Validate.validState(this.currentLoginState == NetHandlerLoginServer.LoginState.KEY, "Unexpected key packet", new Object[0]);
|
| 175 | PrivateKey privatekey = this.server.getKeyPair().getPrivate();
|
| 176 |
|
| 177 | if (!Arrays.equals(this.verifyToken, packetIn.getVerifyToken(privatekey)))
|
| 178 | {
|
| 179 | throw new IllegalStateException("Invalid nonce!");
|
| 180 | }
|
| 181 | else
|
| 182 | {
|
| 183 | this.secretKey = packetIn.getSecretKey(privatekey);
|
| 184 | this.currentLoginState = NetHandlerLoginServer.LoginState.AUTHENTICATING;
|
| 185 | this.networkManager.enableEncryption(this.secretKey);
|
| 186 | (new Thread("User Authenticator #" + AUTHENTICATOR_THREAD_ID.incrementAndGet())
|
| 187 | {
|
| 188 | public void run()
|
| 189 | {
|
| 190 | GameProfile gameprofile = NetHandlerLoginServer.this.loginGameProfile;
|
| 191 |
|
| 192 | try
|
| 193 | {
|
| 194 | String s = (new BigInteger(CryptManager.getServerIdHash(NetHandlerLoginServer.this.serverId, NetHandlerLoginServer.this.server.getKeyPair().getPublic(), NetHandlerLoginServer.this.secretKey))).toString(16);
|
| 195 | NetHandlerLoginServer.this.loginGameProfile = NetHandlerLoginServer.this.server.getMinecraftSessionService().hasJoinedServer(new GameProfile((UUID)null, gameprofile.getName()), s);
|
| 196 |
|
| 197 | if (NetHandlerLoginServer.this.loginGameProfile != null)
|
| 198 | {
|
| 199 | NetHandlerLoginServer.logger.info("UUID of player " + NetHandlerLoginServer.this.loginGameProfile.getName() + " is " + NetHandlerLoginServer.this.loginGameProfile.getId());
|
| 200 | NetHandlerLoginServer.this.currentLoginState = NetHandlerLoginServer.LoginState.READY_TO_ACCEPT;
|
| 201 | }
|
| 202 | else if (NetHandlerLoginServer.this.server.isSinglePlayer())
|
| 203 | {
|
| 204 | NetHandlerLoginServer.logger.warn("Failed to verify username but will let them in anyway!");
|
| 205 | NetHandlerLoginServer.this.loginGameProfile = NetHandlerLoginServer.this.getOfflineProfile(gameprofile);
|
| 206 | NetHandlerLoginServer.this.currentLoginState = NetHandlerLoginServer.LoginState.READY_TO_ACCEPT;
|
| 207 | }
|
| 208 | else
|
| 209 | {
|
| 210 | NetHandlerLoginServer.this.closeConnection("Failed to verify username!");
|
| 211 | NetHandlerLoginServer.logger.error("Username \'" + NetHandlerLoginServer.this.loginGameProfile.getName() + "\' tried to join with an invalid session");
|
| 212 | }
|
| 213 | }
|
| 214 | catch (AuthenticationUnavailableException var3)
|
| 215 | {
|
| 216 | if (NetHandlerLoginServer.this.server.isSinglePlayer())
|
| 217 | {
|
| 218 | NetHandlerLoginServer.logger.warn("Authentication servers are down but will let them in anyway!");
|
| 219 | NetHandlerLoginServer.this.loginGameProfile = NetHandlerLoginServer.this.getOfflineProfile(gameprofile);
|
| 220 | NetHandlerLoginServer.this.currentLoginState = NetHandlerLoginServer.LoginState.READY_TO_ACCEPT;
|
| 221 | }
|
| 222 | else
|
| 223 | {
|
| 224 | NetHandlerLoginServer.this.closeConnection("Authentication servers are down. Please try again later, sorry!");
|
| 225 | NetHandlerLoginServer.logger.error("Couldn\'t verify username because servers are unavailable");
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 | }).start();
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | protected GameProfile getOfflineProfile(GameProfile original)
|
| 234 | {
|
| 235 | UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + original.getName()).getBytes(Charsets.UTF_8));
|
| 236 | return new GameProfile(uuid, original.getName());
|
| 237 | }
|
| 238 |
|
| 239 | static enum LoginState
|
| 240 | {
|
| 241 | HELLO,
|
| 242 | KEY,
|
| 243 | AUTHENTICATING,
|
| 244 | READY_TO_ACCEPT,
|
| 245 | DELAY_ACCEPT,
|
| 246 | ACCEPTED;
|
| 247 | }
|
| 248 | }
|
| 249 |
|