Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IDN (punnycode) support #10

Merged
merged 12 commits into from
Sep 19, 2024
Prev Previous commit
Next Next commit
Move IDN to different file, add test
  • Loading branch information
PrzemyslawKlys committed Aug 1, 2024
commit 0218e441450ce07f21588fb218f64aad58f1adbe
1 change: 1 addition & 0 deletions DnsClientX.Tests/CompareProvidersResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class CompareProvidersResolve(ITestOutputHelper output) {
[Theory]
//[MemberData(nameof(TestData))]
[InlineData("evotec.pl", DnsRecordType.A)]
[InlineData("www.bücher.de", DnsRecordType.A)]
[InlineData("evotec.pl", DnsRecordType.SOA)]
[InlineData("evotec.pl", DnsRecordType.DNSKEY)]
[InlineData("sip.evotec.pl", DnsRecordType.CNAME)]
Expand Down
1 change: 0 additions & 1 deletion DnsClientX.Tests/QueryDnsByEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public async void ShouldWorkForTXT(DnsEndpoint endpoint) {
[InlineData(DnsEndpoint.OpenDNS)]
[InlineData(DnsEndpoint.OpenDNSFamily)]
public async void ShouldWorkForA(DnsEndpoint endpoint) {
var Client = new ClientX(endpoint);
var response = await ClientX.QueryDns("evotec.pl", DnsRecordType.A, endpoint);
foreach (DnsAnswer answer in response.Answers) {
Assert.True(answer.Name == "evotec.pl");
Expand Down
28 changes: 28 additions & 0 deletions DnsClientX.Tests/QueryDnsIDN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace DnsClientX.Tests {
public class QueryDnsIDN {
[Theory]
[InlineData(DnsEndpoint.System)]
[InlineData(DnsEndpoint.SystemTcp)]
[InlineData(DnsEndpoint.Cloudflare)]
[InlineData(DnsEndpoint.CloudflareFamily)]
[InlineData(DnsEndpoint.CloudflareSecurity)]
[InlineData(DnsEndpoint.CloudflareWireFormat)]
[InlineData(DnsEndpoint.CloudflareWireFormatPost)]
[InlineData(DnsEndpoint.Google)]
[InlineData(DnsEndpoint.GoogleWireFormat)]
[InlineData(DnsEndpoint.GoogleWireFormatPost)]
[InlineData(DnsEndpoint.Quad9)]
[InlineData(DnsEndpoint.Quad9ECS)]
[InlineData(DnsEndpoint.Quad9Unsecure)]
[InlineData(DnsEndpoint.OpenDNS)]
[InlineData(DnsEndpoint.OpenDNSFamily)]
public async void ShouldWorkForA(DnsEndpoint endpoint) {
var response = await ClientX.QueryDns("www.bücher.de", DnsRecordType.A, endpoint);
foreach (DnsAnswer answer in response.Answers) {
Assert.True(answer.Name == "www.xn--bcher-kva.de");
Assert.True(answer.Type == DnsRecordType.A);
Assert.True(answer.Data.Length > 0);
}
}
}
}
11 changes: 0 additions & 11 deletions DnsClientX/DnsClientX.Resolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,5 @@ public async Task<DnsResponse[]> Resolve(string[] names, DnsRecordType type, boo

return tasks.Select(task => task.Result).ToArray();
}

/// <summary>
/// Converts a domain name to its Punycode representation. This is useful for internationalized domain names (IDNs).
/// For example www.bücher.de will be converted to www.xn--bcher-kva.de
/// </summary>
/// <param name="domainName"></param>
/// <returns></returns>
public static string ConvertToPunycode(string domainName) {
IdnMapping idn = new IdnMapping();
return idn.GetAscii(domainName);
}
}
}
12 changes: 12 additions & 0 deletions DnsClientX/DnsClientX.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -168,5 +169,16 @@ private HttpClient GetClient(DnsSelectionStrategy strategy) {
}
return client;
}

/// <summary>
/// Converts a domain name to its Punycode representation. This is useful for internationalized domain names (IDNs).
/// For example www.bücher.de will be converted to www.xn--bcher-kva.de
/// </summary>
/// <param name="domainName"></param>
/// <returns></returns>
private static string ConvertToPunycode(string domainName) {
IdnMapping idn = new IdnMapping();
return idn.GetAscii(domainName);
}
}
}
Loading