Fix of fixes

This commit is contained in:
Dereku 2024-09-19 00:59:21 +07:00
parent 9f0ba79d91
commit 56d4318dbe
3 changed files with 25 additions and 7 deletions

View file

@ -6,7 +6,7 @@
<groupId>net.rubeta</groupId>
<artifactId>LegacyPing</artifactId>
<version>1.6</version>
<version>1.7</version>
<packaging>jar</packaging>
<name>LegacyPing</name>

View file

@ -21,7 +21,7 @@ public final class LegacyPing extends JavaPlugin {
final int protocol = config.getInt("displayedProtocol", 17);
for (int port : ports) {
try {
this.threads.add(new ServerThread(this.getServer(), host, port, version, protocol));
this.threads.add(new ServerThread(this, host, port, version, protocol));
} catch (IOException e) {
this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e);
}

View file

@ -8,6 +8,8 @@ import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServerThread extends Thread {
@ -24,10 +26,12 @@ public class ServerThread extends Thread {
private final int protocol;
private final InetAddress address;
private final int port;
private final Logger logger;
private ServerSocket serverSocket;
public ServerThread(final Server server, final String host, int port, String version, int protocol) throws IOException {
this.minecraftServer = server;
public ServerThread(final LegacyPing plugin, final String host, int port, String version, int protocol) throws IOException {
this.minecraftServer = plugin.getServer();
this.logger = plugin.getLogger();
this.setName("LegacyPing thread listening " + host + ":" + port);
this.version = version;
this.protocol = protocol;
@ -38,6 +42,7 @@ public class ServerThread extends Thread {
public ServerThread(final String host, int port) throws IOException {
this.minecraftServer = null;
this.setName("LegacyPing thread listening " + host + ":" + port);
this.logger = Logger.getLogger(host + ":" + port);
this.version = "dummy";
this.protocol = 0;
this.address = InetAddress.getByName(host);
@ -58,7 +63,7 @@ public class ServerThread extends Thread {
this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));
this.serverSocket.setPerformancePreferences(0, 2, 1);
} catch (IOException e) {
System.out.println("Failed to start ServerSocket: " + e);
this.logger.log(Level.WARNING, "Failed to start ServerSocket", e);
this.pause();
continue;
}
@ -77,6 +82,7 @@ public class ServerThread extends Thread {
final String hostname = this.readUtf(inputStream);
final int port = inputStream.readShort();
final int status = this.readVarInt(inputStream);
this.logger.info("Ping from " + socket.getRemoteSocketAddress() + " to " + hostname + ":" + port);
// System.out.println(packetLength + "; Protocol version: " + protocolVersion + ", host: " + hostname
// + ", port: " + port + ", status: " + status);
if (status != 2) {
@ -86,13 +92,21 @@ public class ServerThread extends Thread {
}
outputStream.flush();
this.pause(50L);
if (socket.isClosed() || !socket.isConnected() || inputStream.available() == 0) {
continue;
}
inputStream.readShort(); // legacy data
this.pause(50L);
if (socket.isClosed() || !socket.isConnected() || inputStream.available() == 0) {
continue;
}
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9)
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1)
outputStream.writeLong(inputStream.readLong()); // timestamp
} catch (SocketTimeoutException | EOFException ignore) {
} catch (Exception ex) {
System.out.println("Exception caught: " + ex);
this.logger.log(Level.WARNING, "Unexpectable exception in " + this.getName(), ex);
}
}
}
@ -107,8 +121,12 @@ public class ServerThread extends Thread {
}
void pause() {
this.pause(TimeUnit.SECONDS.toMillis(30L));
}
void pause(long millis) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(30L));
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}