Add regex support to restricted users ip addresses #1227
Seems to work: https://regex101.com/r/zSp6Ke/1 @ljacqu What do you think?
This commit is contained in:
parent
b24dcfed3c
commit
cd4766e0a1
@ -157,8 +157,25 @@ public class ValidationService implements Reloadable {
|
|||||||
|
|
||||||
String ip = PlayerUtils.getPlayerIp(player);
|
String ip = PlayerUtils.getPlayerIp(player);
|
||||||
String domain = player.getAddress().getHostName();
|
String domain = player.getAddress().getHostName();
|
||||||
return restrictions.stream()
|
for(String restriction : restrictions) {
|
||||||
.anyMatch(restriction -> ip.equals(restriction) || domain.equalsIgnoreCase(restriction));
|
if(restriction.startsWith("regex:")) {
|
||||||
|
restriction = restriction.replace("regex:", "");
|
||||||
|
if(ip.matches(restriction)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(domain.matches(restriction)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(ip.equals(restriction)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(domain.equalsIgnoreCase(restriction)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -78,9 +78,11 @@ public final class RestrictionSettings implements SettingsHolder {
|
|||||||
@Comment({
|
@Comment({
|
||||||
"The restricted user feature will kick players listed below",
|
"The restricted user feature will kick players listed below",
|
||||||
"if they don't match the defined IP address. Names are case-insensitive.",
|
"if they don't match the defined IP address. Names are case-insensitive.",
|
||||||
|
"Ip addresses support regex expressions (regex:127\\.0\\.0\\..*)",
|
||||||
"Example:",
|
"Example:",
|
||||||
" AllowedRestrictedUser:",
|
" AllowedRestrictedUser:",
|
||||||
" - playername;127.0.0.1"})
|
" - playername;127.0.0.1",
|
||||||
|
" - playername;regex:127\\.0\\.0\\..*"})
|
||||||
public static final Property<List<String>> RESTRICTED_USERS =
|
public static final Property<List<String>> RESTRICTED_USERS =
|
||||||
newLowercaseListProperty("settings.restrictions.AllowedRestrictedUser");
|
newLowercaseListProperty("settings.restrictions.AllowedRestrictedUser");
|
||||||
|
|
||||||
|
|||||||
@ -350,21 +350,27 @@ public class ValidationServiceTest {
|
|||||||
// given
|
// given
|
||||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
||||||
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
|
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();
|
validationService.reload();
|
||||||
|
|
||||||
Player bobby = mockPlayer("bobby", "127.0.0.4");
|
Player bobby = mockPlayer("bobby", "127.0.0.4");
|
||||||
Player tamara = mockPlayer("taMARA", "8.8.8.8");
|
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");
|
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
|
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
|
||||||
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
|
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
|
||||||
|
boolean isGabrielAdmitted = validationService.fulfillsNameRestrictions(gabriel);
|
||||||
|
boolean isGabriel2Admitted = validationService.fulfillsNameRestrictions(gabriel2);
|
||||||
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
|
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(isBobbyAdmitted, equalTo(true));
|
assertThat(isBobbyAdmitted, equalTo(true));
|
||||||
assertThat(isTamaraAdmitted, equalTo(false));
|
assertThat(isTamaraAdmitted, equalTo(false));
|
||||||
|
assertThat(isGabrielAdmitted, equalTo(true));
|
||||||
|
assertThat(isGabriel2Admitted, equalTo(false));
|
||||||
assertThat(isNotRestrictedAdmitted, equalTo(true));
|
assertThat(isNotRestrictedAdmitted, equalTo(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user