Skip to content

Commit

Permalink
Finished rate limit with key.
Browse files Browse the repository at this point in the history
  • Loading branch information
JKincorperated committed Feb 14, 2024
1 parent d522e8f commit 8774f02
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ example.com/vaultier/<servername>/

## To Do

| To Do | Status |
|-----------------------|---------------|
| IP Rate limit | Not Planned |
| Key Rate limit | In Progress |
| Allow Blocking users | ✔️ |
| Better error handling | Scheduled |
| Optimisations | Scheduled |
| To Do | Status |
|-----------------------|-------------|
| IP Rate limit | Not Planned |
| Key Rate limit | ✔️ |
| Allow Blocking users | ✔️ |
| Better error handling | Scheduled |
| Optimisations | Scheduled |
2 changes: 2 additions & 0 deletions src/main/java/uk/co/jkinc/Vaultier/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class Database implements Serializable {
public ECPublicKey publicKey;
public HashMap<UUID, Integer> playerSequence;
public HashMap<String, Boolean> blockedPlayers;
public transient HashMap<String, Integer> RateLimits;
public transient Long currentTimePeriod;
public transient HashMap<String, Transaction> Transactions;
}
2 changes: 2 additions & 0 deletions src/main/java/uk/co/jkinc/Vaultier/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public DatabaseManager(Vaultier plugin) {
in.close();
fileIn.close();
db.Transactions = new HashMap<String, Transaction>();
db.RateLimits = new HashMap<String, Integer>();
db.currentTimePeriod = new Date().getTime() / (1000 * 60 * 30); // Reset rate limit every 30 minutes
return;
} catch (IOException ignored) {}
catch (ClassNotFoundException j) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/uk/co/jkinc/Vaultier/HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public void startServer() throws IOException {

Player Initiator = Bukkit.getPlayer(player);

if (!RateLimiter.ProcessRequest(apiKey)) {
String response = "{\"error\":\"Rate Limited\"}";
exchange.sendResponseHeaders(429, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
return;
}

if (obj.get("Player") == null || Bukkit.getPlayer(obj.get("Player").getAsString()) == null) {
String response = "{\"error\":\"Unknown or offline player\"}";
exchange.sendResponseHeaders(400, response.length());
Expand Down Expand Up @@ -186,7 +195,7 @@ public void run() {
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
return;
e.printStackTrace();
}
});
server.createContext("/stat/", (HttpExchange exchange) -> {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/uk/co/jkinc/Vaultier/RateLimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.co.jkinc.Vaultier;

import java.util.Date;

public class RateLimiter {
private static final int maxReqs = 60; // 1 every 30 seconds
public static boolean ProcessRequest(String ApiKey) {
Integer requestsThisPeriod = Vaultier.database.db.RateLimits.get(ApiKey);
if (requestsThisPeriod == null) {
requestsThisPeriod = 0;
}

long currentPeriod = new Date().getTime() / (1000 * 60 * 30);

if (Vaultier.database.db.currentTimePeriod != currentPeriod) {
Vaultier.database.db.RateLimits.clear();
return true;
}

if (requestsThisPeriod > maxReqs) {
return false;
} else {
requestsThisPeriod+=1;
Vaultier.database.db.RateLimits.put(ApiKey, requestsThisPeriod);
return true;
}
}


}

0 comments on commit 8774f02

Please sign in to comment.