mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-04 23:57:54 +00:00
feat: improve tracker cdn (#155)
This commit is contained in:
4 files changed
+51
-20
No files matched your search
@@ -147,6 +147,8 @@ Future<Response<String>> proxyRequest<T>(String uri,
|
||||
options.headers!["X-Target-Uri"] = uri;
|
||||
|
||||
// add timestamp to avoid cache
|
||||
return _client.dio.request("/api/v1/proxy?t=${DateTime.now()}",
|
||||
data: data, options: options);
|
||||
return _client.dio.request(
|
||||
"/api/v1/proxy?t=${DateTime.now().millisecondsSinceEpoch}",
|
||||
data: data,
|
||||
options: options);
|
||||
}
|
||||
@@ -42,10 +42,13 @@ const allTrackerSubscribeUrls = [
|
||||
'https://github.com/XIU2/TrackersListCollection/raw/master/http.txt',
|
||||
];
|
||||
const allTrackerCdns = [
|
||||
// jsdelivr: https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_all.txt
|
||||
["https://cdn.jsdelivr.net/gh", r".*github.com(/.*)/raw/master(/.*)"],
|
||||
// nuaa: https://hub.nuaa.cf/ngosang/trackerslist/raw/master/trackers_all.txt
|
||||
["https://hub.nuaa.cf", r".*github.com(/.*)"]
|
||||
// jsdelivr: https://fastly.jsdelivr.net/gh/ngosang/trackerslist/trackers_all.txt
|
||||
["https://fastly.jsdelivr.net/gh", r".*github.com(/.*)/raw/master(/.*)"],
|
||||
// ghproxy: https://ghproxy.com/https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt
|
||||
[
|
||||
"https://ghproxy.com/https://raw.githubusercontent.com",
|
||||
r".*github.com(/.*)/raw(/.*)"
|
||||
]
|
||||
];
|
||||
final allTrackerSubscribeUrlCdns = Map.fromIterable(allTrackerSubscribeUrls,
|
||||
key: (v) => v as String,
|
||||
@@ -265,18 +268,16 @@ class AppController extends GetxController with WindowListener, TrayListener {
|
||||
final btExtConfig = downloaderConfig.value.extra.bt;
|
||||
final result = <String>[];
|
||||
for (var u in btExtConfig.trackerSubscribeUrls) {
|
||||
var allFail = true;
|
||||
for (var cdn in allTrackerSubscribeUrlCdns[u]!) {
|
||||
try {
|
||||
result.addAll(await _fetchTrackers(cdn));
|
||||
allFail = false;
|
||||
break;
|
||||
} catch (e) {
|
||||
logger.w("subscribe trackers fail, url: $cdn", e);
|
||||
}
|
||||
final cdns = allTrackerSubscribeUrlCdns[u];
|
||||
if (cdns == null) {
|
||||
continue;
|
||||
}
|
||||
if (allFail) {
|
||||
throw Exception('subscribe trackers fail, network error');
|
||||
try {
|
||||
final trackers =
|
||||
await Util.anyOk(cdns.map((cdn) => _fetchTrackers(cdn)));
|
||||
result.addAll(trackers);
|
||||
} catch (e) {
|
||||
logger.w("subscribe trackers fail, url: $u", e);
|
||||
}
|
||||
}
|
||||
btExtConfig.subscribeTrackers.clear();
|
||||
|
||||
@@ -28,17 +28,21 @@ class SettingController extends GetxController {
|
||||
void fetchLatestVersion() async {
|
||||
String? releaseDataStr;
|
||||
try {
|
||||
releaseDataStr =
|
||||
(await proxyRequest("https://gopeed.com/api/release")).data;
|
||||
} catch (e) {
|
||||
releaseDataStr = (await proxyRequest(
|
||||
"https://api.github.com/repos/GopeedLab/gopeed/releases/latest"))
|
||||
.data;
|
||||
} catch (e) {
|
||||
releaseDataStr =
|
||||
(await proxyRequest("https://gopeed.com/api/release")).data;
|
||||
}
|
||||
if (releaseDataStr == null) {
|
||||
return;
|
||||
}
|
||||
final releaseData = jsonDecode(releaseDataStr);
|
||||
final tagName = releaseData["tag_name"];
|
||||
if (tagName == null) {
|
||||
return;
|
||||
}
|
||||
latestVersion.value = releaseData["tag_name"].substring(1);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -59,4 +60,27 @@ class Util {
|
||||
static isWeb() {
|
||||
return kIsWeb;
|
||||
}
|
||||
|
||||
// if one future complete, return the result, only all future error, return the last error
|
||||
static anyOk<T>(Iterable<Future<T>> futures) {
|
||||
final completer = Completer<T>();
|
||||
Object lastError;
|
||||
var count = futures.length;
|
||||
for (var future in futures) {
|
||||
future.then((value) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(value);
|
||||
}
|
||||
}).catchError((e) {
|
||||
lastError = e;
|
||||
count--;
|
||||
print(count);
|
||||
if (count == 0) {
|
||||
print("completeError");
|
||||
completer.completeError(lastError);
|
||||
}
|
||||
});
|
||||
}
|
||||
return completer.future;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user