tags:

views:

68

answers:

2

I need to write an application that acts as a proxy between a client and a service. All I want to do is read the data that is traveling in both directions from client to service and vice versa. In essence I want this proxy to be able to be just plugged in without the need to reconfigure port settings in either the client or the service. Is this possible? Is there any specific library I could use in the C# .Net framework that will help?

EDIT: The service is on a remote machine.

EDIT: Example: Lets assume that the client is communicating to the service via port 1234 and the service is communicating to the client via port 5678. I want the application to listen and read the data traveling through these ports without actually reconfiguring either the client or the service. Is that possible?

+3  A: 

If you are to read/listen/capture the data then it does not need to be a proxy. I would recommend WinPcap library (http://www.winpcap.org/). Although it's a C/C++ library, I don't think it's hard for a C#/.NET application to make use of it.

btnguyen
If you dont need real time processing you can use tshark to save data in a .cap file and process later.A totally different approach is to use NAT to intercept/redirect to a different port. Have the proxy listen on that port and replaying to the original one. I dont know how to do this in windows.You could instead change the ip of the serve and put the proxy with the original ip.
LatinSuD
+1  A: 

If you wanna redirect the ongoing connection as soon as your proxy program is up, the short answer is hard, really hard and impossible. As far as I've known, once the connection between 2 ends is established, no way you can change it (unless you have access to the router and modify its NAT on the fly, like a load balancer...). If you just want to read & not to modify the traffic data, use WinPcap or any packet sniffer. Either of these solutions are quite expensive to implement in term of money and technical work :)

Because you didn't give us what you actual wanna do, I assume that you don't need that "much" complexity. Here a solution just in case you meet the following prerequisites:

  • The client connects to the service via a domain name, not an absolute IP and vice versa. This is important because we are going to change the DNS in the host file to "fake" the end-point servers to our proxy server address.
  • You have access rights (administrator) to the client machine & the service machine to make any change.

Then what you should do next is to:

  • Change the IP of the hostname of your service server on your client machine to the IP of the proxy server. This just changes the result of the OS DNS resolver. Do the reversing way for the service server.
  • Drop & re-establish connections between 2 client machine & service server. Now, each connection is properly "proxied" via your proxy server.

Ha, quite intricate but it works I think... Hoping someone has better solutions for this situation and I'm looking forward to hearing that.

instcode