Skip to content

Commit 777cbe9

Browse files
authored
Merge pull request #28 from RedCokeDevelopment/dev
Version 0.1.5.1 - Fix multiple CVE vulnerabilities from dependence
2 parents 04513a6 + 33b26c7 commit 777cbe9

6 files changed

Lines changed: 88 additions & 80 deletions

File tree

pom.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.redcoke</groupId>
88
<artifactId>mcserverping</artifactId>
9-
<version>0.1.5</version>
9+
<version>0.1.5.1</version>
1010

1111
<name>MCServerPing</name>
1212
<description>A Java API for obtaining info about a Minecraft Server</description>
@@ -61,10 +61,6 @@
6161
<name>jcenter-bintray</name>
6262
<url>https://jcenter.bintray.com</url>
6363
</repository>
64-
<repository>
65-
<id>bungeecord-repo</id>
66-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
67-
</repository>
6864
<repository>
6965
<id>maven_central</id>
7066
<name>Maven Central</name>
@@ -76,12 +72,12 @@
7672
<dependency>
7773
<groupId>com.google.code.gson</groupId>
7874
<artifactId>gson</artifactId>
79-
<version>2.9.0</version>
75+
<version>2.11.0</version>
8076
</dependency>
8177
<dependency>
8278
<groupId>org.projectlombok</groupId>
8379
<artifactId>lombok</artifactId>
84-
<version>1.18.32</version>
80+
<version>1.18.34</version>
8581
<scope>provided</scope>
8682
</dependency>
8783
<dependency>
@@ -92,7 +88,7 @@
9288
<dependency>
9389
<groupId>dnsjava</groupId>
9490
<artifactId>dnsjava</artifactId>
95-
<version>3.5.1</version>
91+
<version>3.6.0</version>
9692
</dependency>
9793
<dependency>
9894
<groupId>org.junit.jupiter</groupId>

src/main/java/dev/redcoke/mcserverping/MCServerPing.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212
import java.util.concurrent.*;
1313
import java.util.concurrent.atomic.AtomicInteger;
1414

15+
import dev.redcoke.mcserverping.utils.TextComponentFormatter;
16+
import lombok.AccessLevel;
17+
import lombok.NoArgsConstructor;
1518
import org.xbill.DNS.Lookup;
1619
import org.xbill.DNS.SRVRecord;
1720
import org.xbill.DNS.Type;
1821

