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> <groupId>net.rubeta</groupId>
<artifactId>LegacyPing</artifactId> <artifactId>LegacyPing</artifactId>
<version>1.6</version> <version>1.7</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>LegacyPing</name> <name>LegacyPing</name>

View file

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