From 56d4318dbe0c296befc2471c878ee41aaa2d651b Mon Sep 17 00:00:00 2001 From: Dereku Date: Thu, 19 Sep 2024 00:59:21 +0700 Subject: [PATCH] Fix of fixes --- pom.xml | 2 +- .../net/rubeta/legacyping/LegacyPing.java | 2 +- .../net/rubeta/legacyping/ServerThread.java | 28 +++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 3edbb1e..e5cc334 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.rubeta LegacyPing - 1.6 + 1.7 jar LegacyPing diff --git a/src/main/java/net/rubeta/legacyping/LegacyPing.java b/src/main/java/net/rubeta/legacyping/LegacyPing.java index 2d93a48..a00d242 100644 --- a/src/main/java/net/rubeta/legacyping/LegacyPing.java +++ b/src/main/java/net/rubeta/legacyping/LegacyPing.java @@ -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); } diff --git a/src/main/java/net/rubeta/legacyping/ServerThread.java b/src/main/java/net/rubeta/legacyping/ServerThread.java index 97edb4d..6b49faa 100644 --- a/src/main/java/net/rubeta/legacyping/ServerThread.java +++ b/src/main/java/net/rubeta/legacyping/ServerThread.java @@ -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) { } }