Add support for getting ip bans, ip intel, and ip logs by hashes

This commit is contained in:
Colin McDonald 2016-08-18 16:21:43 -04:00
parent 287e6b744a
commit dc16fa7b6f
5 changed files with 54 additions and 33 deletions

View File

@ -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<IpIntel> 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<IpIntel> callback) {
ipIntelCollection.find(new Document("hashedIp", hashedIp)).first(SyncUtils.vertxWrap(callback));
}
public static void findOrCreateById(String id, SingleResultCallback<IpIntel> callback) {
findById(id, (existingIpIntel, error) -> {
if (error != null) {

View File

@ -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<List<IpLogEntry>> callback) {
ipLogCollection.find(new Document("hashedUserIp", hashedUserIp)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
public static void findByUserAndIp(User user, String userIp, SingleResultCallback<IpLogEntry> callback) {
findByUserAndIp(user.getId(), userIp, callback);
}

View File

@ -16,12 +16,7 @@ public final class GETIpBans implements Handler<RoutingContext> {
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 {

View File

@ -12,11 +12,7 @@ public final class GETIpInteld implements Handler<RoutingContext> {
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);
@ -24,6 +20,16 @@ public final class GETIpInteld implements Handler<RoutingContext> {
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);
}
});
}
}
}

View File

@ -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<RoutingContext> {
@ -13,21 +16,29 @@ public final class GETIpLogId implements Handler<RoutingContext> {
String search = ctx.request().getParam("id");
if (search.length() >= 32) {
User.findById(search, (user, error) -> {
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 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);
}
});
} 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) {