2011-02-05 19:15:04 +01:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
import java.net.InetAddress;
|
2011-02-05 19:15:04 +01:00
|
|
|
import java.net.Socket;
|
2012-07-29 09:33:13 +02:00
|
|
|
import java.security.PrivateKey;
|
|
|
|
import java.security.PublicKey;
|
|
|
|
import java.util.Arrays;
|
2011-02-05 19:15:04 +01:00
|
|
|
import java.util.Random;
|
|
|
|
import java.util.logging.Logger;
|
2012-07-29 09:33:13 +02:00
|
|
|
import javax.crypto.SecretKey;
|
2011-02-05 19:15:04 +01:00
|
|
|
|
|
|
|
public class NetLoginHandler extends NetHandler {
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
private byte[] d;
|
2012-01-14 21:03:48 +01:00
|
|
|
public static Logger logger = Logger.getLogger("Minecraft");
|
|
|
|
private static Random random = new Random();
|
2011-04-20 19:05:14 +02:00
|
|
|
public NetworkManager networkManager;
|
2011-02-05 19:15:04 +01:00
|
|
|
public boolean c = false;
|
2011-04-20 19:05:14 +02:00
|
|
|
private MinecraftServer server;
|
2012-07-29 09:33:13 +02:00
|
|
|
private int g = 0;
|
|
|
|
private String h = null;
|
|
|
|
private volatile boolean i = false;
|
2012-01-14 21:03:48 +01:00
|
|
|
private String loginKey = Long.toString(random.nextLong(), 16); // CraftBukkit - Security fix
|
2012-07-29 09:33:13 +02:00
|
|
|
private SecretKey k = null;
|
2012-03-03 19:38:36 +01:00
|
|
|
public String hostname = ""; // CraftBukkit - add field
|
2012-09-10 06:19:28 +02:00
|
|
|
private boolean login = false; // CraftBukkit
|
2011-02-05 19:15:04 +01:00
|
|
|
|
2012-08-08 13:54:28 +02:00
|
|
|
public NetLoginHandler(MinecraftServer minecraftserver, Socket socket, String s) throws java.io.IOException { // CraftBukkit - throws IOException
|
2011-04-20 19:05:14 +02:00
|
|
|
this.server = minecraftserver;
|
2012-07-29 09:33:13 +02:00
|
|
|
this.networkManager = new NetworkManager(socket, s, this, minecraftserver.E().getPrivate());
|
|
|
|
this.networkManager.e = 0;
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
2011-04-20 19:05:14 +02:00
|
|
|
|
2011-04-16 12:08:13 +02:00
|
|
|
// CraftBukkit start
|
|
|
|
public Socket getSocket() {
|
2012-07-29 09:33:13 +02:00
|
|
|
return this.networkManager.getSocket();
|
2011-04-16 12:08:13 +02:00
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2011-02-05 19:15:04 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void c() {
|
|
|
|
if (this.i) {
|
|
|
|
this.d();
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (this.g++ == 600) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.disconnect("Took too long to log in");
|
2011-02-05 19:15:04 +01:00
|
|
|
} else {
|
2011-05-26 14:48:22 +02:00
|
|
|
this.networkManager.b();
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void disconnect(String s) {
|
2011-02-05 19:15:04 +01:00
|
|
|
try {
|
2012-01-14 21:03:48 +01:00
|
|
|
logger.info("Disconnecting " + this.getName() + ": " + s);
|
2011-06-27 00:25:01 +02:00
|
|
|
this.networkManager.queue(new Packet255KickDisconnect(s));
|
2011-05-26 14:48:22 +02:00
|
|
|
this.networkManager.d();
|
2011-02-05 19:15:04 +01:00
|
|
|
this.c = true;
|
|
|
|
} catch (Exception exception) {
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(Packet2Handshake packet2handshake) {
|
2012-03-03 19:38:36 +01:00
|
|
|
// CraftBukkit start
|
2012-08-08 13:58:08 +02:00
|
|
|
this.hostname = packet2handshake.c == null ? "" : packet2handshake.c + ':' + packet2handshake.d;
|
2012-03-03 19:38:36 +01:00
|
|
|
// CraftBukkit end
|
2012-07-29 09:33:13 +02:00
|
|
|
this.h = packet2handshake.f();
|
|
|
|
if (!this.h.equals(StripColor.a(this.h))) {
|
|
|
|
this.disconnect("Invalid username!");
|
2011-02-05 19:15:04 +01:00
|
|
|
} else {
|
2012-07-29 09:33:13 +02:00
|
|
|
PublicKey publickey = this.server.E().getPublic();
|
2011-02-05 19:15:04 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (packet2handshake.d() != 39) {
|
|
|
|
if (packet2handshake.d() > 39) {
|
|
|
|
this.disconnect("Outdated server!");
|
|
|
|
} else {
|
|
|
|
this.disconnect("Outdated client!");
|
2012-01-20 17:03:15 +01:00
|
|
|
}
|
2011-02-05 19:15:04 +01:00
|
|
|
} else {
|
2012-07-29 09:33:13 +02:00
|
|
|
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));
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void a(Packet252KeyResponse packet252keyresponse) {
|
|
|
|
PrivateKey privatekey = this.server.E().getPrivate();
|
2011-02-05 19:15:04 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
this.k = packet252keyresponse.a(privatekey);
|
|
|
|
if (!Arrays.equals(this.d, packet252keyresponse.b(privatekey))) {
|
|
|
|
this.disconnect("Invalid client reply");
|
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
this.networkManager.queue(new Packet252KeyResponse());
|
|
|
|
}
|
2011-02-05 19:15:04 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void a(Packet205ClientCommand packet205clientcommand) {
|
|
|
|
if (packet205clientcommand.a == 0) {
|
|
|
|
if (this.server.getOnlineMode()) {
|
2012-09-10 06:19:28 +02:00
|
|
|
// CraftBukkit start
|
|
|
|
if (this.login) {
|
|
|
|
this.disconnect("Duplicate login");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.login = true;
|
|
|
|
// CraftBukkit end
|
2012-07-29 09:33:13 +02:00
|
|
|
(new ThreadLoginVerifier(this, server.server)).start(); // CraftBukkit - add CraftServer
|
|
|
|
} else {
|
|
|
|
this.i = true;
|
2011-09-16 09:52:36 +02:00
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void a(Packet1Login packet1login) {}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void d() {
|
|
|
|
// CraftBukkit start
|
|
|
|
EntityPlayer s = this.server.getServerConfigurationManager().attemptLogin(this, this.h, this.hostname);
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (s == null) {
|
|
|
|
return;
|
|
|
|
// CraftBukkit end
|
|
|
|
} else {
|
|
|
|
EntityPlayer entityplayer = this.server.getServerConfigurationManager().processLogin(s); // CraftBukkit - this.h -> s
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (entityplayer != null) {
|
|
|
|
this.server.getServerConfigurationManager().a((INetworkManager) this.networkManager, entityplayer);
|
|
|
|
}
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.c = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(String s, Object[] aobject) {
|
2012-01-14 21:03:48 +01:00
|
|
|
logger.info(this.getName() + " lost connection");
|
2011-02-05 19:15:04 +01:00
|
|
|
this.c = true;
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
public void a(Packet254GetInfo packet254getinfo) {
|
2012-01-14 21:03:48 +01:00
|
|
|
if (this.networkManager.getSocket() == null) return; // CraftBukkit - fix NPE when a client queries a server that is unable to handle it.
|
2011-09-15 02:23:52 +02:00
|
|
|
try {
|
2011-09-27 00:32:05 +02:00
|
|
|
// CraftBukkit start
|
2012-07-29 09:33:13 +02:00
|
|
|
org.bukkit.event.server.ServerListPingEvent pingEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callServerListPingEvent(this.server.server, getSocket().getInetAddress(), this.server.getMotd(), this.server.getServerConfigurationManager().getPlayerCount(), this.server.getServerConfigurationManager().getMaxPlayers());
|
|
|
|
String s = pingEvent.getMotd() + "\u00A7" + this.server.getServerConfigurationManager().getPlayerCount() + "\u00A7" + pingEvent.getMaxPlayers();
|
2011-09-27 00:32:05 +02:00
|
|
|
// CraftBukkit end
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
InetAddress inetaddress = null;
|
|
|
|
|
|
|
|
if (this.networkManager.getSocket() != null) {
|
|
|
|
inetaddress = this.networkManager.getSocket().getInetAddress();
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
this.networkManager.queue(new Packet255KickDisconnect(s));
|
|
|
|
this.networkManager.d();
|
2012-07-29 09:33:13 +02:00
|
|
|
if (inetaddress != null && this.server.ac() instanceof DedicatedServerConnection) {
|
|
|
|
((DedicatedServerConnection) this.server.ac()).a(inetaddress);
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
this.c = true;
|
|
|
|
} catch (Exception exception) {
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
public void onUnhandledPacket(Packet packet) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.disconnect("Protocol error");
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
public String getName() {
|
2012-07-29 09:33:13 +02:00
|
|
|
return this.h != null ? this.h + " [" + this.networkManager.getSocketAddress().toString() + "]" : this.networkManager.getSocketAddress().toString();
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public boolean a() {
|
2011-04-20 22:47:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-05 19:15:04 +01:00
|
|
|
static String a(NetLoginHandler netloginhandler) {
|
2012-01-14 21:03:48 +01:00
|
|
|
return netloginhandler.loginKey;
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
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;
|
2011-02-05 19:15:04 +01:00
|
|
|
}
|
|
|
|
}
|