Skip to content

RequestHandler to set Proxy Auth not working #29

@SorceryAdmin

Description

@SorceryAdmin

for the classes:

public class CustomRequestContext : RequestContextHandler
{

    public WebProxy CurrentProxy { get; set; }
    public bool blockWebRTC { get; set; } = true;
    public CustomRequestContext(bool blockRTC) : base()
    {
        blockWebRTC = blockRTC;

     
    }
    protected override async void OnRequestContextInitialized(IRequestContext requestContext)
    {
        requestContext.SetPreference("credentials_enable_service", true, out _);
        if (CurrentProxy != null)
        {
            requestContext.SetPreference("webrtc.multiple_routes_enabled", false, out _);
            requestContext.SetPreference("webrtc.nonproxied_udp_enabled", false, out _);
            if (blockWebRTC)
                requestContext.SetPreference("webrtc.ip_handling_policy", "disable_non_proxied_udp", out _);

            requestContext.SetProxy(CurrentProxy.Address.Scheme, CurrentProxy.Address.Host, CurrentProxy.Address.Port, out string error);
            requestContext.SetPreference("intl.accept_languages", "en-US,en;q=0.9,en", out string error2);
        }
    }

    protected override IResourceRequestHandler GetResourceRequestHandler(IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
    {
        return base.GetResourceRequestHandler(browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);
    }


}

public class ProxyAuthRequestHandler : CefSharp.Handler.RequestHandler
{
    public NetworkCredential Credentials { get; set; }
    protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
    {
        File.WriteAllText(@$"D:\html\{DateTime.Now.Ticks}.json", $@"{new { originUrl , isProxy , host , port, realm , scheme }}");

        if (Credentials != null && isProxy == true)
        {
            callback.Continue(Credentials.UserName, Credentials.Password);
            return true;
        }
        return false;
    }
}

and calling in file BrowserProcessHandler.cs.cs in the method CreateBrowser

        Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id)
        {
            //Debugger.Break();

            return CefThread.ExecuteOnUiThread(() =>
            {
                var requestContextSettings = new RequestContext(new CustomRequestContext( false) { CurrentProxy = this.Proxy });
                var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url, requestContextSettings);
                browser.RequestHandler = new ProxyAuthRequestHandler() { Credentials = (NetworkCredential)Proxy.Credentials };
                var windowInfo = new WindowInfo();
                windowInfo.WindowName = "BrowserProcess";
                windowInfo.SetAsChild(parentHwnd);
                //Disable Window activation by default
                //https://bitbucket.org/chromiumembedded/cef/issues/1856/branch-2526-cef-activates-browser-window
                windowInfo.ExStyle |= OutOfProcessChromiumWebBrowser.WS_EX_NOACTIVATE;
                browser.CreateBrowser(windowInfo);
                _browsers.TryAdd(id, browser);

                return true;
            });
        }

CustomRequestContext is set and the proxy is done but it promps as is show in the image for a proxy credentials the GetAuthCredentials method is never called. on non auth proxies it works all fine cause CustomRequestContext sets correctly the proxy.
Image

probably I'm missing something on my understanding of the project but I'm unable to make the Request Handler to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions