I’m trying to create a proxy browser using the CEFSharp
browser, which involves modifying the URL from https://the_url/
to https://the_proxy_server/https://the_url
. However, this URL alteration changes the domain and causes issues with Content Security Policy (CSP)
and websites that double-check the domain. I do not use the proxy API because I want to add more things, which proxy API can not achieve, later.
I tried to use GetResourceHandler
instead, but it does not support async
, which will greatly effect the speed. I also tried to override the download part of the CEFSharp but it also did not work. I tried to search the web but there is no similar question. Here is the code I have right now:
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
return false; // Allow navigation
}
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return new CustomResourceRequestHandler();
}
}
public class CustomResourceRequestHandler : ResourceRequestHandler
{
private const string proxyURL = "https://online-proxy-with-client-app.yang2906455468.workers.dev/"; // for demo only
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if (!request.Url.StartsWith(proxyURL))
{
request.Url = proxyURL + request.Url;
}
Console.WriteLine(request.Url);
//new CefSharp.UrlRequest(request, new CustomUrlRequestClient());
return CefReturnValue.Continue; // Continue with the request
}
}
I prefer not to modify any part of the web content because it is hard to change every domain (especially those APIs that use encrypted domains). In this case, I’m looking for a way to override URL handling part in CEFSharp
, and changing the source code is also acceptable if there is no way to do it directly. This would allow me to maintain the original URLs for CSP checks while still using the proxy server.