2022-10-15 11:36:41 -07:00
|
|
|
package com.rusefi.core.net;
|
2020-07-01 20:54:40 -07:00
|
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
import java.util.Objects;
|
2024-04-19 08:31:03 -07:00
|
|
|
import java.util.Properties;
|
2020-07-01 20:54:40 -07:00
|
|
|
|
|
|
|
public class ConnectionAndMeta {
|
2020-10-07 16:06:31 -07:00
|
|
|
public static final String BASE_URL_RELEASE = "https://github.com/rusefi/rusefi/releases/latest/download/";
|
2024-04-19 08:24:06 -07:00
|
|
|
private static final String AUTOUPDATE = "/autoupdate/";
|
2023-01-28 11:49:05 -08:00
|
|
|
public static final String BASE_URL_LTS = "https://rusefi.com/build_server/lts/%s/autoupdate/";
|
2020-10-07 16:06:31 -07:00
|
|
|
|
2020-07-01 20:54:40 -07:00
|
|
|
private static final int BUFFER_SIZE = 32 * 1024;
|
2022-05-24 17:04:25 -07:00
|
|
|
public static final int CENTUM = 100;
|
2021-08-17 03:11:29 -07:00
|
|
|
private final String zipFileName;
|
2020-07-01 20:54:40 -07:00
|
|
|
private HttpsURLConnection httpConnection;
|
|
|
|
private long completeFileSize;
|
|
|
|
private long lastModified;
|
|
|
|
|
|
|
|
public ConnectionAndMeta(String zipFileName) {
|
|
|
|
this.zipFileName = zipFileName;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:24:06 -07:00
|
|
|
public static String getBaseUrl() {
|
2024-04-19 08:31:03 -07:00
|
|
|
Properties props = new Properties();
|
|
|
|
try {
|
|
|
|
props.load(ConnectionAndMeta.class.getResourceAsStream("shared_io.properties"));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
String result = props.getProperty("auto_update_root_url");
|
|
|
|
System.out.println(ConnectionAndMeta.class + ": got [" + result + "]");
|
|
|
|
return result + AUTOUPDATE;
|
2024-04-19 08:24:06 -07:00
|
|
|
}
|
|
|
|
|
2020-07-01 20:54:40 -07:00
|
|
|
public static void downloadFile(String localTargetFileName, ConnectionAndMeta connectionAndMeta, DownloadProgressListener listener) throws IOException {
|
|
|
|
HttpURLConnection httpConnection = connectionAndMeta.getHttpConnection();
|
|
|
|
long completeFileSize = connectionAndMeta.getCompleteFileSize();
|
|
|
|
Objects.requireNonNull(httpConnection, "httpConnection");
|
|
|
|
BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream());
|
|
|
|
FileOutputStream fos = new FileOutputStream(localTargetFileName);
|
|
|
|
BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE);
|
|
|
|
byte[] data = new byte[BUFFER_SIZE];
|
|
|
|
long downloadedFileSize = 0;
|
|
|
|
int newDataSize;
|
|
|
|
|
|
|
|
int printedPercentage = 0;
|
|
|
|
|
|
|
|
while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) {
|
|
|
|
downloadedFileSize += newDataSize;
|
|
|
|
|
|
|
|
// calculate progress
|
2022-05-24 17:04:25 -07:00
|
|
|
int currentPercentage = (int) (CENTUM * downloadedFileSize / completeFileSize);
|
2020-07-01 20:54:40 -07:00
|
|
|
if (currentPercentage > printedPercentage + 5) {
|
|
|
|
System.out.println("Downloaded " + currentPercentage + "%");
|
|
|
|
printedPercentage = currentPercentage;
|
2022-05-24 17:04:25 -07:00
|
|
|
listener.onPercentage(currentPercentage);
|
2020-07-01 20:54:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bout.write(data, 0, newDataSize);
|
|
|
|
}
|
|
|
|
bout.close();
|
|
|
|
in.close();
|
|
|
|
new File(localTargetFileName).setLastModified(connectionAndMeta.getLastModified());
|
|
|
|
}
|
|
|
|
|
|
|
|
public HttpURLConnection getHttpConnection() {
|
|
|
|
return httpConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getCompleteFileSize() {
|
|
|
|
return completeFileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastModified() {
|
|
|
|
return lastModified;
|
|
|
|
}
|
|
|
|
|
2022-07-17 00:10:42 -07:00
|
|
|
public ConnectionAndMeta invoke(String baseUrl) throws IOException {
|
2020-07-01 20:54:40 -07:00
|
|
|
// user can have java with expired certificates or funny proxy, we shall accept any certificate :(
|
2022-07-17 00:10:42 -07:00
|
|
|
SSLContext ctx = null;
|
|
|
|
try {
|
|
|
|
ctx = SSLContext.getInstance("TLS");
|
|
|
|
ctx.init(new KeyManager[0], new TrustManager[]{new AcceptAnyCertificateTrustManager()}, new SecureRandom());
|
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
|
|
|
throw new IOException("TLS exception", e);
|
|
|
|
}
|
2020-07-01 20:54:40 -07:00
|
|
|
|
2020-10-07 16:06:31 -07:00
|
|
|
URL url = new URL(baseUrl + zipFileName);
|
2021-04-28 19:47:35 -07:00
|
|
|
System.out.println("Connecting to " + url);
|
2020-07-01 20:54:40 -07:00
|
|
|
httpConnection = (HttpsURLConnection) url.openConnection();
|
|
|
|
httpConnection.setSSLSocketFactory(ctx.getSocketFactory());
|
|
|
|
completeFileSize = httpConnection.getContentLength();
|
|
|
|
lastModified = httpConnection.getLastModified();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface DownloadProgressListener {
|
2022-05-24 17:04:25 -07:00
|
|
|
void onPercentage(int currentPercentage);
|
2020-07-01 20:54:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class AcceptAnyCertificateTrustManager implements X509TrustManager {
|
|
|
|
@Override
|
|
|
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|