Stormancer / netproxy

A simple .NETCore based, crossplatform, TCP/UDP proxy that supports ipv6
MIT License
124 stars 37 forks source link

Intercept HTTP headers #1

Open imclint21 opened 5 years ago

imclint21 commented 5 years ago

Hi,,

I would like to intercept HTTP headers and response, can you said me how to achieve this ?

Thanks you

Falanwe commented 5 years ago

Hi,

this proxy is not really the right tool to do that: when used as a TCP proxy, it just forwards the content of the TCP connection to the remote endpoint without reading it. To intercept HTTP headers, you would need to parse this content.

It's likely there are products out there that already do what you want. What's your use case, and where do you need to run it?

imclint21 commented 5 years ago

Yes yes you right, but I want to do this for a project :D

I tried to do this, but I don't know why it don't works, I'm not very good with Async and Wait programming in C# :/

I explain you what I want to do, I would like to make a TCP proxy that redirect flow depending on user, so I need to parse Authorization header, getting user, and with this user redirecting to the good IP :)

imclint21 commented 5 years ago

Do you have a telegram/discord/skype or something else @Falanwe ?

hudsonventura commented 3 years ago

@clintnetwork did you manage to get the headers? O want to do something like that. Whenever I read data from the socket, the app erases the socket data and does not forward it to the output.

hudsonventura commented 3 years ago

@clintnetwork maybe you'll like the line 162 of https://github.com/microsoft/referencesource/blob/master/mscorlib/system/io/stream.cs

Code: private async Task CopyToAsyncInternal(Stream source, Stream destination) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0) { Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytesRead)); await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false); } }

jmderuty commented 2 years ago

Sorry, it has been some time.

The project was developed on our side because we needed something very simple for both TCP and UDP:

Both these requirements don't require modifying the headers even if I agree that it would be very useful in the general case, for HTTP requests.

The proxy currently doesn't try to understand protocols, which is actually very useful if you are not forwarding HTTP trafic and we probably want the TCP forwarder this way.

To read and modify headers, the best option is probably to add a new forwarder type configurable in config, specifically for HTTP, using kestrel as a webserver to parse the requests and then forward them. It would enable scenarios like HTTPS to HTTP forwarding or even simple loadbalancing.