Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

Using Fiddler as Reverse Proxy to debug Client Server Communication

$
0
0

Topics

 

 

 

For a long time I was using Membrane Monitor to monitor and trace the client-server communication between my (mobile) devices and a server (usually servers like SAP Mobile Platform, HCPms, MobileDocs, Netweaver Gateway, ...). I described the way for Android devices in an article some time ago: Debugging and Developing Android applications in enterprise environments . The described way is still working, but recently I prefer Fiddler to Membrane Monitor as reverse proxy.

 

By default Fiddler serves as a web proxy. Daniel van Leeuwen described in his blog how to use this to trace http/https communication on SMP server as well as on mobile devices (see Getting Started with Kapsel - Appendix F -- Tips (SP09+) ). Anyway I prefer using a "real" reverse proxy, thus I will describe here steps how to use Fiddler as a reverse proxy.

 

Using Fiddler as Reverse Proxy

 

1. If you do not have Fiddler yet, download it for free Download Fiddler Web Debugging Tool for Free by Telerik

 

2. Install it

 

3. (Optional) Install the Syntax-Highlighting Add-On which you can download from here http://www.telerik.com/fiddler/add-ons

1_Fiddler_Syntax_Extension.png

This will simplify changing the reverse proxy configurations later.

 

4. (Optional) Go to Tools > Fiddler Options and choose Tab Connections. Activate the option "Allow remote computers to connect". Restart Fiddler after that.

This will allow other clients to connect to your reverse proxy

2_AllowRemoteComputersToConnect.png

5. Close/Exit Fiddler

6. Open the registry editor to set a registry key which will define the (local) port Fiddler will forward the traffic. Actually we will not use this port, but we need to specify it, so that Fiddler knows it is running in reverse-proxy mode.

(Run > regedit ). Navigate in regedit to "Computer > HKEY_CURRENT_USER > Software > Microsoft > Fiddler2 and create a new "DWORD (32-bit) Value" (right click and choose "New") named "ReverseProxyForPort". Choose Decimal representation and provide any port. In my case I choose port 8000.

3_ReverseProxyPort_Regedit.png

7. Start Fiddler

By default requests going to http://localhost:8888 or http://127.0.0.1:8888 (default Fiddler listen port) will get redirected to localhost port 8000. Because we want our reverse proxy to route all traffic to a specific other server, we have to use some Fiddler scripting.

8. Switch to tab "FiddlerScript" (you will only have this tab if you installed the AddOn as suggested in step 3). Fiddler provides you a quite powerful way of manipulating requests and responses by using JScript.NET language. The most important methods are "OnBeforeRequest" which is executed by Fiddler before a concrete request is sent out and "OnBeforeResponse" which is called before the response from the backend server is delivered to the client.

4_FiddlerScript.png

9. Go to method "OnBeforeRequest" and add following coding at the beginning of this function. Replace the host with your target server host url. The x-overrideHost is one of several parameters that Fiddler will take care. In this case Fiddler is manipulating any headers but is simply forwarding the request (as it is) to this new target url.

 

        /*              *  Custom reverse proxy mode         *  Simulate the Windows HOSTS file, by pointing one Hostname to a different IP address         *  (Retargets without changing the request's Host header)         */               if ((oSession.HostnameIs("127.0.0.1")) || (oSession.HostnameIs("localhost"))) {           // oSession.bypassGateway = true; // use webproxy?            oSession["x-overrideHost"] = "dewdfwssp2011.dhcp.wdf.sap.corp:8100";        }

In this example the reverse proxy (which is running on port 8888) will forward all related traffic to the system dewdfwssp2011.dhcp.wdf.sap.corp on port 8100, but only if the request url targets 127.0.0.1 or localhost. If you also want to allow other urls simply remove the if-clause.
5_OnBeforeRequest_routing.png

Alternatively you can also use oSession.host instead of x-overrideHost. In this way the host is getting adapted

 

oSession.host="dewdfwssp2011.dhcp.wdf.sap.corp:8100"

After that do not forget to click on "Save Script".

10. Now the reverse proxy configuration is finished and it should work. You can try it directly inside Fiddler when using the "Composer" to create a request. In my case I am calling a sample odata service, because I connected Fiddler to a Netweaver Gateway: http://127.0.0.1:8000/sap/opu/odata/IWBEP/GWDEMO/

6_TestReverseProxy.png

11. Be aware that when calling from other applications than Fiddler you have to call port 8888 (because Fiddler is doing the mapping of port 8888 to local port 8000). Thus in a webbrowser call address http://localhost:8888/sap/opu/odata/IWBEP/GWDEMO/

6_z_TestinBrowser.png

 

 

Using FiddlerScript to rewrite HTTP Responses

 

We can now even go one step ahead. Let's say we want to replace some content of a http reponse. I had this case some days ago, where an url did not get rewritten (because it was part of the message body), so I configured Fiddler as a reverse proxy rewriting these urls inside the response message bodys.

 

In the following example I will only replace a sample string, but you can use the same procedure for complex Regex based search and replace operations.

 

1. In the request I sent in step 10 (above) I was getting the service document of an odata service, which looked like this.

7_RewriteSalesOrderCollection.png

We want to rewrite now the term "SalesOrderCollection"

2. Switch again to tab "FiddlerScript", now go to function "OnBeforeResponse" and add the following coding into the function


       /*        *        * Search and replace content in Response        *        */        if (oSession.HostnameIs("127.0.0.1")){            oSession.utilDecodeResponse(); //decode the response first                   var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);            // Replace actions            var oRegEx = /SalesOrderCollection/gi;            oBody = oBody.replace(oRegEx, "NameChangedCollection");            // Set the response body to the div-less string            oSession.utilSetResponseBody(oBody);                      }

8_FiddlerScript_Replacingrewriting.png

3. After that "Save Script"

4. Switch to the "Composer" tag and execute the same request again. In the response body you can see that the string "SalesOrderCollection" got replaced by "NameChangedCollection"

9_TestReplace.png


Fiddler and FiddlerScript is quite powerful. If you want to learn more, check out the documentation:

http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse



Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>