Search code examples
c#google-chromewindows-servicesfiddlerfiddlercore

Can't capture traffic with FiddlerCore with mobile user-agent in a Windows service


I have a .NET Windows service which is using fiddler core to capture content from a set of web sites. I start Internet Explorer with Process.Start("IEXPLORE.EXE", url) which works. The problem is I need to compare the contents of desktop surfing and mobile surfing. I have tried to change the user agent of Internet Explorer without success. My next idea was using Chrome with a User-Agent Switcher to simulate mobile surfing. The problem with this is that I can't get fiddler core to capture any data from Chrome using a service (regardless of the User-Agent switcher). It all works fine from a console application but the service captures nothing. I'm assuming that this shouldn't be a "session 0 isolation" problem as Fiddler and Chrome are running in the same session (the session of the service user). The version of FiddlerCore is 4.4.0.1.

Here is the code for capturing the data:

    public class TrafficEvent
    {
        public string Url { get; set; }
        public string ContentType { get; set; }
        public byte[] Data { get; set; }
    }

    private List<TrafficEvent> trafficEvents;
    private Fiddler.SessionStateHandler responseHandler;

    private void HandleBeforeResponse(Fiddler.Session session)
    {
        trafficEvents.Add(new TrafficEvent
        {
            Url = session.fullUrl,
            ContentType = session.oResponse.headers.Exists("Content-Type") ? session.oResponse.headers["Content-Type"] : "",
            Data = session.responseBodyBytes
        });
    }

    public void StartFiddler()
    {
        trafficEvents = new List<TrafficEvent>();
        responseHandler = new Fiddler.SessionStateHandler(HandleBeforeResponse);
        Fiddler.FiddlerApplication.BeforeResponse += responseHandler;

        Fiddler.WinINETCache.ClearFiles();
        Fiddler.CONFIG.IgnoreServerCertErrors = false;

        int port = 8888;
        Fiddler.URLMonInterop.SetProxyInProcess("127.0.0.1:" + port.ToString(), "<-loopback>");
        Fiddler.FiddlerApplication.Startup(port, true, false);
    }

    public List<TrafficEvent> StopFiddler()
    {
        Fiddler.FiddlerApplication.BeforeResponse -= responseHandler;
        Fiddler.FiddlerApplication.Shutdown();
        return trafficEvents;
    } 

    public async Task CaptureData(string url)
    {
        StartFiddler();

        var proc = Process.Start("chrome.exe", url);
        proc.EnableRaisingEvents = true;
        await Task.Delay(30 * 1000);
        proc.Kill();

        List<TrafficEvent> trafficEvents = StopFiddler();

        foreach (TrafficEvent trafficEvent in trafficEvents)
        {
            // Store event in database
        }
    }

EDIT: Could not comment the answer of EricLaw.

@EricLaw unfortunatelly the proxy switch for Chrome did not work for the service. Earlier I have tried to set the proxy in the config-file:

<!-- The following section is to force use of Fiddler for all applications, including those running in service accounts -->
<system.net>
    <defaultProxy>
        <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
    </defaultProxy>
</system.net>

That did not work either. Changing the User-Agent via Fiddler worked well with Windows.Forms.WebControl but not with Internet Explorer or Chrome. I am currently running the service as an application, which is not optimal but working for now. Will try the UA Pick, thanks.

SOLUTION My initial problem was not beeing able to capture data from mobiles. It reallye did not matter whether it was Chrome or Internet Explorer. Thanks to the answer of @EricLaw I used the UA-pick in IE, which works for capturing mobile sites in a service!


Solution

  • Firstly, keep in mind that you can change the User-Agent header sent using Fiddler itself. Alternatively, you can use my UA Pick add-on.

    If you want to keep going with the Chrome route, the problem you're encountering is that a service account isn't guaranteed to check the active user account proxy settings, which is where Fiddler registers as the proxy by default.

    The simplest approach is to just start Chrome with a command line that points at the proxy server, e.g. chrome.exe --proxy-server=127.0.0.1:8888