/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

  1. package net.minecraft.server;
  2. import java.io.Serializable;
  3. import java.net.InetAddress;
  4. import java.net.Socket;
  5. import java.security.PrivateKey;
  6. import java.security.PublicKey;
  7. import java.util.Arrays;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Random;
  11. import java.util.logging.Logger;
  12. import javax.crypto.SecretKey;
  13. public class NetLoginHandler extends NetHandler {
  14. private byte[] d;
  15. public static Logger logger = Logger.getLogger("Minecraft");
  16. private static Random random = new Random();
  17. public NetworkManager networkManager;
  18. public boolean c = false;
  19. private MinecraftServer server;
  20. private int g = 0;
  21. private String h = null;
  22. private volatile boolean i = false;
  23. private String loginKey = "";
  24. private SecretKey k = null;
  25. public NetLoginHandler(MinecraftServer minecraftserver, Socket socket, String s) {
  26. this.server = minecraftserver;
  27. this.networkManager = new NetworkManager(socket, s, this, minecraftserver.F().getPrivate());
  28. this.networkManager.e = 0;
  29. }
  30. public void c() {
  31. if (this.i) {
  32. this.d();
  33. }
  34. if (this.g++ == 600) {
  35. this.disconnect("Took too long to log in");
  36. } else {
  37. this.networkManager.b();
  38. }
  39. }
  40. public void disconnect(String s) {
  41. try {
  42. logger.info("Disconnecting " + this.getName() + ": " + s);
  43. this.networkManager.queue(new Packet255KickDisconnect(s));
  44. this.networkManager.d();
  45. this.c = true;
  46. } catch (Exception exception) {
  47. exception.printStackTrace();
  48. }
  49. }
  50. public void a(Packet2Handshake packet2handshake) {
  51. this.h = packet2handshake.f();
  52. if (!this.h.equals(StripColor.a(this.h))) {
  53. this.disconnect("Invalid username!");
  54. } else {
  55. PublicKey publickey = this.server.F().getPublic();
  56. if (packet2handshake.d() != 49) {
  57. if (packet2handshake.d() > 49) {
  58. this.disconnect("Outdated server!");
  59. } else {
  60. this.disconnect("Outdated client!");
  61. }
  62. } else {
  63. this.loginKey = this.server.getOnlineMode() ? Long.toString(random.nextLong(), 16) : "-";
  64. this.d = new byte[4];
  65. random.nextBytes(this.d);
  66. this.networkManager.queue(new Packet253KeyRequest(this.loginKey, publickey, this.d));
  67. }
  68. }
  69. }
  70. public void a(Packet252KeyResponse packet252keyresponse) {
  71. PrivateKey privatekey = this.server.F().getPrivate();
  72. this.k = packet252keyresponse.a(privatekey);
  73. if (!Arrays.equals(this.d, packet252keyresponse.b(privatekey))) {
  74. this.disconnect("Invalid client reply");
  75. }
  76. this.networkManager.queue(new Packet252KeyResponse());
  77. }
  78. public void a(Packet205ClientCommand packet205clientcommand) {
  79. if (packet205clientcommand.a == 0) {
  80. if (this.server.getOnlineMode()) {
  81. (new ThreadLoginVerifier(this)).start();
  82. } else {
  83. this.i = true;
  84. }
  85. }
  86. }
  87. public void a(Packet1Login packet1login) {}
  88. public void d() {
  89. String s = this.server.getServerConfigurationManager().attemptLogin(this.networkManager.getSocketAddress(), this.h);
  90. if (s != null) {
  91. this.disconnect(s);
  92. } else {
  93. EntityPlayer entityplayer = this.server.getServerConfigurationManager().processLogin(this.h);
  94. if (entityplayer != null) {
  95. this.server.getServerConfigurationManager().a((INetworkManager) this.networkManager, entityplayer);
  96. }
  97. }
  98. this.c = true;
  99. }
  100. public void a(String s, Object[] aobject) {
  101. logger.info(this.getName() + " lost connection");
  102. this.c = true;
  103. }
  104. public void a(Packet254GetInfo packet254getinfo) {
  105. try {
  106. ServerConfigurationManagerAbstract serverconfigurationmanagerabstract = this.server.getServerConfigurationManager();
  107. String s = null;
  108. if (packet254getinfo.a == 1) {
  109. 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())});
  110. Object object;
  111. for (Iterator iterator = list.iterator(); iterator.hasNext(); s = s + object.toString().replaceAll(", "")) {
  112. object = iterator.next();
  113. if (s == null) {
  114. s = "\u00A7";
  115. } else {
  116. s = s + ";
  117. }
  118. }
  119. } else {
  120. s = this.server.getMotd() + "\u00A7" + serverconfigurationmanagerabstract.getPlayerCount() + "\u00A7" + serverconfigurationmanagerabstract.getMaxPlayers();
  121. }
  122. InetAddress inetaddress = null;
  123. if (this.networkManager.getSocket() != null) {
  124. inetaddress = this.networkManager.getSocket().getInetAddress();
  125. }
  126. this.networkManager.queue(new Packet255KickDisconnect(s));
  127. this.networkManager.d();
  128. if (inetaddress != null && this.server.ae() instanceof DedicatedServerConnection) {
  129. ((DedicatedServerConnection) this.server.ae()).a(inetaddress);
  130. }
  131. this.c = true;
  132. } catch (Exception exception) {
  133. exception.printStackTrace();
  134. }
  135. }
  136. public void onUnhandledPacket(Packet packet) {
  137. this.disconnect("Protocol error");
  138. }
  139. public String getName() {
  140. return this.h != null ? this.h + " [" + this.networkManager.getSocketAddress().toString() + "]" : this.networkManager.getSocketAddress().toString();
  141. }
  142. public boolean a() {
  143. return true;
  144. }
  145. static String a(NetLoginHandler netloginhandler) {
  146. return netloginhandler.loginKey;
  147. }
  148. static MinecraftServer b(NetLoginHandler netloginhandler) {
  149. return netloginhandler.server;
  150. }
  151. static SecretKey c(NetLoginHandler netloginhandler) {
  152. return netloginhandler.k;
  153. }
  154. static String d(NetLoginHandler netloginhandler) {
  155. return netloginhandler.h;
  156. }
  157. static boolean a(NetLoginHandler netloginhandler, boolean flag) {
  158. return netloginhandler.i = flag;
  159. }
  160. }