arsoft.tools.net(DNS 服务器)将流量从一个 URL 重定向到另一个 URL(DNS 转发)



按照 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

更新 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;
            }
        }
    }
}

一段时间后,以下是操作方法:

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;
            }
        }

相关内容

  • 没有找到相关文章