justcoding121 / titanium-web-proxy

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

Redirecting socket request #572

Open lightfiretw opened 5 years ago

lightfiretw commented 5 years ago

When trying to change the request URI for socket requests, the requests URI doesn't change.

Code

        private async Task OnRequest(object sender, SessionEventArgs e)
        {
            e.HttpClient.Request.RequestUri = new Uri("https://mywebsite.com/?socket=" + e.HttpClient.Request.RequestUri.AbsoluteUri);
            e.HttpClient.Request.Host = new Uri("https://mywebsite.com").Host;
        }

When testing on https://www.websocket.org/echo.html The first request which is https://echo.websocket.org/?encoding=text, should be redirected to https://mywebsite.com/?socket=https://echo.websocket.org/?encoding=text But it turns into https://mywebsite.com/?encoding=text instead.

honfika commented 5 years ago

I can't reproduce this issue.

I've added the following code to the BefureRequest handler:

            if (e.HttpClient.Request.RequestUri.ToString().Contains("websocket.org"))
            {
                e.HttpClient.Request.RequestUri =
                    new Uri("http://www.httpvshttps.com/?socket=" + e.HttpClient.Request.RequestUri.AbsoluteUri);
                e.HttpClient.Request.Host = new Uri("http://www.httpvshttps.com").Host;
            }

When opening https://www.websocket.org/echo.html Wiresharks shows: image

As expected. (With HTTPS it seems to be the same)

Could you please create a small test project to reproduce the problem?


I've found a related issue, that the HeaderText proeprty (and because that the ToString(), too) shows incorrect request URL, but it is not sent to the server.

honfika commented 5 years ago

By the way: as a workaround for the HeaderText and Request.ToString issue you can set the OriginalUrl ptoperty, too (path only):

e.HttpClient.Request.OriginalUrl = "/?socket=" + e.HttpClient.Request.RequestUri.AbsoluteUri;

honfika commented 5 years ago

Do you have an upstream proxy? I've reproduced the issue in the following configuration:

client => TWP => Fiddler => server

lightfiretw commented 5 years ago

Do you have an upstream proxy? I've reproduced the issue in the following configuration:

Yes, I do. I'm using Burp suite to check if calls are correctly sent. So my flow is like Client=>Burp=>Server. Also noticed something interesting, when using HTTPS for socket upgrade and redirecting the HOST changes(changes to https://mywebsite.com/), but trying with HTTP nothing get changed(https://echo.websocket.org/?encoding=text), it's like it bypassed the proxy.

honfika commented 5 years ago

Could you please try the latest package?

lightfiretw commented 5 years ago

Sure, unfortunately it is still the same. HTTPS the request host is changed but not the request path, HTTP it just ignores the modification for the request and connects to the original request.

I noticed for HTTPS that the httpCmd in RequestHandler.cs handleHttpSessionRequest is set before invokeBeforeRequest(args) is called, causing handleWebSocketUpgrade to connect to unmodified path. Not sure if I'm correct of not though.

Currently I am just straight up setting handleWebSocketUpgrade to suit my needs.

honfika commented 4 years ago

Please try the latest package again.

wY1k commented 1 year ago

unfortunately it is still the same.