More optimizations & fix NPE when console unbans a player

This commit is contained in:
Alfie Cleveland 2017-09-24 16:26:36 +01:00
parent 6394b7e4ef
commit 5270321c37
3 changed files with 50 additions and 29 deletions

View File

@ -267,6 +267,18 @@ public final class APIv3 {
} }
} }
public static void respondRawJson(RoutingContext ctx, int code, String response) {
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
if (response == null) {
ctx.response().setStatusCode(404);
ctx.response().end("{}");
} else {
ctx.response().setStatusCode(code);
ctx.response().end(response);
}
}
private void httpGet(Router router, String route, Class<?> handler) { private void httpGet(Router router, String route, Class<?> handler) {
router.get(route).blockingHandler((Handler<RoutingContext>) SpringUtils.getBean(handler), false); router.get(route).blockingHandler((Handler<RoutingContext>) SpringUtils.getBean(handler), false);
} }

View File

@ -3,6 +3,8 @@ package net.frozenorb.apiv3.web.route;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.unsorted.actor.Actor; import net.frozenorb.apiv3.unsorted.actor.Actor;
@ -16,17 +18,19 @@ import net.frozenorb.apiv3.domain.ServerGroup;
import net.frozenorb.apiv3.domain.User; import net.frozenorb.apiv3.domain.User;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.GeoJsonPoint; import net.frozenorb.apiv3.util.GeoJsonPoint;
import net.frozenorb.apiv3.util.SpringUtils;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -36,13 +40,13 @@ import io.vertx.ext.web.RoutingContext;
@Component @Component
public final class GETDumpsType implements Handler<RoutingContext> { public final class GETDumpsType implements Handler<RoutingContext> {
private final DecimalFormat coordinateFormat = new DecimalFormat("#.#####"); private String totpCache = SpringUtils.getBean(Gson.class).toJson("");
private String banCache = SpringUtils.getBean(Gson.class).toJson("");
private List<UUID> totpCache = ImmutableList.of(); private String blacklistCache = SpringUtils.getBean(Gson.class).toJson("");
private List<UUID> banCache = ImmutableList.of(); private String ipBanCache = SpringUtils.getBean(Gson.class).toJson("");
private List<UUID> blacklistCache = ImmutableList.of(); private String deniableCache = SpringUtils.getBean(Gson.class).toJson("");
private List<String> ipBanCache = ImmutableList.of(); private String emptyIpIntelJSON = SpringUtils.getBean(Gson.class).toJson(ImmutableSet.of());
private Map<String, Map<String, List<UUID>>> grantCache = ImmutableMap.of(); private Map<String, String> grantCache = ImmutableMap.of();
private Map<String, GeoJsonPoint> ipIntelCache = ImmutableMap.of(); private Map<String, GeoJsonPoint> ipIntelCache = ImmutableMap.of();
// 5 minutes, can't use TimeUnit expression in annotation // 5 minutes, can't use TimeUnit expression in annotation
@ -60,7 +64,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
totpCache.add(user.getId()); totpCache.add(user.getId());
} }
this.totpCache = totpCache; this.totpCache = SpringUtils.getBean(Gson.class).toJson(totpCache);
}); });
Punishment.findByType(ImmutableSet.of( Punishment.findByType(ImmutableSet.of(
@ -74,6 +78,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
List<UUID> banCache = new LinkedList<>(); List<UUID> banCache = new LinkedList<>();
List<UUID> blacklistCache = new LinkedList<>(); List<UUID> blacklistCache = new LinkedList<>();
List<UUID> accessDeniableCache = new LinkedList<>();
for (Punishment punishment : punishments) { for (Punishment punishment : punishments) {
if (!punishment.isActive()) { if (!punishment.isActive()) {
@ -82,13 +87,16 @@ public final class GETDumpsType implements Handler<RoutingContext> {
if (punishment.getType() == Punishment.PunishmentType.BAN) { if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser()); banCache.add(punishment.getUser());
accessDeniableCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) { } else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser()); blacklistCache.add(punishment.getUser());
accessDeniableCache.add(punishment.getUser());
} }
} }
this.banCache = banCache; this.banCache = SpringUtils.getBean(Gson.class).toJson(banCache);
this.blacklistCache = blacklistCache; this.blacklistCache = SpringUtils.getBean(Gson.class).toJson(blacklistCache);
this.deniableCache = SpringUtils.getBean(Gson.class).toJson(accessDeniableCache);
}); });
Grant.findAll((grants, error) -> { Grant.findAll((grants, error) -> {
@ -97,10 +105,10 @@ public final class GETDumpsType implements Handler<RoutingContext> {
return; return;
} }
Map<String, Map<String, List<UUID>>> grantCache = new HashMap<>(); Map<String, Map<String, List<UUID>>> grantCachePreJSON = new HashMap<>();
for (ServerGroup serverGroup : ServerGroup.findAll()) { for (ServerGroup serverGroup : ServerGroup.findAll()) {
grantCache.put(serverGroup.getId().toLowerCase(), new HashMap<>()); grantCachePreJSON.put(serverGroup.getId().toLowerCase(), new HashMap<>());
} }
for (Grant grant : grants) { for (Grant grant : grants) {
@ -113,7 +121,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
continue; continue;
} }
Map<String, List<UUID>> serverGroupGrantDump = grantCache.get(serverGroup.getId().toLowerCase()); Map<String, List<UUID>> serverGroupGrantDump = grantCachePreJSON.get(serverGroup.getId().toLowerCase());
List<UUID> users = serverGroupGrantDump.get(grant.getRank()); List<UUID> users = serverGroupGrantDump.get(grant.getRank());
if (users == null) { if (users == null) {
@ -125,7 +133,13 @@ public final class GETDumpsType implements Handler<RoutingContext> {
} }
} }
this.grantCache = grantCache; Map<String, String> grantCacheJSON = Maps.newHashMap();
for (Entry<String, Map<String, List<UUID>>> entry : grantCachePreJSON.entrySet()) {
grantCacheJSON.put(entry.getKey(), SpringUtils.getBean(Gson.class).toJson(entry.getValue()));
}
this.grantCache = grantCacheJSON;
}); });
IpBan.find((ipBans, error) -> { IpBan.find((ipBans, error) -> {
@ -144,7 +158,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
ipBanCache.add(ipBan.getUserIp()); ipBanCache.add(ipBan.getUserIp());
} }
this.ipBanCache = ipBanCache; this.ipBanCache = SpringUtils.getBean(Gson.class).toJson(ipBanCache);
}); });
} }
@ -153,24 +167,19 @@ public final class GETDumpsType implements Handler<RoutingContext> {
switch (dumpType.toLowerCase()) { switch (dumpType.toLowerCase()) {
case "totp": case "totp":
APIv3.respondJson(ctx, 200, totpCache); APIv3.respondRawJson(ctx, 200, totpCache);
return; return;
case "ban": case "ban":
APIv3.respondJson(ctx, 200, banCache); APIv3.respondRawJson(ctx, 200, banCache);
return; return;
case "blacklist": case "blacklist":
APIv3.respondJson(ctx, 200, blacklistCache); APIv3.respondRawJson(ctx, 200, blacklistCache);
return; return;
case "accessdeniable": // Lowercase d because we convert to lowercase above case "accessdeniable": // Lowercase d because we convert to lowercase above
List<UUID> result = new LinkedList<>(); APIv3.respondRawJson(ctx, 200, deniableCache);
result.addAll(banCache);
result.addAll(blacklistCache);
APIv3.respondJson(ctx, 200, result);
return; return;
case "ipban": case "ipban":
APIv3.respondJson(ctx, 200, ipBanCache); APIv3.respondRawJson(ctx, 200, ipBanCache);
return; return;
case "grant": case "grant":
String serverGroup = ServerGroup.DEFAULT_GROUP_ID; String serverGroup = ServerGroup.DEFAULT_GROUP_ID;
@ -192,7 +201,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
} }
} }
APIv3.respondJson(ctx, 200, grantCache.get(serverGroup.toLowerCase())); APIv3.respondRawJson(ctx, 200, grantCache.get(serverGroup.toLowerCase()));
return; return;
case "rankusers": case "rankusers":
Rank rank = Rank.findById(ctx.request().getParam("rank")); Rank rank = Rank.findById(ctx.request().getParam("rank"));
@ -226,7 +235,7 @@ public final class GETDumpsType implements Handler<RoutingContext> {
}); });
return; return;
case "ipintel": case "ipintel":
APIv3.respondJson(ctx, 200, ipIntelCache.values()); APIv3.respondRawJson(ctx, 200, emptyIpIntelJSON);
return; return;
case "ipintelformatted": case "ipintelformatted":
List<Map<String, Object>> features = new ArrayList<>(ipIntelCache.size()); List<Map<String, Object>> features = new ArrayList<>(ipIntelCache.size());

View File

@ -60,7 +60,7 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
if (!punishment.isActive()) continue; if (!punishment.isActive()) continue;
SyncUtils.<Void>runBlocking(v -> punishment.delete(removedBy, reason, v)); SyncUtils.<Void>runBlocking(v -> punishment.delete(removedBy, reason, v));
SyncUtils.<AuditLogEntry>runBlocking(v -> AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v)); SyncUtils.<AuditLogEntry>runBlocking(v -> AuditLog.log(removedBy == null ? null : removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v));
removedPunishments.add(punishment); removedPunishments.add(punishment);
} }