From 3b72fa06a0b4684432405a1bafe66af0ee29af89 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 5 Jun 2021 19:08:59 +0530 Subject: [PATCH] DnsServer: redirecting web browser request to /dns-query to the html info page. --- DnsServerCore/Dns/DnsServer.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/DnsServerCore/Dns/DnsServer.cs b/DnsServerCore/Dns/DnsServer.cs index 0ab8000f..812f99b7 100644 --- a/DnsServerCore/Dns/DnsServer.cs +++ b/DnsServerCore/Dns/DnsServer.cs @@ -837,7 +837,7 @@ namespace DnsServerCore.Dns break; default: - await SendErrorAsync(stream, requestConnection, 406, "Only application/dns-message and application/dns-json types are accepted."); + await RedirectAsync(stream, httpRequest.Protocol, requestConnection, "https://" + httpRequest.Headers[HttpRequestHeader.Host]); break; } @@ -923,6 +923,22 @@ namespace DnsServerCore.Dns { } } + private static async Task RedirectAsync(Stream outputStream, string protocol, string connection, string location) + { + try + { + string statusString = "302 Found"; + byte[] bufferContent = Encoding.UTF8.GetBytes("" + statusString + "

" + statusString + "

Location: " + location + "

"); + byte[] bufferHeader = Encoding.UTF8.GetBytes(protocol + " " + statusString + "\r\nDate: " + DateTime.UtcNow.ToString("r") + "\r\nLocation: " + location + "\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: " + bufferContent.Length + "\r\nX-Robots-Tag: noindex, nofollow\r\nConnection: " + connection + "\r\n\r\n"); + + await outputStream.WriteAsync(bufferHeader); + await outputStream.WriteAsync(bufferContent); + await outputStream.FlushAsync(); + } + catch + { } + } + private static async Task SendFileAsync(Stream outputStream, string connection, string filePath) { using (FileStream fS = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))