Skip to content

Commit

Permalink
configurable checksum file
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jul 17, 2022
1 parent a7a7a10 commit 549d4a1
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 84 deletions.
14 changes: 7 additions & 7 deletions mc-server-updater-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

<dependencies>
<dependency>
<groupId>me.HSGamer</groupId>
<artifactId>HSCore-collections</artifactId>
<version>3.17</version>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-collections</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>me.HSGamer</groupId>
<artifactId>HSCore-web</artifactId>
<version>3.17</version>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-web</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
<version>20220320</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import me.hsgamer.mcserverupdater.api.LatestBuild;
import me.hsgamer.mcserverupdater.api.Updater;
import me.hsgamer.mcserverupdater.updater.*;
import me.hsgamer.mcserverupdater.util.ChecksumUtils;
import me.hsgamer.mcserverupdater.util.Utils;

import java.io.File;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -39,6 +41,7 @@ public final class UpdateBuilder {
private String version = "default";
private String build = "latest";
private File outputFile = new File("server.jar");
private boolean checkOnly = false;

private UpdateBuilder(String project) {
this.updater = Optional.ofNullable(UPDATERS.get(project)).map(Supplier::get).orElse(null);
Expand Down Expand Up @@ -118,6 +121,65 @@ public UpdateBuilder outputFile(String outputFile) {
return outputFile(new File(outputFile));
}

/**
* Set the checksum supplier
*
* @param checksumSupplier the checksum supplier
* @return the update process
*/
public UpdateBuilder checksumSupplier(ChecksumUtils.ChecksumSupplier checksumSupplier) {
ChecksumUtils.setChecksumSupplier(checksumSupplier);
return this;
}

/**
* Set the checksum consumer
*
* @param checkConsumer the checksum consumer
* @return the update process
*/
public UpdateBuilder checkConsumer(ChecksumUtils.ChecksumConsumer checkConsumer) {
ChecksumUtils.setChecksumConsumer(checkConsumer);
return this;
}

/**
* Set the checksum file
*
* @param checksumFile the checksum file
* @return the update process
*/
public UpdateBuilder checksumFile(File checksumFile) {
ChecksumUtils.setChecksumSupplier(() -> Utils.isFailedToCreateFile(checksumFile) ? "" : Utils.getString(checksumFile));
ChecksumUtils.setChecksumConsumer(checksum -> {
if (!Utils.isFailedToCreateFile(checksumFile)) {
Utils.writeString(checksumFile, checksum);
}
});
return this;
}

/**
* Set the checksum file
*
* @param checksumFile the checksum file
* @return the update process
*/
public UpdateBuilder checksumFile(String checksumFile) {
return checksumFile(new File(checksumFile));
}

/**
* Set if the update process should only check the checksum
*
* @param checkOnly the check only
* @return the update process
*/
public UpdateBuilder checkOnly(boolean checkOnly) {
this.checkOnly = checkOnly;
return this;
}

/**
* Execute the update process
*
Expand Down Expand Up @@ -148,19 +210,28 @@ public CompletableFuture<UpdateStatus> executeAsync() {
Checksum checksum = (Checksum) update;
if (checksum.checksum(outputFile, version, build)) {
return UpdateStatus.UP_TO_DATE;
} else if (checkOnly) {
return UpdateStatus.OUT_OF_DATE;
}
}
} else if (Utils.isFailedToCreateFile(outputFile)) {
return UpdateStatus.FILE_FAILED;
}
} catch (Exception e) {
throw new RuntimeException(e);
throw new CompletionException(e);
}

try {
return update.update(outputFile, version, build) ? UpdateStatus.SUCCESS : UpdateStatus.FAILED;
if (update.update(outputFile, version, build)) {
if (update instanceof Checksum) {
((Checksum) update).setChecksum(outputFile, version, build);
}
return UpdateStatus.SUCCESS;
} else {
return UpdateStatus.FAILED;
}
} catch (Exception e) {
throw new RuntimeException(e);
throw new CompletionException(e);
}
}).exceptionally(UpdateStatus::unknownError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public final class UpdateStatus {
* The output file is up-to-date
*/
public static final UpdateStatus UP_TO_DATE = new UpdateStatus(true, "Up-to-date version");
/**
* The output file is out-of-date
*/
public static final UpdateStatus OUT_OF_DATE = new UpdateStatus(true, "Out-of-date version");
/**
* Update successfully
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

public interface Checksum {
boolean checksum(File file, String version, String build) throws Exception;

default void setChecksum(File file, String version, String build) throws Exception {
// EMPTY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public interface FileDigestChecksum extends SimpleChecksum {
MessageDigest getMessageDigest() throws Exception;

@Override
default String getFileChecksum(File file) throws Exception {
default String getCurrentChecksum(File file) throws Exception {
MessageDigest messageDigest = getMessageDigest();
messageDigest.update(Files.readAllBytes(file.toPath()));
byte[] checksumValue = messageDigest.digest();
return Utils.toHex(checksumValue);
}

@Override
default void setChecksum(File file, String version, String build) throws Exception {
// IGNORED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

import me.hsgamer.hscore.web.UserAgent;
import me.hsgamer.hscore.web.WebUtils;
import me.hsgamer.mcserverupdater.util.Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.regex.Pattern;

public abstract class GithubBranchUpdater implements SimpleFileUpdater, LatestBuild, UrlInputStreamUpdater {
public abstract class GithubBranchUpdater implements SimpleChecksum, LatestBuild, UrlInputStreamUpdater {
private final String refLatestCommitUrl;
private final String downloadUrl;
private final String filesUrl;
Expand All @@ -32,7 +30,7 @@ protected GithubBranchUpdater(String repo) {
public String getFile(String version, String build) {
String url = String.format(filesUrl, build);
try {
URLConnection connection = WebUtils.openConnection(url, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
InputStream inputStream = connection.getInputStream();
JSONObject object = new JSONObject(new JSONTokener(inputStream));
JSONArray array = object.getJSONArray("tree");
Expand Down Expand Up @@ -64,7 +62,7 @@ public String getFileUrl(String version, String build) {
public String getLatestBuild(String version) {
String url = String.format(refLatestCommitUrl, getBranch(version));
try {
URLConnection connection = WebUtils.openConnection(url, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
return jsonObject.getString("sha");
Expand All @@ -78,9 +76,4 @@ public String getLatestBuild(String version) {
public String getChecksum(String version, String build) {
return build;
}

@Override
public File getChecksumFile() throws IOException {
return Utils.getFile("github.mirror.nvdadr.commit");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import me.hsgamer.hscore.web.UserAgent;
import me.hsgamer.hscore.web.WebUtils;
import me.hsgamer.mcserverupdater.util.Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.Objects;
import java.util.regex.Pattern;

public abstract class GithubReleaseUpdater implements SimpleFileUpdater, LatestBuild, UrlInputStreamUpdater {
public abstract class GithubReleaseUpdater implements SimpleChecksum, LatestBuild, UrlInputStreamUpdater {
private final String repo;
private final boolean versionAsTag;
private final String releasesUrl;
Expand All @@ -36,7 +34,7 @@ protected GithubReleaseUpdater(String repo, boolean versionAsTag) {
public String getFileUrl(String version, String build) {
String assetUrl = String.format(releaseAssetUrl, build);
try {
URLConnection connection = WebUtils.openConnection(assetUrl, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(assetUrl));
InputStream inputStream = connection.getInputStream();
JSONArray array = new JSONArray(new JSONTokener(inputStream));
for (int i = 0; i < array.length(); i++) {
Expand All @@ -59,7 +57,7 @@ public String getLatestBuild(String version) {
if (versionAsTag) {
String tagToIdUrl = String.format(releaseByTagUrl, version);
try {
URLConnection connection = WebUtils.openConnection(tagToIdUrl, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(tagToIdUrl));
InputStream inputStream = connection.getInputStream();
object = new JSONObject(new JSONTokener(inputStream));
} catch (IOException e) {
Expand All @@ -68,7 +66,7 @@ public String getLatestBuild(String version) {
} else {
String url = releasesUrl + "?per_page=1";
try {
URLConnection connection = WebUtils.openConnection(url, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
InputStream inputStream = connection.getInputStream();
JSONArray array = new JSONArray(new JSONTokener(inputStream));
object = array.getJSONObject(0);
Expand All @@ -86,9 +84,4 @@ public String getLatestBuild(String version) {
public String getChecksum(String version, String build) {
return repo + "||" + build;
}

@Override
public File getChecksumFile() throws IOException {
return Utils.getFile("github.release");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

import me.hsgamer.hscore.web.UserAgent;
import me.hsgamer.hscore.web.WebUtils;
import me.hsgamer.mcserverupdater.util.Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.regex.Pattern;

public abstract class JenkinsUpdater implements SimpleFileUpdater, LatestBuild {
public abstract class JenkinsUpdater implements SimpleChecksum, InputStreamUpdater, LatestBuild {
private final String jenkinsUrl;
private final String jobUrl;
private final String artifactUrl;
Expand All @@ -37,26 +35,21 @@ public String getChecksum(String version, String build) {
public InputStream getInputStream(String version, String build) {
String url = getArtifactUrl(version, build);
try {
URLConnection connection = WebUtils.openConnection(url, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
return connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
public File getChecksumFile() throws IOException {
return Utils.getFile("jenkins.build");
}

@Override
public String getLatestBuild(String version) {
String url = getJobUrl(version);
String api = url + "api/json";
String treeUrl = api + "?tree=lastSuccessfulBuild[number]";
try {
URLConnection connection = WebUtils.openConnection(treeUrl, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(treeUrl));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
JSONObject build = jsonObject.getJSONObject("lastSuccessfulBuild");
Expand All @@ -81,7 +74,7 @@ public String getArtifactUrl(String version, String build) {
String artifactListUrl = getJobUrl(version) + build + "/api/json?tree=artifacts[fileName,relativePath]";
String artifact = "INVALID";
try {
URLConnection connection = WebUtils.openConnection(artifactListUrl, UserAgent.CHROME);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(artifactListUrl));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
JSONArray artifacts = jsonObject.getJSONArray("artifacts");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package me.hsgamer.mcserverupdater.api;

import me.hsgamer.mcserverupdater.util.ChecksumUtils;

import java.io.File;

public interface SimpleChecksum extends Checksum {
String getChecksum(String version, String build);

String getFileChecksum(File file) throws Exception;
default String getCurrentChecksum(File file) throws Exception {
return ChecksumUtils.getChecksumSupplier().get();
}

@Override
default boolean checksum(File file, String version, String build) throws Exception {
String checksumCode = getChecksum(version, build);
if (checksumCode == null) {
String checksum = getChecksum(version, build);
if (checksum == null) {
return false;
}
String checksumString = getFileChecksum(file);
return checksumString.equals(checksumCode);
String currentChecksum = getCurrentChecksum(file);
return currentChecksum.equals(checksum);
}

@Override
default void setChecksum(File file, String version, String build) throws Exception {
ChecksumUtils.getChecksumConsumer().accept(getChecksum(version, build));
}
}
Loading

0 comments on commit 549d4a1

Please sign in to comment.