网络质量探测 1.1.3:自更新、HTTP/3 与中心节点
版本名 1.1.3,构建号 6。应用标识和签名未改。包含自动更新、覆盖安装、目标历史、IPv4/IPv6 开关、HTTP(含 HTTP/3)以及中心服务端一键添加节点。
This commit is contained in:
1 parent
943259774f
commit
8cdc0f436f
66 files changed
+5489
-78
No files matched your search
@@ -0,0 +1,237 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
/// Numeric version. A leading v and a +build suffix are ignored.
|
||||
class SemVer implements Comparable<SemVer> {
|
||||
const SemVer(this.major, this.minor, this.patch);
|
||||
|
||||
final int major;
|
||||
final int minor;
|
||||
final int patch;
|
||||
|
||||
static SemVer? parse(String raw) {
|
||||
var text = raw.trim();
|
||||
if (text.startsWith('v') || text.startsWith('V')) {
|
||||
text = text.substring(1);
|
||||
}
|
||||
final plus = text.indexOf('+');
|
||||
if (plus >= 0) {
|
||||
text = text.substring(0, plus);
|
||||
}
|
||||
final dash = text.indexOf('-');
|
||||
if (dash >= 0) {
|
||||
text = text.substring(0, dash);
|
||||
}
|
||||
final parts = text.split('.');
|
||||
if (parts.length < 3) {
|
||||
return null;
|
||||
}
|
||||
final major = int.tryParse(parts[0]);
|
||||
final minor = int.tryParse(parts[1]);
|
||||
final patch = int.tryParse(parts[2]);
|
||||
if (major == null || minor == null || patch == null) {
|
||||
return null;
|
||||
}
|
||||
return SemVer(major, minor, patch);
|
||||
}
|
||||
|
||||
bool isNewerThan(SemVer other) => compareTo(other) > 0;
|
||||
|
||||
@override
|
||||
int compareTo(SemVer other) {
|
||||
if (major != other.major) {
|
||||
return major.compareTo(other.major);
|
||||
}
|
||||
if (minor != other.minor) {
|
||||
return minor.compareTo(other.minor);
|
||||
}
|
||||
return patch.compareTo(other.patch);
|
||||
}
|
||||
}
|
||||
|
||||
class ReleaseFile {
|
||||
const ReleaseFile({required this.name, required this.url});
|
||||
|
||||
final String name;
|
||||
final String url;
|
||||
}
|
||||
|
||||
class UpdateEndpoints {
|
||||
const UpdateEndpoints({required this.github, required this.gitea});
|
||||
|
||||
static const githubDefault =
|
||||
'https://api.github.com/repos/yanjiu0721/netquality/releases/latest';
|
||||
static const giteaDefault =
|
||||
'https://git.yanjiu.ltd/api/v1/repos/YanJiu/netquality/releases/latest';
|
||||
|
||||
final String github;
|
||||
final String gitea;
|
||||
}
|
||||
|
||||
class UpdateOffer {
|
||||
const UpdateOffer({
|
||||
required this.version,
|
||||
required this.assetName,
|
||||
required this.assetUrl,
|
||||
this.checksumName,
|
||||
this.checksumUrl,
|
||||
});
|
||||
|
||||
final String version;
|
||||
final String assetName;
|
||||
final String assetUrl;
|
||||
final String? checksumName;
|
||||
final String? checksumUrl;
|
||||
}
|
||||
|
||||
class UpdateCheck {
|
||||
const UpdateCheck({this.offer, required this.reachable, this.message = ''});
|
||||
|
||||
final UpdateOffer? offer;
|
||||
final bool reachable;
|
||||
final String message;
|
||||
}
|
||||
|
||||
/// Picks the client archive for this OS and CPU. Server packages are ignored.
|
||||
String? pickClientAsset(
|
||||
List<ReleaseFile> assets, {
|
||||
required String os,
|
||||
required String arch,
|
||||
}) {
|
||||
final wantOs = os.toLowerCase();
|
||||
final wantArch = arch.toLowerCase();
|
||||
for (final asset in assets) {
|
||||
final name = asset.name.toLowerCase();
|
||||
if (name.contains('server') || name.contains('reflector')) {
|
||||
continue;
|
||||
}
|
||||
if (wantOs == 'android') {
|
||||
if (name.endsWith('.apk') && name.contains(wantArch)) {
|
||||
return asset.name;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
final archOk =
|
||||
name.contains(wantArch) ||
|
||||
(wantArch == 'amd64' && name.contains('x86_64')) ||
|
||||
(wantArch == 'arm64' && name.contains('aarch64'));
|
||||
if (!archOk || !name.contains(wantOs)) {
|
||||
continue;
|
||||
}
|
||||
if (wantOs == 'windows' && (name.endsWith('.zip') || name.endsWith('.exe'))) {
|
||||
return asset.name;
|
||||
}
|
||||
if (wantOs == 'linux' &&
|
||||
(name.endsWith('.tar.gz') || name.endsWith('.tgz'))) {
|
||||
return asset.name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
bool looksLikeChecksum(String name) {
|
||||
final lower = name.toLowerCase();
|
||||
return lower.contains('sha256') || lower.contains('checksum');
|
||||
}
|
||||
|
||||
UpdateCheck interpretRelease({
|
||||
required String current,
|
||||
required String? body,
|
||||
required String os,
|
||||
required String arch,
|
||||
}) {
|
||||
if (body == null || body.trim().isEmpty) {
|
||||
return const UpdateCheck(reachable: false, message: '检查更新失败');
|
||||
}
|
||||
Object? decoded;
|
||||
try {
|
||||
decoded = jsonDecode(body);
|
||||
} on FormatException {
|
||||
return const UpdateCheck(reachable: false, message: '检查更新失败');
|
||||
}
|
||||
if (decoded is! Map) {
|
||||
return const UpdateCheck(reachable: false, message: '检查更新失败');
|
||||
}
|
||||
final tag = decoded['tag_name'];
|
||||
final remote = tag is String ? SemVer.parse(tag) : null;
|
||||
final local = SemVer.parse(current);
|
||||
if (remote == null || local == null) {
|
||||
return const UpdateCheck(reachable: true, message: '发布说明里的版本号无法比较');
|
||||
}
|
||||
if (!remote.isNewerThan(local)) {
|
||||
return UpdateCheck(reachable: true, message: '当前已经是 $current');
|
||||
}
|
||||
final assets = <ReleaseFile>[];
|
||||
final rawAssets = decoded['assets'];
|
||||
if (rawAssets is List) {
|
||||
for (final item in rawAssets) {
|
||||
if (item is! Map) {
|
||||
continue;
|
||||
}
|
||||
final name = item['name'];
|
||||
final url = item['browser_download_url'];
|
||||
final link = url is String ? url : '';
|
||||
if (name is String && link.isNotEmpty) {
|
||||
assets.add(ReleaseFile(name: name, url: link));
|
||||
}
|
||||
}
|
||||
}
|
||||
final pickedName = pickClientAsset(assets, os: os, arch: arch);
|
||||
if (pickedName == null) {
|
||||
return UpdateCheck(
|
||||
reachable: true,
|
||||
message: '发现 ${remote.major}.${remote.minor}.${remote.patch},但没有适合这台设备的安装包',
|
||||
);
|
||||
}
|
||||
final picked = assets.firstWhere((asset) => asset.name == pickedName);
|
||||
ReleaseFile? sums;
|
||||
for (final asset in assets) {
|
||||
if (looksLikeChecksum(asset.name)) {
|
||||
sums = asset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return UpdateCheck(
|
||||
reachable: true,
|
||||
offer: UpdateOffer(
|
||||
version: '${remote.major}.${remote.minor}.${remote.patch}',
|
||||
assetName: picked.name,
|
||||
assetUrl: picked.url,
|
||||
checksumName: sums?.name,
|
||||
checksumUrl: sums?.url,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<UpdateCheck> findUpdate({
|
||||
required String current,
|
||||
required UpdateEndpoints endpoints,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
required String os,
|
||||
required String arch,
|
||||
}) async {
|
||||
String? body;
|
||||
try {
|
||||
body = await fetch(Uri.parse(endpoints.github));
|
||||
} on Object {
|
||||
body = null;
|
||||
}
|
||||
var check = interpretRelease(current: current, body: body, os: os, arch: arch);
|
||||
if (!check.reachable) {
|
||||
try {
|
||||
body = await fetch(Uri.parse(endpoints.gitea));
|
||||
} on Object {
|
||||
body = null;
|
||||
}
|
||||
check = interpretRelease(current: current, body: body, os: os, arch: arch);
|
||||
if (!check.reachable) {
|
||||
return const UpdateCheck(
|
||||
reachable: false,
|
||||
message: 'GitHub 和备用源都连不上,没有检查到更新',
|
||||
);
|
||||
}
|
||||
}
|
||||
return check;
|
||||
}
|
||||
Reference in new issue
Block a user