import 'dart:async'; import 'package:flutter/services.dart'; class AliyunHttpdns { static const MethodChannel _channel = MethodChannel('aliyun_httpdns'); /// 1) 初始化:使用 accountId/secretKey/aesSecretKey static Future init({ required int accountId, String? secretKey, String? aesSecretKey, }) async { final ok = await _channel.invokeMethod('initialize', { 'accountId': accountId, if (secretKey != null) 'secretKey': secretKey, if (aesSecretKey != null) 'aesSecretKey': aesSecretKey, }); return ok ?? false; } /// 构建底层 service,只有在调用了 initialize / 一系列 setXxx 后, /// 调用本方法才会真正创建底层实例并应用配置 static Future build() async { final ok = await _channel.invokeMethod('build'); return ok ?? false; } /// 2) 设置日志开关 static Future setLogEnabled(bool enabled) async { await _channel.invokeMethod('setLogEnabled', { 'enabled': enabled, }); } /// 3) 设置持久化缓存 static Future setPersistentCacheIPEnabled(bool enabled, {int? discardExpiredAfterSeconds}) async { await _channel .invokeMethod('setPersistentCacheIPEnabled', { 'enabled': enabled, if (discardExpiredAfterSeconds != null) 'discardExpiredAfterSeconds': discardExpiredAfterSeconds, }); } /// 4) 是否允许复用过期 IP static Future setReuseExpiredIPEnabled(bool enabled) async { await _channel .invokeMethod('setReuseExpiredIPEnabled', { 'enabled': enabled, }); } /// 设置是否使用 HTTPS 解析链路,避免明文流量被系统拦截 static Future setHttpsRequestEnabled(bool enabled) async { await _channel .invokeMethod('setHttpsRequestEnabled', { 'enabled': enabled, }); } /// 5) 伪异步解析:返回 IPv4/IPv6 数组 /// 返回格式:{"ipv4": `List`, "ipv6": `List`} static Future>> resolveHostSyncNonBlocking( String hostname, { String ipType = 'auto', // auto/ipv4/ipv6/both Map? sdnsParams, String? cacheKey, }) async { final Map? res = await _channel .invokeMethod('resolveHostSyncNonBlocking', { 'hostname': hostname, 'ipType': ipType, if (sdnsParams != null) 'sdnsParams': sdnsParams, if (cacheKey != null) 'cacheKey': cacheKey, }); final Map> out = { 'ipv4': [], 'ipv6': [], }; if (res == null) return out; final v4 = res['ipv4']; final v6 = res['ipv6']; if (v4 is List) { out['ipv4'] = v4.map((e) => e.toString()).toList(); } if (v6 is List) { out['ipv6'] = v6.map((e) => e.toString()).toList(); } return out; } // 解析域名,返回 A/AAAA 记录等(保留旧接口以兼容,未在本任务使用) static Future?> resolve(String hostname, {Map? options}) async { final res = await _channel.invokeMethod>('resolve', { 'hostname': hostname, if (options != null) 'options': options, }); return res?.map((key, value) => MapEntry(key.toString(), value)); } // 1) setPreResolveHosts: 传入 host 列表,native 侧调用 SDK 预解析 static Future setPreResolveHosts(List hosts, {String ipType = 'auto'}) async { await _channel.invokeMethod('setPreResolveHosts', { 'hosts': hosts, 'ipType': ipType, }); } // 2) setLogEnabled: 已有,同步保留(在此文件顶部已有 setLogEnabled 实现) // 3) setPreResolveAfterNetworkChanged: 是否在网络切换时自动刷新解析 static Future setPreResolveAfterNetworkChanged(bool enabled) async { await _channel.invokeMethod( 'setPreResolveAfterNetworkChanged', { 'enabled': enabled, }); } // 4) getSessionId: 获取会话 id static Future getSessionId() async { final sid = await _channel.invokeMethod('getSessionId'); return sid; } // 5) cleanAllHostCache: 清除所有缓存 static Future cleanAllHostCache() async { await _channel.invokeMethod('cleanAllHostCache'); } /// 设置 IP 优选列表 /// [hostPortMap] 域名和端口的映射,例如:{'www.aliyun.com': 443} static Future setIPRankingList(Map hostPortMap) async { await _channel.invokeMethod('setIPRankingList', { 'hostPortMap': hostPortMap, }); } }