GetPreview(this Stream stream)
67 | {
68 | stream.Position = 0;
69 | using StreamReader sr = new(stream);
70 |
71 | char[] buffer = new char[Math.Min(stream.Length, Constants.PreviewMaxLength)];
72 | int bytesRead = await sr.ReadAsync(buffer, 0, buffer.Length);
73 | string text = new(buffer, 0, bytesRead);
74 |
75 | return text;
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/DnsOverHttps/NuGet.md:
--------------------------------------------------------------------------------
1 | # DnsOverHttps
2 |
3 | 
4 |
5 | ### An async and lightweight C# library for Cloudflare's DNS over HTTPS.
6 |
7 | ## Usage
8 | This library provides an easy interface for interacting with Cloudflare's DNS over HTTPS endpoints.
9 |
10 | DoH is a protocol that enhances the privacy and security of DNS queries by encrypting them using HTTPS. This helps prevent unauthorized access or tampering of DNS data during transmission. Learn more about it [here](https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/).
11 |
12 | To get started, import the library into your solution with either the `NuGet Package Manager` or the `dotnet` CLI.
13 | ```rust
14 | dotnet add package DnsOverHttps
15 | ```
16 |
17 | For the primary class to become available, import the used namespace.
18 | ```csharp
19 | using DnsOverHttps;
20 | ```
21 |
22 | Need more examples? Under the `Example` directory you can find a working demo project that implements this library.
23 |
24 | ## Properties
25 | - Built for **.NET 8**, **.NET 7** and **.NET 6**
26 | - Fully **async**
27 | - Extensive **XML documentation**
28 | - **No external dependencies** (makes use of built-in `HttpClient` and `JsonSerializer`)
29 | - **Custom exceptions** (`DnsOverHttpsException`) for easy debugging
30 | - Example project to demonstrate all capabilities of the library
31 |
32 | ## Features
33 | - Resolve one or all DNS records under a hostname
34 | - Ask for DNSSEC validation
35 | - Query in parallel
36 | - Specify advanced parameters
37 |
38 | ## Code Samples
39 |
40 | ### Initializing a new API client
41 | ```csharp
42 | DnsOverHttpsClient dns = new();
43 | ```
44 |
45 | ### Resolving A DNS records including DNSSEC
46 | ```csharp
47 | Response response = await dns.Resolve("discord.com", ResourceRecordType.A, true, true);
48 | ```
49 |
50 | ### Using helper methods to return the first or all answers
51 | ```csharp
52 | Answer? nsAnswer = await dns.ResolveFirst("example.com", ResourceRecordType.NS);
53 | Answer[] aAnswers = await dns.ResolveAll("reddit.com", ResourceRecordType.A);
54 | ```
55 |
56 | ## Resources
57 | - Cloudflare: https://cloudflare.com
58 | - 1.1.1.1: https://1.1.1.1
59 | - Introduction: https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https
60 |
61 | *This is a community-ran library. Not affiliated with Cloudflare, Inc.*
62 |
63 | *Icon made by **Freepik** at [Flaticon](https://www.flaticon.com).*
--------------------------------------------------------------------------------
/DnsOverHttps/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akacdev/DnsOverHttps/f279a1eefea51c301e7ba20fb5918d7482a75c5f/DnsOverHttps/icon.png
--------------------------------------------------------------------------------
/Example/Example.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Example/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using DnsOverHttps;
4 |
5 | namespace Example
6 | {
7 | public static class Program
8 | {
9 | private static readonly DnsOverHttpsClient Client = new();
10 |
11 | public static async Task Main()
12 | {
13 | Console.WriteLine($"> Resolving the first NS record on example.com");
14 | Answer? nsAnswer = await Client.ResolveFirst("example.com", ResourceRecordType.NS);
15 |
16 | Console.WriteLine($"Result:");
17 | PrintAnswer(nsAnswer);
18 |
19 |
20 | Console.WriteLine($"\n> Resolving all A records on reddit.com");
21 | Answer[] aAnswers = await Client.ResolveAll("reddit.com", ResourceRecordType.A);
22 |
23 | Console.WriteLine($"Result:");
24 | foreach (Answer answer in aAnswers) PrintAnswer(answer);
25 | Console.WriteLine();
26 |
27 |
28 | Console.WriteLine($"\n> Resolving an invalid domain");
29 | Answer? nxDomain = await Client.ResolveFirst("5525fe855b7366f93447cd039ab885.com", ResourceRecordType.A);
30 | Console.WriteLine($"Result is {(nxDomain is null ? "null" : $"not null: {nxDomain.Value.Data}")}");
31 | Console.WriteLine();
32 |
33 |
34 | Console.WriteLine($"\n> Resolving A records on discord.com with DNSSEC");
35 | Response response = await Client.Resolve("discord.com", ResourceRecordType.A, true, true);
36 |
37 | Console.WriteLine($"Result:");
38 | foreach (Answer answer in response.Answers) PrintAnswer(answer);
39 |
40 |
41 | Console.WriteLine($"\n> Resolving multiple records in parallel on github.com");
42 | Response[] responses = await Client.Resolve("github.com", [ResourceRecordType.A, ResourceRecordType.MX, ResourceRecordType.NS]);
43 |
44 | foreach (Response resp in responses)
45 | foreach (Answer answer in resp.Answers) PrintAnswer(answer);
46 |
47 |
48 | Console.WriteLine("\nDemo finished");
49 | Console.ReadKey();
50 | }
51 |
52 | public static void PrintAnswer(Answer? answer)
53 | {
54 | Console.WriteLine($"\tType: {answer.Value.Type}; TTL: {answer.Value.TTL}; Translation: {answer.Value.Name} => {answer.Value.Data}");
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 actually-akac
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DnsOverHttps
2 |
3 |
4 |

5 |
6 |
7 |
8 | An async and lightweight C# library for Cloudflare's DNS over HTTPS.
9 |
10 |
11 | ## Usage
12 | This library provides an easy interface for interacting with Cloudflare's DNS over HTTPS endpoints.
13 |
14 | DoH is a protocol that enhances the privacy and security of DNS queries by encrypting them using HTTPS. This helps prevent unauthorized access or tampering of DNS data during transmission. Learn more about it [here](https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/).
15 |
16 | To get started, import the library into your solution with either the `NuGet Package Manager` or the `dotnet` CLI.
17 | ```rust
18 | dotnet add package DnsOverHttps
19 | ```
20 |
21 | For the primary class to become available, import the used namespace.
22 | ```csharp
23 | using DnsOverHttps;
24 | ```
25 |
26 | Need more examples? Under the `Example` directory you can find a working demo project that implements this library.
27 |
28 | ## Properties
29 | - Built for **.NET 8**, **.NET 7** and **.NET 6**
30 | - Fully **async**
31 | - Extensive **XML documentation**
32 | - **No external dependencies** (makes use of built-in `HttpClient` and `JsonSerializer`)
33 | - **Custom exceptions** (`DnsOverHttpsException`) for easy debugging
34 | - Example project to demonstrate all capabilities of the library
35 |
36 | ## Features
37 | - Resolve one or all DNS records under a hostname
38 | - Ask for DNSSEC validation
39 | - Query in parallel
40 | - Specify advanced parameters
41 |
42 | ## Code Samples
43 |
44 | ### Initializing a new API client
45 | ```csharp
46 | DnsOverHttpsClient dns = new();
47 | ```
48 |
49 | ### Resolving A DNS records including DNSSEC
50 | ```csharp
51 | Response response = await dns.Resolve("discord.com", ResourceRecordType.A, true, true);
52 | ```
53 |
54 | ### Using helper methods to return the first or all answers
55 | ```csharp
56 | Answer? nsAnswer = await dns.ResolveFirst("example.com", ResourceRecordType.NS);
57 | Answer[] aAnswers = await dns.ResolveAll("reddit.com", ResourceRecordType.A);
58 | ```
59 |
60 | ## Resources
61 | - Cloudflare: https://cloudflare.com
62 | - 1.1.1.1: https://1.1.1.1
63 | - Introduction: https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https
64 |
65 | *This is a community-ran library. Not affiliated with Cloudflare, Inc.*
66 |
67 | *Icon made by **Freepik** at [Flaticon](https://www.flaticon.com).*
--------------------------------------------------------------------------------