Skip to content

Commit

Permalink
Merge pull request #14 from v4Guard/develop
Browse files Browse the repository at this point in the history
v4Guard Plugin v1.1.3b
  • Loading branch information
samfces authored Oct 14, 2022
2 parents c8379ea + 4e1a39e commit 17ed9a0
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.v4guard</groupId>
<artifactId>v4guard-plugin</artifactId>
<version>1.1.3</version>
<version>1.1.3b</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.v4guard.plugin.bungee.accounts;

import io.v4guard.plugin.core.accounts.auth.Authentication;
import io.v4guard.plugin.core.accounts.messaging.MessageReceiver;
import io.v4guard.plugin.core.v4GuardCore;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
import org.bson.Document;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class BungeeMessageReceiver extends MessageReceiver implements Listener {

public BungeeMessageReceiver(Plugin plugin) {
plugin.getProxy().getPluginManager().registerListener(plugin, this);
if(!MessageReceiver.CHANNEL.equals("BungeeCord")) {
plugin.getProxy().registerChannel(MessageReceiver.CHANNEL);
}
for(String s : ProxyServer.getInstance().getChannels()) {
System.out.println(s);
}
}

@EventHandler
public void onMessage(PluginMessageEvent e){
if (!e.getTag().equals(MessageReceiver.CHANNEL)) {
return;
}
DataInputStream in = new DataInputStream(new ByteArrayInputStream(e.getData()));
try {
String data = in.readUTF();
Document doc = Document.parse(data);
Authentication auth = Authentication.deserialize(doc);
v4GuardCore.getInstance().getAccountShieldManager().sendSocketMessage(auth);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/io/v4guard/plugin/bungee/v4GuardBungee.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.v4guard.plugin.bungee;

import io.v4guard.plugin.bungee.accounts.BungeeMessageReceiver;
import io.v4guard.plugin.bungee.listener.AntiVPNListener;
import io.v4guard.plugin.bungee.messager.Messager;
import io.v4guard.plugin.core.accounts.AccountShieldManager;
import io.v4guard.plugin.core.mode.v4GuardMode;
import io.v4guard.plugin.core.v4GuardCore;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -30,6 +32,7 @@ public void onEnable(){
try {
core = new v4GuardCore(v4GuardMode.BUNGEE);
core.getCheckManager().addProcessor(new BungeeCheckProcessor());
core.setAccountShieldManager(new AccountShieldManager(new BungeeMessageReceiver(this)));
}
catch (Exception e) {
this.getProxy().getConsole().sendMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.v4guard.plugin.core.accounts;

import io.v4guard.plugin.core.accounts.auth.Authentication;
import io.v4guard.plugin.core.accounts.messaging.MessageReceiver;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;

public class AccountShieldManager {

private MessageReceiver receiver;

public AccountShieldManager(MessageReceiver receiver) {
this.receiver = receiver;
}

public MessageReceiver getReceiver() {
return receiver;
}

public void sendSocketMessage(Authentication auth){

Document shieldSettings = (Document) v4GuardCore.getInstance().getBackendConnector().getSettings().getOrDefault("addons", new Document());
boolean shieldEnabled = (boolean) shieldSettings.getOrDefault("accshield", false);
if(!shieldEnabled) return;

String location = v4GuardCore.getInstance().getCheckManager().getProcessors().get(0).getPlayerServer(auth.getUsername());
if(location != null){
location = "not-connected";
}
Document finalDocument = new Document("username", auth.getUsername())
.append("type", auth.getAuthType().toString())
.append("location", location);
v4GuardCore.getInstance().getBackendConnector().getSocket().emit("accshield:login", finalDocument.toJson());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.v4guard.plugin.core.accounts.auth;

public enum AuthType {

LOGIN, REGISTER, UNKNOWN;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.v4guard.plugin.core.accounts.auth;

import org.bson.Document;

public class Authentication {

private final String username;
private final AuthType authType;

public Authentication(String username, AuthType authType) {
this.username = username;
this.authType = authType;
}

public String getUsername() {
return username;
}

public AuthType getAuthType() {
return authType;
}

public Document serialize(){
return new Document("username", username)
.append("authType", authType.toString());
}

public static Authentication deserialize(Document doc){
return new Authentication(doc.getString("username"), AuthType.valueOf(doc.getString("authType")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.v4guard.plugin.core.accounts.messaging;

public abstract class MessageReceiver {

// This is a bit of a "dirty" solution.
// If we place a custom channel name the message does not reach BungeeCord/Velocity.
// We will temporarily use the BungeeCord channel until we find a solution to the problem.
// protected static final String CHANNEL = "v4guard:accountshield";

protected static final String CHANNEL = "BungeeCord";

}
12 changes: 11 additions & 1 deletion src/main/java/io/v4guard/plugin/core/v4GuardCore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.v4guard.plugin.core;

import io.v4guard.plugin.core.accounts.AccountShieldManager;
import io.v4guard.plugin.core.check.CheckManager;
import io.v4guard.plugin.core.mode.v4GuardMode;
import io.v4guard.plugin.core.socket.BackendConnector;
Expand All @@ -19,8 +20,9 @@ public class v4GuardCore {
private CompletableTaskManager completableTaskManager;
private BackendConnector backendConnector;
private CheckManager checkManager;
private AccountShieldManager accountShieldManager;

public static final String pluginVersion = "1.1.3";
public static final String pluginVersion = "1.1.3b";

private boolean debug = false;
private v4GuardMode pluginMode;
Expand Down Expand Up @@ -87,6 +89,14 @@ public boolean isDebugEnabled() {
return debug;
}

public AccountShieldManager getAccountShieldManager() {
return accountShieldManager;
}

public void setAccountShieldManager(AccountShieldManager accountShieldManager) {
this.accountShieldManager = accountShieldManager;
}

public void initializeLogger(){
logger = Logger.getLogger("v4Guard");
logger.setUseParentHandlers(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void run() {
}.runTask(v4GuardSpigot.getV4Guard());
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.v4guard.plugin.velocity.accounts;

import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import io.v4guard.plugin.core.accounts.auth.Authentication;
import io.v4guard.plugin.core.accounts.messaging.MessageReceiver;
import io.v4guard.plugin.core.v4GuardCore;
import io.v4guard.plugin.velocity.v4GuardVelocity;
import net.md_5.bungee.api.plugin.Listener;
import org.bson.Document;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class VelocityMessageReceiver extends MessageReceiver implements Listener {

public VelocityMessageReceiver(v4GuardVelocity plugin) {
plugin.getServer().getEventManager().register(plugin, this);
plugin.getServer().getChannelRegistrar().register(new LegacyChannelIdentifier(MessageReceiver.CHANNEL));
plugin.getServer().getChannelRegistrar().register(MinecraftChannelIdentifier.from(MessageReceiver.CHANNEL));
}

@Subscribe(order = PostOrder.FIRST)
public void onMessage(PluginMessageEvent e){
if (!e.getIdentifier().getId().equals(MessageReceiver.CHANNEL)) {
return;
}
DataInputStream in = new DataInputStream(new ByteArrayInputStream(e.getData()));
try {
String data = in.readUTF();
Document doc = Document.parse(data);
Authentication auth = Authentication.deserialize(doc);
v4GuardCore.getInstance().getAccountShieldManager().sendSocketMessage(auth);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

}
5 changes: 3 additions & 2 deletions src/main/java/io/v4guard/plugin/velocity/v4GuardVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer;
import io.v4guard.plugin.core.accounts.AccountShieldManager;
import io.v4guard.plugin.core.mode.v4GuardMode;
import io.v4guard.plugin.core.v4GuardCore;
import io.v4guard.plugin.velocity.accounts.VelocityMessageReceiver;
import io.v4guard.plugin.velocity.listener.AntiVPNListener;
import io.v4guard.plugin.velocity.messager.Messager;
import net.kyori.adventure.text.Component;
Expand All @@ -18,8 +20,6 @@
url = "https://v4guard.io", description = "v4Guard Plugin for Minecraft Servers", authors = {"DigitalSynware"})
public class v4GuardVelocity {



private static v4GuardCore core;

private static v4GuardVelocity v4GuardVelocity;
Expand Down Expand Up @@ -47,6 +47,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
try {
core = new v4GuardCore(v4GuardMode.VELOCITY);
core.getCheckManager().addProcessor(new VelocityCheckProcessor());
core.setAccountShieldManager(new AccountShieldManager(new VelocityMessageReceiver(this)));
} catch (Exception e) {
server.getConsoleCommandSource().sendMessage(Component.text("§c[v4guard-plugin] (Velocity) Enabling... [ERROR]"));
server.getConsoleCommandSource().sendMessage(Component.text("§cPlease check the console for more information and report this error."));
Expand Down

0 comments on commit 17ed9a0

Please sign in to comment.