Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version checker #15

Draft
wants to merge 6 commits into
base: ver/1.1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public class Evaluator {
public static final AtomicBoolean SUPPRESS_EXCEPTION_MESSAGES = new AtomicBoolean(false);

public static Evaluation evaluate(
final String cmd,
final Collection<? extends Chain> chains,
final Policy defaultPolicy,
final boolean denyColonInFirstArg,
final EvalCause cause,
final Consumer<Supplier<String>> debugLogger,
final Consumer<Supplier<String>> warningLogger
final String cmd,
final Collection<? extends Chain> chains,
final Policy defaultPolicy,
final boolean denyColonInFirstArg,
final EvalCause cause,
final Consumer<Supplier<String>> debugLogger,
final Consumer<Supplier<String>> warningLogger
) {
try {
Objects.requireNonNull(cmd, "cmd");
Expand All @@ -57,45 +57,45 @@ public static Evaluation evaluate(
final MatchResult res = chain.matches(cmd, cause, debugLogger);
if (res.matched()) {
return new Evaluation(
cmd,
chain.policy(),
chain,
res.rule(),
"in chain #" + i + " (id='" + chain.id() + "'; " + res.description()
cmd,
chain.policy(),
chain,
res.rule(),
"in chain #" + i + " (id='" + chain.id() + "'; " + res.description()
);
}
i++;
}

if (denyColonInFirstArg && Chain.transformToArgs(cmd)[0].contains(":")) {
return new Evaluation(cmd, Policy.DENY, null, null, "colon found in first arg")
.withDueToColonInFirstArg(true);
.withDueToColonInFirstArg(true);
}
} catch (Exception ex) {
if (!SUPPRESS_EXCEPTION_MESSAGES.get()) {
warningLogger.accept(() ->
"An exception was caught whilst evaluating cmd '" + cmd + "': '" + ex.getClass().getSimpleName() +
"'. For best security practice, this cmd will be forcefully denied due to the error. " +
"Message: '" + ex.getMessage() + "'; Stack trace:\n"
"An exception was caught whilst evaluating cmd '" + cmd + "': '" + ex.getClass().getSimpleName() +
"'. For best security practice, this cmd will be forcefully denied due to the error. " +
"Message: '" + ex.getMessage() + "'; Stack trace:\n"
);
//noinspection CallToPrintStackTrace
ex.printStackTrace();
}
return new Evaluation(
cmd == null ? "/??? NULL COMMAND ???" : cmd,
Policy.DENY,
null,
null,
"exception caught, denying for security"
cmd == null ? "/??? NULL COMMAND ???" : cmd,
Policy.DENY,
null,
null,
"exception caught, denying for security"
).withDueToException(true);
}

return new Evaluation(
cmd,
defaultPolicy,
null,
null,
"default policy, no chains matched"
cmd,
defaultPolicy,
null,
null,
"default policy, no chains matched"
).withDueToDefaultPolicy(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.arcaneplugins.blackwidow.lib.cmdblocking;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.util.Scanner;
import java.util.function.Consumer;

public class UpdateChecker {
public UpdateChecker(String resourceName) {
this.resourceName = resourceName;
}

private final String resourceName;
private String errorMessage;

public boolean hadError() {
return errorMessage != null;
}

public String getErrorMessage() {
return errorMessage;
}

public boolean getLatestVersion(Consumer<String> consumer) {

try (InputStream inputStream = new URI(
"https://hangar.papermc.io/api/v1/projects/" +
resourceName + "/latest?channel=Release")
.toURL().openStream()) {

final Scanner scanner = new Scanner(inputStream);
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
return true;
} catch (FileNotFoundException e) {
errorMessage = "Error checking for latest version, file not found: " + e.getMessage();
} catch (Exception e) {
errorMessage = "Error checking for latest version. " + e.getMessage();
}

return false;
}
}
3 changes: 2 additions & 1 deletion blackwidowpluginbukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase><goals>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.github.arcaneplugins.blackwidow.plugin.bukkit.component.cmdblocking.CmdBlocker;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.ListenerManager;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.logic.LogicManager;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.logic.BukkitVersionChecker;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.util.ClassUtil;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.util.DebugCategory;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.util.ExceptionUtil;
Expand Down Expand Up @@ -54,6 +55,7 @@ public final class BlackWidow extends JavaPlugin {
private final LogicManager logicManager = new LogicManager(this);
private final EnumSet<DebugCategory> debugCategories = EnumSet.noneOf(DebugCategory.class);
private final MiniMessage miniMessage = MiniMessage.miniMessage();
public final BukkitVersionChecker bukkitVersionChecker = new BukkitVersionChecker(this);

private boolean usingPaper = false;
private BukkitAudiences adventure = null;
Expand Down Expand Up @@ -97,6 +99,7 @@ public void onEnable() {
loadComponents();
listenerManager().load();
commandManager().load();
bukkitVersionChecker.load(true);
} catch (Exception ex) {
ExceptionUtil.logException(this, ex, "An error occurred whilst enabling BlackWidow.");
return;
Expand Down Expand Up @@ -149,6 +152,7 @@ public void softReload() {
loadConfigs();
logicManager().load();
loadComponents();
bukkitVersionChecker.load(false);
} catch (Exception ex) {
ExceptionUtil.logException(this, ex, "An error occurred whilst performing a soft-reload.");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ public final void load() {
"this issue ASAP, as a manually-adjusted file version can cause instability.");
}
if (installedVer < latestVer) {
int lastVersion = installedFileVersion();
plugin().getLogger().info("Config '" + fileName() + "' is outdated, automatically upgrading.");
while (installedVer < latestFileVersion()) {
plugin().getLogger().info("Upgrading '" + fileName() + "' from v" +
installedVer + " to v" + (installedVer + 1) + ".");
upgradeFile();
write();
installedVer = installedFileVersion();
if (installedVer == lastVersion){
// prevent potential endless loop
plugin().getLogger().warning("There was an error upgrading the file version, " +
"the version number was not incremented");
break;
}
lastVersion = installedVer;
}
} else if (installedVer > latestVer) {
plugin().getLogger().warning("Config '" + fileName() + "' apparently has version '" + installedVer +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import io.github.arcaneplugins.blackwidow.plugin.bukkit.BlackWidow;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.cfg.YamlCfg;
import io.github.arcaneplugins.blackwidow.plugin.bukkit.util.DebugCategory;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.serialize.SerializationException;

import java.util.Collections;
import java.util.Objects;
Expand All @@ -32,7 +34,7 @@ public final class Settings extends YamlCfg {
// REMEMBER TO UPDATE THE METHOD BELOW 'upgradeFile' IF THIS IS INCREMENTED.
// ALSO REMEMBER TO UPDATE THE FILE ITSELF - BOTH THE 'original' AND 'installed' VALUES SHOULD MATCH THIS.
// <<< !!! WARNING !!! NOTICE THIS MESSAGE !!! >>>
private static final int LATEST_FILE_VERSION = 1;
private static final int LATEST_FILE_VERSION = 2;

public Settings(
final BlackWidow plugin
Expand Down Expand Up @@ -81,10 +83,18 @@ public void upgradeFile() {
//noinspection SwitchStatementWithTooFewBranches
switch (installedVer) {
case 1 -> {
// Do nothing.
final CommentedConfigurationNode updChkNode = root().node("update-checker");
try {
updChkNode.node("enabled").set(true);
updChkNode.node("run-on-startup").set(true);
updChkNode.node("repeat-timer-duration-mins").set(60);
updChkNode.node("log-updates").set(true);
updChkNode.node("notify-players-with-permission").set(true);

//noinspection UnnecessaryBreak
break;
root().node("do-not-touch", "version", "installed").set(2);
} catch (SerializationException e) {
plugin().getLogger().warning("");
}
}
default -> throw new IllegalArgumentException("No upgrade logic defined for file version v" + installedVer);
}
Expand Down
Loading