fix:调整解析中继地址时兼容域名地址

This commit is contained in:
liyi 2025-02-12 16:07:03 +08:00
parent eed848a554
commit 044990cad5

View File

@ -186,7 +186,7 @@ class StartChartManage {
for (int i = 0; i <= relayInfoEntity.relay_list!.length; i++) {
final data = relayInfoEntity.relay_list?[i];
if (data?.peerID != FromPeerId) {
final parseUdpUrl = _parseUdpUrl(data?.listenAddr ?? '');
final parseUdpUrl = await _parseUdpUrl(data?.listenAddr ?? '');
remoteHost = parseUdpUrl['host'] ?? '';
remotePort = parseUdpUrl['port'] ?? '';
relayPeerId = data?.peerID ?? '';
@ -893,8 +893,8 @@ class StartChartManage {
}
/// UDP URL IP
Map<String, dynamic> _parseUdpUrl(String url) {
// IP
/// UDP URL IP
Future<Map<String, dynamic>> _parseUdpUrl(String url) async {
final regex = RegExp(r'udp://([a-zA-Z0-9.-]+):(\d+)').firstMatch(url);
if (regex != null) {
@ -903,7 +903,20 @@ class StartChartManage {
final port = int.tryParse(portStr ?? '');
if (host != null && port != null) {
return {'host': host, 'port': port};
try {
// DNS
final List<InternetAddress> addresses =
await InternetAddress.lookup(host);
if (addresses.isEmpty) {
throw FormatException('DNS resolution failed for $host');
}
// 使 IP
final String resolvedIp = addresses.first.address;
return {'host': resolvedIp, 'port': port};
} catch (e) {
throw FormatException('DNS resolution error for $host: $e');
}
}
}
throw FormatException('无法解析 URL 格式: $url');