Skip to content

Commit 95d01c0

Browse files
committed
Prevent exploit where server is flooded by idling connection, depleting resources.
Make compatible with latest Bukkit Remove updater code.
1 parent 1095428 commit 95d01c0

File tree

7 files changed

+57
-231
lines changed

7 files changed

+57
-231
lines changed

java/net/minestatus/minequery/Minequery.java

+18-33
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
// Modified by Jared "LgZ-optical" Klopper
20+
1921
package net.minestatus.minequery;
2022

21-
import net.minestatus.minequery.util.Updater;
2223
import net.minestatus.minequery.net.QueryServer;
24+
25+
import org.bukkit.configuration.file.FileConfiguration;
2326
import org.bukkit.plugin.java.JavaPlugin;
2427

2528
import java.io.File;
2629
import java.io.IOException;
2730
import java.net.BindException;
28-
import java.util.concurrent.Executors;
2931
import java.util.concurrent.ScheduledExecutorService;
30-
import java.util.concurrent.TimeUnit;
3132
import java.util.logging.Level;
3233
import java.util.logging.Logger;
3334

@@ -70,10 +71,6 @@ public final class Minequery extends JavaPlugin {
7071
*/
7172
private QueryServer server;
7273

73-
/**
74-
* The main updater instance.
75-
*/
76-
private Updater updater;
7774

7875
/**
7976
* The main updater scheduler.
@@ -147,16 +144,6 @@ public void onEnable() {
147144
log(Level.SEVERE, "Error starting server listener", ex);
148145
}
149146
}
150-
151-
// Updater mode
152-
if (getConfiguration().getBoolean("updater.enabled", false)) {
153-
log(Level.INFO, "Stopping Minequery updater");
154-
155-
// Schedule the updater.
156-
updaterScheduler = Executors.newSingleThreadScheduledExecutor();
157-
updaterScheduler.scheduleAtFixedRate(new Updater(), 0, 5, TimeUnit.SECONDS);
158-
159-
}
160147
}
161148

162149
private void loadConfiguration() {
@@ -178,27 +165,17 @@ private void loadConfiguration() {
178165
// Set up the default configuration.
179166

180167
// Details
181-
getConfiguration().setProperty("details.server_name", "My Server");
168+
getConfiguration().set("details.server_name", "My Server");
182169

183170
// Server mode
184-
getConfiguration().setProperty("server.ip", "");
185-
getConfiguration().setProperty("server.port", 25566);
186-
getConfiguration().setProperty("server.enabled", true);
187-
188-
// Updater mode
189-
getConfiguration().setProperty("updater.enabled", false);
190-
getConfiguration().setProperty("updater.services.minestatus.key", "");
191-
getConfiguration().setProperty("updater.services.minestatus.url", "");
192-
getConfiguration().setProperty("updater.services.myserverlist.key", "");
193-
getConfiguration().setProperty("updater.services.myserverlist.url", "");
194-
195-
getConfiguration().save();
171+
getConfiguration().set("server.ip", "");
172+
getConfiguration().set("server.port", 25566);
173+
getConfiguration().set("server.enabled", true);
174+
saveConfig();
196175
}
197176
} catch (IOException ex) {
198177
log(Level.WARNING, "Failed to create plugin configuration file.");
199178
}
200-
201-
getConfiguration().load();
202179
serverIP = getServer().getIp();
203180
serverPort = getServer().getPort();
204181
minequeryIP = getConfiguration().getString("server.ip", serverIP);
@@ -280,5 +257,13 @@ public int getMinequeryPort() {
280257
public static Minequery getInstance() {
281258
return instance;
282259
}
283-
260+
261+
/**
262+
* Get the config file
263+
*
264+
* @return The instance of configuration
265+
*/
266+
public FileConfiguration getConfiguration() {
267+
return getConfig();
268+
}
284269
}

java/net/minestatus/minequery/net/QueryServer.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.net.InetSocketAddress;
2525
import java.net.ServerSocket;
2626
import java.net.Socket;
27+
import java.util.Collections;
28+
import java.util.HashSet;
29+
import java.util.Set;
2730
import java.util.logging.Level;
2831

