api修改和更新检查器修改

modrinth_api.dart:拦截器改为只替换 options.baseUrl,path 保持相对 /v2/search、query 留在 queryParameters,避免"整串写回 path"导致的 host 缺失 + query 重复拼接。mirror_service.dart:新增 apiBase getter,返回当前镜像的 API base(official→api.modrinth.com、mcim→modrinth-api.mcimirror.top、custom→自定义 base)

Signed-off-by: driftinblock <3480428251@qq.com>
This commit is contained in:
Ventiの酒 committed 2026-11-04 17:43:57 +08:00
1 parent ddd72c43fb
commit e32125aa8e
35 files changed
+4171 -1325

No files matched your search

+37 -11
View File
@@ -25,6 +25,28 @@ class MirrorService extends ChangeNotifier {
return builtinMirrors[_current] ?? _current;
}
/// 当前镜像的 API base(含 scheme、无尾斜杠),供请求 baseUrl 使用。
///
/// 仅替换 host,路径与查询串由调用方保持原样,避免与 queryParameters 二次拼接。
/// MCIM 现行规则:Modrinth API 为 `mod.mcimirror.top/modrinth`(带 /modrinth 前缀),
/// 请求路径 `/v2/search` 会拼成 `.../modrinth/v2/search`。
String get apiBase {
switch (_current) {
case 'official':
return 'https://api.modrinth.com';
case 'mcim':
return 'https://mod.mcimirror.top/modrinth';
case 'custom':
if (_customUrl.isEmpty) return 'https://api.modrinth.com';
final trimmed = _customUrl.endsWith('/')
? _customUrl.substring(0, _customUrl.length - 1)
: _customUrl;
return trimmed;
default:
return 'https://api.modrinth.com';
}
}
Future<void> load() async {
final prefs = await SharedPreferences.getInstance();
_current = prefs.getString(_prefKey) ?? 'mcim';
@@ -43,9 +65,10 @@ class MirrorService extends ChangeNotifier {
/// 把 Modrinth 官方域名改写为镜像域名。
///
/// 镜像规则:
/// - MCIM:api.modrinth.com → modrinth-api.mcimirror.top
/// cdn.modrinth.com → modrinth-cdn.mcimirror.top
/// 镜像规则(MCIM 官方 2026 现行规则,已核对 docs.mcimirror.top):
/// - MCIM:api.modrinth.com/v2/... → mod.mcimirror.top/modrinth/v2/...(带前缀)
/// cdn.modrinth.com/... → mod.mcimirror.top/...(仅换 host)
/// 旧的 modrinth-api.mcimirror.top / modrinth-cdn.mcimirror.top 已废弃、DNS 无法解析
/// - official:原样返回
/// - custom:以自定义 base 替换 scheme/host,保留路径与查询串
String rewrite(String url) {
@@ -62,13 +85,16 @@ class MirrorService extends ChangeNotifier {
}
return url;
}
// MCIM
final newHost = uri.host == 'api.modrinth.com'
? 'modrinth-api.mcimirror.top'
: uri.host == 'cdn.modrinth.com'
? 'modrinth-cdn.mcimirror.top'
: uri.host;
return uri.replace(host: newHost).toString();
// MCIM:API 需加 /modrinth 路径前缀,CDN 仅换 host
if (uri.host == 'api.modrinth.com') {
return uri
.replace(host: 'mod.mcimirror.top', path: '/modrinth${uri.path}')
.toString();
}
if (uri.host == 'cdn.modrinth.com') {
return uri.replace(host: 'mod.mcimirror.top').toString();
}
return url;
}
/// 测速:对镜像的 api 域名发 HEAD 请求,返回毫秒数;失败返回 -1。
@@ -78,7 +104,7 @@ class MirrorService extends ChangeNotifier {
Future<int> speedTest(String mirrorId) async {
final String probe = switch (mirrorId) {
'official' => 'https://api.modrinth.com/v2/project/sodium/version?limit=1',
'mcim' => 'https://modrinth-api.mcimirror.top/v2/project/sodium/version?limit=1',
'mcim' => 'https://mod.mcimirror.top/modrinth/v2/project/sodium/version?limit=1',
_ => _customUrl,
};
if (probe.isEmpty) return -1;
+46 -5
View File
@@ -15,8 +15,10 @@ class ModrinthApi {
));
d.interceptors.add(InterceptorsWrapper(
onRequest: (options, handler) {
// 镜像改写:api.modrinth.com / cdn.modrinth.com → 镜像节点
options.path = mirror.rewrite(options.uri.toString());
// 镜像改写:仅替换 base(host + 可选路径前缀),路径与查询串保持原样。
// 原实现把整串写回 options.path,因当时 baseUrl 为空、uri 无 host,
// 最终拼成 ///v2/... 报 "No host specified";且整串回写会与 queryParameters 二次拼接。
options.baseUrl = mirror.apiBase;
// Modrinth 要求标明客户端身份,否则可能被限流
options.headers['User-Agent'] = 'ZVManager/2.0 (gitee.com/driftinblock/zvmanager)';
handler.next(options);
@@ -25,6 +27,45 @@ class ModrinthApi {
return d;
}
/// 官方源 Dio(不经镜像拦截器),用于镜像连接失败时兜底重试。
Dio get _officialDio => Dio(BaseOptions(
baseUrl: 'https://api.modrinth.com',
connectTimeout: const Duration(seconds: 15),
receiveTimeout: const Duration(seconds: 30),
headers: {'User-Agent': 'ZVManager/2.0 (gitee.com/driftinblock/zvmanager)'},
));
/// 统一 GET:按当前镜像请求;若镜像出现连接层故障(DNS 失效/超时/拒连),
/// 自动回退 Modrinth 官方源重试一次。MCIM 为第三方镜像,域名与可用性可能变化,
/// 此兜底保证单点故障不会让搜索直接失败。
Future<Response> _get(String path, {Map<String, dynamic>? queryParameters}) async {
try {
return await _dio.get(path, queryParameters: queryParameters);
} on DioException catch (e) {
if (mirror.currentId == 'official' || !_isConnectionFailure(e)) rethrow;
return await _officialDio.get(path, queryParameters: queryParameters);
}
}
/// 判断是否为「连接层」故障(而非 4xx/5xx 业务响应),仅这类才值得回退。
/// 不引入 dart:io,改用字符串匹配 SocketException 关键字,保证 Web 构建也可用。
bool _isConnectionFailure(DioException e) {
switch (e.type) {
case DioExceptionType.connectionError:
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.receiveTimeout:
return true;
case DioExceptionType.unknown:
final s = '${e.error} ${e.message}';
return s.contains('SocketException') ||
s.contains('Failed host lookup') ||
s.contains('No address associated');
default:
return false;
}
}
/// 搜索项目。facets 例:[["versions:1.20.1"], ["categories:fabric"], ["project_type:mod"]]
///
/// Modrinth 的 facets 是「AND 组合、组内 OR」的结构,
@@ -41,7 +82,7 @@ class ModrinthApi {
];
if (mcVersion != null && mcVersion.isNotEmpty) facets.add(['versions:$mcVersion']);
if (loader != null && loader.isNotEmpty) facets.add(['categories:$loader']);
final resp = await _dio.get('/v2/search', queryParameters: {
final resp = await _get('/v2/search', queryParameters: {
'query': query,
'limit': limit,
'facets': facets,
@@ -52,7 +93,7 @@ class ModrinthApi {
/// 获取项目最新兼容版本列表(按日期倒序)。
Future<List<ModrinthVersion>> versions(String projectId) async {
final resp = await _dio.get('/v2/project/$projectId/version');
final resp = await _get('/v2/project/$projectId/version');
final list = (resp.data as List).map((e) => ModrinthVersion.fromJson(e)).toList();
list.sort((a, b) => b.datePublished.compareTo(a.datePublished));
return list;
@@ -60,7 +101,7 @@ class ModrinthApi {
/// 项目详情(用于依赖信息)。
Future<Map<String, dynamic>> project(String idOrSlug) async {
final resp = await _dio.get('/v2/project/$idOrSlug');
final resp = await _get('/v2/project/$idOrSlug');
return Map<String, dynamic>.from(resp.data);
}
}