-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdateChecker.java
More file actions
151 lines (122 loc) · 5.85 KB
/
UpdateChecker.java
File metadata and controls
151 lines (122 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package me.adarsh.autoupdate;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class UpdateChecker implements Runnable {
/** The frequency in seconds to check for updates */
public static final long CHECK_UPDATE_FREQUENCY = 10*60; // 10 minutes
/** The url to the jenkins last build information */
public static final String LAST_BUILD_URL_VV = "https://ci.viaversion.com/job/ViaVersion/lastBuild/api/json?random=%f";
/** The url to the jenkins last build information */
public static final String LAST_BUILD_URL_VB = "https://ci.viaversion.com/job/ViaBackwards/lastBuild/api/json?random=%f";
/** The url to the jenkins last build information */
public static final String LAST_BUILD_URL_VR = "https://ci.viaversion.com/job/ViaRewind/lastBuild/api/json?random=%f";
/** The url to download the viaversion jar from */
public static final String DOWNLOAD_URL_VV = "https://ci.viaversion.com/job/ViaVersion/lastBuild/artifact/%s";
/** The url to download the viabackwards jar from */
public static final String DOWNLOAD_URL_VB = "https://ci.viaversion.com/job/ViaBackwards/lastBuild/artifact/%s";
/** The url to download the viarewind jar from */
public static final String DOWNLOAD_URL_VR = "https://ci.viaversion.com/job/ViaRewind/lastBuild/artifact/%s";
/** The url to the jeskins last build information */
private final String LAST_BUILD_URL;
/** The url to the target download */
private final String DOWNLOAD_URL;
private ViaVersionAutoUpdate viaVersionAutoUpdate;
public UpdateChecker(ViaVersionAutoUpdate viaVersionAutoUpdate, String type) {
this.viaVersionAutoUpdate = viaVersionAutoUpdate;
switch (type) {
case "VV": {
DOWNLOAD_URL = DOWNLOAD_URL_VV;
LAST_BUILD_URL = LAST_BUILD_URL_VV;
break;
}
case "VB": {
DOWNLOAD_URL = DOWNLOAD_URL_VB;
LAST_BUILD_URL = LAST_BUILD_URL_VB;
break;
}
case "VR": {
DOWNLOAD_URL = DOWNLOAD_URL_VR;
LAST_BUILD_URL = LAST_BUILD_URL_VR;
break;
}
default:
throw new IllegalArgumentException("Invalid type: " + type);
}
}
@Override
public void run() {
try {
// Download the json
String lastBuild = downloadLastBuildInfo();
// Parse the json
JsonObject json = new JsonParser().parse(lastBuild).getAsJsonObject();
// Get the file name (json.artifacts[0].filename)
String fileName = json.getAsJsonArray("artifacts")
.get(0).getAsJsonObject()
.get("fileName").getAsString();
String relativePath = json.getAsJsonArray("artifacts")
.get(0).getAsJsonObject()
.get("relativePath").getAsString();
// Check if it's a new update
File oldJar = viaVersionAutoUpdate.getPlugin().getViaVersionJar();
if (oldJar == null || !oldJar.getName().equalsIgnoreCase(fileName)) {
// New update!
installUpdate(oldJar, fileName, relativePath);
// Restart the server to apply the update
viaVersionAutoUpdate.startRestartCountdown();
}
} catch (Exception e) {
System.err.println("[ViaVersionAutoUpdate] An error occured while checking for updates");
e.printStackTrace();
}
viaVersionAutoUpdate.getPlugin().runTaskLaterAsync(this, CHECK_UPDATE_FREQUENCY);
}
/**
* Install an update
* @param oldJar The old jar to be deleted
* @param fileName The new file to be downloaded
*/
private void installUpdate(File oldJar, String fileName, String relativePath) throws IOException {
File newJar = new File(viaVersionAutoUpdate.getPlugin().getPluginsDirectory(), fileName);
URL url = new URL(String.format(DOWNLOAD_URL, relativePath));
URLConnection connection = url.openConnection();
// Spoof a user-agent, jenkins doesn't like the java default
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");
try (InputStream in = connection.getInputStream()) {
Files.copy(in, newJar.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Try to delete old jar now, if not, delete it on exit
if (oldJar != null && !oldJar.delete()) {
oldJar.deleteOnExit();
}
}
}
/**
* Download the last build info from LAST_BUILD_URL
*
* @return A json string of the last build info
*/
private String downloadLastBuildInfo() throws IOException {
URL url = new URL(String.format(LAST_BUILD_URL, Math.random()));
URLConnection connection = url.openConnection();
// Spoof a user-agent, jenkins doesn't like the java default
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");
try (InputStream in = connection.getInputStream()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] b = new byte[4096];
int i;
while ((i = in.read(b)) >= 0) {
buffer.write(b, 0, i);
}
return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
}
}
}