Paper/src/main/java/net/minecraft/server/PendingConnection.java

218 Zeilen
7.5 KiB
Java

package net.minecraft.server;
import java.io.Serializable;
2012-07-29 09:33:13 +02:00
import java.net.InetAddress;
import java.net.Socket;
2012-07-29 09:33:13 +02:00
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;
2012-07-29 09:33:13 +02:00
import javax.crypto.SecretKey;
2012-12-20 05:03:52 +01:00
public class PendingConnection extends Connection {
private static Random random = new Random();
2013-03-13 23:33:27 +01:00
private byte[] d;
private final MinecraftServer server;
public final NetworkManager networkManager;
public boolean b = false;
private int f = 0;
private String g = null;
private volatile boolean h = false;
private String loginKey = Long.toString(random.nextLong(), 16); // CraftBukkit - Security fix
2013-03-13 23:33:27 +01:00
private boolean j = false;
private SecretKey k = null;
public String hostname = ""; // CraftBukkit - add field
2012-12-20 05:03:52 +01:00
public PendingConnection(MinecraftServer minecraftserver, Socket socket, String s) throws java.io.IOException { // CraftBukkit - throws IOException
this.server = minecraftserver;
2013-03-13 23:33:27 +01:00
this.networkManager = new NetworkManager(minecraftserver.getLogger(), socket, s, this, minecraftserver.F().getPrivate());
2012-07-29 09:33:13 +02:00
this.networkManager.e = 0;
}
// CraftBukkit start
public Socket getSocket() {
2012-07-29 09:33:13 +02:00
return this.networkManager.getSocket();
}
// CraftBukkit end
2012-07-29 09:33:13 +02:00
public void c() {
2013-03-13 23:33:27 +01:00
if (this.h) {
2012-07-29 09:33:13 +02:00
this.d();
}
2013-03-13 23:33:27 +01:00
if (this.f++ == 600) {
this.disconnect("Took too long to log in");
} else {
2011-05-26 14:48:22 +02:00
this.networkManager.b();
}
}
public void disconnect(String s) {
try {
2013-03-13 23:33:27 +01:00
this.server.getLogger().info("Disconnecting " + this.getName() + ": " + s);
this.networkManager.queue(new Packet255KickDisconnect(s));
2011-05-26 14:48:22 +02:00
this.networkManager.d();
2013-03-13 23:33:27 +01:00
this.b = true;
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void a(Packet2Handshake packet2handshake) {
// CraftBukkit start
this.hostname = packet2handshake.c == null ? "" : packet2handshake.c + ':' + packet2handshake.d;
// CraftBukkit end
2013-03-13 23:33:27 +01:00
this.g = packet2handshake.f();
if (!this.g.equals(StripColor.a(this.g))) {
2012-07-29 09:33:13 +02:00
this.disconnect("Invalid username!");
} else {
PublicKey publickey = this.server.F().getPublic();
2013-03-13 23:33:27 +01:00
if (packet2handshake.d() != 60) {
if (packet2handshake.d() > 60) {
2012-07-29 09:33:13 +02:00
this.disconnect("Outdated server!");
} else {
this.disconnect("Outdated client!");
}
} 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));
}
}
}
2012-07-29 09:33:13 +02:00
public void a(Packet252KeyResponse packet252keyresponse) {
PrivateKey privatekey = this.server.F().getPrivate();
2013-03-13 23:33:27 +01:00
this.k = packet252keyresponse.a(privatekey);
2012-07-29 09:33:13 +02:00
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());
}
2012-07-29 09:33:13 +02:00
public void a(Packet205ClientCommand packet205clientcommand) {
if (packet205clientcommand.a == 0) {
if (this.server.getOnlineMode()) {
2013-03-13 23:33:27 +01:00
if (this.j) {
2012-09-10 06:19:28 +02:00
this.disconnect("Duplicate login");
return;
}
2013-03-13 23:33:27 +01:00
this.j = true;
2012-07-29 09:33:13 +02:00
(new ThreadLoginVerifier(this, server.server)).start(); // CraftBukkit - add CraftServer
} else {
2013-03-13 23:33:27 +01:00
this.h = true;
}
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
2013-03-13 23:33:27 +01:00
EntityPlayer s = this.server.getPlayerList().attemptLogin(this, this.g, 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 {
2012-12-20 05:03:52 +01:00
EntityPlayer entityplayer = this.server.getPlayerList().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) {
2012-12-20 05:03:52 +01:00
this.server.getPlayerList().a((INetworkManager) this.networkManager, entityplayer);
2012-07-29 09:33:13 +02:00
}
}
2013-03-13 23:33:27 +01:00
this.b = true;
}
public void a(String s, Object[] aobject) {
2013-03-13 23:33:27 +01:00
this.server.getLogger().info(this.getName() + " lost connection");
this.b = true;
}
2011-09-15 02:23:52 +02:00
public void a(Packet254GetInfo packet254getinfo) {
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 {
2012-12-20 05:03:52 +01:00
PlayerList playerlist = this.server.getPlayerList();
String s = null;
// CraftBukkit
2012-12-20 05:03:52 +01:00
org.bukkit.event.server.ServerListPingEvent pingEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callServerListPingEvent(this.server.server, getSocket().getInetAddress(), this.server.getMotd(), playerlist.getPlayerCount(), playerlist.getMaxPlayers());
if (packet254getinfo.a == 1) {
// CraftBukkit start - fix decompile issues, don't create a list from an array
2013-03-13 23:33:27 +01:00
Object[] list = new Object[] { 1, 60, this.server.getVersion(), pingEvent.getMotd(), playerlist.getPlayerCount(), pingEvent.getMaxPlayers() };
for (Object object : list) {
if (s == null) {
s = "\u00A7";
} else {
s = s + "\0";
}
s += org.apache.commons.lang.StringUtils.replace(object.toString(), "\0", "");
}
// CraftBukkit end
} else {
// CraftBukkit
2012-12-20 05:03:52 +01:00
s = pingEvent.getMotd() + "\u00A7" + playerlist.getPlayerCount() + "\u00A7" + pingEvent.getMaxPlayers();
}
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();
if (inetaddress != null && this.server.ae() instanceof DedicatedServerConnection) {
((DedicatedServerConnection) this.server.ae()).a(inetaddress);
2012-07-29 09:33:13 +02:00
}
2013-03-13 23:33:27 +01:00
this.b = true;
2011-09-15 02:23:52 +02:00
} catch (Exception exception) {
exception.printStackTrace();
}
}
2012-02-29 22:31:04 +01:00
public void onUnhandledPacket(Packet packet) {
this.disconnect("Protocol error");
}
public String getName() {
2013-03-13 23:33:27 +01:00
return this.g != null ? this.g + " [" + this.networkManager.getSocketAddress().toString() + "]" : this.networkManager.getSocketAddress().toString();
}
2012-07-29 09:33:13 +02:00
public boolean a() {
2011-04-20 22:47:26 +02:00
return true;
}
2012-12-20 05:03:52 +01:00
static String a(PendingConnection pendingconnection) {
return pendingconnection.loginKey;
}
2012-12-20 05:03:52 +01:00
static MinecraftServer b(PendingConnection pendingconnection) {
return pendingconnection.server;
2012-07-29 09:33:13 +02:00
}
2012-12-20 05:03:52 +01:00
static SecretKey c(PendingConnection pendingconnection) {
2013-03-13 23:33:27 +01:00
return pendingconnection.k;
2012-07-29 09:33:13 +02:00
}
2012-12-20 05:03:52 +01:00
static String d(PendingConnection pendingconnection) {
2013-03-13 23:33:27 +01:00
return pendingconnection.g;
2012-07-29 09:33:13 +02:00
}
2012-12-20 05:03:52 +01:00
static boolean a(PendingConnection pendingconnection, boolean flag) {
2013-03-13 23:33:27 +01:00
return pendingconnection.h = flag;
}
}