Skip to content

Commit

Permalink
Merge pull request #10 from v4Guard/develop
Browse files Browse the repository at this point in the history
v4Guard Plugin v1.1.2
  • Loading branch information
samfces authored Aug 30, 2022
2 parents 61815c4 + e4386f7 commit 041ba00
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 16 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.1c</version>
<version>1.1.2</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ public boolean onExpire(VPNCheck check) {
}
return false;
}

@Override
public void kickPlayer(String username, String reason){
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(username);
if (player != null) {
player.disconnect(TextComponent.fromLegacyText(reason));
}
}
}
16 changes: 15 additions & 1 deletion src/main/java/io/v4guard/plugin/bungee/messager/Messager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.ArrayList;
import java.util.List;

public class Messager {

public void broadcastWithPermission(String message, String permission){
public List<String> broadcastWithPermission(String message, String permission){
List<String> broadcastedPlayers = new ArrayList<>();
if(permission.equals("*")){
for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){
player.sendMessage(new ComponentBuilder(message).create());
broadcastedPlayers.add(player.getName());
}
} else{
for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){
if(player.hasPermission(permission)){
player.sendMessage(new ComponentBuilder(message).create());
broadcastedPlayers.add(player.getName());
}
}
}
return broadcastedPlayers;
}

public void sendToPlayer(String message, String username){
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(username);
if(player != null){
player.sendMessage(new ComponentBuilder(message).create());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface CheckProcessor<K> {
void onPreLogin(String username, K event);
void onPostLogin(String username, K event);
boolean onExpire(VPNCheck VPNCheck);
void kickPlayer(String username, String reason);
}
12 changes: 12 additions & 0 deletions src/main/java/io/v4guard/plugin/core/socket/BackendConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import java.util.stream.Stream;

public class BackendConnector {

private Socket socket;
private IO.Options options;
private SocketStatus socketStatus = SocketStatus.DISCONNECTED;
private final Runtime runtime = Runtime.getRuntime();
private HashMap<String, Object> settings;
private boolean reconnected = false;
private String authCode;

private HashMap<String, Emitter.Listener> registeredListeners = new HashMap<>();

Expand Down Expand Up @@ -76,9 +78,11 @@ public void handleEvents() {
registerListener("auth", new AuthListener(this));
registerListener("settings", new SettingsListener(this));
registerListener("setting", new SettingListener(this));
registerListener("console", new ConsoleListener());
//registerListener("ipset", new IPSetListener(this));
registerListener("check", new CheckListener());
registerListener("message", new MessageListener(this));
registerListener("kick", new KickListener());
}

public void registerListener(String event, Emitter.Listener listener){
Expand Down Expand Up @@ -129,6 +133,14 @@ public void setReconnected(boolean reconnected) {
this.reconnected = reconnected;
}

public String getAuthCode() {
return authCode;
}

public void setAuthCode(String authCode) {
this.authCode = authCode;
}

public Runtime getRuntime() {
return runtime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.v4guard.plugin.core.socket.listener;

import io.v4guard.plugin.core.v4GuardCore;
import io.v4guard.plugin.core.socket.BackendConnector;
import io.v4guard.plugin.core.socket.SocketStatus;
import io.socket.client.IO;
import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.socket.BackendConnector;
import io.v4guard.plugin.core.socket.SocketStatus;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;

import java.io.File;
Expand All @@ -14,8 +14,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AuthListener implements Emitter.Listener {

Expand All @@ -28,10 +28,23 @@ public AuthListener(BackendConnector backendConnector) {
@Override
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
backendConnector.setAuthCode(doc.getString("code"));
try {
backendConnector.setSocketStatus(SocketStatus.valueOf(doc.getString("status")));
if (backendConnector.getSocketStatus().equals(SocketStatus.NOT_AUTHENTICATED)) {
v4GuardCore.getInstance().getLogger().log(Level.WARNING,"This instance is not connected with your account. Connect it using this link: https://dashboard.v4guard.io/link/" + doc.getString("code"));
new Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
if(backendConnector.getSocketStatus().equals(SocketStatus.NOT_AUTHENTICATED)){
v4GuardCore.getInstance().getLogger().log(
Level.WARNING,
"This instance is not connected with your account. Connect it using this link: https://dashboard.v4guard.io/link/"
+ backendConnector.getAuthCode());
} else {
cancel();
}
}
}, 1000L, 60L*1000L);
} else if (backendConnector.getSocketStatus().equals(SocketStatus.PRE_AUTHENTICATED)) {
File secrets;
if (!v4GuardCore.getInstance().getDataFolder().exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.v4guard.plugin.core.socket.listener;

import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;
import java.util.logging.Level;

public class ConsoleListener implements Emitter.Listener {

@Override
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
String message = (String) doc.getOrDefault("message", null);
Level lvl = Level.parse((String) doc.getOrDefault("level", "INFO"));
if(message == null) return;
v4GuardCore.getInstance().getLogger().log(lvl, message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.v4guard.plugin.core.socket.listener;

import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.check.CheckProcessor;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;

import java.util.List;

public class KickListener implements Emitter.Listener {

@Override
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
String username = doc.getString("username");
List<String> reason = doc.get("message", List.class);
String formattedReason = "", sep = "";
for (String s : reason) {
formattedReason += sep + s;
sep = "\n";
}
for(CheckProcessor cp : v4GuardCore.getInstance().getCheckManager().getProcessors()){
cp.kickPlayer(username, formattedReason);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.v4guard.plugin.core.socket.listener;

import io.socket.emitter.Emitter;
import io.v4guard.plugin.bungee.v4GuardBungee;
import io.v4guard.plugin.core.socket.BackendConnector;
import io.v4guard.plugin.core.v4GuardCore;
import io.v4guard.plugin.spigot.v4GuardSpigot;
import io.socket.emitter.Emitter;
import io.v4guard.plugin.velocity.v4GuardVelocity;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

public class MessageListener implements Emitter.Listener {

BackendConnector backendConnector;
Expand All @@ -19,13 +23,34 @@ public MessageListener(BackendConnector backendConnector) {
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
String permission = doc.getOrDefault("permission", "*").toString();
List<String> players = (List<String>) doc.getOrDefault("players", new ArrayList<>());
String message = (String) doc.getOrDefault("message", "Disconnected");
switch (v4GuardCore.getInstance().getPluginMode()){
case BUNGEE:
v4GuardBungee.getV4Guard().getMessager().broadcastWithPermission(doc.getString("message"), permission);
case BUNGEE: {
List<String> broadcasted = v4GuardBungee.getV4Guard().getMessager().broadcastWithPermission(message, permission);
players.removeAll(broadcasted);
for(String player : players){
v4GuardBungee.getV4Guard().getMessager().sendToPlayer(message, player);
}
break;
case SPIGOT:
v4GuardSpigot.getV4Guard().getMessager().broadcastWithPermission(doc.getString("message"), permission);
}
case SPIGOT: {
List<String> broadcasted = v4GuardSpigot.getV4Guard().getMessager().broadcastWithPermission(message, permission);
players.removeAll(broadcasted);
for(String player : players){
v4GuardSpigot.getV4Guard().getMessager().sendToPlayer(message, player);
}
break;
}

case VELOCITY: {
List<String> broadcasted = v4GuardVelocity.getV4Guard().getMessager().broadcastWithPermission(message, permission);
players.removeAll(broadcasted);
for(String player : players){
v4GuardVelocity.getV4Guard().getMessager().sendToPlayer(message, player);
}
break;
}

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/v4guard/plugin/core/v4GuardCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class v4GuardCore {
private BackendConnector backendConnector;
private CheckManager checkManager;

public static final String pluginVersion = "1.1.1c";
public static final String pluginVersion = "1.1.2";

private boolean debug = false;
private v4GuardMode pluginMode = v4GuardMode.UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public void run() {
}
return false;
}

@Override
public void kickPlayer(String username, String reason){
Player player = Bukkit.getPlayer(username);
if (player != null) {
player.kickPlayer(reason);
}
}
}
16 changes: 15 additions & 1 deletion src/main/java/io/v4guard/plugin/spigot/messager/Messager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class Messager {

public static void broadcastWithPermission(String message, String permission){
public List<String> broadcastWithPermission(String message, String permission){
List<String> broadcastedPlayers = new ArrayList<>();
if(permission.equals("*")){
for(Player player : Bukkit.getServer().getOnlinePlayers()){
player.sendMessage(message);
broadcastedPlayers.add(player.getName());
}
} else{
for(Player player : Bukkit.getServer().getOnlinePlayers()){
if(player.hasPermission(permission)){
player.sendMessage(message);
broadcastedPlayers.add(player.getName());
}
}
}
return broadcastedPlayers;
}

public void sendToPlayer(String message, String username){
Player player = Bukkit.getServer().getPlayer(username);
if(player != null){
player.sendMessage(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public boolean onExpire(VPNCheck status) {
return false;
}

@Override
public void kickPlayer(String username, String reason){
Optional<Player> pp = v4GuardVelocity.getV4Guard().getServer().getPlayer(username);
if(pp.isPresent()) {
pp.get().disconnect(Component.text(reason));
}
}

private void doChecks(PreLoginEvent e, Continuation continuation) {
new CompletableNameCheckTask(e.getUsername()) {
@Override
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/io/v4guard/plugin/velocity/messager/Messager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@


import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import io.v4guard.plugin.velocity.v4GuardVelocity;
import net.kyori.adventure.text.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Messager {

public void broadcastWithPermission(String message, String permission){
public List<String> broadcastWithPermission(String message, String permission){
List<String> broadcastedPlayers = new ArrayList<>();
if(permission.equals("*")){
for(Player player : v4GuardVelocity.getV4Guard().getServer().getAllPlayers()){
player.sendMessage(Component.text(message));
broadcastedPlayers.add(player.getUsername());
}
} else{
for(Player player : v4GuardVelocity.getV4Guard().getServer().getAllPlayers()){
if(player.hasPermission(permission)){
player.sendMessage(Component.text(message));
broadcastedPlayers.add(player.getUsername());
}
}
}
return broadcastedPlayers;
}

public void sendToPlayer(String message, String username){
Optional<Player> player = v4GuardVelocity.getV4Guard().getServer().getPlayer(username);
if(player.isPresent()){
player.get().sendMessage(Component.text(message));
}
}

}

0 comments on commit 041ba00

Please sign in to comment.