fix:调整解析中继地址时兼容域名地址
This commit is contained in:
parent
eed848a554
commit
044990cad5
@ -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');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user