1

All I want to do is forward every request coming into my server and port, to the same server and a different port, and optionally add one header.

That is it. is there a really simple C# program I could write, that just takes bytes from here and pushes them to this other port, and same with the response, just throws it down to the client?

1
  • Forwarding TCP connections are easy, but adding a header (presumably to an HTTP request) involves a proxy that has to inspect and parse the request. Commented Dec 7, 2010 at 16:01

1 Answer 1

2

sTCPPipe by Luigi Auriemma is a great simple C++ TCP pipe implementation that does exactly what you need, but does not allow the addition of extra headers.

For a C# implementation that does HTTP header inspection and acts as a proxy and not just a simple tunnel, look at the Mentalis proxy project. You can easily modify it to direct all requests to one address instead of the address specified in the HTTP Host Header, but the source is delegate spaghetti.

Or you can write one yourself with a TcpListener that listens on say, port 8080, and after accepting a connection connects to another host (using a different socket) and relays all traffic between the two. If you don't use non-blocking sockets, you'll need to use a few threads to accomplish this.

If it's for commercial use, then the challenge with writing a proxy is to make sure it is reliable and can withstand all types of buffer overflow attacks.

Sign up to request clarification or add additional context in comments.

4 Comments

delegate spaghetti is the phrase I've been looking for recently, thanks :)
I was looking for a code example, not a reference. Mentalis is too difficult to consume.
Mentalis is a huge project, but all the HTTP proxy functionality is in HttpListener.cs and HttpClient.cs, which are not that hard to understand if you just read through the code. Typically you'll want to modify the RebuildQuery() method to add a custom header. I'll see if I can give you a simple code sample.
Would you please give more description on TCP listener part?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.