justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.93k stars 618 forks source link

Can't block images #770

Open jiehuuu opened 4 years ago

jiehuuu commented 4 years ago

I'm trying to block images, however, it doesn't work

I am using BeforeResponse event

Tried this:

if (e.HttpClient.Response.ContentType.Contains("image")) {
 e.SetResponseBodyString("");
}

and this

if (e.HttpClient.Response.ContentType.Contains("image")) {
 byte[] btBody = Encoding.UTF8.GetBytes("empty");
 var response = new OkResponse(btBody);
 response.HttpVersion = e.HttpClient.Request.HttpVersion;
 e.Respond(response);
}
honfika commented 4 years ago

Try this:

                if (e.HttpClient.Response.ContentType?.Contains("image") == true)
                {
                    await e.GetResponseBody();
                    e.SetResponseBodyString("");
                }

This works for me.

jiehuuu commented 4 years ago

Thanks for your response... but not working on my side...

https://prnt.sc/sqn99w

private async Task ProxyServer_BeforeResponse(object sender, SessionEventArgs e)
        {
            if (e.HttpClient.Response.ContentType?.Contains("image") == true)
            {
                Console.WriteLine("Image found: replacing request body");

                await e.GetResponseBody();
                e.SetResponseBodyString("");

                Console.WriteLine("Image found: body replaced to empty");
            }
}
honfika commented 4 years ago

Is this BeforeResponse handler called?

jiehuuu commented 4 years ago

Yes, i inserted a breakpoint and hited!

can u send me ur project file?

honfika commented 4 years ago

I used the Basic example from this repository.

rgbcrow commented 4 years ago

Hello, i tried again and worked... but some images are not blocked, like svg or base64 images

honfika commented 4 years ago

Maybe the content type is different.

base64 image is which is embedded in the html? of course it is not blocked with this method... you have to modify html body of the http response for that. It is not trivial to block every image in the webpage, since it can be drawn for example by a javascript, too...

may i close this issue?