Skip to content

Commit

Permalink
Merge pull request #677 from afischerdev/new-apk
Browse files Browse the repository at this point in the history
App: certificate fallback
  • Loading branch information
afischerdev committed Apr 3, 2024
2 parents aa393ab + 6e858b6 commit 107a672
Showing 1 changed file with 40 additions and 9 deletions.
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -326,13 +329,16 @@ 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()) {
if (!versionChanged) { // no diff file on version change
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");
Expand Down Expand Up @@ -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();
}
Expand All @@ -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());
Expand Down

0 comments on commit 107a672

Please sign in to comment.