V I R U S
V I R U S

Reputation: 31

C# redirect network requests

I'm looking for a good solution, on how to redirect socket requests.

For example, I have one tool/app which trying to connect to 25.25.25.25:80 but. I need to redirect all those requests to 54.54.54.54:80 (also http request).

The one solution I know, is to modify "hosts" file and to rewrite the host. But it seems not to be a good solution in my case. I don't want to modify user system files. I want to modify the query route as long as my secondary app is running. If you close it, first app will query original host.

Is it possible to do that with C#?

Upvotes: 0

Views: 2356

Answers (4)

V I R U S
V I R U S

Reputation: 31

So, the main purpose of it, is to rewrite the resources the application use. I my case that's are images.

Such example: There is a application, which requires user to log in (username/password). After the user logged it, this application fetches one image (avatar), from lets say www.supersite.com/avatars/%username%.gif. I have no influence on www.supersite.com, and i do not want to hack the resources of the application to rewrite the URL. The purpose of the second app (we trying to create) is, to listen for the http-call of www.supersite.com/avatars/%username%.gif and rewrite it to www.mysite.com/avatars/%username%.gif as long, as the second app is running.

Hope that was good example. :)

Upvotes: 1

kol
kol

Reputation: 28698

Write a proxy application, which is listening at 25.25.25.25:80 (HttpListener), forwards every incoming request to 54.54.54.54:80 (e.g. WebClient), and sends back the answer of the application listening at the latter address.

EDIT:

If you don't have access to the machine with address 25.25.25.25, and you want to solve the problem on the client machine, then you can use WinPcap (use P/Invoke or one of the nice .NET wrappers). Write an application, which has a pcap instance, which takes each outgoing packet with destination IP address 25.25.25.25 and TCP port 80, rewrites the address to 54.54.54.54, recalculates the checksum field of the TCP header, and sends the packet. You will also need another pcap instance, which takes incoming TCP packets with source address 54.54.54.54, rewrites the address to 25.25.25.25, recalculates the checksum field of the TCP header, and sends the packet. (Once I wrote similar programs, so I'm pretty sure this approach would work. You may need to use queues, one for the incoming, and one for the outgoing packets, and multiple threads.)

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151604

it seems not to be a good solution in my case.

It also seems like that to me. It would be nice if you could explain what you're trying to achieve here, perhaps someone else has encountered the same problem and there is an easier solution for it.

Anyway, basically you can't do it, unless you use some kind of proxy which you can influence. Where does the proxy program you want to create (hereafter: proxy) run? And can you alter the tool that issues the request (tool)?

If your proxy runs on the 25.25.25.25 server, you can capture all incoming requests on port 80 and forward that to 54.54.54.54, but the original server won't be able to run (since your proxy already occupies port 80), or vice versa.

If the proxy runs on the client, you'd have to let the tool connect to that proxy (i.e. 127.0.0.1) and rather request the IP address to connect to, with a fallback to the default address (25.25.25.25) if the proxy isn't running or doesn't respond in time.

Upvotes: 0

Michal B.
Michal B.

Reputation: 5719

It should be possible to do it in C#. Here you have what you want, but the source code is C++. Still I think it is worth looking: http://www.quantumg.net/portforward.php.

Upvotes: 0

Related Questions