Skip to content

Commit

Permalink
Keep client version in index.html and refuse update if client is outd…
Browse files Browse the repository at this point in the history
…ated #257
  • Loading branch information
pdinklag committed Aug 6, 2024
1 parent 7c95df1 commit 1a13b26
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
51 changes: 49 additions & 2 deletions src/main/java/de/pdinklag/mcstats/Updater.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.pdinklag.mcstats;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -17,6 +18,7 @@
import org.json.JSONArray;
import org.json.JSONObject;

import de.pdinklag.mcstats.Log.Category;
import de.pdinklag.mcstats.util.ClientUtils;
import de.pdinklag.mcstats.util.JSONUtils;
import de.pdinklag.mcstats.util.MinecraftServerUtils;
Expand All @@ -42,6 +44,9 @@ public abstract class Updater {
private static final String DATABASE_SUMMARY = "summary.json";
private static final String DATABASE_LEGACY_SUMMARY = "summary.json.gz";

private static final String CLIENT_INDEX_HTML = "index.html";
private static final String CLIENT_VERSION_PREFIX = "<!-- MinecraftStats Client v";

private static final String EVENT_INITIAL_RANKING_FIELD = "initialRanking";
private static final String EVENT_RANKING_FIELD = "ranking";

Expand Down Expand Up @@ -323,15 +328,54 @@ public Updater(Config config) {
*/
protected abstract String getVersion();

public void run(ConsoleWriter consoleWriter) {
public boolean run(ConsoleWriter consoleWriter) {
// initialize log
final Log log;
try {
log = new Log(config.getLogfilePath(), consoleWriter);
Log.setCurrent(log);
} catch (IOException ex) {
consoleWriter.writeError("failed to initialize logging", ex);
return;
return false;
}

// before doing anything meaningful, check that the client is up to date to
// avoid things breaking
final Path documentRoot = config.getDocumentRoot();
final Path indexHtmlPath = documentRoot.resolve(CLIENT_INDEX_HTML);
if (Files.isRegularFile(indexHtmlPath)) {
try {
final Version clientVersion;
final BufferedReader r = Files.newBufferedReader(indexHtmlPath);
String firstLine = r.readLine();
if (firstLine.startsWith(CLIENT_VERSION_PREFIX)) {
final int begin = CLIENT_VERSION_PREFIX.length();
final int end = firstLine.indexOf(' ', begin);

clientVersion = Version.parse(firstLine.substring(begin, end));

log.writeLine(Category.SILENT_PROGRESS,
"parsed client version " + clientVersion.toString() + " from " + CLIENT_INDEX_HTML);
} else {
log.writeLine(Category.SILENT_PROGRESS,
"no version information was found in " + CLIENT_INDEX_HTML + ", assuming outdated client");
clientVersion = new Version(2, 0, 0);
}

if (clientVersion.compareTo(MIN_CLIENT_VERSION) < 0) {
log.writeLine(Log.Category.PROGRESS,
"Your MinecraftStats web files (version " + clientVersion
+ ") are outdated -- at least version "
+ MIN_CLIENT_VERSION + " is required.");
return false;
}
} catch (Exception ex) {
log.writeError("failed to read verson from " + indexHtmlPath.toString(), ex);
return false;
}
} else {
log.writeLine(Log.Category.SILENT_PROGRESS, "bypassing client version check because " + CLIENT_INDEX_HTML
+ " was not found in the document root");
}

// discover and instantiate stats
Expand Down Expand Up @@ -791,5 +835,8 @@ public void run(ConsoleWriter consoleWriter) {
consoleWriter.writeError("failed to close log", ex);
}
Log.setCurrent(null);

// done
return true;
}
}
12 changes: 7 additions & 5 deletions src/main/java/de/pdinklag/mcstats/bukkit/BukkitUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ protected String getVersion() {
}

@Override
public void run(ConsoleWriter consoleWriter) {
super.run(consoleWriter);

public boolean run(ConsoleWriter consoleWriter) {
final boolean success = super.run(consoleWriter);
if (firstUpdate) {
plugin.getLogger().info("Web frontend updated. This will now happen every "
+ config.getUpdateInterval() + " minute(s) without any further logging to the console.");
if (success) {
plugin.getLogger().info("Web frontend updated. This will now happen every "
+ config.getUpdateInterval() + " minute(s) without any further logging to the console.");
}
firstUpdate = false;
}
return success;
}
}
10 changes: 7 additions & 3 deletions src/main/java/de/pdinklag/mcstats/cli/CLIUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ protected String getVersion() {
}

@Override
public void run(ConsoleWriter consoleWriter) {
super.run(consoleWriter);
Log.getCurrent().writeLine(Log.Category.PROGRESS, "update finished");
public boolean run(ConsoleWriter consoleWriter) {
if (super.run(consoleWriter)) {
Log.getCurrent().writeLine(Log.Category.PROGRESS, "update finished");
return true;
} else {
return false;
}
}
}
1 change: 1 addition & 0 deletions www/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- MinecraftStats Client v3.4.0 - DO NOT EDIT OR (RE)MOVE THIS LINE, IT IS REQUIRED BY THE UPDATER -->
<!doctype html>
<html lang="en">
<head>
Expand Down

0 comments on commit 1a13b26

Please sign in to comment.