我有一些包装代码运行一组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解析吗?
发布评论