107 lines
3.7 KiB
Java
107 lines
3.7 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.logging.LogUtils
|
|
* org.jspecify.annotations.Nullable
|
|
* org.slf4j.Logger
|
|
*/
|
|
package net.minecraft.client.server;
|
|
|
|
import com.mojang.logging.LogUtils;
|
|
import java.io.IOException;
|
|
import java.net.DatagramPacket;
|
|
import java.net.DatagramSocket;
|
|
import java.net.InetAddress;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import net.minecraft.DefaultUncaughtExceptionHandler;
|
|
import org.jspecify.annotations.Nullable;
|
|
import org.slf4j.Logger;
|
|
|
|
public class LanServerPinger
|
|
extends Thread {
|
|
private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
public static final String MULTICAST_GROUP = "224.0.2.60";
|
|
public static final int PING_PORT = 4445;
|
|
private static final long PING_INTERVAL = 1500L;
|
|
private final String motd;
|
|
private final DatagramSocket socket;
|
|
private boolean isRunning = true;
|
|
private final String serverAddress;
|
|
|
|
public LanServerPinger(String motd, String serverAddress) throws IOException {
|
|
super("LanServerPinger #" + UNIQUE_THREAD_ID.incrementAndGet());
|
|
this.motd = motd;
|
|
this.serverAddress = serverAddress;
|
|
this.setDaemon(true);
|
|
this.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
|
|
this.socket = new DatagramSocket();
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
String pingString = LanServerPinger.createPingString(this.motd, this.serverAddress);
|
|
byte[] ping = pingString.getBytes(StandardCharsets.UTF_8);
|
|
while (!this.isInterrupted() && this.isRunning) {
|
|
try {
|
|
InetAddress group = InetAddress.getByName(MULTICAST_GROUP);
|
|
DatagramPacket packet = new DatagramPacket(ping, ping.length, group, 4445);
|
|
this.socket.send(packet);
|
|
}
|
|
catch (IOException e) {
|
|
LOGGER.warn("LanServerPinger: {}", (Object)e.getMessage());
|
|
break;
|
|
}
|
|
try {
|
|
LanServerPinger.sleep(1500L);
|
|
}
|
|
catch (InterruptedException interruptedException) {}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void interrupt() {
|
|
super.interrupt();
|
|
this.isRunning = false;
|
|
}
|
|
|
|
public static String createPingString(String motd, String address) {
|
|
return "[MOTD]" + motd + "[/MOTD][AD]" + address + "[/AD]";
|
|
}
|
|
|
|
public static String parseMotd(String pingString) {
|
|
int startIndex = pingString.indexOf("[MOTD]");
|
|
if (startIndex < 0) {
|
|
return "missing no";
|
|
}
|
|
int endIndex = pingString.indexOf("[/MOTD]", startIndex + "[MOTD]".length());
|
|
if (endIndex < startIndex) {
|
|
return "missing no";
|
|
}
|
|
return pingString.substring(startIndex + "[MOTD]".length(), endIndex);
|
|
}
|
|
|
|
public static @Nullable String parseAddress(String pingString) {
|
|
int endMotdIndex = pingString.indexOf("[/MOTD]");
|
|
if (endMotdIndex < 0) {
|
|
return null;
|
|
}
|
|
int secondEndMotdIndex = pingString.indexOf("[/MOTD]", endMotdIndex + "[/MOTD]".length());
|
|
if (secondEndMotdIndex >= 0) {
|
|
return null;
|
|
}
|
|
int startIndex = pingString.indexOf("[AD]", endMotdIndex + "[/MOTD]".length());
|
|
if (startIndex < 0) {
|
|
return null;
|
|
}
|
|
int endIndex = pingString.indexOf("[/AD]", startIndex + "[AD]".length());
|
|
if (endIndex < startIndex) {
|
|
return null;
|
|
}
|
|
return pingString.substring(startIndex + "[AD]".length(), endIndex);
|
|
}
|
|
}
|
|
|