mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-16 06:36:44 +02:00
Always follow redirects and store cookies between redirects, including for downloads —useful for https://xeiaso.net/blog/2025/anubis (#2264)
This commit is contained in:
@ -7,7 +7,6 @@ import 'dart:io';
|
|||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'package:battery_plus/battery_plus.dart';
|
import 'package:battery_plus/battery_plus.dart';
|
||||||
import 'package:fluttertoast/fluttertoast.dart';
|
import 'package:fluttertoast/fluttertoast.dart';
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
import 'package:crypto/crypto.dart';
|
import 'package:crypto/crypto.dart';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
@ -246,9 +245,9 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
var reqHeaders = headers ?? {};
|
var reqHeaders = headers ?? {};
|
||||||
var req = Request('GET', Uri.parse(url));
|
var req = Request('GET', Uri.parse(url));
|
||||||
req.headers.addAll(reqHeaders);
|
req.headers.addAll(reqHeaders);
|
||||||
var client = IOClient(createHttpClient(allowInsecure));
|
var headersClient = IOClient(createHttpClient(allowInsecure));
|
||||||
StreamedResponse response = await client.send(req);
|
StreamedResponse headersResponse = await headersClient.send(req);
|
||||||
var resHeaders = response.headers;
|
var resHeaders = headersResponse.headers;
|
||||||
|
|
||||||
// Use the headers to decide what the file extension is, and
|
// Use the headers to decide what the file extension is, and
|
||||||
// whether it supports partial downloads (range request), and
|
// whether it supports partial downloads (range request), and
|
||||||
@ -276,21 +275,20 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
rangeFeatureEnabled =
|
rangeFeatureEnabled =
|
||||||
resHeaders['accept-ranges']?.trim().toLowerCase() == 'bytes';
|
resHeaders['accept-ranges']?.trim().toLowerCase() == 'bytes';
|
||||||
}
|
}
|
||||||
|
headersClient.close();
|
||||||
|
|
||||||
// If you have an existing file that is usable,
|
// If you have an existing file that is usable,
|
||||||
// decide whether you can use it (either return full or resume partial)
|
// decide whether you can use it (either return full or resume partial)
|
||||||
var fullContentLength = response.contentLength;
|
var fullContentLength = headersResponse.contentLength;
|
||||||
if (useExisting && downloadedFile.existsSync()) {
|
if (useExisting && downloadedFile.existsSync()) {
|
||||||
var length = downloadedFile.lengthSync();
|
var length = downloadedFile.lengthSync();
|
||||||
if (fullContentLength == null || !rangeFeatureEnabled) {
|
if (fullContentLength == null || !rangeFeatureEnabled) {
|
||||||
// If there is no content length reported, assume it the existing file is fully downloaded
|
// If there is no content length reported, assume it the existing file is fully downloaded
|
||||||
// Also if the range feature is not supported, don't trust the content length if any (#1542)
|
// Also if the range feature is not supported, don't trust the content length if any (#1542)
|
||||||
client.close();
|
|
||||||
return downloadedFile;
|
return downloadedFile;
|
||||||
} else {
|
} else {
|
||||||
// Check if resume needed/possible
|
// Check if resume needed/possible
|
||||||
if (length == fullContentLength) {
|
if (length == fullContentLength) {
|
||||||
client.close();
|
|
||||||
return downloadedFile;
|
return downloadedFile;
|
||||||
}
|
}
|
||||||
if (length > fullContentLength) {
|
if (length > fullContentLength) {
|
||||||
@ -330,7 +328,6 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
if (shouldReturn) {
|
if (shouldReturn) {
|
||||||
logs?.add(
|
logs?.add(
|
||||||
'Existing partial download completed - not repeating: ${tempDownloadedFile.uri.pathSegments.last}');
|
'Existing partial download completed - not repeating: ${tempDownloadedFile.uri.pathSegments.last}');
|
||||||
client.close();
|
|
||||||
return downloadedFile;
|
return downloadedFile;
|
||||||
} else {
|
} else {
|
||||||
logs?.add(
|
logs?.add(
|
||||||
@ -346,17 +343,18 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
: null;
|
: null;
|
||||||
int rangeStart = targetFileLength ?? 0;
|
int rangeStart = targetFileLength ?? 0;
|
||||||
IOSink? sink;
|
IOSink? sink;
|
||||||
|
req = Request('GET', Uri.parse(url));
|
||||||
|
req.headers.addAll(reqHeaders);
|
||||||
if (rangeFeatureEnabled && fullContentLength != null && rangeStart > 0) {
|
if (rangeFeatureEnabled && fullContentLength != null && rangeStart > 0) {
|
||||||
client.close();
|
reqHeaders.addAll({'range': 'bytes=$rangeStart-${fullContentLength - 1}'});
|
||||||
client = IOClient(createHttpClient(allowInsecure));
|
|
||||||
req = Request('GET', Uri.parse(url));
|
|
||||||
req.headers.addAll(reqHeaders);
|
|
||||||
req.headers.addAll({'range': 'bytes=$rangeStart-${fullContentLength - 1}'});
|
|
||||||
response = await client.send(req);
|
|
||||||
sink = tempDownloadedFile.openWrite(mode: FileMode.writeOnlyAppend);
|
sink = tempDownloadedFile.openWrite(mode: FileMode.writeOnlyAppend);
|
||||||
} else if (tempDownloadedFile.existsSync()) {
|
} else if (tempDownloadedFile.existsSync()) {
|
||||||
tempDownloadedFile.deleteSync(recursive: true);
|
tempDownloadedFile.deleteSync(recursive: true);
|
||||||
}
|
}
|
||||||
|
var responseWithClient =
|
||||||
|
await sourceRequestStreamResponse('GET', url, reqHeaders, {});
|
||||||
|
HttpClient responseClient = responseWithClient.key;
|
||||||
|
HttpClientResponse response = responseWithClient.value;
|
||||||
sink ??= tempDownloadedFile.openWrite(mode: FileMode.writeOnly);
|
sink ??= tempDownloadedFile.openWrite(mode: FileMode.writeOnly);
|
||||||
|
|
||||||
// Perform the download
|
// Perform the download
|
||||||
@ -369,7 +367,8 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
const downloadUIUpdateInterval = Duration(milliseconds: 500);
|
const downloadUIUpdateInterval = Duration(milliseconds: 500);
|
||||||
const downloadBufferSize = 32 * 1024; // 32KB
|
const downloadBufferSize = 32 * 1024; // 32KB
|
||||||
final downloadBuffer = BytesBuilder();
|
final downloadBuffer = BytesBuilder();
|
||||||
await response.stream
|
await response
|
||||||
|
.asBroadcastStream()
|
||||||
.map((chunk) {
|
.map((chunk) {
|
||||||
received += chunk.length;
|
received += chunk.length;
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
@ -407,31 +406,15 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
}
|
}
|
||||||
if (response.statusCode < 200 || response.statusCode > 299) {
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
||||||
tempDownloadedFile.deleteSync(recursive: true);
|
tempDownloadedFile.deleteSync(recursive: true);
|
||||||
throw response.reasonPhrase ?? tr('unexpectedError');
|
throw response.reasonPhrase;
|
||||||
}
|
}
|
||||||
if (tempDownloadedFile.existsSync()) {
|
if (tempDownloadedFile.existsSync()) {
|
||||||
tempDownloadedFile.renameSync(downloadedFile.path);
|
tempDownloadedFile.renameSync(downloadedFile.path);
|
||||||
}
|
}
|
||||||
client.close();
|
responseClient.close();
|
||||||
return downloadedFile;
|
return downloadedFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, String>> getHeaders(String url,
|
|
||||||
{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 = IOClient(createHttpClient(allowInsecure));
|
|
||||||
var response = await client.send(req);
|
|
||||||
if (response.statusCode < 200 || response.statusCode > 299) {
|
|
||||||
throw ObtainiumError(response.reasonPhrase ?? tr('unexpectedError'));
|
|
||||||
}
|
|
||||||
var returnHeaders = response.headers;
|
|
||||||
client.close();
|
|
||||||
return returnHeaders;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<List<PackageInfo>> getAllInstalledInfo() async {
|
Future<List<PackageInfo>> getAllInstalledInfo() async {
|
||||||
return await pm.getInstalledPackages() ?? [];
|
return await pm.getInstalledPackages() ?? [];
|
||||||
}
|
}
|
||||||
|
@ -510,6 +510,75 @@ HttpClient createHttpClient(bool insecure) {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<MapEntry<HttpClient, HttpClientResponse>> sourceRequestStreamResponse(
|
||||||
|
String method,
|
||||||
|
String url,
|
||||||
|
Map<String, String>? requestHeaders,
|
||||||
|
Map<String, dynamic> additionalSettings,
|
||||||
|
{bool followRedirects = true,
|
||||||
|
Object? postBody}) async {
|
||||||
|
var currentUrl = Uri.parse(url);
|
||||||
|
var redirectCount = 0;
|
||||||
|
const maxRedirects = 10;
|
||||||
|
List<Cookie> cookies = [];
|
||||||
|
while (redirectCount < maxRedirects) {
|
||||||
|
var httpClient =
|
||||||
|
createHttpClient(additionalSettings['allowInsecure'] == true);
|
||||||
|
var request = await httpClient.openUrl(method, currentUrl);
|
||||||
|
if (requestHeaders != null) {
|
||||||
|
requestHeaders.forEach((key, value) {
|
||||||
|
request.headers.set(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
request.cookies.addAll(cookies);
|
||||||
|
request.followRedirects = false;
|
||||||
|
if (postBody != null) {
|
||||||
|
request.headers.contentType = ContentType.json;
|
||||||
|
request.write(jsonEncode(postBody));
|
||||||
|
}
|
||||||
|
final response = await request.close();
|
||||||
|
|
||||||
|
if (followRedirects &&
|
||||||
|
(response.statusCode >= 300 && response.statusCode <= 399)) {
|
||||||
|
final location = response.headers.value(HttpHeaders.locationHeader);
|
||||||
|
if (location != null) {
|
||||||
|
currentUrl = Uri.parse(ensureAbsoluteUrl(location, currentUrl));
|
||||||
|
redirectCount++;
|
||||||
|
cookies = response.cookies;
|
||||||
|
httpClient.close();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MapEntry(httpClient, response);
|
||||||
|
}
|
||||||
|
throw ObtainiumError('Too many redirects ($maxRedirects)');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Response> httpClientResponseStreamToFinalResponse(
|
||||||
|
HttpClient httpClient,
|
||||||
|
String method,
|
||||||
|
String url,
|
||||||
|
HttpClientResponse response) async {
|
||||||
|
final bytes =
|
||||||
|
(await response.fold<BytesBuilder>(BytesBuilder(), (b, d) => b..add(d)))
|
||||||
|
.toBytes();
|
||||||
|
|
||||||
|
final headers = <String, String>{};
|
||||||
|
response.headers.forEach((name, values) {
|
||||||
|
headers[name] = values.join(', ');
|
||||||
|
});
|
||||||
|
|
||||||
|
httpClient.close();
|
||||||
|
|
||||||
|
return http.Response.bytes(
|
||||||
|
bytes,
|
||||||
|
response.statusCode,
|
||||||
|
headers: headers,
|
||||||
|
request: http.Request(method, Uri.parse(url)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
abstract class AppSource {
|
abstract class AppSource {
|
||||||
List<String> hosts = [];
|
List<String> hosts = [];
|
||||||
bool hostChanged = false;
|
bool hostChanged = false;
|
||||||
@ -567,64 +636,15 @@ abstract class AppSource {
|
|||||||
Future<Response> sourceRequest(
|
Future<Response> sourceRequest(
|
||||||
String url, Map<String, dynamic> additionalSettings,
|
String url, Map<String, dynamic> additionalSettings,
|
||||||
{bool followRedirects = true, Object? postBody}) async {
|
{bool followRedirects = true, Object? postBody}) async {
|
||||||
|
var method = postBody == null ? 'GET' : 'POST';
|
||||||
var requestHeaders = await getRequestHeaders(additionalSettings);
|
var requestHeaders = await getRequestHeaders(additionalSettings);
|
||||||
|
var streamedResponseAndClient = await sourceRequestStreamResponse(
|
||||||
if (requestHeaders != null || followRedirects == false) {
|
method, url, requestHeaders, additionalSettings);
|
||||||
var method = postBody == null ? 'GET' : 'POST';
|
return await httpClientResponseStreamToFinalResponse(
|
||||||
var currentUrl = url;
|
streamedResponseAndClient.key,
|
||||||
var redirectCount = 0;
|
method,
|
||||||
const maxRedirects = 10;
|
url,
|
||||||
while (redirectCount < maxRedirects) {
|
streamedResponseAndClient.value);
|
||||||
var httpClient =
|
|
||||||
createHttpClient(additionalSettings['allowInsecure'] == true);
|
|
||||||
var request = await httpClient.openUrl(method, Uri.parse(currentUrl));
|
|
||||||
if (requestHeaders != null) {
|
|
||||||
requestHeaders.forEach((key, value) {
|
|
||||||
request.headers.set(key, value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
request.followRedirects = false;
|
|
||||||
if (postBody != null) {
|
|
||||||
request.headers.contentType = ContentType.json;
|
|
||||||
request.write(jsonEncode(postBody));
|
|
||||||
}
|
|
||||||
final response = await request.close();
|
|
||||||
|
|
||||||
if (followRedirects &&
|
|
||||||
(response.statusCode == 301 || response.statusCode == 302)) {
|
|
||||||
final location = response.headers.value(HttpHeaders.locationHeader);
|
|
||||||
if (location != null) {
|
|
||||||
currentUrl = location;
|
|
||||||
redirectCount++;
|
|
||||||
httpClient.close();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final bytes = (await response.fold<BytesBuilder>(
|
|
||||||
BytesBuilder(), (b, d) => b..add(d)))
|
|
||||||
.toBytes();
|
|
||||||
|
|
||||||
final headers = <String, String>{};
|
|
||||||
response.headers.forEach((name, values) {
|
|
||||||
headers[name] = values.join(', ');
|
|
||||||
});
|
|
||||||
|
|
||||||
httpClient.close();
|
|
||||||
|
|
||||||
return http.Response.bytes(
|
|
||||||
bytes,
|
|
||||||
response.statusCode,
|
|
||||||
headers: headers,
|
|
||||||
request: http.Request(method, Uri.parse(url)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
throw ObtainiumError('Too many redirects ($maxRedirects)');
|
|
||||||
} else {
|
|
||||||
return postBody == null
|
|
||||||
? http.get(Uri.parse(url))
|
|
||||||
: http.post(Uri.parse(url), body: jsonEncode(postBody));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void runOnAddAppInputChange(String inputUrl) {
|
void runOnAddAppInputChange(String inputUrl) {
|
||||||
|
Reference in New Issue
Block a user