我可以临时覆盖.NET应用程序中的DNS解析吗?

编程入门 行业动态 更新时间:2024-10-22 23:32:51
本文介绍了我可以临时覆盖.NET应用程序中的DNS解析吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些包装代码运行一组NUnit测试,扫描实时网站的某些响应代码。

我想要针对不同的服务器。手动运行时,可以通过在Windows \System32 \drivers中编辑/ etc / hosts文件,然后临时将www.mysite设置为10.0.0.whatever

有没有办法在.NET控制台应用程序中执行相同的操作?暂时覆盖DNS记录或以某种方式截取分辨率并返回不同的IP地址? >编辑:这是为了测试一个Web场中的多个服务器。我有三台实时服务器,所有这些服务器都是www.example。因为服务器使用HTTP主机头,所以我不能只针对server1,然后是server2,然后是server3来测试,因为HTTP请求要 http :// server1 / 不会返回与 http://www.example。 com / 被解析为server1 ...

解决方案

在过去的C ++中,我能够钩住WSOCK32.DLL的gethostbyname函数和重新路由DNS请求。我使用Microsoft Detours库来做到这一点。

对于C#,我发现这一点: easyhook.codeplex/ 可能会帮助你。基本上你可以钩住 gethostbyname windows函数并执行自己的代码或返回不同的结果(不同的IP)。

另一种可能的解决方案是在应用程序启动和结束时暂时(和编程性地)编辑主机文件。从您自己的代码。

编辑:我发现我的旧的C ++代码,也许会给你一个提示该怎么做。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ret = GetHostByNameFunction(name); //检查是否要重新路由的主机名 if(strcmp(host,(char *)name)== 0) { //编辑返回的IP由常规gethostbyname ret-> h_addr_list [0] = hostIP; ret-> h_length = 15; } //返回结果 return ret; }

EDIT2:找到另一个与较新版本的easyhooks的链接

I have some wrapper code that runs a set of NUnit tests that scan live websites for certain response codes.

I'd like to run these tests against a different server. When running manually, I can do this by editing the /etc/hosts file in Windows\System32\drivers and temporarily setting www.mysite to 10.0.0.whatever

Is there any way I can do the same within a .NET console application - temporarily override a DNS record or somehow intercept the resolution and return a different IP address?

EDIT: This is for testing multiple servers in a web farm. I have three live servers, all of which THINK they are www.example. Because the servers use HTTP host headers, I can't just run a test against server1, then server2, then server3, because an HTTP request to server1/ will NOT return the same thing as a request to www.example/ that's resolved to server1...

解决方案

In the past with C++ I was able to hook to the WSOCK32.DLL's gethostbyname function and reroute DNS requests. I used the Microsoft Detours library to do that.

As for C# I found this: easyhook.codeplex/ maybe it will help you. Basically you can hook to the gethostbyname windows function and execute your own code or return a different result (different IP).

The other possible solution is to temporarily (and programatically) edit the hosts file when the application starts and ends. From your own code.

EDIT: I found my old C++ code, maybe it will give you a hint what to do.

struct hostent FAR * WSAAPI MyGetHostByName(IN const char FAR * name) { // Call the regular function struct hostent* ret = GetHostByNameFunction(name); // Check if it's the hostname you want to reroute if ( strcmp(host, (char*)name) == 0 ) { // Edit the IP returned by the regular gethostbyname ret->h_addr_list[0] = hostIP; ret->h_length = 15; } // Return the result return ret; }

EDIT2: Found another link with newer release of easyhooks

更多推荐

我可以临时覆盖.NET应用程序中的DNS解析吗?

本文发布于:2023-11-15 16:38:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   NET   DNS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!