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;