Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Fixed NameSimilarityCheck and AutoWhitelist duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
xxneox committed May 30, 2021
1 parent fc7e9b7 commit f69d78d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Check(EpicGuard epicGuard) {
* @param expression The expression
* @return The return value is based on the check's behaviour. True means positive detection, false means negative.
*/
public boolean assertCheck(CheckMode mode, boolean expression) {
public boolean evaluate(CheckMode mode, boolean expression) {
if (mode == CheckMode.ALWAYS || mode == CheckMode.ATTACK && this.epicGuard.attackManager().isAttack()) {
return expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().accountLimitCheck().checkMode());
List<String> accounts = this.epicGuard.storageManager().accounts(user);

return this.assertCheck(mode, !accounts.contains(user.nickname()) && accounts.size() >= this.epicGuard.config().accountLimitCheck().accountLimit());
return this.evaluate(mode, !accounts.contains(user.nickname()) && accounts.size() >= this.epicGuard.config().accountLimitCheck().accountLimit());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public GeographicalCheck(EpicGuard epicGuard) {
@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().geographical().checkMode());
return this.assertCheck(mode, this.isRestricted(user.address()));
return this.evaluate(mode, this.isRestricted(user.address()));
}

private boolean isRestricted(String address) {
Expand All @@ -45,10 +45,10 @@ private boolean isRestricted(String address) {
return true;
}

if (this.epicGuard.config().geographical().checkType().equals("WHITELIST")) {
return !this.epicGuard.config().geographical().countries().contains(country);
} else {
if (this.epicGuard.config().geographical().isBlacklist()) {
return this.epicGuard.config().geographical().countries().contains(country);
} else {
return !this.epicGuard.config().geographical().countries().contains(country);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.EvictingQueue;
import me.xneox.epicguard.core.EpicGuard;
import me.xneox.epicguard.core.check.Check;
import me.xneox.epicguard.core.check.CheckMode;
import me.xneox.epicguard.core.user.PendingUser;
import org.apache.commons.text.similarity.LevenshteinDistance;

Expand All @@ -21,11 +22,20 @@ public NameSimilarityCheck(EpicGuard epicGuard) {

@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().nameSimilarityCheck().checkMode());

for (String nick : this.nameHistory) {
if (this.distanceAlgorithm.apply(nick, user.nickname()) <= this.epicGuard.config().nameSimilarityCheck().distance()) {
return true;
if (nick.equals(user.nickname())) {
return false; // ignore identical nickname.
}

int distance = this.distanceAlgorithm.apply(nick, user.nickname());
if (distance <= this.epicGuard.config().nameSimilarityCheck().distance()) {
return this.evaluate(mode, true);
}
}

this.nameHistory.add(user.nickname());
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public NicknameCheck(EpicGuard epicGuard) {
@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().nicknameCheck().checkMode());
return this.assertCheck(mode, user.nickname().matches(this.epicGuard.config().nicknameCheck().expression()));
return this.evaluate(mode, user.nickname().matches(this.epicGuard.config().nicknameCheck().expression()));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ProxyCheck(EpicGuard epicGuard) {
@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().proxyCheck().checkMode());
return this.assertCheck(mode, this.epicGuard.proxyManager().isProxy(user.address()));
return this.evaluate(mode, this.epicGuard.proxyManager().isProxy(user.address()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ReconnectCheck(EpicGuard epicGuard) {
@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().misc().reconnectCheckMode());
return this.assertCheck(mode, needsReconnect(user));
return this.evaluate(mode, needsReconnect(user));
}

private boolean needsReconnect(PendingUser pendingUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ServerListCheck(EpicGuard epicGuard) {
@Override
public boolean handle(@Nonnull PendingUser user) {
CheckMode mode = CheckMode.valueOf(this.epicGuard.config().misc().serverListCheckMode());
return this.assertCheck(mode, !this.epicGuard.storageManager().pingCache().contains(user.address()));
return this.evaluate(mode, !this.epicGuard.storageManager().pingCache().contains(user.address()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,45 @@
@ConfigSerializable
public class PluginConfiguration {

// Config sections
@Comment("GeographicalCheck will filter countries/cities your players can connect from.")
private final Geographical geographical = new Geographical();

@Comment("Detect users who are connecting using proxies or VPNs.")
private final ProxyCheck proxyCheck = new ProxyCheck();

@Comment("This check will limit how many accounts can be registered from single IP address")
private final AccountLimitCheck accountLimitCheck = new AccountLimitCheck();

@Comment("Every vanilla client sends the Settings packet shortly after joining.\n" +
"Some bots doesn't do this, and this check will try to detect that.")
private final SettingsCheck settingsCheck = new SettingsCheck();

@Comment("Nickname-check will block players if their nickname matches\n" +
"the regex expression set below.")
private final NicknameCheck nicknameCheck = new NicknameCheck();

@Comment("NameSimilarityCheck will detect similar nicknames of the connecting users\n" +
"(!) Experimental! https://neox.gitbook.io/epicguard-wiki/configuring/name-similarity-check")
private final NameSimilarityCheck nameSimilarityCheck = new NameSimilarityCheck();

private final ConsoleFilter consoleFilter = new ConsoleFilter();
@Comment("If a player is online for long enough (see option below)\n" +
"He will be added to the whitelist, and be exempt from every future detections")
private final AutoWhitelist autoWhitelist = new AutoWhitelist();

private final ConsoleFilter consoleFilter = new ConsoleFilter();
private final Misc misc = new Misc();

@ConfigSerializable
public static final class Geographical {
@Comment("Country check will filter countries/cities your players can connect from.\n" +
"NEVER - check is disabled.\n" +
@Comment("NEVER - check is disabled.\n" +
"ATTACK - check will be performed only during bot-attack.\n" +
"ALWAYS - check will be always performed.")
private final String checkMode = "NEVER";

@Comment("This will define if the 'countries' list should be a blacklist or a whitelist.\n" +
"BLACKLIST - countries below are blocked\n" +
"WHITELIST - only countries below are allowed")
private final String checkType = "BLACKLIST";
"true - configured countries are blocked\n" +
"false - only configured countries are allowed")
private final boolean isBlacklist = false;

@Comment("List of country codes: https://dev.maxmind.com/geoip/legacy/codes/iso3166/")
private final List<String> countries = Arrays.asList("US", "DE");
Expand All @@ -61,8 +76,8 @@ public String checkMode() {
return this.checkMode;
}

public String checkType() {
return this.checkType;
public boolean isBlacklist() {
return this.isBlacklist;
}

public List<String> countries() {
Expand All @@ -76,8 +91,7 @@ public List<String> cityBlacklist() {

@ConfigSerializable
public static final class ProxyCheck {
@Comment("Detect users who are connecting using proxies or VPNs.\n" +
"NEVER - check is disabled.\n" +
@Comment("NEVER - check is disabled.\n" +
"ATTACK - check will be performed only during bot-attack.\n" +
"ALWAYS - check will be always performed.")
private final String checkMode = "ALWAYS";
Expand Down Expand Up @@ -108,8 +122,7 @@ public int cacheDuration() {

@ConfigSerializable
public static final class AccountLimitCheck {
@Comment("This check will limit how many accounts can be registered from single IP address\n" +
"NEVER - check is disabled.\n" +
@Comment("NEVER - check is disabled.\n" +
"ATTACK - check will be performed only during bot-attack.\n" +
"ALWAYS - check will be always performed.")
private final String checkMode = "ALWAYS";
Expand All @@ -128,8 +141,7 @@ public int accountLimit() {

@ConfigSerializable
public static final class SettingsCheck {
@Comment("Every vanilla client sends the Settings packet shortly after joining.\n" +
"Some bots doesn't do this, and this check will try to detect that.")
@Comment("Enable or disable this check.")
private final boolean enabled = true;

@Comment("Delay in seconds after which we check if the player has already sent this packet.\n" +
Expand All @@ -147,9 +159,7 @@ public int delay() {

@ConfigSerializable
public static final class NicknameCheck {
@Comment("Nickname-check will block players if their nickname matches\n"
+ "the regex expression set below.\n" +
"NEVER - check is disabled.\n" +
@Comment("NEVER - check is disabled.\n" +
"ATTACK - check will be performed only during bot-attack.\n" +
"ALWAYS - check will be always performed.")
private final String checkMode = "ALWAYS";
Expand All @@ -169,22 +179,21 @@ public String expression() {

@ConfigSerializable
public static final class NameSimilarityCheck {
@Comment("NameSimilarityCheck will detect similar nicknames\n"
+ "of the connecting users.\n" +
"NEVER - check is disabled.\n" +
@Comment("NEVER - check is disabled.\n" +
"ATTACK - check will be performed only during bot-attack.\n" +
"ALWAYS - check will be always performed.")
private final String checkMode = "ATTACK";
private final String checkMode = "NEVER";

@Comment("How many nicknames should be keep in the history?\n" +
"When an user is connecting to the server, his nickname will be added to the history.\n" +
"Then the nickname will be compared with other nicknames stored in the history.\n" +
"(!) Requires restart to apply.")
private final int historySize = 10;
private final int historySize = 5;

@Comment("The lower the distance, the similar the names.\n" +
"If the distance detected is lower or the same as\n" +
"configured below, it will be considered as a positive detection.")
"If the distance detected is lower or same as configured here,\n" +
"it will be considered as a positive detection.\n" +
"Values below 1 are ignored, as it means identical name.")
private final int distance = 1;

public String checkMode() {
Expand Down Expand Up @@ -229,9 +238,7 @@ public List<String> filterMessages() {

@ConfigSerializable
public static final class AutoWhitelist {
@Comment("If a player is online for long enough (see option below)\n" +
"He will be added to the whitelist, and be exempt from every future detections" +
"ADDRESS - Only whitelist the user's IP address.\n" +
@Comment("ADDRESS - Only whitelist the user's IP address.\n" +
"NICKNAME - Only whitelist the user's nickname.\n" +
"MIXED - Whitelist the user's IP address AND nickname.\n" +
"DISABLED - Disable this feature")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public void handle(@Nonnull UUID uuid, @Nonnull String address, @Nonnull String
if (mode != WhitelistMode.DISABLED) {
this.epicGuard.platform().runTaskLater(() -> {
if (user != null) {
if (mode == WhitelistMode.MIXED || mode == WhitelistMode.ADDRESS) {
if ((mode == WhitelistMode.MIXED || mode == WhitelistMode.ADDRESS) && !this.epicGuard.storageManager().isWhitelisted(address)) {
this.epicGuard.storageManager().whitelistPut(address);
}

if (mode == WhitelistMode.MIXED || mode == WhitelistMode.NICKNAME) {
if ((mode == WhitelistMode.MIXED || mode == WhitelistMode.NICKNAME) && !this.epicGuard.storageManager().isWhitelisted(nickname)) {
this.epicGuard.storageManager().whitelistPut(nickname);
}
}
Expand Down

0 comments on commit f69d78d

Please sign in to comment.