gpt4 book ai didi

c# - arsoft.tools.net(DNS Server) 将流量从一个 URL 重定向到另一个(DNS 转发)

转载 作者:太空狗 更新时间:2023-10-30 01:32:15 26 4
gpt4 key购买 nike

遵循 arsoft.tools.net 在 https://docs.ar-soft.de/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;
}
}

关于c# - arsoft.tools.net(DNS Server) 将流量从一个 URL 重定向到另一个(DNS 转发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com