-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathDownloadUtils.java
More file actions
371 lines (336 loc) · 15.3 KB
/
DownloadUtils.java
File metadata and controls
371 lines (336 loc) · 15.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.installer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import net.minecraftforge.installer.actions.ProgressCallback;
import net.minecraftforge.installer.json.Artifact;
import net.minecraftforge.installer.json.Manifest;
import net.minecraftforge.installer.json.Mirror;
import net.minecraftforge.installer.json.Util;
import net.minecraftforge.installer.json.Version.Download;
import net.minecraftforge.installer.json.Version.Library;
import net.minecraftforge.installer.json.Version.LibraryDownload;
public class DownloadUtils {
public static final String LIBRARIES_URL = "https://libraries.minecraft.net/";
public static final String MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
public static boolean OFFLINE_MODE = false;
public static boolean downloadLibrary(ProgressCallback monitor, Mirror mirror, Library library, File root, List<Artifact> grabbed, List<File> additionalLibraryDirs) {
Artifact artifact = library.getName();
File target = artifact.getLocalPath(root);
LibraryDownload download = library.getDownloads() == null ? null : library.getDownloads().getArtifact();
if (download == null) {
download = new LibraryDownload();
download.setPath(artifact.getPath());
}
monitor.message(String.format("Considering library %s", artifact.getDescriptor()));
if (target.exists()) {
if (download.getSha1() != null) {
String sha1 = getSha1(target);
if (download.getSha1().equals(sha1)) {
monitor.message(" File exists: Checksum validated.");
return true;
}
monitor.message(" File exists: Checksum invalid, deleting file:");
monitor.message(" Expected: " + download.getSha1());
monitor.message(" Actual: " + sha1);
if (!target.delete()) {
monitor.stage(" Failed to delete file, aborting.");
return false;
}
} else {
monitor.message(" File exists: No checksum, Assuming valid.");
return true;
}
}
target.getParentFile().mkdirs();
// Try extracting first
try (final InputStream input = DownloadUtils.class.getResourceAsStream("/maven/" + artifact.getPath())) {
if (input != null) {
monitor.message(" Extracting library from /maven/" + artifact.getPath());
Files.copy(input, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (download.getSha1() != null) {
String sha1 = getSha1(target);
if (download.getSha1().equals(sha1)) {
monitor.message(" Extraction completed: Checksum validated.");
grabbed.add(artifact);
return true;
}
monitor.message(" Extraction failed: Checksum invalid, deleting file:");
monitor.message(" Expected: " + download.getSha1());
monitor.message(" Actual: " + sha1);
if (!target.delete()) {
monitor.stage(" Failed to delete file, aborting.");
return false;
}
return false;
} else {
monitor.message(" Extraction completed: No checksum, Assuming valid.");
}
grabbed.add(artifact);
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
// Try searching local installs if the file can be validated
if (download.getSha1() != null) {
String providedSha1 = download.getSha1();
for (File libDir : additionalLibraryDirs) {
File inLibDir = new File(libDir, artifact.getPath());
if (inLibDir.exists()) {
monitor.message(String.format(" Found artifact in local folder %s", libDir.toString()));
String sha1 = DownloadUtils.getSha1(inLibDir);
if (providedSha1.equals(sha1)) {
monitor.message(" Checksum validated");
} else {
// Do not fail immediately. We may have other sources
monitor.message(" Invalid checksum. Not using.");
continue;
}
// Valid checksum, copy the lib
try {
Files.copy(inLibDir.toPath(), target.toPath());
monitor.message(" Successfully copied local file");
grabbed.add(artifact);
return true;
} catch (IOException e) {
// The copy may have failed when the file is in use. Don't abort, we may have other sources
e.printStackTrace();
monitor.message(String.format(" Failed to copy from local folder: %s", e.toString()));
// Clean up the file that may have been created if the copy failed
if (target.exists()) {
if (!target.delete()) {
monitor.message(" Failed to delete failed copy, aborting");
return false;
}
}
}
}
}
}
String url = download.getUrl();
if (url == null || url.isEmpty()) {
monitor.message(" Invalid library, missing url");
return false;
}
if (download(monitor, mirror, download, target)) {
grabbed.add(artifact);
return true;
}
return false;
}
private static boolean download(ProgressCallback monitor, Mirror mirror, LibraryDownload download, File target) {
String url = download.getUrl();
if (url.startsWith("http") && !url.startsWith(LIBRARIES_URL) && mirror != null && url.endsWith(download.getPath())) {
// TODO: Vanilla launcher is dumb so we fake classifier only deps. One day the launcher will be sane/document...
// Anyways, the path is not the same as the real maven path. So we don't have a good way to determine the mirrored url
if (download(monitor, mirror, download, target, mirror.getUrl() + download.getPath())) // Use unmirrored if mirror fails.
return true;
}
return download(monitor, mirror, download, target, url);
}
public static boolean download(ProgressCallback monitor, Mirror mirror, Download download, File target) {
return download(monitor, mirror, download, target, download.getUrl());
}
private static boolean download(ProgressCallback monitor, Mirror mirror, Download download, File target, String url) {
monitor.message(" Downloading library from " + url);
try {
URLConnection connection = getConnection(url);
if (connection != null) {
Files.copy(connection.getInputStream(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (download.getSha1() != null) {
String sha1 = getSha1(target);
if (download.getSha1().equals(sha1)) {
monitor.message(" Download completed: Checksum validated.");
return true;
}
monitor.message(" Download failed: Checksum invalid, deleting file:");
monitor.message(" Expected: " + download.getSha1());
monitor.message(" Actual: " + sha1);
if (!target.delete()) {
monitor.stage(" Failed to delete file, aborting.");
return false;
}
}
monitor.message(" Download completed: No checksum, Assuming valid.");
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static String getSha1(File target) {
try {
return HashFunction.SHA1.hash(Files.readAllBytes(target.toPath()));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static boolean checksumValid(File target, String checksum) {
if (checksum == null || checksum.isEmpty())
return true;
String sha1 = getSha1(target);
return sha1 != null && sha1.equals(checksum);
}
private static URLConnection getConnection(String address) {
if (OFFLINE_MODE) {
System.out.println("Offline Mode: Not downloading: " + address);
return null;
}
URL url = null;
try {
url = new URL(address);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
try {
int MAX = 3;
URLConnection connection = null;
for (int x = 0; x < MAX; x++) { //Maximum of 3 redirects.
connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection instanceof HttpURLConnection) {
HttpURLConnection hcon = (HttpURLConnection)connection;
hcon.setInstanceFollowRedirects(false);
int res = hcon.getResponseCode();
if (res == HttpURLConnection.HTTP_MOVED_PERM || res == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = hcon.getHeaderField("Location");
hcon.disconnect(); //Kill old connection.
if (x == MAX-1) {
System.out.println("Invalid number of redirects: " + location);
return null;
} else {
System.out.println("Following redirect: " + location);
url = new URL(url, location); // Nested in case of relative urls.
}
} else {
break;
}
} else {
break;
}
}
return connection;
} catch (SSLHandshakeException e) {
System.out.println("Failed to establish connection to " + address);
System.out.println(" Host: " + url.getHost() + " [" + getIpString(url.getHost()) + "]");
e.printStackTrace();
return null;
} catch (IOException e) {
System.out.println("Failed to establish connection to " + address);
System.out.println(" Host: " + url.getHost() + " [" + getIpString(url.getHost()) + "]");
e.printStackTrace();
return null;
}
}
public static List<String> getIps(String host) {
try {
InetAddress[] addresses = InetAddress.getAllByName(host);
return Arrays.stream(addresses).map(InetAddress::getHostAddress).collect(Collectors.toList());
} catch (UnknownHostException e1) {
return Arrays.asList("Unknown");
}
}
public static String getIpString(String host) {
return getIps(host).stream().collect(Collectors.joining(", "));
}
public static boolean checkCertificate(String host) {
try {
HttpURLConnection con = (HttpURLConnection)new URL(host).openConnection();
con.setRequestMethod("HEAD");
con.connect();
return true;
} catch (SSLException e) {
System.out.println("Error checking " + host + ": " + e.getMessage());
return false; // Something screwed up with the SSL connection, most likely missing root certs.
} catch (IOException e) {
return true; // Something else went wrong, assume the server is down or something, and let it error later down the line.
}
}
public static Mirror[] downloadMirrors(String url) {
return downloadString(url, Util::loadMirrorList);
}
public static Manifest downloadManifest() {
return downloadString(MANIFEST_URL, Util::loadManifest);
}
public static <R> R downloadString(String url, Function<InputStream, R> reader) {
try {
URLConnection connection = getConnection(url);
if (connection != null) {
try (InputStream stream = connection.getInputStream()) {
return reader.apply(stream);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static boolean downloadFile(File target, String url) {
try {
URLConnection connection = getConnection(url);
if (connection != null) {
Files.copy(connection.getInputStream(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean extractFile(Artifact art, File target, String checksum) {
final InputStream input = DownloadUtils.class.getResourceAsStream("/maven/" + art.getPath());
if (input == null) {
System.out.println("File not found in installer archive: /maven/" + art.getPath());
return false;
}
if (!target.getParentFile().exists())
target.getParentFile().mkdirs();
try {
Files.copy(input, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return checksumValid(target, checksum);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean extractFile(String name, File target) {
final String path = name.charAt(0) == '/' ? name : '/' + name;
final InputStream input = DownloadUtils.class.getResourceAsStream(path);
if (input == null) {
System.out.println("File not found in installer archive: " + path);
return false;
}
if (!target.getParentFile().exists())
target.getParentFile().mkdirs();
try {
Files.copy(input, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return true; //checksumValid(target, checksum); //TODO: zip checksums?
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}