Added "allow insecure request" option (#1825)

This commit is contained in:
Imran Remtulla
2024-09-08 03:58:51 -04:00
parent 19bb9a0331
commit d801994fed
26 changed files with 72 additions and 18 deletions

View File

@@ -26,7 +26,6 @@ class DirectAPKLink extends AppSource {
@override
String sourceSpecificStandardizeURL(String url, {bool forSelection = false}) {
print('AAA');
if (!forSelection) {
return url;
}

View File

@@ -350,7 +350,8 @@ class HTML extends AppSource {
? rel.hashCode.toString()
: (await checkPartialDownloadHashDynamic(rel,
headers: await getRequestHeaders(additionalSettings,
forAPKDownload: true)))
forAPKDownload: true),
allowInsecure: additionalSettings['allowInsecure'] == true))
.toString();
return APKDetails(version, [rel].map((e) => MapEntry(e, e)).toList(),
AppNames(uri.host, tr('app')));

View File

@@ -17,6 +17,7 @@ import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/io_client.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/components/generated_form_modal.dart';
import 'package:obtainium/custom_errors.dart';
@@ -146,17 +147,23 @@ Future<File> downloadFileWithRetry(String url, String fileName,
bool fileNameHasExt, Function? onProgress, String destDir,
{bool useExisting = true,
Map<String, String>? headers,
int retries = 3}) async {
int retries = 3,
bool allowInsecure = false}) async {
try {
return await downloadFile(
url, fileName, fileNameHasExt, onProgress, destDir,
useExisting: useExisting, headers: headers);
useExisting: useExisting,
headers: headers,
allowInsecure: allowInsecure);
} catch (e) {
if (retries > 0 && e is ClientException) {
await Future.delayed(const Duration(seconds: 5));
return await downloadFileWithRetry(
url, fileName, fileNameHasExt, onProgress, destDir,
useExisting: useExisting, headers: headers, retries: (retries - 1));
useExisting: useExisting,
headers: headers,
retries: (retries - 1),
allowInsecure: allowInsecure);
} else {
rethrow;
}
@@ -173,11 +180,14 @@ String hashListOfLists(List<List<int>> data) {
Future<String> checkPartialDownloadHashDynamic(String url,
{int startingSize = 1024,
int lowerLimit = 128,
Map<String, String>? headers}) async {
Map<String, String>? headers,
bool allowInsecure = false}) async {
for (int i = startingSize; i >= lowerLimit; i -= 256) {
List<String> ab = await Future.wait([
checkPartialDownloadHash(url, i, headers: headers),
checkPartialDownloadHash(url, i, headers: headers)
checkPartialDownloadHash(url, i,
headers: headers, allowInsecure: allowInsecure),
checkPartialDownloadHash(url, i,
headers: headers, allowInsecure: allowInsecure)
]);
if (ab[0] == ab[1]) {
return ab[0];
@@ -187,13 +197,13 @@ Future<String> checkPartialDownloadHashDynamic(String url,
}
Future<String> checkPartialDownloadHash(String url, int bytesToGrab,
{Map<String, String>? headers}) async {
{Map<String, String>? headers, bool allowInsecure = false}) async {
var req = Request('GET', Uri.parse(url));
if (headers != null) {
req.headers.addAll(headers);
}
req.headers[HttpHeaders.rangeHeader] = 'bytes=0-$bytesToGrab';
var client = http.Client();
var client = IOClient(createHttpClient(allowInsecure));
var response = await client.send(req);
if (response.statusCode < 200 || response.statusCode > 299) {
throw ObtainiumError(response.reasonPhrase ?? tr('unexpectedError'));
@@ -204,12 +214,14 @@ Future<String> checkPartialDownloadHash(String url, int bytesToGrab,
Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
Function? onProgress, String destDir,
{bool useExisting = true, Map<String, String>? headers}) async {
{bool useExisting = true,
Map<String, String>? headers,
bool allowInsecure = false}) async {
// Send the initial request but cancel it as soon as you have the headers
var reqHeaders = headers ?? {};
var req = Request('GET', Uri.parse(url));
req.headers.addAll(reqHeaders);
var client = http.Client();
var client = IOClient(createHttpClient(allowInsecure));
StreamedResponse response = await client.send(req);
var resHeaders = response.headers;
@@ -275,7 +287,7 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
IOSink? sink;
if (rangeFeatureEnabled && fullContentLength != null && rangeStart > 0) {
client.close();
client = http.Client();
client = IOClient(createHttpClient(allowInsecure));
req = Request('GET', Uri.parse(url));
req.headers.addAll(reqHeaders);
req.headers.addAll({'range': 'bytes=$rangeStart-${fullContentLength - 1}'});
@@ -318,12 +330,12 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
}
Future<Map<String, String>> getHeaders(String url,
{Map<String, String>? headers}) async {
{Map<String, String>? headers, bool allowInsecure = false}) async {
var req = http.Request('GET', Uri.parse(url));
if (headers != null) {
req.headers.addAll(headers);
}
var client = http.Client();
var client = IOClient(createHttpClient(allowInsecure));
var response = await client.send(req);
if (response.statusCode < 200 || response.statusCode > 299) {
throw ObtainiumError(response.reasonPhrase ?? tr('unexpectedError'));
@@ -468,7 +480,9 @@ class AppsProvider with ChangeNotifier {
notificationsProvider?.notify(notif);
}
prevProg = prog;
}, APKDir.path, useExisting: useExisting);
}, APKDir.path,
useExisting: useExisting,
allowInsecure: app.additionalSettings['allowInsecure'] == true);
// Set to 90 for remaining steps, will make null in 'finally'
if (apps[app.id] != null) {
apps[app.id]!.downloadProgress = -1;
@@ -1036,7 +1050,8 @@ class AppsProvider with ChangeNotifier {
.getRequestHeaders(app.additionalSettings,
forAPKDownload:
fileUrl.key.endsWith('.apk') ? true : false),
useExisting: false);
useExisting: false,
allowInsecure: app.additionalSettings['allowInsecure'] == true);
notificationsProvider
.notify(DownloadedNotification(fileUrl.key, fileUrl.value));
} catch (e) {

View File

@@ -2,11 +2,13 @@
// AppSource is an abstract class with a concrete implementation for each source
import 'dart:convert';
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:html/dom.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:obtainium/app_sources/apkmirror.dart';
import 'package:obtainium/app_sources/apkpure.dart';
import 'package:obtainium/app_sources/aptoide.dart';
@@ -399,6 +401,15 @@ getSourceRegex(List<String> hosts) {
return '(${hosts.join('|').replaceAll('.', '\\.')})';
}
HttpClient createHttpClient(bool insecure) {
final client = HttpClient();
if (insecure) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
return client;
}
abstract class AppSource {
List<String> hosts = [];
bool hostChanged = false;
@@ -462,7 +473,9 @@ abstract class AppSource {
if (requestHeaders != null) {
req.headers.addAll(requestHeaders);
}
return Response.fromStream(await Client().send(req));
return Response.fromStream(await IOClient(
createHttpClient(additionalSettings['allowInsecure'] == true))
.send(req));
} else {
return get(Uri.parse(url));
}
@@ -538,6 +551,10 @@ abstract class AppSource {
GeneratedFormSwitch('shizukuPretendToBeGooglePlay',
label: tr('shizukuPretendToBeGooglePlay'), defaultValue: false)
],
[
GeneratedFormSwitch('allowInsecure',
label: tr('allowInsecure'), defaultValue: false)
],
[
GeneratedFormSwitch('exemptFromBackgroundUpdates',
label: tr('exemptFromBackgroundUpdates'))