2932
/**
@@ -49,7 +52,13 @@ public final class QueryServer extends Thread {
4952
* The connection listener.
5053
*/
5154
private ServerSocket listener;
52-
55+
56+
/**
57+
* Synchronized collection of all active connection remote addresses
58+
*/
59+
private Set<String> activeConnectionAddresses;
60+
61+
5362
/**
5463
* Creates a new <code>QueryServer</code> object.
5564
*
@@ -61,6 +70,7 @@ public final class QueryServer extends Thread {
6170
public QueryServer(String host, int port) {
6271
this.host = host;
6372
this.port = port;
73+
this.activeConnectionAddresses = Collections.synchronizedSet(new HashSet<String>());
6474
}
6575

6676
/**
@@ -89,13 +99,24 @@ public void run() {
8999
while (!listener.isClosed()) {
90100
// Wait for and accept all incoming connections.
91101
Socket socket = getListener().accept();
92-
102+
if (activeConnectionAddresses.contains(socket.getInetAddress().getHostAddress())) {
103+
socket.close();
104+
} else {
105+
activeConnectionAddresses.add(socket.getInetAddress().getHostAddress());
106+
}
93107
// Create a new thread to handle the request.
94-
(new Thread(new RequestHandler(socket))).start();
108+
(new Thread(new RequestHandler(socket, this))).start();
95109
}
96110
} catch (IOException ignored) {}
97111
}
98112

113+
/**
114+
* Releases the sockets address
115+
*/
116+
public void releaseSocket(Socket socket) {
117+
activeConnectionAddresses.remove(socket.getInetAddress().getHostAddress());
118+
}
119+
99120
/**
100121
* Gets the <code>QueryServer</code> host.
101122
*
@@ -122,5 +143,4 @@ public int getPort() {
122143
public ServerSocket getListener() {
123144
return listener;
124145
}
125-
126146
}

java/net/minestatus/minequery/net/RequestHandler.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ public final class RequestHandler extends Thread {
4545
* The socket we are using to obtain a request.
4646
*/
4747
private final Socket socket;
48-
48+
49+
/**
50+
* The socket manager
51+
*/
52+
private final QueryServer server;
53+
4954
/**
5055
* Creates a new <code>QueryServer</code> object.
5156
*
5257
* @param socket
5358
* The socket we are using to obtain a request
5459
*/
55-
public RequestHandler(Socket socket) {
60+
public RequestHandler(Socket socket, QueryServer server) {
5661
this.socket = socket;
62+
this.server = server;
5763
}
5864

5965
/**
@@ -65,7 +71,10 @@ public void run() {
6571

6672
// Read the request and handle it.
6773
handleRequest(socket, reader.readLine());
68-
74+
75+
// Release the address
76+
server.releaseSocket(socket);
77+
6978
// Finally close the socket.
7079
socket.close();
7180
} catch (IOException ignored) {
@@ -103,9 +112,9 @@ private void handleRequest(Socket socket, String request) throws IOException, JS
103112
resp.append("PLAYERLIST ").append(new JSONArray(data.get("playerList")).toString()).append("\n");
104113
resp.append("SERVERNAME ").append(data.get("serverName")).append("\n");
105114
resp.append("SERVERIP ").append(data.get("serverIP")).append("\n");
106-
resp.append("EXTENDEDPLAYERLIST ").append(new JSONArray((List) data.get("extendedPlayerList")).toString()).append("\n");
107-
resp.append("PLUGINS ").append(new JSONArray((List) data.get("plugins")).toString()).append("\n");
108-
resp.append("VERSIONS ").append(new JSONObject((Map) data.get("versions")).toString()).append("\n");
115+
resp.append("EXTENDEDPLAYERLIST ").append(new JSONArray((List<?>) data.get("extendedPlayerList")).toString()).append("\n");
116+
resp.append("PLUGINS ").append(new JSONArray((List<?>) data.get("plugins")).toString()).append("\n");
117+
resp.append("VERSIONS ").append(new JSONObject((Map<?, ?>) data.get("versions")).toString()).append("\n");
109118

110119
// Send the response.
111120
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

java/net/minestatus/minequery/util/Updater.java

-111
This file was deleted.

java/net/minestatus/minequery/util/helper/DataHelper.java

-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package net.minestatus.minequery.util.helper;
2020

21-
import com.nijiko.permissions.PermissionHandler;
2221
import net.minestatus.minequery.Minequery;
2322
import org.bukkit.entity.Player;
2423
import org.bukkit.plugin.Plugin;
@@ -100,15 +99,6 @@ private static List<Map<String, Object>> getExtendedPlayerList() {
10099
playerMap.put("isSleeping", player.isSleeping());
101100
playerMap.put("isOp", player.isOp());
102101

103-
// Permissions specific values.
104-
if (PermissionsHelper.isPermissionsAvailable()) {
105-
Map<String, Object> permissionsMap = new HashMap<String, Object>();
106-
PermissionHandler permissionHandler = PermissionsHelper.getPermissionHandler();
107-
permissionsMap.put("groups", permissionHandler.getGroups(player.getWorld().getName(), player.getName()));
108-
109-
playerMap.put("permissions", permissionsMap);
110-
}
111-
112102
playerList.add(playerMap);
113103
}
114104

0 commit comments

Comments
 (0)