我已经以以下方式实现了这个功能(以下是在Windows上的C++指令 - 我不会粘贴太多代码,因为可以从上面的URL中读取):
- 实现导出的CefProxyHandler类,遵循其他CEF类示例(如CefApp); 类有一个单一的方法,GetProxyForUrl
- 在cef_types.h中定义cef_proxy_type_t枚举(直接,命名,PAC)和cef_proxy_info_t结构(包含类型和字符串列表)
- 在cef_types_wrappers.h中对cef_proxy_info_t定义CefProxyInfoTraits(遵循其他结构示例),以及类CefProxyInfo:public CefStructBase
- 在libcef/browser/browser_main.cc中,在PreMainMessageLoopRun中使用CefProxyServiceFactory :: CreateProxyConfigService替换ProxyServiceFactory::CreateProxyConfigService ; Cef工厂将创建一个CefProxyService,最终创建一个CefProxyConfigService
- 从src/net/proxy/proxy_config_service_win.cc中受到启发,实现CefProxyConfigServiceWin 类; 主要是
static void GetCurrentProxyConfig(net::ProxyConfig* config) {
CefRefPtr<CefApp> app = CefContentClient::Get()->application();
if(app.get()) {
CefRefPtr<CefProxyHandler> handler = app->GetProxyHandler();
if(handler.get()) {
// ... use handler->GetProxyForUrl etc.
}
}
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0};
if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) {
LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " <<
GetLastError();
*config = net::ProxyConfig::CreateDirect();
config->set_source(net::PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
return;
}
SetFromIEConfig(config, ie_config);
FreeIEConfig(&ie_config);
}
- 重新添加了net::ProxyConfigServiceWin::set_force_auto_detect,这在旧版本中存在
- 在libcef/browser/url_request_context_getter.cc中(受旧版CEF分支启发)实现ProxyConfigServiceNull:public net::ProxyConfigService(所有空实现),class CefProxyResolver:public net::ProxyResolver(这里是GetProxyForUrl所在的位置),并在CefURLRequestContextGetter :: GetURLRequestContext()中检查自定义代理处理程序,例如:
bool fCustomProxyHandler = false;
CefRefPtr<CefApp> app = CefContentClient::Get()->application();
if(app.get()) {
CefRefPtr<CefProxyHandler> handler = app->GetProxyHandler();
if(handler.get()) {
#if defined(OS_WIN)
// Force auto-detect so the client resolver will be called.
net::ProxyConfigServiceWin::set_force_auto_detect(true);
#endif
// The client will provide proxy resolution.
storage_->set_proxy_service(
new net::ProxyService(
net::ProxyService::CreateSystemProxyConfigService(io_loop_->message_loop_proxy(), file_loop_),
new CefProxyResolver(handler),
NULL
)
);
fCustomProxyHandler = true;
}
}
if(!fCustomProxyHandler) {
// custom proxy resolution not provided
scoped_ptr<net::ProxyService> system_proxy_service;
system_proxy_service.reset(
ProxyServiceFactory::CreateProxyService(
NULL,
url_request_context_.get(),
url_request_context_->network_delegate(),
CefContentBrowserClient::Get()->proxy_config_service().release(),
command_line));
storage_->set_proxy_service(system_proxy_service.release());
}
这样做,虽然不容易,但会为您提供完全的控制权-您甚至可以根据需要处理每个url的代理,动态更改同一浏览器/渲染器/插件中的代理而无需重新启动(请记住,例如挂起的下载或甚至插件流可能会受到影响)。
--proxy-server
命令行开关。示例值可能是socks5://127.0.0.1:8888
。您可以在CefApp::OnBeforeCommandLineProcessing
中以编程方式设置它。在此回调中设置时,名称不应包含--
前缀。uses ceflib;
procedure AppCefGetProxyForUrl(const url: ustring; var proxyType: TCefProxyType;
var proxyList: ustring );
begin
proxyType := CEF_PROXY_TYPE_NAMED;
proxyList := 'ip:port';
end;
initialization
CefGetProxyForUrl := @AppCefGetProxyForUrl;
end.
DCEF3
还是旧版的Delphi Chromium Embedded
? - TLama作者说
,代理的一些问题应该得到解决,我已经尝试了他在这里
提到的CefGetProxyForUrl
技巧,但是该过程从未通过。我已经尝试了最新的主干版本。 - TLama