我可以暂时覆盖一个.NET应用程序中的DNS解析? [英] Can I temporarily override DNS resolution within a .NET application?
问题描述
我有一个运行一组扫描特定的响应码直播网站NUnit的测试一些包装代码。
I have some wrapper code that runs a set of NUnit tests that scan live websites for certain response codes.
我想运行针对不同的这些测试服务器。当手动运行,我可以通过编辑/ etc做到这一点/ hosts文件中Windows\System32\drivers和临时设置www.mysite.com到10.0.0.whatever
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.com to 10.0.0.whatever
有没有什么方法可以让我一个.NET控制台应用程序中做同样的 - 暂时覆盖DNS记录或以某种方式拦截分辨率,并返回一个不同的IP地址
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?
编辑:这是一个Web场测试中的多台服务器。我有三个Live服务器,所有这些都认为他们是www.mysite.com。由于服务器使用HTTP主机头,我不能只是运行对服务器1,服务器2话,那么服务器3的测试,因为HTTP请求的http://服务器1 / 将不会返回同样的事情作为 http://www.mysite.com/请求说的解决到Server1 ..
This is for testing multiple servers in a web farm. I have three live servers, all of which THINK they are www.mysite.com. 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 http://server1/ will NOT return the same thing as a request to http://www.mysite.com/ that's resolved to server1...
推荐答案
在与C ++过去我能勾到WSOCK32.DLL的gethostbyname函数和重新路由DNS请求。我用微软走弯路库来做到这一点。
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.
至于C#我发现这一点: HTTP ://easyhook.codeplex.com/ 的,也许它会帮助你。基本上,你可以连接到的gethostbyname
窗口功能,并执行自己的代码或返回不同的结果(不同IP)。
As for C# I found this: http://easyhook.codeplex.com/ 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).
其他可能的解决方案是暂时(和编程)编辑hosts文件的应用程序开始和结束时间。从你自己的代码
The other possible solution is to temporarily (and programatically) edit the hosts file when the application starts and ends. From your own code.
编辑:我发现我的旧的C ++代码,也许它会给你一个提示做什么
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:找到另一个链接向easyhooks的较新版本
Found another link with newer release of easyhooks
这篇关于我可以暂时覆盖一个.NET应用程序中的DNS解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!