Convert `AbusiveHostRule` to a record

This commit is contained in:
Chris Eager 2021-12-08 14:24:36 -08:00 committed by Chris Eager
parent a70b057e1c
commit dba1711e8d
4 changed files with 15 additions and 37 deletions

View File

@ -92,7 +92,6 @@ import org.whispersystems.textsecuregcm.util.Hex;
import org.whispersystems.textsecuregcm.util.ImpossiblePhoneNumberException;
import org.whispersystems.textsecuregcm.util.NonNormalizedPhoneNumberException;
import org.whispersystems.textsecuregcm.util.Username;
import org.whispersystems.textsecuregcm.util.UsernameValidator;
import org.whispersystems.textsecuregcm.util.Util;
import org.whispersystems.textsecuregcm.util.VerificationCode;
@ -728,14 +727,14 @@ public class AccountController {
List<AbusiveHostRule> abuseRules = abusiveHostRules.getAbusiveHostRulesFor(sourceHost);
for (AbusiveHostRule abuseRule : abuseRules) {
if (abuseRule.isBlocked()) {
if (abuseRule.blocked()) {
logger.info("Blocked host: {}, {}, {} ({})", transport, number, sourceHost, forwardedFor);
blockedHostMeter.mark();
return new CaptchaRequirement(true, false);
}
if (!abuseRule.getRegions().isEmpty()) {
if (abuseRule.getRegions().stream().noneMatch(number::startsWith)) {
if (!abuseRule.regions().isEmpty()) {
if (abuseRule.regions().stream().noneMatch(number::startsWith)) {
logger.info("Restricted host: {}, {}, {} ({})", transport, number, sourceHost, forwardedFor);
filteredHostMeter.mark();
return new CaptchaRequirement(true, false);

View File

@ -7,27 +7,6 @@ package org.whispersystems.textsecuregcm.storage;
import java.util.List;
public class AbusiveHostRule {
public record AbusiveHostRule(String host, boolean blocked, List<String> regions) {
private final String host;
private final boolean blocked;
private final List<String> regions;
public AbusiveHostRule(String host, boolean blocked, List<String> regions) {
this.host = host;
this.blocked = blocked;
this.regions = regions;
}
public List<String> getRegions() {
return regions;
}
public boolean isBlocked() {
return blocked;
}
public String getHost() {
return host;
}
}

View File

@ -112,10 +112,10 @@ public class AbusiveHostRules {
try (Timer.Context timer = insertTimer.time()) {
return handle.createUpdate(
"INSERT INTO abusive_host_rules(host, blocked, notes, regions) VALUES(:host::inet, :blocked, :notes, :regions) ON CONFLICT DO NOTHING")
.bind("host", rule.getHost())
.bind("blocked", rule.isBlocked() ? 1 : 0)
.bind("host", rule.host())
.bind("blocked", rule.blocked() ? 1 : 0)
.bind("notes", notes)
.bind("regions", String.join(",", rule.getRegions()))
.bind("regions", String.join(",", rule.regions()))
.execute();
}
}));

View File

@ -69,9 +69,9 @@ class AbusiveHostRulesTest {
List<AbusiveHostRule> rules = abusiveHostRules.getAbusiveHostRulesFor("192.168.1.1");
assertThat(rules.size()).isEqualTo(1);
assertThat(rules.get(0).getRegions().isEmpty()).isTrue();
assertThat(rules.get(0).getHost()).isEqualTo("192.168.1.1");
assertThat(rules.get(0).isBlocked()).isTrue();
assertThat(rules.get(0).regions().isEmpty()).isTrue();
assertThat(rules.get(0).host()).isEqualTo("192.168.1.1");
assertThat(rules.get(0).blocked()).isTrue();
}
@Test
@ -84,9 +84,9 @@ class AbusiveHostRulesTest {
List<AbusiveHostRule> rules = abusiveHostRules.getAbusiveHostRulesFor("192.168.1.1");
assertThat(rules.size()).isEqualTo(1);
assertThat(rules.get(0).getRegions().isEmpty()).isTrue();
assertThat(rules.get(0).getHost()).isEqualTo("192.168.1.0/24");
assertThat(rules.get(0).isBlocked()).isTrue();
assertThat(rules.get(0).regions().isEmpty()).isTrue();
assertThat(rules.get(0).host()).isEqualTo("192.168.1.0/24");
assertThat(rules.get(0).blocked()).isTrue();
}
@Test
@ -112,8 +112,8 @@ class AbusiveHostRulesTest {
List<AbusiveHostRule> rules = abusiveHostRules.getAbusiveHostRulesFor("192.168.1.100");
assertThat(rules.size()).isEqualTo(1);
assertThat(rules.get(0).isBlocked()).isFalse();
assertThat(rules.get(0).getRegions()).isEqualTo(Arrays.asList("+1", "+49"));
assertThat(rules.get(0).blocked()).isFalse();
assertThat(rules.get(0).regions()).isEqualTo(Arrays.asList("+1", "+49"));
}
@Test