From 59eb6d10c12b150b1f9ca61f916e9d0d3c1bad04 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Fri, 15 Jan 2021 17:48:09 -0500 Subject: [PATCH] Gate based on destination rather than random. --- .../textsecuregcm/auth/AmbiguousIdentifier.java | 8 ++++++++ .../textsecuregcm/controllers/MessageController.java | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/auth/AmbiguousIdentifier.java b/service/src/main/java/org/whispersystems/textsecuregcm/auth/AmbiguousIdentifier.java index b06ce03c5..ffe6c81ab 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/auth/AmbiguousIdentifier.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/auth/AmbiguousIdentifier.java @@ -37,4 +37,12 @@ public class AmbiguousIdentifier { public boolean hasNumber() { return number != null; } + + public int sendingGateHash() { + if (uuid != null) { + return (int)(uuid.getLeastSignificantBits() & 0xff); + } else { + return number.hashCode() & 0xff; + } + } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java index ecb9dd7d1..9ef2ad33d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java @@ -124,7 +124,7 @@ public class MessageController { @Valid IncomingMessageList messages) throws RateLimitExceededException { - if (random.nextDouble() <= getSuccessPercentage()) { + if (shouldSend(destinationName)) { if (!source.isPresent() && !accessKey.isPresent()) { throw new WebApplicationException(Response.Status.UNAUTHORIZED); } @@ -199,6 +199,12 @@ public class MessageController { } } + private boolean shouldSend(final AmbiguousIdentifier destination) { + final double hash = destination.sendingGateHash(); + + return (hash / 255.0) <= getSuccessPercentage(); + } + private double getSuccessPercentage() { if (featureFlagsManager.isFeatureFlagActive("SEND_MESSAGE_1_PERCENT")) { return 0.01;