6

I downloaded CEF (chromuim embedded framework) binary distributation that comes with (cefclient & cefsimple) c++ examples, And Realized that cefclient can change proxy settings on run-time.

And the key to do that is to Grab the RequestContext and call the function SetPreference.

on CefClient all works just nice.

but on CefSharp calling SetPreference always returns false, and also HasPreference returns false for the preference name "proxy".

askedMar 18, 2016 at 22:53
Aladdin's user avatar
2
  • 1
    Jump onGitter, read over the conversation from yesterday, you all the details you need. Likely your calling on the incorrect thread, there's only one thread that will work.gitter.im/cefsharp/CefSharpCommentedMar 19, 2016 at 9:38
  • thanks a lot , a was wondering how to get to run the code on the proper thread, but I was distracted with code defferences between c++ and c# wrappers.CommentedMar 19, 2016 at 20:43

5 Answers5

8

thanks toamaitland the proper way to actively inforce changing the request-context prefrences, is to run the code on CEF UIThread as following:

    Cef.UIThreadTaskFactory.StartNew(delegate {        var rc = this.browser.GetBrowser().GetHost().RequestContext;        var v = new Dictionary<string, object>();        v["mode"] = "fixed_servers";        v["server"] = "scheme://host:port";        string error;        bool success = rc.SetPreference("proxy", v, out error);        //success=true,error=""    });
answeredMar 19, 2016 at 20:51
Aladdin's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting "trying to modify a reference that is not user modifiable"
@amaitland can you please describe where and how the above ? I want to change proxy at runtime after Cef.Initialized so please let me know . Thank you
I tried the above but nothing happens it is still using my default ip , Any suggestions ?
4

if anyone need any other soulition i found this solution.

Cef.UIThreadTaskFactory.StartNew(delegate        {            string ip = "ip or adress";            string port = "port";            var rc = this.browser.GetBrowser().GetHost().RequestContext;            var dict = new Dictionary<string, object>();            dict.Add("mode", "fixed_servers");            dict.Add("server", "" + ip + ":" + port + "");            string error;            bool success = rc.SetPreference("proxy", dict, out error);        });
answeredMay 26, 2016 at 12:50
FreeCLoud's user avatar

Comments

3

I downloaded CefSharp.WinForms 65.0.0 and made class, that can help to start working with proxy:

public class ChromeTest{    public static ChromiumWebBrowser Create(WebProxy proxy = null, Action<ChromiumWebBrowser> onInited = null)    {       var result = default(ChromiumWebBrowser);        var settings = new CefSettings();        result = new ChromiumWebBrowser("about:blank");        if (proxy != null)            result.RequestHandler = new _requestHandler(proxy?.Credentials as NetworkCredential);        result.IsBrowserInitializedChanged += (s, e) =>        {            if (!e.IsBrowserInitialized)                return;            var br = (ChromiumWebBrowser)s;            if (proxy != null)            {                var v = new Dictionary<string, object>                {                    ["mode"] = "fixed_servers",                    ["server"] = $"{proxy.Address.Scheme}://{proxy.Address.Host}:{proxy.Address.Port}"                };                if (!br.GetBrowser().GetHost().RequestContext.SetPreference("proxy", v, out string error))                    MessageBox.Show(error);            }            onInited?.Invoke(br);        };        return result;    }    private class _requestHandler : DefaultRequestHandler    {        private NetworkCredential _credential;        public _requestHandler(NetworkCredential credential = null) : base()        {            _credential = credential;        }        public override bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)        {            if (isProxy == true)            {                if (_credential == null)                    throw new NullReferenceException("credential is null");                callback.Continue(_credential.UserName, _credential.Password);                return true;            }            return false;        }    }}

Using:

        var p = new WebProxy("Scheme://Host:Port", true, new[] { "" }, new NetworkCredential("login", "pass"));        var p1 = new WebProxy("Scheme://Host:Port", true, new[] { "" }, new NetworkCredential("login", "pass"));        var p2 = new WebProxy("Scheme://Host:Port", true, new[] { "" }, new NetworkCredential("login", "pass"));        wb1 = ChromeTest.Create(p1, b => b.Load("http://speed-tester.info/check_ip.php"));        groupBox1.Controls.Add(wb1);        wb1.Dock = DockStyle.Fill;        wb2 = ChromeTest.Create(p2, b => b.Load("http://speed-tester.info/check_ip.php"));        groupBox2.Controls.Add(wb2);        wb2.Dock = DockStyle.Fill;        wb3 = ChromeTest.Create(p, b => b.Load("http://speed-tester.info/check_ip.php"));        groupBox3.Controls.Add(wb3);        wb3.Dock = DockStyle.Fill;
answeredAug 7, 2018 at 9:59
D. Nikitin's user avatar

7 Comments

Whats the locking for? Nothing you are doing should require locking. Whats the string visitor for? IsBrowserInitializedChanged Should already be called on the cef ui thread, no need to create a task. Keep a reference to the RequestContext you create and simplify your code You should also include what version of CefSharp you are using.
Please improve the description of your source code. Otherwise, this post does not seem to provide aquality answer to the question. Please either edit your answer, or just post it as a comment to the question.
Thank's @amaitland and sorry, I did not completely clean up the code from the working solution and left some pieces. I've edited code according your comments.
@amaitland, now expressionrc.SetPreference("proxy", v, out string error) throwingSystem.NullReferenceException, I checkedrc.Equals(br.GetBrowser().GetHost().RequestContext) and gotfalse. I expected thatrc is just reference to currentRequestContext. Is it normal behaviour?
Without a stacktrace I cannot really say.
|
1

If you want dynamic proxy resolver (proxy hanlder), which allow you to use different proxy for different host - you should:

1) Prepare javascript

