Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App: certificate fallback #677

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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