mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 04:43:41 +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;
|
options.headers!["X-Target-Uri"] = uri;
|
||||||
|
|
||||||
// add timestamp to avoid cache
|
// add timestamp to avoid cache
|
||||||
return _client.dio.request("/api/v1/proxy?t=${DateTime.now()}",
|
return _client.dio.request(
|
||||||
data: data, options: options);
|
"/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',
|
'https://github.com/XIU2/TrackersListCollection/raw/master/http.txt',
|
||||||
];
|
];
|
||||||
const allTrackerCdns = [
|
const allTrackerCdns = [
|
||||||
// jsdelivr: https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_all.txt
|
// jsdelivr: https://fastly.jsdelivr.net/gh/ngosang/trackerslist/trackers_all.txt
|
||||||
["https://cdn.jsdelivr.net/gh", r".*github.com(/.*)/raw/master(/.*)"],
|
["https://fastly.jsdelivr.net/gh", r".*github.com(/.*)/raw/master(/.*)"],
|
||||||
// nuaa: https://hub.nuaa.cf/ngosang/trackerslist/raw/master/trackers_all.txt
|
// ghproxy: https://ghproxy.com/https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt
|
||||||
["https://hub.nuaa.cf", r".*github.com(/.*)"]
|
[
|
||||||
|
"https://ghproxy.com/https://raw.githubusercontent.com",
|
||||||
|
r".*github.com(/.*)/raw(/.*)"
|
||||||
|
]
|
||||||
];
|
];
|
||||||
final allTrackerSubscribeUrlCdns = Map.fromIterable(allTrackerSubscribeUrls,
|
final allTrackerSubscribeUrlCdns = Map.fromIterable(allTrackerSubscribeUrls,
|
||||||
key: (v) => v as String,
|
key: (v) => v as String,
|
||||||
@@ -265,18 +268,16 @@ class AppController extends GetxController with WindowListener, TrayListener {
|
|||||||
final btExtConfig = downloaderConfig.value.extra.bt;
|
final btExtConfig = downloaderConfig.value.extra.bt;
|
||||||
final result = <String>[];
|
final result = <String>[];
|
||||||
for (var u in btExtConfig.trackerSubscribeUrls) {
|
for (var u in btExtConfig.trackerSubscribeUrls) {
|
||||||
var allFail = true;
|
final cdns = allTrackerSubscribeUrlCdns[u];
|
||||||
for (var cdn in allTrackerSubscribeUrlCdns[u]!) {
|
if (cdns == null) {
|
||||||
try {
|
continue;
|
||||||
result.addAll(await _fetchTrackers(cdn));
|
|
||||||
allFail = false;
|
|
||||||
break;
|
|
||||||
} catch (e) {
|
|
||||||
logger.w("subscribe trackers fail, url: $cdn", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (allFail) {
|
try {
|
||||||
throw Exception('subscribe trackers fail, network error');
|
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();
|
btExtConfig.subscribeTrackers.clear();
|
||||||
|
|||||||
@@ -28,17 +28,21 @@ class SettingController extends GetxController {
|
|||||||
void fetchLatestVersion() async {
|
void fetchLatestVersion() async {
|
||||||
String? releaseDataStr;
|
String? releaseDataStr;
|
||||||
try {
|
try {
|
||||||
releaseDataStr =
|
|
||||||
(await proxyRequest("https://gopeed.com/api/release")).data;
|
|
||||||
} catch (e) {
|
|
||||||
releaseDataStr = (await proxyRequest(
|
releaseDataStr = (await proxyRequest(
|
||||||
"https://api.github.com/repos/GopeedLab/gopeed/releases/latest"))
|
"https://api.github.com/repos/GopeedLab/gopeed/releases/latest"))
|
||||||
.data;
|
.data;
|
||||||
|
} catch (e) {
|
||||||
|
releaseDataStr =
|
||||||
|
(await proxyRequest("https://gopeed.com/api/release")).data;
|
||||||
}
|
}
|
||||||
if (releaseDataStr == null) {
|
if (releaseDataStr == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final releaseData = jsonDecode(releaseDataStr);
|
final releaseData = jsonDecode(releaseDataStr);
|
||||||
|
final tagName = releaseData["tag_name"];
|
||||||
|
if (tagName == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
latestVersion.value = releaseData["tag_name"].substring(1);
|
latestVersion.value = releaseData["tag_name"].substring(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
@@ -59,4 +60,27 @@ class Util {
|
|||||||
static isWeb() {
|
static isWeb() {
|
||||||
return kIsWeb;
|
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