/net/minecraft/server/NetLoginHandler.java
https://bitbucket.org/agaricusb/mc-dev · Java · 195 lines · 160 code · 35 blank · 0 comment · 31 complexity · 5bf1b58208211553d71fda5ea1f2321f MD5 · raw file
- package net.minecraft.server;
- import java.io.Serializable;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Random;
- import java.util.logging.Logger;
- import javax.crypto.SecretKey;
- public class NetLoginHandler extends NetHandler {
- private byte[] d;
- public static Logger logger = Logger.getLogger("Minecraft");
- private static Random random = new Random();
- public NetworkManager networkManager;
- public boolean c = false;
- private MinecraftServer server;
- private int g = 0;
- private String h = null;
- private volatile boolean i = false;
- private String loginKey = "";
- private SecretKey k = null;
- public NetLoginHandler(MinecraftServer minecraftserver, Socket socket, String s) {
- this.server = minecraftserver;
- this.networkManager = new NetworkManager(socket, s, this, minecraftserver.F().getPrivate());
- this.networkManager.e = 0;
- }
- public void c() {
- if (this.i) {
- this.d();
- }
- if (this.g++ == 600) {
- this.disconnect("Took too long to log in");
- } else {
- this.networkManager.b();
- }
- }
- public void disconnect(String s) {
- try {
- logger.info("Disconnecting " + this.getName() + ": " + s);
- this.networkManager.queue(new Packet255KickDisconnect(s));
- this.networkManager.d();
- this.c = true;
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- public void a(Packet2Handshake packet2handshake) {
- this.h = packet2handshake.f();
- if (!this.h.equals(StripColor.a(this.h))) {
- this.disconnect("Invalid username!");
- } else {
- PublicKey publickey = this.server.F().getPublic();
- if (packet2handshake.d() != 49) {
- if (packet2handshake.d() > 49) {
- this.disconnect("Outdated server!");
- } else {
- this.disconnect("Outdated client!");
- }
- } else {
- this.loginKey = this.server.getOnlineMode() ? Long.toString(random.nextLong(), 16) : "-";
- this.d = new byte[4];
- random.nextBytes(this.d);
- this.networkManager.queue(new Packet253KeyRequest(this.loginKey, publickey, this.d));
- }
- }
- }
- public void a(Packet252KeyResponse packet252keyresponse) {
- PrivateKey privatekey = this.server.F().getPrivate();
- this.k = packet252keyresponse.a(privatekey);
- if (!Arrays.equals(this.d, packet252keyresponse.b(privatekey))) {
- this.disconnect("Invalid client reply");
- }
- this.networkManager.queue(new Packet252KeyResponse());
- }
- public void a(Packet205ClientCommand packet205clientcommand) {
- if (packet205clientcommand.a == 0) {
- if (this.server.getOnlineMode()) {
- (new ThreadLoginVerifier(this)).start();
- } else {
- this.i = true;
- }
- }
- }
- public void a(Packet1Login packet1login) {}
- public void d() {
- String s = this.server.getServerConfigurationManager().attemptLogin(this.networkManager.getSocketAddress(), this.h);
- if (s != null) {
- this.disconnect(s);
- } else {
- EntityPlayer entityplayer = this.server.getServerConfigurationManager().processLogin(this.h);
- if (entityplayer != null) {
- this.server.getServerConfigurationManager().a((INetworkManager) this.networkManager, entityplayer);
- }
- }
- this.c = true;
- }
- public void a(String s, Object[] aobject) {
- logger.info(this.getName() + " lost connection");
- this.c = true;
- }
- public void a(Packet254GetInfo packet254getinfo) {
- try {
- ServerConfigurationManagerAbstract serverconfigurationmanagerabstract = this.server.getServerConfigurationManager();
- String s = null;
- if (packet254getinfo.a == 1) {
- List list = Arrays.asList(new Serializable[] { Integer.valueOf(1), Integer.valueOf(49), this.server.getVersion(), this.server.getMotd(), Integer.valueOf(serverconfigurationmanagerabstract.getPlayerCount()), Integer.valueOf(serverconfigurationmanagerabstract.getMaxPlayers())});
- Object object;
- for (Iterator iterator = list.iterator(); iterator.hasNext(); s = s + object.toString().replaceAll(", "")) {
- object = iterator.next();
- if (s == null) {
- s = "\u00A7";
- } else {
- s = s + ";
- }
- }
- } else {
- s = this.server.getMotd() + "\u00A7" + serverconfigurationmanagerabstract.getPlayerCount() + "\u00A7" + serverconfigurationmanagerabstract.getMaxPlayers();
- }
- InetAddress inetaddress = null;
- if (this.networkManager.getSocket() != null) {
- inetaddress = this.networkManager.getSocket().getInetAddress();
- }
- this.networkManager.queue(new Packet255KickDisconnect(s));
- this.networkManager.d();
- if (inetaddress != null && this.server.ae() instanceof DedicatedServerConnection) {
- ((DedicatedServerConnection) this.server.ae()).a(inetaddress);
- }
- this.c = true;
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- public void onUnhandledPacket(Packet packet) {
- this.disconnect("Protocol error");
- }
- public String getName() {
- return this.h != null ? this.h + " [" + this.networkManager.getSocketAddress().toString() + "]" : this.networkManager.getSocketAddress().toString();
- }
- public boolean a() {
- return true;
- }
- static String a(NetLoginHandler netloginhandler) {
- return netloginhandler.loginKey;
- }
- static MinecraftServer b(NetLoginHandler netloginhandler) {
- return netloginhandler.server;
- }
- static SecretKey c(NetLoginHandler netloginhandler) {
- return netloginhandler.k;
- }
- static String d(NetLoginHandler netloginhandler) {
- return netloginhandler.h;
- }
- static boolean a(NetLoginHandler netloginhandler, boolean flag) {
- return netloginhandler.i = flag;
- }
- }