chhsiao90 / nitmproxy

Proxy server based on netty
MIT License
151 stars 67 forks source link

Unable to modify response #108

Open abbasvalliani opened 2 years ago

abbasvalliani commented 2 years ago

@chhsiao90 I don't believe there is call made in Http2EventHandler to modify the response data? For e.g. adding javascript injection to a response etc.

It would be great if we can modify the response object using the listener interface.

abbasvalliani commented 2 years ago

@chhsiao90 Do you want me to implement this feature or may be easier for you to do it? Quite urgent for me at the moment and happy to develop this if needed. We need to do this for both Http1 and Http2 as well as WebSockets. But Http1 and Http2 is critical for now.

chhsiao90 commented 2 years ago

Hi @abbasvalliani

I commit a change to allow http2 response interception in https://github.com/chhsiao90/nitmproxy/commit/fc4abec4743c72cc27ee4a8a34a1b49d4b1f72a8. With the update, it's possible to intercept the response data with current implementation by rewriting the ByteBuf of the http2 data frame.

It's not the most straightforward way to intercept data. The most straightforward way is to intercept the full response, and return another full response, ex: a method like FullResopnse intercept(FullResponse response), but I wold not prefer in this way as it will have significant performance impact to nitmproxy.

So I would prefer to provide some helper interceptors to simplify the process of interception based on current implementation.

I will have some draft by this week.

abbasvalliani commented 2 years ago

@chhsiao90 I completely agree. One amazing part of NITM is how fast it is and we shouldn't compromise it. My use case and I am assuming most use cases would include intercepting Content-Type of HTML and replacing it with their own content. If the helper can provide interception with the header being available, that would be fantastic and potentially even a better helper to just intercept by Content-Type with the HTML content-type made easily available.

chhsiao90 commented 2 years ago

May I know how do you plan to inject javascript inside a HTML page? Append the script to the end of html? Find specific tag and replace? It will help me to decide how to implement the API.

abbasvalliani commented 2 years ago

More likely will be appended to the script tag at the end of the file.

abbasvalliani commented 2 years ago

@chhsiao90 Changes look good. Can we push a release if possible? Thanks!