代理设置 Chromium Embedded

4
如何在Chromium Embedded中手动设置代理服务器的IP:端口。这只会影响控件,而不像在IE中全局设置。谢谢。

3
我想知道为什么这个问题被踩和提名关闭?虽然这个问题没有表达得很好,但是还是可以理解的。问问题者只是想使用自己的代理设置,而不是WinHTTP代理解析器中的设置。但这并不像看上去那么容易! - TLama
1
你正在使用DCEF3还是旧版的Delphi Chromium Embedded? - TLama
@TLama,我正在使用DCEF3,看起来这是CEF3的一个bug :) - user1647411
1
似乎作者说,代理的一些问题应该得到解决,我已经尝试了他在这里提到的CefGetProxyForUrl技巧,但是该过程从未通过。我已经尝试了最新的主干版本。 - TLama
1
@TLama 我会等一段时间,直到它真正稳定了。 - user1647411
@TLama,你解决了DCEF1的问题吗? - user1889268
3个回答

1
更大的控制权可通过先前存在的类CefProxyHandler获得。
最新的实现仅使用命令行标志(我发现这些不够好)。您可以在CEF的旧SVN分支中找到以前的代码。

我已经以以下方式实现了这个功能(以下是在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());
}

关键是提供存储_set_proxy_service自己的实现。最后要做的就是提供CefProxyHandler(以前的版本可以在CefApp或CefClient中添加,类似于生命周期处理程序、请求处理程序等)。上面的示例是从CefApp提供它。

最新的分支(2357,可能更低)在浏览器进程处理程序和应用程序之间进行了更多的分离。由您决定,所有需要的是通过可用的全局上下文获取器访问代理处理程序提供程序(通常为CefApp),然后从应用程序直接获取代理处理程序并调用GetProxyHandler,或者浏览器进程处理程序(如果您计划在那里添加代理获取器),CefClient等等。之后,您可以轻松地让GetProxyHandler查询您的应用程序以获取代理设置(使用Post任务、PostMessage以及在Windows上使用WaitForSingleObject等待),以便通过GetProxyForUrl检索代理服务器/端口/用户/密码。

这样做,虽然不容易,但会为您提供完全的控制权-您甚至可以根据需要处理每个url的代理,动态更改同一浏览器/渲染器/插件中的代理而无需重新启动(请记住,例如挂起的下载或甚至插件流可能会受到影响)。


OnBeforeCommandLineProcessing存在两个问题:只能在进程启动时设置代理,而且当涉及到用户名/密码时,它们以明文形式传递。需要一种编码形式来解决这个问题。 - Cristian Amarie

1
在CEF 3中,您可以使用--proxy-server命令行开关。示例值可能是socks5://127.0.0.1:8888。您可以在CefApp::OnBeforeCommandLineProcessing中以编程方式设置它。在此回调中设置时,名称不应包含--前缀。

0
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.

如何在类中使用这个?不进行初始化。 - user1647411

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接