4

根据https://docs.ar-soft.de/arsoft.tools.net/上的 arsoft.tools.net 文档,我们刚刚开发了一个 dns 服务器来拦截来自网络的每个请求,一切正常!

让我们慢下来的事情是:当客户端键入 www.google.com(例如)时,我们想要显示或重定向到 www.yahoo.com(或 IP 地址),我们如何实现这一点的任何线索?

有些人在这里寻找相同的解决方案: https ://arsofttoolsnet.codeplex.com/discussions?searchText=redirect

Tks

更新 1:

到目前为止,这就是我所拥有的,但是重定向不起作用:

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();
        //response.AnswerRecords.Clear();
        //response.AdditionalRecords.Clear();

        if ((response.Questions.Count == 1))
        {
            // send query to upstream server
            DnsQuestion question = response.Questions[0];

            if (question.Name.ToString().Contains("www.google.com"))
            {
                DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(DomainName.Parse("www.yahoo.com"), question.RecordType, question.RecordClass);
                //DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(question.Name, question.RecordType, question.RecordClass);

                foreach (DnsRecordBase record in (upstreamResponse.AnswerRecords))
                {
                    response.AnswerRecords.Add(record);
                }
                foreach (DnsRecordBase record in (upstreamResponse.AdditionalRecords))
                {
                    response.AdditionalRecords.Add(record);
                }

                response.ReturnCode = ReturnCode.NoError;
                e.Response = response;
            }
        }
    }
}
4

1 回答 1

3

一段时间后,这里是如何做的:

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();
        DnsQuestion question = response.Questions[0];
        DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(!question.Name.ToString().Contains("www.google.com") ? question.Name : DomainName.Parse("www.yahoo.com"), question.RecordType, question.RecordClass);

        foreach (DnsRecordBase record in upstreamResponse.AnswerRecords)
        {
            response.AnswerRecords.Add(record);
        }
        foreach (DnsRecordBase record in (upstreamResponse.AdditionalRecords))
        {
            response.AdditionalRecords.Add(record);
        }

        response.ReturnCode = ReturnCode.NoError;
        e.Response = response;
    }
}

如果你想指向某个IP地址:

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("yourdomain.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;
            }
        }
于 2016-06-05T11:03:59.580 回答