-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from v4Guard/develop
v4Guard Plugin v1.1.0
- Loading branch information
Showing
17 changed files
with
453 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/io/v4guard/plugin/bungee/BungeeCheckProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.v4guard.plugin.bungee; | ||
|
||
import io.v4guard.plugin.core.check.CheckProcessor; | ||
import io.v4guard.plugin.core.socket.BackendConnector; | ||
import io.v4guard.plugin.core.socket.SocketStatus; | ||
import io.v4guard.plugin.core.tasks.types.CompletableIPCheckTask; | ||
import io.v4guard.plugin.core.tasks.types.CompletableNameCheckTask; | ||
import io.v4guard.plugin.core.utils.CheckStatus; | ||
import io.v4guard.plugin.core.utils.StringUtils; | ||
import net.md_5.bungee.api.chat.TextComponent; | ||
import net.md_5.bungee.api.connection.ProxiedPlayer; | ||
import net.md_5.bungee.api.event.LoginEvent; | ||
import net.md_5.bungee.api.event.PostLoginEvent; | ||
import net.md_5.bungee.api.event.PreLoginEvent; | ||
import org.bson.Document; | ||
|
||
import java.util.List; | ||
|
||
public class BungeeCheckProcessor implements CheckProcessor { | ||
|
||
@Override | ||
public void onPreLogin(String username, Object event) { | ||
PreLoginEvent e = (PreLoginEvent) event; BackendConnector conn = v4GuardBungee.getCoreInstance().getBackendConnector(); | ||
if (!conn.getSocketStatus().equals(SocketStatus.AUTHENTICATED)) { | ||
return; | ||
} | ||
v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatusMap().remove(e.getConnection().getName()); | ||
Document kickMessages = (Document) v4GuardBungee.getCoreInstance().getBackendConnector().getSettings().get("messages"); | ||
final boolean wait = (boolean) v4GuardBungee.getCoreInstance().getBackendConnector().getSettings().get("waitResponse"); | ||
if (wait) { | ||
e.registerIntent(v4GuardBungee.getV4Guard()); | ||
} | ||
new CompletableNameCheckTask(e.getConnection().getName()) { | ||
@Override | ||
public void complete(boolean nameIsValid) { | ||
if(nameIsValid){ | ||
new CompletableIPCheckTask(e.getConnection().getAddress().getAddress().getHostAddress(), e.getConnection().getName(), e.getConnection().getVersion()){ | ||
@Override | ||
public void complete() { | ||
//Build kick message | ||
String kickReasonMessage = StringUtils.buildMultilineString((List<String>) kickMessages.get("kick")); | ||
Document data = (Document) this.getData().get("result"); | ||
kickReasonMessage = StringUtils.replacePlaceholders(kickReasonMessage, (Document) data.get("variables")); | ||
|
||
String username = this.getUsername(); | ||
|
||
if (wait) { | ||
if (this.isBlocked()) { | ||
e.setCancelled(true); | ||
e.setCancelReason(TextComponent.fromLegacyText(kickReasonMessage)); | ||
e.completeIntent(v4GuardBungee.getV4Guard()); | ||
} | ||
} else { | ||
if (this.isBlocked()) { | ||
v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatusMap().put(username, new CheckStatus(username, kickReasonMessage, true)); | ||
} | ||
} | ||
} | ||
}; | ||
} else { | ||
String username = this.getUsername(); | ||
String kickReasonMessage = StringUtils.buildMultilineString((List<String>) kickMessages.get("invalidUsername")); | ||
kickReasonMessage = StringUtils.replacePlaceholders(kickReasonMessage, new Document("username", username)); | ||
e.setCancelled(true); | ||
e.setCancelReason(TextComponent.fromLegacyText(kickReasonMessage)); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void onLogin(String username, Object event) { | ||
LoginEvent e = (LoginEvent) event; | ||
CheckStatus status = v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatus(e.getConnection().getName()); | ||
if(status != null && status.isBlocked()){ | ||
e.setCancelled(true); | ||
e.setCancelReason(TextComponent.fromLegacyText(status.getReason())); | ||
v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatusMap().remove(e.getConnection().getName()); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onPostLogin(String username, Object event) { | ||
PostLoginEvent e = (PostLoginEvent) event; | ||
ProxiedPlayer player = e.getPlayer(); | ||
if(player == null) return; | ||
CheckStatus status = v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatus(player.getName()); | ||
if(status != null && status.isBlocked()){ | ||
player.disconnect(TextComponent.fromLegacyText(status.getReason())); | ||
v4GuardBungee.getCoreInstance().getCheckManager().getCheckStatusMap().remove(player.getName()); | ||
} | ||
} | ||
} |
69 changes: 15 additions & 54 deletions
69
src/main/java/io/v4guard/plugin/bungee/listener/AntiVPNListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,29 @@ | ||
package io.v4guard.plugin.bungee.listener; | ||
|
||
import io.v4guard.plugin.bungee.v4GuardBungee; | ||
import io.v4guard.plugin.core.socket.BackendConnector; | ||
import io.v4guard.plugin.core.socket.SocketStatus; | ||
import io.v4guard.plugin.core.tasks.types.CompletableIPCheckTask; | ||
import io.v4guard.plugin.core.tasks.types.CompletableNameCheckTask; | ||
import io.v4guard.plugin.core.utils.StringUtils; | ||
import net.md_5.bungee.api.ProxyServer; | ||
import net.md_5.bungee.api.chat.TextComponent; | ||
import net.md_5.bungee.api.connection.ProxiedPlayer; | ||
import net.md_5.bungee.api.event.LoginEvent; | ||
import net.md_5.bungee.api.event.PostLoginEvent; | ||
import net.md_5.bungee.api.event.PreLoginEvent; | ||
import net.md_5.bungee.api.plugin.Listener; | ||
import net.md_5.bungee.event.EventHandler; | ||
import net.md_5.bungee.event.EventPriority; | ||
import org.bson.Document; | ||
|
||
import java.util.List; | ||
|
||
public class AntiVPNListener implements Listener { | ||
|
||
|
||
@EventHandler(priority = EventPriority.HIGHEST) | ||
public void onPreLogin(PreLoginEvent e) { | ||
BackendConnector conn = v4GuardBungee.getCoreInstance().getBackendConnector(); | ||
if (!conn.getSocketStatus().equals(SocketStatus.AUTHENTICATED)) { | ||
return; | ||
} | ||
Document kickMessages = (Document) v4GuardBungee.getCoreInstance().getBackendConnector().getSettings().get("messages"); | ||
final boolean wait = (boolean) v4GuardBungee.getCoreInstance().getBackendConnector().getSettings().get("waitResponse"); | ||
if (wait) { | ||
e.registerIntent(v4GuardBungee.getV4Guard()); | ||
} | ||
new CompletableNameCheckTask(e.getConnection().getName()) { | ||
@Override | ||
public void complete(boolean nameIsValid) { | ||
if(nameIsValid){ | ||
new CompletableIPCheckTask(e.getConnection().getAddress().getAddress().getHostAddress(), e.getConnection().getName(), e.getConnection().getVersion()){ | ||
@Override | ||
public void complete() { | ||
if (this.isBlocked()) { | ||
String username = this.getUsername(); | ||
String kickReasonMessage = StringUtils.buildMultilineString((List<String>) kickMessages.get("kick")); | ||
Document data = (Document) this.getData().get("result"); | ||
kickReasonMessage = StringUtils.replacePlaceholders(kickReasonMessage, (Document) data.get("variables")); | ||
ProxiedPlayer pp = ProxyServer.getInstance().getPlayer(username); | ||
if(pp != null) { | ||
pp.disconnect(TextComponent.fromLegacyText(kickReasonMessage)); | ||
} else { | ||
e.setCancelled(true); | ||
e.setCancelReason(TextComponent.fromLegacyText(kickReasonMessage)); | ||
} | ||
} | ||
if (wait) { | ||
e.completeIntent(v4GuardBungee.getV4Guard()); | ||
} | ||
} | ||
}; | ||
} else { | ||
String username = this.getUsername(); | ||
String kickReasonMessage = StringUtils.buildMultilineString((List<String>) kickMessages.get("invalidUsername")); | ||
kickReasonMessage = StringUtils.replacePlaceholders(kickReasonMessage, new Document("username", username)); | ||
e.setCancelled(true); | ||
e.setCancelReason(TextComponent.fromLegacyText(kickReasonMessage)); | ||
} | ||
} | ||
}; | ||
v4GuardBungee.getCoreInstance().getCheckManager().runPreLoginCheck(e.getConnection().getName(), e); | ||
} | ||
|
||
@EventHandler | ||
public void onLogin(LoginEvent e){ | ||
v4GuardBungee.getCoreInstance().getCheckManager().runLoginCheck(e.getConnection().getName(), e); | ||
} | ||
|
||
@EventHandler | ||
public void onPostLogin(PostLoginEvent e){ | ||
v4GuardBungee.getCoreInstance().getCheckManager().runPostLoginCheck(e.getPlayer().getName(), e); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/io/v4guard/plugin/core/check/CheckManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.v4guard.plugin.core.check; | ||
|
||
import io.v4guard.plugin.core.utils.CheckStatus; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class CheckManager { | ||
|
||
private final HashMap<String, CheckStatus> checkStatusMap; | ||
private final List<CheckProcessor> processors; | ||
|
||
public CheckManager() { | ||
this.checkStatusMap = new HashMap<>(); | ||
this.processors = new ArrayList<>(); | ||
} | ||
|
||
public CheckProcessor getProcessorByClass(Class clazz) { | ||
for (CheckProcessor processor : processors) { | ||
if (processor.getClass().equals(clazz)) { | ||
return processor; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public void addProcessor(CheckProcessor processor) { | ||
this.processors.add(processor); | ||
} | ||
|
||
public List<CheckProcessor> getProcessors() { | ||
return processors; | ||
} | ||
|
||
public HashMap<String, CheckStatus> getCheckStatusMap() { | ||
return checkStatusMap; | ||
} | ||
|
||
public CheckStatus getCheckStatus(String username) { | ||
return checkStatusMap.get(username); | ||
} | ||
|
||
public void runPreLoginCheck(String name, Object e) { | ||
for (CheckProcessor processor : processors) { | ||
processor.onPreLogin(name, e); | ||
} | ||
} | ||
|
||
public void runLoginCheck(String name, Object e) { | ||
for (CheckProcessor processor : processors) { | ||
processor.onLogin(name, e); | ||
} | ||
} | ||
|
||
public void runPostLoginCheck(String name, Object e) { | ||
for (CheckProcessor processor : processors) { | ||
processor.onPostLogin(name, e); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/io/v4guard/plugin/core/check/CheckProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.v4guard.plugin.core.check; | ||
|
||
public interface CheckProcessor<K> { | ||
|
||
void onPreLogin(String username, K event); | ||
void onLogin(String username, K event); | ||
void onPostLogin(String username, K event); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/io/v4guard/plugin/core/utils/CheckStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.v4guard.plugin.core.utils; | ||
|
||
public class CheckStatus { | ||
|
||
private final String name; | ||
private final String reason; | ||
private boolean blocked; | ||
|
||
public CheckStatus(String name, String reason, boolean blocked) { | ||
this.name = name; | ||
this.reason = reason; | ||
this.blocked = blocked; | ||
} | ||
|
||
public boolean isBlocked() { | ||
return blocked; | ||
} | ||
|
||
public void setBlocked(boolean blocked) { | ||
this.blocked = blocked; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getReason() { | ||
return reason; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.