Skip to content

Commit

Permalink
Add regex support to restricted users ip addresses #1227
Browse files Browse the repository at this point in the history
Seems to work: https://regex101.com/r/zSp6Ke/1
@ljacqu What do you think?
  • Loading branch information
sgdc3 committed Jul 2, 2017
1 parent b24dcfe commit cd4766e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/main/java/fr/xephi/authme/service/ValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,25 @@ public boolean fulfillsNameRestrictions(Player player) {

String ip = PlayerUtils.getPlayerIp(player);
String domain = player.getAddress().getHostName();
return restrictions.stream()
.anyMatch(restriction -> ip.equals(restriction) || domain.equalsIgnoreCase(restriction));
for(String restriction : restrictions) {
if(restriction.startsWith("regex:")) {
restriction = restriction.replace("regex:", "");
if(ip.matches(restriction)) {
return true;
}
if(domain.matches(restriction)) {
return true;
}

This comment has been minimized.

Copy link
@ljacqu

ljacqu Jul 3, 2017

Member

Looks good. These two if statements can be summed up together with an OR, that's the only comment I'd have.
Thanks for extending the tests!

} else {
if(ip.equals(restriction)) {
return true;
}
if(domain.equalsIgnoreCase(restriction)) {
return true;
}
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ public final class RestrictionSettings implements SettingsHolder {
@Comment({
"The restricted user feature will kick players listed below",
"if they don't match the defined IP address. Names are case-insensitive.",
"Ip addresses support regex expressions (regex:127\\.0\\.0\\..*)",
"Example:",
" AllowedRestrictedUser:",
" - playername;127.0.0.1"})
" - playername;127.0.0.1",
" - playername;regex:127\\.0\\.0\\..*"})
public static final Property<List<String>> RESTRICTED_USERS =
newLowercaseListProperty("settings.restrictions.AllowedRestrictedUser");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,27 @@ public void shouldCheckNameRestrictions() {
// given
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8"));
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*"));
validationService.reload();

Player bobby = mockPlayer("bobby", "127.0.0.4");
Player tamara = mockPlayer("taMARA", "8.8.8.8");
Player gabriel = mockPlayer("Gabriel", "93.23.44.65");
Player gabriel2 = mockPlayer("Gabriel", "93.23.33.34");
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");

// when
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
boolean isGabrielAdmitted = validationService.fulfillsNameRestrictions(gabriel);
boolean isGabriel2Admitted = validationService.fulfillsNameRestrictions(gabriel2);
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);

// then
assertThat(isBobbyAdmitted, equalTo(true));
assertThat(isTamaraAdmitted, equalTo(false));
assertThat(isGabrielAdmitted, equalTo(true));
assertThat(isGabriel2Admitted, equalTo(false));
assertThat(isNotRestrictedAdmitted, equalTo(true));
}

Expand Down

0 comments on commit cd4766e

Please sign in to comment.