1922
/**
2023
* API for pinging and obtaining info about a Minecraft server.
2124
*/
25+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2226
public final class MCServerPing {
23-
24-
private MCServerPing() {
25-
}
26-
27-
2827
/**
2928
* Pings a Minecraft Server to obtain server info.
3029
*
3130
* @param address server address
3231
* @return MCServerPingResponse
33-
* @throws IOException
32+
* @throws IOException failed to resolve hostname
3433
*/
35-
public static MCServerPingResponse getPing(final String address) throws IOException, TimeoutException {
34+
@SuppressWarnings("unused")
35+
public static ServerPingResponse getPing(final String address) throws IOException, TimeoutException {
3636
return getPing(address, 25565);
3737
}
3838

@@ -41,10 +41,12 @@ public static MCServerPingResponse getPing(final String address) throws IOExcept
4141
*
4242
* @param address server address
4343
* @param port server port
44-
* @return MCServerPingResponse
45-
* @throws IOException
44+
* @return MCServerPingResponse server info
45+
* @throws IOException failed to resolve hostname
46+
* @throws TimeoutException when the server does not respond within 3 seconds
47+
* @see ServerPingResponse
4648
*/
47-
public static MCServerPingResponse getPing(final String address, final int port)
49+
public static ServerPingResponse getPing(final String address, final int port)
4850
throws IOException, TimeoutException {
4951

5052
if (address == null) {
@@ -59,11 +61,8 @@ public static MCServerPingResponse getPing(final String address, final int port)
5961
if (srvRecords != null) {
6062
for (var srvRecord : srvRecords) {
6163
var srv = (SRVRecord) srvRecord;
62-
63-
6464
serverHost = srv.getTarget().toString().replaceFirst("\\.$", "");
6565
serverPort = srv.getPort();
66-
6766
}
6867
}
6968

@@ -149,13 +148,12 @@ public static MCServerPingResponse getPing(final String address, final int port)
149148

150149
jsonObj.addProperty("ping", ping);
151150

152-
return MCServerPingResponse.serverPingFromJsonObj(jsonObj);
151+
return ServerPingResponse.serverPingFromJsonObj(jsonObj);
153152
}
154153

155154

156155
/**
157156
* Throws IOException when condition is false.
158-
*
159157
* @param b Condition
160158
* @param m Exception cause
161159
* @throws IOException Exception
@@ -166,6 +164,13 @@ public static void io(final boolean b, final String m) throws IOException {
166164
}
167165
}
168166

167+
/**
168+
* Reads a VarInt from a DataInputStream.
169+
* @param in DataInputStream
170+
* @return int
171+
* @throws IOException Failed to read VarInt or invalid VarInt
172+
* @throws TimeoutException Timed out
173+
*/
169174
public static int readVarInt(DataInputStream in) throws IOException, TimeoutException {
170175
int i = 0;
171176
int j = 0;

src/main/java/dev/redcoke/mcserverping/MCServerPingResponse.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dev.redcoke.mcserverping;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
import org.jetbrains.annotations.CheckReturnValue;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
@Getter
13+
@RequiredArgsConstructor
14+
public final class ServerPingResponse {
15+
private final int ping;
16+
private final String version;
17+
private final int protocol;
18+
@Nullable private final Integer maxPlayers;
19+
@Nullable private final Integer onlinePlayers;
20+
private final String motd;
21+
private final JsonArray descriptionExtras;
22+
private final String serverIcon;
23+
24+
private static final Gson gson = new Gson().newBuilder().setPrettyPrinting().disableHtmlEscaping().create();
25+
26+
public static ServerPingResponse serverPingFromJsonObj(JsonObject jsonObj) {
27+
var serverPing = jsonObj.get("ping").getAsInt();
28+
String versionName;
29+
int serverProtocol;
30+
if (jsonObj.get("version").getAsJsonObject().has("name")) { // 1.19+ format
31+
versionName = jsonObj.get("version").getAsJsonObject().get("name").getAsString();
32+
serverProtocol = jsonObj.get("version").getAsJsonObject().get("protocol").getAsInt();
33+
} else { // legacy SLP format (pre 1.19.4)
34+
versionName = jsonObj.get("version").getAsString();
35+
serverProtocol = jsonObj.get("protocol").getAsInt();
36+
}
37+
Integer playerMax = null;
38+
Integer playerOnline = null;
39+
if (jsonObj.has("players")) { // Players object is optional somehow
40+
playerMax = jsonObj.get("players").getAsJsonObject().get("max").getAsInt();
41+
playerOnline = jsonObj.get("players").getAsJsonObject().get("online").getAsInt();
42+
}
43+
var serverMOTD = jsonObj.get("description").getAsJsonObject().get("text").getAsString();
44+
var serverDescriptionExtra = (jsonObj.get("description").getAsJsonObject().get("extra") == null) ? null : jsonObj.get("description").getAsJsonObject().get("extra").getAsJsonArray();
45+
var favIcon = jsonObj.get("favicon").getAsString();
46+
return new ServerPingResponse(
47+
serverPing, versionName, serverProtocol, playerMax, playerOnline, serverMOTD, serverDescriptionExtra, favIcon
48+
);
49+
}
50+
51+
@CheckReturnValue
52+
@SuppressWarnings("unused")
53+
public String getAsJsonString() {
54+
return gson.toJson(this);
55+
}
56+
57+
@CheckReturnValue
58+
@SuppressWarnings("unused")
59+
public JsonObject getAsJsonObject() {
60+
return JsonParser.parseString(getAsJsonString()).getAsJsonObject();
61+
}
62+
}

src/main/java/dev/redcoke/mcserverping/TextComponentFormatter.java renamed to src/main/java/dev/redcoke/mcserverping/utils/TextComponentFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.redcoke.mcserverping;
1+
package dev.redcoke.mcserverping.utils;
22

33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonObject;

src/test/java/dev/redcoke/mcserverping/MCServerPingDemoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void test() {
1313
List<String> servers = List.of("hypixel.net", "2b2t.org", "play.cubecraft.net");
1414
for (var server : servers) {
1515
try {
16-
MCServerPing.getPing(server, 25565).getAsJsonString();
16+
var ignored = MCServerPing.getPing(server, 25565).getAsJsonString();
1717
} catch (IOException | TimeoutException ex) {
1818
Assertions.fail(ex);
1919
}

0 commit comments

Comments
 (0)