Skip to content
Snippets Groups Projects
Commit 5ad6d939 authored by Felix Sidokhine's avatar Felix Sidokhine
Browse files

added comparing of checksums

parent 104dec09
No related branches found
No related tags found
No related merge requests found
...@@ -59,7 +59,7 @@ public class JAMSUpdater implements AppUpdater { ...@@ -59,7 +59,7 @@ public class JAMSUpdater implements AppUpdater {
@Override @Override
public void doUpdate() { public void doUpdate() {
//Some logic here about replacing the existing files. //Some logic here about replacing the existing files.
boolean res = updateDownloader.downloadFiles(); boolean res = updateDownloader.downloadFiles(getRemoteVersions());
//Notify back up-stream to the launcher that we want the update to happen. //Notify back up-stream to the launcher that we want the update to happen.
if(res) { if(res) {
doUpdate.set(true); doUpdate.set(true);
......
package net.jami.jams.updater; package net.jami.jams.updater;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.updater.FileDescription;
import net.jami.jams.common.utils.VersioningUtils;
import net.jami.jams.common.utils.X509Utils; import net.jami.jams.common.utils.X509Utils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import static net.jami.jams.updater.JAMSUpdater.UPDATE_SERVER_URL;
@Slf4j @Slf4j
public class UpdateDownloader { public class UpdateDownloader {
private SSLSocketFactory sslSocketFactory;
private SSLContext sslContext; private SSLContext sslContext;
private static final String KEYSTORE_TYPE = "JKS"; private static final String KEYSTORE_TYPE = "JKS";
private KeyStore trustStore; private KeyStore trustStore;
public UpdateDownloader() { public UpdateDownloader() {
try { try {
InputStream is = UpdateCheckTask.class.getClassLoader().getResourceAsStream("oem/ca.crt"); InputStream is = UpdateCheckTask.class.getClassLoader().getResourceAsStream("oem/ca.crt");
...@@ -33,8 +45,18 @@ public class UpdateDownloader { ...@@ -33,8 +45,18 @@ public class UpdateDownloader {
} }
} }
private void downloadFile(File tmpFolder,FileDescription fileDescription) throws Exception{
HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
HttpResponse httpResponse = httpClient.execute(new HttpGet(UPDATE_SERVER_URL + "/updates/" + fileDescription.getFileName()));
if(httpResponse.getStatusLine().getStatusCode() != 200) return;
FileOutputStream fos = new FileOutputStream(tmpFolder.getPath() + "/" + fileDescription.getFileName());
httpResponse.getEntity().writeTo(fos);
fos.close();
}
//TODO: Download the files and dump them into a tmp folder. //TODO: Download the files and dump them into a tmp folder.
public boolean downloadFiles(){ public boolean downloadFiles(HashMap<String, FileDescription> files){
try { try {
//We can perpetually reload this,it doesn't really harm anything. //We can perpetually reload this,it doesn't really harm anything.
//Build the SSL context here, (this is fairly simple) //Build the SSL context here, (this is fairly simple)
...@@ -43,9 +65,29 @@ public class UpdateDownloader { ...@@ -43,9 +65,29 @@ public class UpdateDownloader {
ks.setKeyEntry("licenses", JAMSUpdater.privateKey,"".toCharArray(),new Certificate[]{JAMSUpdater.certificate}); ks.setKeyEntry("licenses", JAMSUpdater.privateKey,"".toCharArray(),new Certificate[]{JAMSUpdater.certificate});
sslContext = SSLContexts.custom().loadKeyMaterial(ks, "".toCharArray()).loadTrustMaterial(trustStore, null).build(); sslContext = SSLContexts.custom().loadKeyMaterial(ks, "".toCharArray()).loadTrustMaterial(trustStore, null).build();
//Try to download the files and store the to /tmp //Try to download the files and store the to /tmp
File tmpFolder = new File(System.getProperty("user.dir") + "/tmp/");
if(!tmpFolder.mkdirs()){
log.error("Could not create temporary folder to store the update files!");
return false;
}
files.forEach((k,v) -> {
try {
downloadFile(tmpFolder,v);
}
catch (Exception e){
log.error("Could not download a file with error {}",e.getMessage());
}
});
//Check file checksums and trigger the update cycle itself - but this is already done in the outer function. //Check file checksums and trigger the update cycle itself - but this is already done in the outer function.
return true; HashMap<String,FileDescription> downloadedFileInfo = VersioningUtils.checkVersion(System.getProperty("user.dir") + "/tmp");
AtomicBoolean checksumOk = new AtomicBoolean(true);
files.forEach((k,v) ->{
if(downloadedFileInfo.containsKey(k) && !downloadedFileInfo.get(k).getMd5hash().equals(v.getMd5hash())){
checksumOk.set(false);
}
});
return checksumOk.get();
} }
catch (Exception e){ catch (Exception e){
log.error("Could not check for updates with error: {}",e.getMessage()); log.error("Could not check for updates with error: {}",e.getMessage());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment