0

I am trying to create an application that uses CefSharp. The application changes proxies every 10 seconds using the code shown below.

Cef.UIThreadTaskFactory.StartNew(delegate {
    var rc = _browser.GetBrowser().GetHost().RequestContext;
    var v = new Dictionary<string, object>();
    v["mode"] = "fixed_servers";
    v["server"] = proxy.ProxyString;
    string error;
    bool success = rc.SetPreference("proxy", v, out error);
});

Now, the proxy wont work for the first few times (3-4), I've actually judged it a few times and its around 3 or 4 times every time. Usually I pick a random proxy from the dictionary, then remove it from the dictionary once received in IP:PORT format.

Now, I thought it was a bit strange how it wouldnt work for the first few times and I started to see a pattern. I decided to stop returning random proxies and return the same ones. I then decided to wait until I saw one that worked, replaced my whole proxy list with that 1 proxy and rebooted my application to just see if it were some of the proxies.

It did the exactly same thing, it didn't work, OnLoadingStateChangedAsync didn't fire where as 3 attempts later (On the SAME proxy), it did.

Can someone explain this strange behaviour in Cefsharp? I'm using Offscreen version of Cefsharp

Better overview of the code:

while (true)
{
    if (_isWorking && _browser.IsBrowserInitialized)
    {
        if ((DateTime.Now - _lastProxyChange).TotalSeconds >= 10)
        {
            // random proxy 
            var proxy = Program.GetServer().GetBaseHandler().GetProxyHandler().GetProxy();

            if (proxy != null)
            {
                Logger.Trace("[#" + _slaveIdentifier + "] Connecting to a new proxy [" + proxy.ProxyString + "]...");

                Cef.UIThreadTaskFactory.StartNew(delegate {
                    var rc = _browser.GetBrowser().GetHost().RequestContext;
                    var v = new Dictionary<string, object>();
                    v["mode"] = "fixed_servers";
                    v["server"] = proxy.ProxyString;
                    string error;
                    bool success = rc.SetPreference("proxy", v, out error);
                });

                _browser.Reload();
            }
            else
            {
                Logger.Warn("Failed to change proxy, none avalible.");
            }

            _lastProxyChange = DateTime.Now;
        }
    }

    if (_isWorking || !_browser.IsBrowserInitialized || Program.GetServer() == null || Program.GetServer().GetBaseHandler() == null)
    {
        continue;
    }

    StartWork();
}

This is also my OnLoadingStateChangedAsync method.

private void OnLoadingStateChangedAsync(object sender, LoadingStateChangedEventArgs args)
{
    try
    {
        if (!args.IsLoading)
        {
            if (_browser.Address == BrowserUtilities.LoginPage)
            {
                OnLoginPage();
            }
            else if (_browser.Address.StartsWith(BrowserUtilities.LoginChallenge))
            {
                OnLoginChallenge();
            }
            else
            {
                Logger.Warn("Something else: " + _browser.Address);
            }
        }
    }
    catch (Exception e)
    {
        Logger.Error(e);
    }
    finally
    {
    }
}

Here is also my console output, showing the pattern:

  05:58:34 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:38 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:42 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:46 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:46 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:47 - Something else: https;//example.com/
  05:58:48 - Something else: https;//example.com/
  05:58:50 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:50 - Something else: https;//example.com/
  05:58:51 - Something else: https;//example.com/
  05:58:52 - Something else: https;//example.com/
  05:58:54 - [#0] Connecting to a new proxy [46.36.65.10:3128]...
  05:58:54 - Something else: https;//example.com/
  05:58:55 - Something else: https;//example.com/
  05:58:56 - Something else: https;//example.com/
Tim Vox
  • 19
  • 1
  • There are many reasons why this could be occurring. Are you pursuing the usual research with Fiddler or something similar? Have you gotten it to work without CefSharp? Have you tried other headless options like the WebBrowser class? Is a headless browser even necessary (i.e. Javascript execution), or would HttpClient suffice? – Robert Harvey Aug 24 '17 at 05:03
  • Have you tried to set proxy at the time cef initialization? If not give it a try and check if same is happening – Amogh Aug 27 '17 at 20:13

0 Answers0