1.2. Просто проверки на работу.

This commit is contained in:
Dereku 2023-12-03 19:20:52 +07:00
parent 520e143a37
commit 34b6a5061d
2 changed files with 36 additions and 12 deletions

View file

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

View file

@ -27,13 +27,27 @@ public class ServerThread extends Thread {
this.serverSocket.setPerformancePreferences(0, 2, 1); this.serverSocket.setPerformancePreferences(0, 2, 1);
} }
public ServerThread(final String host, int port) throws IOException {
this.minecraftServer = null;
this.setName("LegacyPing thread listening " + host + ":" + port);
final InetAddress address = InetAddress.getByName(host);
this.serverSocket = new ServerSocket(port, 10, address);
this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));
this.serverSocket.setPerformancePreferences(0, 2, 1);
}
public static void main(String[] args) throws IOException {
new ServerThread("", 25565).start();
}
@Override @Override
public void run() { public void run() {
this.working.set(true); this.working.set(true);
while (this.working.get() || !this.serverSocket.isClosed()) { while (this.working.get() && !this.serverSocket.isClosed()) {
try { try {
final Socket socket = this.serverSocket.accept(); final Socket socket = this.serverSocket.accept();
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS); socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
System.out.println("[LegacyPing] Got new ping from " + socket.getRemoteSocketAddress());
final InputStream inputStream = socket.getInputStream(); final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream(); final OutputStream outputStream = socket.getOutputStream();
final int firstByte = inputStream.read(); final int firstByte = inputStream.read();
@ -49,7 +63,9 @@ public class ServerThread extends Thread {
} }
outputStream.flush(); outputStream.flush();
socket.close(); socket.close();
} catch (IOException ignore) { } catch (SocketTimeoutException ignore) {
} catch (IOException ex) {
System.out.println("Exception caught: " + ex);
} }
} }
} }
@ -66,9 +82,7 @@ public class ServerThread extends Thread {
final ByteArrayOutputStream result = new ByteArrayOutputStream(); final ByteArrayOutputStream result = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(result); final DataOutputStream dos = new DataOutputStream(result);
dos.write(255); // Packet 0xFF (PacketDisconnect) dos.write(255); // Packet 0xFF (PacketDisconnect)
final String response = ((CraftServer) this.minecraftServer).getServer().motd final String response = this.getMotd() + '\u0000' + this.getOnlinePlayers() + '\u0000' + this.getMaxPlayers();
+ '\u0000' + this.minecraftServer.getOnlinePlayers().length
+ '\u0000' + this.minecraftServer.getMaxPlayers();
dos.writeShort(response.length()); dos.writeShort(response.length());
for (char c : response.toCharArray()) { for (char c : response.toCharArray()) {
dos.writeChar(c); dos.writeChar(c);
@ -83,9 +97,7 @@ public class ServerThread extends Thread {
private void responseForLegacyPing(final OutputStream outputStream) throws IOException { private void responseForLegacyPing(final OutputStream outputStream) throws IOException {
final DataOutputStream clientOutput = new DataOutputStream(outputStream); final DataOutputStream clientOutput = new DataOutputStream(outputStream);
clientOutput.write(255); clientOutput.write(255);
final String response = ((CraftServer) this.minecraftServer).getServer().motd final String response = this.getMotd() + '\u00a7' + this.getOnlinePlayers() + '\u00a7' + this.getMaxPlayers();
+ '\u00a7' + this.minecraftServer.getOnlinePlayers().length
+ '\u00a7' + this.minecraftServer.getMaxPlayers();
clientOutput.writeShort(response.length()); clientOutput.writeShort(response.length());
for (char c : response.toCharArray()) { for (char c : response.toCharArray()) {
clientOutput.writeChar(c); clientOutput.writeChar(c);
@ -94,9 +106,9 @@ public class ServerThread extends Thread {
} }
private void responseForModernPing(final OutputStream outputStream) throws IOException { private void responseForModernPing(final OutputStream outputStream) throws IOException {
final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.minecraftServer.getMaxPlayers())) final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.getMaxPlayers()))
.replace("%now%", String.valueOf(this.minecraftServer.getOnlinePlayers().length)) .replace("%now%", String.valueOf(this.getOnlinePlayers()))
.replace("%motd%", ((CraftServer) this.minecraftServer).getServer().motd); .replace("%motd%", this.getMotd());
byte[] bytes = responseJson.getBytes(); byte[] bytes = responseJson.getBytes();
final DataOutputStream clientOutput = new DataOutputStream(outputStream); final DataOutputStream clientOutput = new DataOutputStream(outputStream);
this.writeVarInt(clientOutput, bytes.length + 3); this.writeVarInt(clientOutput, bytes.length + 3);
@ -106,6 +118,18 @@ public class ServerThread extends Thread {
// Packet data end // Packet data end
} }
private String getMotd() {
return this.minecraftServer == null ? "This is an example of the Message of the Day" : ((CraftServer) this.minecraftServer).getServer().motd;
}
private int getOnlinePlayers() {
return this.minecraftServer == null ? 13 : this.minecraftServer.getOnlinePlayers().length;
}
private int getMaxPlayers() {
return this.minecraftServer == null ? 37 : this.minecraftServer.getMaxPlayers();
}
private void writeVarInt(DataOutputStream out, int paramInt) throws IOException { private void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
while (true) { while (true) {
if ((paramInt & 0xFFFFFF80) == 0) { if ((paramInt & 0xFFFFFF80) == 0) {