Skip to content

Commit

Permalink
Merge pull request #13 from v4Guard/develop
Browse files Browse the repository at this point in the history
v4Guard Plugin v1.1.3
  • Loading branch information
samfces authored Oct 13, 2022
2 parents 668b3d8 + 4767678 commit c8379ea
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 18 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.2b</version>
<version>1.1.3</version>
<packaging>jar</packaging>

<properties>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/v4guard/plugin/bungee/BungeeCheckProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,19 @@ public void kickPlayer(String username, String reason){
player.disconnect(TextComponent.fromLegacyText(reason));
}
}

@Override
public boolean isPlayerOnline(String username) {
return ProxyServer.getInstance().getPlayer(username) != null;
}

@Override
public String getPlayerServer(String username) {
if(!isPlayerOnline(username)) return null;
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(username);
if (player != null) {
return player.getServer().getInfo().getName();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public interface CheckProcessor<K> {
void onPostLogin(String username, K event);
boolean onExpire(VPNCheck VPNCheck);
void kickPlayer(String username, String reason);
boolean isPlayerOnline(String username);
String getPlayerServer(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.socket.listener.*;
import io.v4guard.plugin.core.utils.HashCalculator;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -39,6 +40,7 @@ public BackendConnector() throws IOException, URISyntaxException {
headers.put("v4g-name", Collections.singletonList(new File(System.getProperty("user.dir")).getName()));
headers.put("v4g-service", Collections.singletonList("minecraft"));
headers.put("v4g-mode", Collections.singletonList(v4GuardCore.getInstance().getPluginMode().name()));
headers.put("v4g-integrity", Collections.singletonList(HashCalculator.calculateHash()));
this.options = IO.Options.builder()
.setForceNew(false)
.setMultiplex(true)
Expand Down Expand Up @@ -83,6 +85,8 @@ public void handleEvents() {
registerListener("check", new CheckListener());
registerListener("message", new MessageListener(this));
registerListener("kick", new KickListener());
registerListener("cleancache", new CleanCacheListener(this));
registerListener("find", new FindListener(this));
}

public void registerListener(String event, Emitter.Listener listener){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

public enum SocketStatus
{

NOT_AUTHENTICATED,
PRE_AUTHENTICATED,
DISCONNECTED,
DISCONNECTED,
BLOCKED_AUTHENTICATION,
AUTHENTICATED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public void call(Object... args) {
backendConnector.setAuthCode(doc.getString("code"));
try {
backendConnector.setSocketStatus(SocketStatus.valueOf(doc.getString("status")));

if (backendConnector.getSocketStatus().equals(SocketStatus.BLOCKED_AUTHENTICATION)) return;

if (backendConnector.getSocketStatus().equals(SocketStatus.NOT_AUTHENTICATED)) {
new Timer().schedule(new java.util.TimerTask() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.v4guard.plugin.core.socket.listener;

import io.socket.emitter.Emitter;
import io.v4guard.plugin.core.check.common.CheckStatus;
import io.v4guard.plugin.core.check.common.VPNCheck;
import io.v4guard.plugin.core.socket.BackendConnector;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;

public class CleanCacheListener implements Emitter.Listener {

BackendConnector backendConnector;

public CleanCacheListener(BackendConnector backendConnector) {
this.backendConnector = backendConnector;
}

@Override
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
if(doc.containsKey("username")){
v4GuardCore.getInstance().getCheckManager().cleanupChecks(doc.getString("username"));
} else {
for(VPNCheck s : v4GuardCore.getInstance().getCheckManager().getCheckStatusMap().values()){
if(s.getStatus() != CheckStatus.WAITING){
v4GuardCore.getInstance().getCheckManager().getCheckStatusMap().remove(s.getName());
}
}
}
}

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

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

public class FindListener implements Emitter.Listener {

BackendConnector backendConnector;

public FindListener(BackendConnector backendConnector) {
this.backendConnector = backendConnector;
}

@Override
public void call(Object... args) {
Document doc = Document.parse(args[0].toString());
if(doc.containsKey("username")){
String taskID = doc.getString("taskID");
String username = doc.getString("username");
//Obviously the first check processor in the list of processors is the one that is active.
if(v4GuardCore.getInstance().getCheckManager().getProcessors().get(0).isPlayerOnline(username)){
String server = v4GuardCore.getInstance().getCheckManager().getProcessors().get(0).getPlayerServer(username);
backendConnector.getSocket().emit("find",
new Document("taskID", taskID)
.append("username", username)
.append("location", server)
.toJson()
);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package io.v4guard.plugin.core.tasks.types;

import io.v4guard.plugin.core.check.common.CheckStatus;
import io.v4guard.plugin.core.tasks.common.CompletableTask;
import io.v4guard.plugin.core.check.common.VPNCheck;
import io.v4guard.plugin.core.tasks.common.CompletableTask;
import io.v4guard.plugin.core.utils.StringUtils;
import io.v4guard.plugin.core.v4GuardCore;
import org.bson.Document;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

public abstract class CompletableIPCheckTask implements CompletableTask {
private final String taskID;
private final String address;
private final String username;
private int version;
private final ConcurrentHashMap<String, Object> data;
private Document data;
private VPNCheck check;

public CompletableIPCheckTask(String address, String username, int version, String virtualHost) {
this.address = address;
this.username = username;
this.version = version;
this.taskID = UUID.randomUUID().toString();
this.data = new ConcurrentHashMap();
this.data = new Document();
v4GuardCore.getInstance().getCompletableTaskManager().getTasks().put(this.taskID, this);
Document doc = new Document();
doc.put("taskID", this.taskID);
Expand Down Expand Up @@ -57,12 +57,11 @@ public String getTaskID() {

private boolean isBlocked() {
if (!this.isCompleted()) throw new UnsupportedOperationException("Task is not completed yet");
Document result = (Document)this.data.get("result");
return result.getBoolean("block");
return getData().getBoolean("block");
}

public String translateVariables(String reason) {
Document variables = ((Document)this.data.get("result")).get("variables", Document.class);
Document variables = getData().get("variables", Document.class);
AtomicReference<String> result = new AtomicReference<>(reason);
for (String key : variables.keySet()) {
String value = variables.getString(key);
Expand All @@ -74,7 +73,7 @@ public String translateVariables(String reason) {
public void check() {
if (this.isCompleted()) {
this.check = v4GuardCore.getInstance().getCheckManager().buildCheckStatus(this.getUsername(), this.getAddress());
this.replacePlaceholders(check);
this.prepareReason(check);
check.setStatus(isBlocked() ? CheckStatus.USER_DENIED : CheckStatus.USER_ALLOWED);
v4GuardCore.getInstance().getCheckManager().getCheckStatusMap().put(username, check);
this.complete();
Expand All @@ -83,14 +82,14 @@ public void check() {
}

public boolean isCompleted() {
return this.data.size() > 0;
return getData().size() > 0;
}

public void addData(Object object) {
this.data.put("result", object);
public void addData(Document doc) {
this.data = doc;
}

public ConcurrentHashMap<String, Object> getData() {
public Document getData() {
return this.data;
}

Expand All @@ -106,8 +105,12 @@ public VPNCheck getCheck() {
return check;
}

public void replacePlaceholders(VPNCheck status){
Document data = (Document) this.getData().get("result");
status.setReason(StringUtils.replacePlaceholders(status.getReason(), (Document) data.get("variables")));
public void prepareReason(VPNCheck status){
if(getData().containsKey("message")){
String reason = StringUtils.buildMultilineString(getData().get("message", List.class));
status.setReason(StringUtils.replacePlaceholders(reason, (Document) getData().get("variables")));
return;
}
status.setReason(StringUtils.replacePlaceholders(status.getReason(), (Document) getData().get("variables")));
}
}
38 changes: 38 additions & 0 deletions src/main/java/io/v4guard/plugin/core/utils/HashCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.v4guard.plugin.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class HashCalculator {

public static String calculateHash(){
try {
File jarFile = new File(HashCalculator.class.getProtectionDomain().getCodeSource().getLocation().toURI());
MessageDigest digest = MessageDigest.getInstance("SHA-1");
InputStream fis = new FileInputStream(jarFile);
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
byte[] messageDigest = digest.digest();
StringBuilder hexString = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
String hex = Integer.toHexString(0xff & aMessageDigest);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
return "7634670e00000000000000000000000000000001";
}
}



}
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.2b";
public static final String pluginVersion = "1.1.3";

private boolean debug = false;
private v4GuardMode pluginMode;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/v4guard/plugin/spigot/SpigotCheckProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,15 @@ public void kickPlayer(String username, String reason){
player.kickPlayer(reason);
}
}

@Override
public boolean isPlayerOnline(String username){
return Bukkit.getPlayer(username) != null;
}

@Override
public String getPlayerServer(String username) {
if(!isPlayerOnline(username)) return null;
return "spigot";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public void kickPlayer(String username, String reason){
}
}

@Override
public boolean isPlayerOnline(String username){
return v4GuardVelocity.getV4Guard().getServer().getPlayer(username).isPresent();
}

@Override
public String getPlayerServer(String username) {
if(!isPlayerOnline(username)) return null;
return v4GuardVelocity.getV4Guard().getServer().getPlayer(username).get().getCurrentServer().get().getServerInfo().getName();
}

private void doChecks(PreLoginEvent e, Continuation continuation) {
new CompletableNameCheckTask(e.getUsername()) {
@Override
Expand Down

0 comments on commit c8379ea

Please sign in to comment.