feat: add CI

This commit is contained in:
2025-01-11 14:09:32 +01:00
parent b7e3d82b51
commit 13ed5f0d77
7 changed files with 111 additions and 17 deletions

View File

@ -62,18 +62,8 @@ public class InfoDumper {
obj.put("magic", magicBE);
obj.put("players", playersBE);
obj.put("sv_maxClients", maxPlayersBE);
obj.put("info", infoString);
saveJSONFile(String.format("stats_%d.json", Math.abs(server.hashCode())), obj);
}
public static void saveJSONFile(String fileName, JSONObject obj) {
try {
var writer = new BufferedWriter(new FileWriter(fileName));
writer.write(obj.toJSONString());
writer.close();
}
catch (IOException ex) {
System.err.println("saveJSONFile: IOException while writing a JSON file");
}
Utils.saveJSONFile(String.format("stats_%d.json", Math.abs(server.hashCode())), obj);
}
}

View File

@ -23,6 +23,9 @@ import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
*
* @author Diamante
@ -72,7 +75,7 @@ public class MasterServerPinger {
int count = input.read(bytes);
out.write(bytes, 0, count);
System.out.println("readReplyFromMaster: finished reading bytes from socket");
}
catch (IOException ex) {
@ -102,6 +105,40 @@ public class MasterServerPinger {
System.out.println(String.format("readReplyFromMaster: got %d servers", serverCountBE));
var root = new JSONObject();
var serverArray = new JSONArray();
// Process server data
for (int i = 4; i < bytes.length; i += 6) {
if (i + 6 > bytes.length) {
System.err.println("readReplyFromMaster: Incomplete server data detected");
break;
}
byte[] ipBytesLE = new byte[4];
System.arraycopy(bytes, i, ipBytesLE, 0, 4);
var ipBytesBE = Utils.longSwap(ipBytesLE);
var ipAddress = Utils.bytesToIP(ipBytesBE);
var portBytesLE = new byte[2];
System.arraycopy(bytes, i + 4, portBytesLE, 0, 2);
var port = ((portBytesLE[1] & 0xFF) << 8) | (portBytesLE[0] & 0xFF);
System.out.println(String.format("Server: %s:%d", ipAddress, port));
var serverObject = new JSONObject();
serverObject.put("IP", ipAddress);
serverObject.put("port", port);
serverArray.add(serverObject);
}
root.put("totalServers", serverCountBE);
root.put("servers", serverArray);
Utils.saveJSONFile(String.format("server_dump_%d.json", System.currentTimeMillis() / 1000L), root);
try {
out.close();
}

View File

@ -105,6 +105,7 @@ public class Server {
hash = 71 * hash + Objects.hashCode(this.address);
hash = 71 * hash + Objects.hashCode(this.netPort);
hash = 71 * hash + Objects.hashCode(this.version);
hash = 71 * hash + Objects.hashCode(this.time);
return hash;
}

View File

@ -16,15 +16,19 @@
*/
package com.diamante.serverlist;
import java.net.InetAddress;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.BufferOverflowException;
import java.nio.ReadOnlyBufferException;
import java.nio.ByteBuffer;
import org.json.simple.JSONObject;
/**
*
* @author Diamante
@ -43,7 +47,7 @@ public class Utils {
public static final int OLD_CLIENT_MAGIC = 1414022477; // THEM
public static final int NEW_CLIENT_MAGIC = 1129268293;
public static final int CLIENT_VERSION = 17039893;
public static boolean isServerMagic(int magic) {
@ -151,4 +155,27 @@ public class Utils {
return null;
}
}
public static String bytesToIP(int in) {
String ipAddress = String.format(
"%d.%d.%d.%d",
(in & 0xff),
(in >> 8 & 0xff),
(in >> 16 & 0xff),
(in >> 24 & 0xff)
);
return ipAddress;
}
public static void saveJSONFile(String fileName, JSONObject obj) {
try {
var writer = new BufferedWriter(new FileWriter(fileName));
writer.write(obj.toJSONString());
writer.close();
}
catch (IOException ex) {
System.err.println("saveJSONFile: IOException while writing a JSON file");
}
}
}