CefSharp 3 set proxy at Runtime

后端 未结 4 1464
执念已碎
执念已碎 2020-12-10 17:09

I downloaded CEF (chromuim embedded framework) binary distributation that comes with (cefclient & cefsimple) c++ examples, And Realized that cefclient can change proxy s

4条回答
  •  天命终不由人
    2020-12-10 17:37

    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();
    v["mode"] = "pac_script";
    v["pac_url"] = "data:application/x-ns-proxy-autoconfig;base64," + base64;
    

    3) Call SetPreference as in accepted answer https://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 use https://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

提交回复
热议问题