From dc16fa7b6f2f044cb89084d296aac4a95e4b17f7 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Thu, 18 Aug 2016 16:21:43 -0400 Subject: [PATCH] Add support for getting ip bans, ip intel, and ip logs by hashes --- .../net/frozenorb/apiv3/model/IpIntel.java | 7 +++- .../net/frozenorb/apiv3/model/IpLogEntry.java | 4 ++ .../apiv3/route/ipBans/GETIpBans.java | 7 +--- .../apiv3/route/ipIntel/GETIpInteld.java | 28 ++++++++----- .../apiv3/route/ipLog/GETIpLogId.java | 41 ++++++++++++------- 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/IpIntel.java b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java index 1bdf0e4..e6514a5 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpIntel.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java @@ -10,6 +10,7 @@ import io.vertx.core.CompositeFuture; import io.vertx.core.Future; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.Setter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.maxmind.MaxMindLocation; import net.frozenorb.apiv3.maxmind.MaxMindResult; @@ -28,7 +29,7 @@ public final class IpIntel { private static final MongoCollection ipIntelCollection = APIv3.getDatabase().getCollection("ipIntel", IpIntel.class); - @Getter @Id private String id; + @Getter @Setter @Id private String id; @Getter private String hashedIp; @Getter private Instant lastUpdatedAt; @Getter private MaxMindResult result; @@ -46,6 +47,10 @@ public final class IpIntel { ipIntelCollection.find(new Document("_id", id)).first(SyncUtils.vertxWrap(callback)); } + public static void findByHashedIp(String hashedIp, SingleResultCallback callback) { + ipIntelCollection.find(new Document("hashedIp", hashedIp)).first(SyncUtils.vertxWrap(callback)); + } + public static void findOrCreateById(String id, SingleResultCallback callback) { findById(id, (existingIpIntel, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java index 8ec23cd..5197df0 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java @@ -57,6 +57,10 @@ public final class IpLogEntry { ipLogCollection.find(new Document("userIp", userIp)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); } + public static void findByHashedIp(String hashedUserIp, SingleResultCallback> callback) { + ipLogCollection.find(new Document("hashedUserIp", hashedUserIp)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); + } + public static void findByUserAndIp(User user, String userIp, SingleResultCallback callback) { findByUserAndIp(user.getId(), userIp, callback); } diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java index 64f8a18..bf74f9c 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java @@ -16,12 +16,7 @@ public final class GETIpBans implements Handler { int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize")); String userIp = ctx.request().getParam("userIp"); - if (userIp != null && !IpUtils.isValidIp(userIp)) { - ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); - return; - } - - IpBan.findPaginated(userIp == null ? new Document() : new Document("userIp", userIp), skip, pageSize, (grants, error) -> { + IpBan.findPaginated(userIp == null ? new Document() : (IpUtils.isValidIp(userIp) ? new Document("userIp", userIp) : new Document("hashedUserIp", userIp)), skip, pageSize, (grants, error) -> { if (error != null) { ErrorUtils.respondInternalError(ctx, error); } else { diff --git a/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIpInteld.java b/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIpInteld.java index e5f4e0d..c8769e4 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIpInteld.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIpInteld.java @@ -12,18 +12,24 @@ public final class GETIpInteld implements Handler { public void handle(RoutingContext ctx) { String userIp = ctx.request().getParam("userIp"); - if (!IpUtils.isValidIp(userIp)) { - ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); - return; + if (IpUtils.isValidIp(userIp)) { + IpIntel.findOrCreateById(userIp, (ipIntel, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, ipIntel); + } + }); + } else { + IpIntel.findByHashedIp(userIp, (ipIntel, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + ipIntel.setId(null); // hide actual ip field if we found the entry via hash + APIv3.respondJson(ctx, 200, ipIntel); + } + }); } - - IpIntel.findOrCreateById(userIp, (ipIntel, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - } else { - APIv3.respondJson(ctx, 200, ipIntel); - } - }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java index b374406..5b115a2 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java @@ -6,6 +6,9 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.IpLogEntry; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.UuidUtils; + +import java.util.UUID; public final class GETIpLogId implements Handler { @@ -13,21 +16,29 @@ public final class GETIpLogId implements Handler { String search = ctx.request().getParam("id"); if (search.length() >= 32) { - User.findById(search, (user, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - } else if (user == null) { - ErrorUtils.respondNotFound(ctx, "User", search); - } else { - IpLogEntry.findByUser(user, (ipLog, error2) -> { - if (error2 != null) { - ErrorUtils.respondInternalError(ctx, error2); - } else { - APIv3.respondJson(ctx, 200, ipLog); - } - }); - } - }); + UUID uuid = null; + + try { + uuid = UuidUtils.parseUuid(search); + } catch (IllegalArgumentException ignored) {} + + if (uuid != null) { + IpLogEntry.findByUser(uuid, (ipLog, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, ipLog); + } + }); + } else { + IpLogEntry.findByHashedIp(search, (ipLogs, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, ipLogs); + } + }); + } } else { IpLogEntry.findByIp(search, (ipLogs, error) -> { if (error != null) {