网络质量探测 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,147 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../models/session_config.dart';
|
||||
|
||||
class CatalogServer {
|
||||
const CatalogServer({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.confidential,
|
||||
this.controlUrl = '',
|
||||
this.credential = '',
|
||||
this.credentialExpiresAt = '',
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String name;
|
||||
final bool confidential;
|
||||
final String controlUrl;
|
||||
final String credential;
|
||||
final String credentialExpiresAt;
|
||||
}
|
||||
|
||||
class Catalog {
|
||||
const Catalog({
|
||||
required this.account,
|
||||
required this.group,
|
||||
required this.servers,
|
||||
});
|
||||
|
||||
final String account;
|
||||
final String group;
|
||||
final List<CatalogServer> servers;
|
||||
|
||||
/// Parses a catalog. Confidential entries drop any address or credential
|
||||
/// the server might have included by mistake.
|
||||
factory Catalog.parse(String body) {
|
||||
final decoded = jsonDecode(body);
|
||||
if (decoded is! Map) {
|
||||
throw const FormatException('目录无效');
|
||||
}
|
||||
final rawServers = decoded['servers'];
|
||||
final servers = <CatalogServer>[];
|
||||
if (rawServers is List) {
|
||||
for (final item in rawServers) {
|
||||
if (item is! Map) {
|
||||
continue;
|
||||
}
|
||||
final confidential = item['confidential'] == true;
|
||||
final url = item['controlUrl'] is String ? item['controlUrl'] as String : '';
|
||||
final credential = item['credential'] is String
|
||||
? item['credential'] as String
|
||||
: '';
|
||||
servers.add(
|
||||
CatalogServer(
|
||||
id: item['id'] is String ? item['id'] as String : '',
|
||||
name: item['name'] is String ? item['name'] as String : '未命名',
|
||||
confidential: confidential,
|
||||
controlUrl: confidential ? '' : url,
|
||||
credential: confidential ? '' : credential,
|
||||
credentialExpiresAt: confidential
|
||||
? ''
|
||||
: (item['credentialExpiresAt'] is String
|
||||
? item['credentialExpiresAt'] as String
|
||||
: ''),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
return Catalog(
|
||||
account: decoded['account'] is String ? decoded['account'] as String : '',
|
||||
group: decoded['group'] is String ? decoded['group'] as String : '',
|
||||
servers: servers,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CenterSyncResult {
|
||||
const CenterSyncResult({required this.nodes, required this.credentials});
|
||||
|
||||
final List<RemoteNode> nodes;
|
||||
|
||||
/// Node id to short-lived credential. Confidential nodes are absent.
|
||||
/// The node's long-lived token is never in this map.
|
||||
final Map<String, String> credentials;
|
||||
}
|
||||
|
||||
/// Replaces the servers that belong to [link] with the catalog.
|
||||
/// Manual nodes and servers from other centers stay. Confidential nodes
|
||||
/// are stored with an empty address.
|
||||
CenterSyncResult mergeCenterCatalog({
|
||||
required List<RemoteNode> existing,
|
||||
required CenterLink link,
|
||||
required Catalog catalog,
|
||||
required String Function() newId,
|
||||
}) {
|
||||
final kept = [
|
||||
for (final node in existing)
|
||||
if (!node.fromCenter || node.centerLinkId != link.id) node,
|
||||
];
|
||||
final previous = {
|
||||
for (final node in existing)
|
||||
if (node.fromCenter && node.centerLinkId == link.id) node.serverId: node,
|
||||
};
|
||||
final credentials = <String, String>{};
|
||||
final added = <RemoteNode>[];
|
||||
for (final server in catalog.servers) {
|
||||
if (server.id.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
final old = previous[server.id];
|
||||
final id = old?.id ?? newId();
|
||||
final node = RemoteNode(
|
||||
id: id,
|
||||
name: server.name,
|
||||
baseUrl: server.confidential ? '' : server.controlUrl,
|
||||
visibility: server.confidential ? 'confidential' : 'center',
|
||||
fromCenter: true,
|
||||
confidential: server.confidential,
|
||||
centerLinkId: link.id,
|
||||
serverId: server.id,
|
||||
);
|
||||
added.add(node);
|
||||
if (!server.confidential && server.credential.isNotEmpty) {
|
||||
credentials[id] = server.credential;
|
||||
}
|
||||
}
|
||||
return CenterSyncResult(nodes: [...kept, ...added], credentials: credentials);
|
||||
}
|
||||
|
||||
/// Center-issued names cannot be changed on the client.
|
||||
RemoteNode renameNode(RemoteNode node, String name) {
|
||||
if (node.fromCenter) {
|
||||
return node;
|
||||
}
|
||||
return node.copyWith(name: name);
|
||||
}
|
||||
|
||||
bool storedNodeHides(RemoteNode node, String secret) {
|
||||
if (!node.confidential || secret.isEmpty) {
|
||||
return true;
|
||||
}
|
||||
final raw = jsonEncode(node.toJson());
|
||||
return !raw.contains(secret);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../models/session_config.dart';
|
||||
import 'catalog.dart';
|
||||
|
||||
class CenterClientException implements Exception {
|
||||
CenterClientException(this.message);
|
||||
final String message;
|
||||
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
Future<String> loginCenter({
|
||||
required String baseUrl,
|
||||
required String account,
|
||||
required String password,
|
||||
}) async {
|
||||
final body = await _send(
|
||||
baseUrl,
|
||||
'POST',
|
||||
'/v1/center/login',
|
||||
'',
|
||||
{'account': account.trim(), 'password': password},
|
||||
);
|
||||
final token = body['token'];
|
||||
if (token is! String || token.isEmpty) {
|
||||
throw CenterClientException('中心没有返回口令');
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
Future<Catalog> fetchCatalog({
|
||||
required String baseUrl,
|
||||
required String token,
|
||||
}) async {
|
||||
final body = await _send(baseUrl, 'GET', '/v1/center/catalog', token, null);
|
||||
return Catalog.parse(jsonEncode(body));
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _send(
|
||||
String baseUrl,
|
||||
String method,
|
||||
String path,
|
||||
String token,
|
||||
Map<String, Object?>? jsonBody,
|
||||
) async {
|
||||
final normalized = normalizeNodeUrl(baseUrl);
|
||||
if (normalized == null) {
|
||||
throw CenterClientException('中心地址需要是 http 或 https,且不能把口令写在地址里');
|
||||
}
|
||||
final client = HttpClient();
|
||||
try {
|
||||
final request = await client.openUrl(
|
||||
method,
|
||||
Uri.parse(normalized).resolve(path),
|
||||
);
|
||||
request.followRedirects = false;
|
||||
if (token.isNotEmpty) {
|
||||
request.headers.set(HttpHeaders.authorizationHeader, 'Bearer $token');
|
||||
}
|
||||
if (jsonBody != null) {
|
||||
request.headers.contentType = ContentType.json;
|
||||
request.add(utf8.encode(jsonEncode(jsonBody)));
|
||||
}
|
||||
final response = await request.close().timeout(const Duration(seconds: 15));
|
||||
final text = await response.transform(utf8.decoder).join();
|
||||
if (response.statusCode >= 300 && response.statusCode < 400) {
|
||||
throw CenterClientException('中心地址发生了跳转。请填写最终的 HTTPS 地址');
|
||||
}
|
||||
if (response.statusCode == 401) {
|
||||
throw CenterClientException('中心口令不正确,或账号密码不对');
|
||||
}
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
throw CenterClientException(_message(text) ?? '中心拒绝了这次请求');
|
||||
}
|
||||
final decoded = jsonDecode(text);
|
||||
if (decoded is! Map) {
|
||||
throw CenterClientException('中心返回的内容无效');
|
||||
}
|
||||
return Map<String, dynamic>.from(decoded);
|
||||
} on CenterClientException {
|
||||
rethrow;
|
||||
} on SocketException {
|
||||
throw CenterClientException('连不上中心。请检查地址和防火墙');
|
||||
} on HandshakeException {
|
||||
throw CenterClientException('TLS 握手失败。请改用正确的 HTTPS 地址');
|
||||
} on FormatException {
|
||||
throw CenterClientException('中心返回的内容无效');
|
||||
} finally {
|
||||
client.close(force: true);
|
||||
}
|
||||
}
|
||||
|
||||
String? _message(String body) {
|
||||
try {
|
||||
final json = jsonDecode(body);
|
||||
if (json is Map && json['message'] is String) {
|
||||
return json['message'] as String;
|
||||
}
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user