From 6e858b6c91aeeed0210fd34c628497d8c7b28b97 Mon Sep 17 00:00:00 2001 From: afischerdev Date: Sat, 2 Mar 2024 15:41:32 +0100 Subject: [PATCH] added app certificate fallback --- .../btools/routingapp/DownloadWorker.java | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/DownloadWorker.java b/brouter-routing-app/src/main/java/btools/routingapp/DownloadWorker.java index c5d5eaab..1aa74161 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/DownloadWorker.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/DownloadWorker.java @@ -73,6 +73,7 @@ public class DownloadWorker extends Worker { int version = -1; int appversion = -1; String errorCode = null; + private boolean bHttpDownloadProblem; public DownloadWorker( @NonNull Context context, @@ -255,6 +256,7 @@ private boolean downloadLookup() throws IOException, InterruptedException { newappversion = meta.minAppVersion; } else { String lookupLocation = mServerConfig.getLookupUrl() + fileName; + if (bHttpDownloadProblem) lookupLocation = lookupLocation.replace("https://", "http://"); URL lookupUrl = new URL(lookupLocation); downloadProgressListener.onDownloadStart(fileName, DownloadType.LOOKUP); changed = downloadFile(lookupUrl, tmplookupFile, size, false, DownloadType.LOOKUP); @@ -305,6 +307,7 @@ private void downloadProfiles() throws IOException, InterruptedException { //if (profileFile.exists()) { String profileLocation = mServerConfig.getProfilesUrl() + fileName; + if (bHttpDownloadProblem) profileLocation = profileLocation.replace("https://", "http://"); URL profileUrl = new URL(profileLocation); int size = (int) (profileFile.exists() ? profileFile.length() : 0); @@ -326,6 +329,8 @@ private void downloadProfiles() throws IOException, InterruptedException { private void downloadSegment(String segmentBaseUrl, String segmentName) throws IOException, InterruptedException { File segmentFile = new File(baseDir, SEGMENTS_DIR + segmentName); File segmentFileTemp = new File(segmentFile.getAbsolutePath() + "_tmp"); + if (bHttpDownloadProblem) segmentBaseUrl = segmentBaseUrl.replace("https://", "http://"); + if (DEBUG) Log.d(LOG_TAG, "Download " + segmentName + " " + version + " " + versionChanged); try { if (segmentFile.exists()) { @@ -333,6 +338,7 @@ private void downloadSegment(String segmentBaseUrl, String segmentName) throws I String md5 = Rd5DiffManager.getMD5(segmentFile); if (DEBUG) Log.d(LOG_TAG, "Calculating local checksum " + md5); String segmentDeltaLocation = segmentBaseUrl + "diff/" + segmentName.replace(SEGMENT_SUFFIX, "/" + md5 + SEGMENT_DIFF_SUFFIX); + if (bHttpDownloadProblem) segmentDeltaLocation = segmentDeltaLocation.replace("https://", "http://"); URL segmentDeltaUrl = new URL(segmentDeltaLocation); if (httpFileExists(segmentDeltaUrl)) { File segmentDeltaFile = new File(segmentFile.getAbsolutePath() + "_diff"); @@ -376,13 +382,28 @@ private void downloadSegment(String segmentBaseUrl, String segmentName) throws I } private boolean httpFileExists(URL downloadUrl) throws IOException { - HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(); - connection.setConnectTimeout(5000); - connection.setRequestMethod("HEAD"); - connection.setDoInput(false); + HttpURLConnection connection = null; try { + connection = (HttpURLConnection) downloadUrl.openConnection(); + connection.setConnectTimeout(5000); + connection.setRequestMethod("HEAD"); + connection.setDoInput(false); connection.connect(); return connection.getResponseCode() == HttpURLConnection.HTTP_OK; + } catch (javax.net.ssl.SSLHandshakeException e) { + String url = downloadUrl.toString().replace("https://", "http://"); + downloadUrl = new URL(url); + try { + connection = (HttpURLConnection) downloadUrl.openConnection(); + connection.setConnectTimeout(5000); + connection.setRequestMethod("HEAD"); + connection.setDoInput(false); + connection.connect(); + bHttpDownloadProblem = true; + return connection.getResponseCode() == HttpURLConnection.HTTP_OK; + } finally { + connection.disconnect(); + } } finally { connection.disconnect(); } @@ -391,14 +412,24 @@ private boolean httpFileExists(URL downloadUrl) throws IOException { private boolean downloadFile(URL downloadUrl, File outputFile, int fileSize, boolean limitDownloadSpeed, DownloadType type) throws IOException, InterruptedException { if (DEBUG) Log.d(LOG_TAG, "download " + outputFile.getAbsolutePath()); - HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(); - connection.setConnectTimeout(5000); - connection.setDefaultUseCaches(false); - + HttpURLConnection connection = null; InputStream input = null; OutputStream output = null; try { - connection.connect(); + try { + connection = (HttpURLConnection) downloadUrl.openConnection(); + connection.setConnectTimeout(5000); + connection.setDefaultUseCaches(false); + connection.connect(); + } catch (javax.net.ssl.SSLHandshakeException e) { + String url = downloadUrl.toString().replace("https://", "http://"); + downloadUrl = new URL(url); + connection = (HttpURLConnection) downloadUrl.openConnection(); + connection.setConnectTimeout(5000); + connection.setDefaultUseCaches(false); + connection.connect(); + bHttpDownloadProblem = true; + } if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP Request failed: " + downloadUrl + " returned " + connection.getResponseCode());