Skip to content

Commit

Permalink
Merge pull request #5 from v4Guard/develop
Browse files Browse the repository at this point in the history
v4Guard Plugin v1.1.0
  • Loading branch information
samfces authored Aug 8, 2022
2 parents ad70f40 + 6b14d77 commit 351c75d
Show file tree
Hide file tree
Showing 17 changed files with 453 additions and 185 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<groupId>io.v4guard</groupId>
<artifactId>v4guard-plugin</artifactId>
<version>1.0.4</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<finalName>v4Guard-1.0.4</finalName>
<finalName>v4Guard-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/io/v4guard/plugin/bungee/BungeeCheckProcessor.java
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());
}
}
}
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);
}

}
1 change: 1 addition & 0 deletions src/main/java/io/v4guard/plugin/bungee/v4GuardBungee.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void onEnable(){
this.getProxy().getConsole().sendMessage("§e[v4guard-plugin] (Bungee) Enabling...");
try {
core = new v4GuardCore(v4GuardMode.BUNGEE);
core.getCheckManager().addProcessor(new BungeeCheckProcessor());
} catch (Exception e) {
this.getProxy().getConsole().sendMessage("§c[v4guard-plugin] (Bungee) Enabling... [ERROR]");
this.getProxy().getConsole().sendMessage("§cPlease check the console for more information and report this error.");
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/v4guard/plugin/core/check/CheckManager.java
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);
}
}
}
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);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

package io.v4guard.plugin.core.socket;

import io.v4guard.plugin.core.v4GuardCore;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.socket.listener.*;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;

Expand All @@ -18,7 +18,6 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

public class BackendConnector {
Expand All @@ -33,7 +32,7 @@ public class BackendConnector {

public BackendConnector() throws IOException, URISyntaxException {
HashMap<String, List<String>> headers = new HashMap<>();
headers.put("v4g-version", Collections.singletonList("1.0.0"));
headers.put("v4g-version", Collections.singletonList(v4GuardCore.pluginVersion));
headers.put("v4g-hostname", Collections.singletonList(getHostname()));
headers.put("v4g-name", Collections.singletonList(new File(System.getProperty("user.dir")).getName()));
headers.put("v4g-service", Collections.singletonList("minecraft"));
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/v4guard/plugin/core/utils/CheckStatus.java
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;
}
}
22 changes: 14 additions & 8 deletions src/main/java/io/v4guard/plugin/core/v4GuardCore.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package io.v4guard.plugin.core;

import io.v4guard.plugin.core.check.CheckManager;
import io.v4guard.plugin.core.mode.v4GuardMode;
import io.v4guard.plugin.core.socket.BackendConnector;
import io.v4guard.plugin.core.tasks.CompletableTaskManager;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class v4GuardCore {

private static v4GuardCore INSTANCE;
private final File folder;

private CompletableTaskManager completableTaskManager;
private BackendConnector backendConnector;
private static v4GuardCore INSTANCE;
private File folder;
private CheckManager checkManager;

public static final String pluginVersion = "1.1.0";

private boolean debug = false;
private v4GuardMode pluginMode = v4GuardMode.UNKNOWN;
Expand All @@ -44,6 +47,7 @@ public v4GuardCore(v4GuardMode mode) throws IOException, URISyntaxException {

this.completableTaskManager = new CompletableTaskManager();
this.backendConnector = new BackendConnector();
this.checkManager = new CheckManager();
new Thread(() -> {
try {
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-l", "-c", "apt-get --yes install ipset" });
Expand Down Expand Up @@ -74,6 +78,10 @@ public BackendConnector getBackendConnector() {
return backendConnector;
}

public CheckManager getCheckManager() {
return checkManager;
}

public static v4GuardCore getInstance() {
return INSTANCE;
}
Expand All @@ -94,6 +102,4 @@ public void initializeLogger(){
logger = Logger.getLogger("v4Guard");
logger.setUseParentHandlers(true);
}


}
Loading

0 comments on commit 351c75d

Please sign in to comment.