简体   繁体   中英

Titanium Web Proxy - Can't modify request body

I'm having an issue with Titanium Web Proxy. What I want is to change the content of certain websites before I load them in the browser. I've implemented everything and it's working okay, minus the changing content part. Here is that part of the code:

        public async Task OnRequest(object sender, SessionEventArgs e)
    {
        //Console.WriteLine(e.HttpClient.Request.Url);

        // read request headers
        var requestHeaders = e.HttpClient.Request.Headers;

        var method = e.HttpClient.Request.Method.ToUpper();
        if ((method == "POST" || method == "PUT" || method == "PATCH"))
        {
            // Get/Set request body bytes
            byte[] bodyBytes = await e.GetRequestBody();
            await Task.Run(() => e.SetRequestBody(bodyBytes));

            // Get/Set request body as string
            string bodyString = await e.GetRequestBodyAsString();
            await Task.Run(() => e.SetRequestBodyString(bodyString));

            // store request 
            // so that you can find it from response handler 
            e.UserData = e.HttpClient.Request;
        }

        if(e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("websiteiwanttochange"))
        {
            byte[] bodyBytes = await e.GetResponseBody();
            e.SetRequestBody(bodyBytes);
            string bodyString = await e.GetResponseBodyAsString();
            e.SetRequestBodyString(bodyString);
            e.UserData = e.WebSession.Request;

            Console.WriteLine("\n\n\n\n\n\n\nTesting\n\n\n\n\n\n");
            Console.WriteLine(bodyBytes.ToString());
            //Console.WriteLine(bodyString);
        }

        // To cancel a request with a custom HTML content
        // Filter URL
        if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("google.com"))
        {
            e.Ok("<!DOCTYPE html>" +
                "<html><body><h1>" +
                "Website Blocked" +
                "</h1>" +
                "<p>Blocked by titanium web proxy.</p>" +
                "</body>" +
                "</html>");
        }

        // Redirect example
        if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
        {
            e.Redirect("https://www.paypal.com");
        }
    }

Which is pretty much the example I got from Titanium Web Proxy Github. However, this code never reaches ""\n\n\n\n\n\n\nTesting\n\n\n\n\n\n" although it loads the website properly.

I've tried to add blocking for that website, like it's for google, it works properly and blocks it.

Does anyone knows what could be the problem? I'm stuck with this for a while, so any help would be appreciated.

Content changing is not implemented in this example, I just want to see the content as string, then try to change it.

If I understand correctly, you want to modify a response received from the remote server before the browser gets it. The right place to hook is the OnBeforeResponse event. Here is a sample code where I change the body of the response from http://example.com :

void Main()
{
    var proxyServer = new ProxyServer(userTrustRootCertificate: false);

    proxyServer.BeforeResponse += OnBeforeResponse;

    var httpProxy = new ExplicitProxyEndPoint(IPAddress.Loopback, 8080, decryptSsl: true);

    proxyServer.AddEndPoint(httpProxy);
    proxyServer.Start();

    Console.ReadLine();

    proxyServer.BeforeResponse -= OnBeforeResponse;
    proxyServer.Stop();
}

public async Task OnBeforeResponse(object sender, SessionEventArgs ev)
{
    var request = ev.HttpClient.Request;
    var response = ev.HttpClient.Response;

    if (String.Equals(ev.HttpClient.Request.RequestUri.Host, "www.example.com", StringComparison.OrdinalIgnoreCase)) {
        var body = await ev.GetResponseBodyAsString();
        
        body = body.Replace("<title>Example Domain</title>", "<title>My Example Domain</title>");
        
        ev.SetResponseBodyString(body);
    }
}

And test:

$ curl.exe --proxy http://localhost:8080 http://www.example.com
<!doctype html>
<html>
<head>
    <title>My Example Domain</title>

... stripped ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM