|
2 | 2 |
|
3 | 3 | import me.clip.placeholderapi.PlaceholderAPI; |
4 | 4 | import org.bukkit.Bukkit; |
| 5 | +import org.bukkit.OfflinePlayer; |
5 | 6 | import org.bukkit.configuration.file.FileConfiguration; |
6 | 7 |
|
7 | 8 | import java.io.BufferedReader; |
|
12 | 13 | import java.net.ServerSocket; |
13 | 14 | import java.net.Socket; |
14 | 15 | import java.util.Arrays; |
| 16 | +import java.util.HashMap; |
15 | 17 | import java.util.List; |
16 | 18 | import java.util.Locale; |
| 19 | +import java.util.Map; |
17 | 20 | import java.util.UUID; |
18 | 21 | import java.util.logging.Logger; |
19 | 22 |
|
@@ -103,32 +106,86 @@ public void run() { |
103 | 106 | return; |
104 | 107 | } |
105 | 108 |
|
| 109 | + // Debug message |
| 110 | + if (config.getBoolean("debug")) { |
| 111 | + logger.info("Received request: " + request); |
| 112 | + } |
| 113 | + |
| 114 | + // Get parameters and route |
106 | 115 | String route = request.substring(initialSplit + 1, routeSplit); |
| 116 | + Map<String, String> params = new HashMap<>(); |
107 | 117 | int routeQuerySplit = route.indexOf('?'); |
108 | 118 | if (routeQuerySplit != -1) { |
109 | 119 | route = route.substring(0, routeQuerySplit); |
| 120 | + String paramString = request.split(" ")[1].split("\\?")[1]; |
| 121 | + String[] paramSplit = paramString.split("&"); |
| 122 | + for (String param : paramSplit) { |
| 123 | + String key = param.split("=")[0]; |
| 124 | + String value = param.split("=")[1]; |
| 125 | + |
| 126 | + // Debug message |
| 127 | + if (config.getBoolean("debug")) { |
| 128 | + logger.info("Added parameter " + key + " with value " + value); |
| 129 | + } |
| 130 | + params.put(key, value); |
| 131 | + } |
110 | 132 | } |
111 | 133 |
|
112 | 134 | // Remove initial slash |
113 | 135 | route = route.replaceFirst("/", ""); |
114 | 136 |
|
115 | | - logger.info("Received request with route: " + route); |
| 137 | + // Debug message |
| 138 | + if (config.getBoolean("debug")) { |
| 139 | + logger.info("Route: " + route); |
| 140 | + } |
116 | 141 |
|
117 | 142 | // Check if route exists |
118 | 143 | if (config.contains("routes." + route)) { |
| 144 | + // Initialize some parameters |
119 | 145 | String key = "routes." + route + "."; |
120 | 146 | String configMethod = config.getString(key + "method"); |
| 147 | + String authkey = config.getString("authkey"); |
| 148 | + boolean keyRequired = method.equals("POST"); |
121 | 149 |
|
| 150 | + // Ensure the method matches the route itself |
122 | 151 | if (!method.equals(configMethod)) { |
123 | 152 | respondNotAllowed(printWriter); |
124 | 153 | return; |
125 | 154 | } |
126 | 155 |
|
127 | | - String response = PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(UUID.randomUUID()), config.getString(key + "response")); |
| 156 | + // If auth key is specified, override keyRequired. |
| 157 | + if (config.contains(key + ".authkey")) { |
| 158 | + keyRequired = config.getBoolean(key + ".authkey"); |
| 159 | + } |
| 160 | + |
| 161 | + // Check if a key is required, and if the key is specified |
| 162 | + if (keyRequired && !authkey.equals(params.getOrDefault("key", ""))) { |
| 163 | + respondUnauthorized(printWriter); |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + // Set the player, depending on the request. |
| 168 | + OfflinePlayer player; |
| 169 | + if (method.equals("POST")) { |
| 170 | + if (params.containsKey("uuid")) { |
| 171 | + player = Bukkit.getOfflinePlayer(UUID.fromString(params.get("uuid"))); |
| 172 | + } else if (params.containsKey("username")) { |
| 173 | + player = Bukkit.getPlayer(params.get("username")); |
| 174 | + } else { |
| 175 | + respondBadRequest(printWriter); |
| 176 | + return; |
| 177 | + } |
| 178 | + } else { |
| 179 | + player = Bukkit.getOfflinePlayer(UUID.randomUUID()); |
| 180 | + } |
| 181 | + |
| 182 | + // Build the response by letting PAPI parse the placeholders |
| 183 | + String response = PlaceholderAPI.setPlaceholders(player, config.getString(key + "response")); |
128 | 184 |
|
129 | 185 | // Wrap the response |
130 | 186 | String json = "{\"success\": true, \"response\": " + response + "}"; |
131 | 187 |
|
| 188 | + // Send it off! |
132 | 189 | respondOk(printWriter, json); |
133 | 190 | } else { |
134 | 191 | respondNotFound(printWriter); |
@@ -165,6 +222,15 @@ private void respondNotFound(PrintWriter writer) { |
165 | 222 | writer.printf("HTTP/1.2 404 Not Found%n%n"); |
166 | 223 | } |
167 | 224 |
|
| 225 | + /** |
| 226 | + * Responds to the HTTP request with a not found header. |
| 227 | + * |
| 228 | + * @param writer The {@link PrintWriter} to print the response to. |
| 229 | + */ |
| 230 | + private void respondUnauthorized(PrintWriter writer) { |
| 231 | + writer.printf("HTTP/1.2 401 Unauthorized%n%n"); |
| 232 | + } |
| 233 | + |
168 | 234 | /** |
169 | 235 | * Responds to the HTTP request with an OK header. |
170 | 236 | * |
|
0 commit comments