var proxy1Str = "PROXY 1.2.3.4:5678";var proxy2Str = "PROXY 2.3.4.5:6789";var ProxyPacScript =     $"var proxy1 = \"{(proxy1Str.IsNullOrEmpty() ? "DIRECT" : proxy1Str)}\";" +    $"var proxy2 = \"{(proxy2Str.IsNullOrEmpty() ? "DIRECT" : proxy2Str)}\";" +    @"function FindProxyForURL(url, host) {        if (shExpMatch(host, ""*example.com"")) {            return proxy1;        }        return proxy2;    }";var bytes = Encoding.UTF8.GetBytes(ProxyPacScript);var base64 = Convert.ToBase64String(bytes);

2) Set it correctly

var v = new Dictionary<string, object>();v["mode"] = "pac_script";v["pac_url"] = "data:application/x-ns-proxy-autoconfig;base64," + base64;

3) Call SetPreference as in accepted answerhttps://stackoverflow.com/a/36106994/9252162

As result all request to *example.com will flow throught proxy1, all others through proxy2.

To do it I spent all day but with help of source (https://cs.chromium.org/) I found solution. Hope it helps someone.

Main problems:

1) In new version (72 or 74 as I remember) there is no ability to use "file://..." as pac_url.

2) We can't usehttps://developer.chrome.com/extensions/proxy in cef.. or i can't find how to do it.

p.s. How to use another types of proxy (https, socks) -https://chromium.googlesource.com/chromium/src/+/master/net/docs/proxy.md#evaluating-proxy-lists-proxy-fallback

answeredOct 2, 2019 at 15:41
Sergei's user avatar

4 Comments

Example looks incomplete, you build the package url, doesn't actually call SetPreference, best to show a complete example.
Forgot it. Thanks. Add step #3.
Hi @СергейРыбаков where did you find the "pac_script" mode? Is there a place where I can see a list of possible modes?
@Juan I'am sorry, I don't know where to find all values. May be in source?
0

With the new version of CefSharp, it's quite simple to set proxy:

browser = new ChromiumWebBrowser();panel1.Controls.Add(browser);browser.Dock = DockStyle.Fill;await browser.LoadUrlAsync("about:blank");await Cef.UIThreadTaskFactory.StartNew(() =>{    browser.GetBrowser().GetHost().RequestContext.SetProxy("127.0.0.1", 1088, out string _);});
answeredJul 28, 2022 at 13:35
beligg's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.