This is the thirteenth part of the Chatterbox series. For your convenience you can find other parts in the table of contents in Part 1 – Origins
Some networks communicate over websockets which are much harder to scrape with a Chrome extension. I tried some code on the internet (mostly based on replacing the websocket constructor and injecting proxy implementation) but none of them worked reliably over a longer time. However, we can easily get that with Fiddler. You can take the code from GitHub
This scrapes websockets traffic and parses it. We can extend the code to do whatever we want with the received response. The only thing we need to do is to configure the Chrome browser to go through the Fiddler and then do the magic.
Now, let’s say that you extracted the message from a websocket. What do you do with it? You can for instance call your service:
1 2 3 4 5 6 7 8 9 |
var request = String.Format( "POST https://some.service/handler HTTP/1.1\n" + "User-Agent: Fiddler\n" + "Content-Type: application/json; charset=utf-8\n" + "Host: some.service\n" + "Content-Length: {0}\n\n{1}", System.Text.Encoding.UTF8.GetByteCount(message), message); FiddlerApplication.oProxy.SendRequest(request, null); |
But what if your Fiddler needs to go through a proxy (for instance to connect over a tunnel to some intranet)? You can add this logic to send your request outside of the proxy:
1 2 3 4 5 |
static function OnBeforeRequest(oSession: Session){ if (oSession.HostnameIs("some.service")){ oSession.bypassGateway = true; } } |
What if you need to reply some traffic from the browser? For instance, to refresh the state? You can do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
static function AskForEvents(stateInfo: Object){ var sessions = FiddlerApplication.UI.GetAllSessions(); for(var i = sessions.Length -1;i>=0;--i){ var oSession = sessions[i]; if(oSession.fullUrl.Contains("the session you need")){ FiddlerObject.log("Replaying: " + oSession.fullUrl); FiddlerApplication.UI.actRemoveAllSessions(); FiddlerApplication.oProxy.SendRequest(oSession.oRequest.headers, oSession.requestBodyBytes, new System.Collections.Specialized.StringDictionary()); return; } } } |
And then you can grab the response:
1 2 3 4 5 6 7 8 |
static function OnBeforeResponse(oSession: Session) { var url = oSession.fullUrl; if(url.Contains("the session you need")){ var body = Fiddler.WebFormats.JSON.JsonDecode(oSession.GetResponseBodyAsString()); // TODO } } |
And what if you need to call some external application? Fiddler is based on .NET so you just call System.Diagnostics.Process.Start("cmd.exe", "/c echo 123")
and you’re good.