1

i have a local network and i want to create a local DNS server to redirect all requests of clients (like www.google.com) to a local IP. I used arsoft.tools.net and referring to THIS similar question, i tried this c# code :

class Program
{
    static void Main(string[] args)
    {
        using (DnsServer server = new DnsServer(System.Net.IPAddress.Any, 10, 10))
        {
            server.QueryReceived += OnQueryReceived;

            server.Start();

            Console.WriteLine("Press any key to stop server");
            Console.ReadLine();
        }
    }

    static async Task OnQueryReceived(object sender, QueryReceivedEventArgs e)
        {
            DnsMessage query = e.Query as DnsMessage;
            if (query == null) return;
            DnsMessage response = query.CreateResponseInstance();

            if (response.Questions.Any())
            {
                DnsQuestion question = response.Questions[0];
                DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(question.Name, question.RecordType, question.RecordClass);

                response.AdditionalRecords.AddRange(upstreamResponse.AdditionalRecords);
                response.ReturnCode = ReturnCode.NoError;

                if (!question.Name.ToString().Contains("example.com"))
                {
                    response.AnswerRecords.AddRange(upstreamResponse.AnswerRecords);
                }
                else
                {
                    response.AnswerRecords.AddRange(
                        upstreamResponse.AnswerRecords
                            .Where(w => !(w is ARecord))
                            .Concat(
                                upstreamResponse.AnswerRecords
                                    .OfType<ARecord>()
                                    .Select(a => new ARecord(a.Name, a.TimeToLive, IPAddress.Parse("192.168.0.199"))) // some local ip address
                            )
                    );
                }

                e.Response = response;
            }
        }
}

all things about detecting requests is fine, but response don't redirect client to local IP and i received DNS Error in client browser.have i missed something?! or something is wrong?! Is OS (android, windows or ...) of client important?! thanks for any idea to help.

0 Answers0