在Chrome扩展中使用PAC文件重定向HTTPS请求似乎失败了

Using a PAC file in a Chrome Extension to redirect HTTPS requests seems to fail

本文关键字:请求 HTTPS 失败 重定向 文件 扩展 Chrome PAC      更新时间:2023-09-26

我正在为自己和同事设置Chrome扩展(版本40)

它的功能之一是自动将浏览器中收到的请求重定向到我们的暂存服务器IP地址。这使我们可以避免像当前那样编辑主机文件以在暂存和生产之间切换。每分钟都很重要。。

不管怎样,当使用HTTP请求的代理配置时,我设置的以下脚本非常有效。但是,当尝试在使用https请求url的网站上运行它时,它失败了,并出现以下错误。

net::ERR_TUNNEL_CONNECTION_FAILED

以下是我迄今为止设置的代码的主要要点。

PAC文件:https://awesomeurl.com/proxy.pac

function FindProxyForURL(url, host) {
  var domains = ["url1", "url2", "url3", "url4"], // fake company urls
      base = ".awesomecompany.com",
      ii;
  // our local URLs from the domains below example.com don"t need a proxy:
  for(ii=0; ii<domains.length; ii++) {
    if(shExpMatch(host, domains[ii] + base)) {
      return "PROXY 11.111.111.111"; // Fake IP Address
    }
  }
  return "DIRECT";
}

这是生成Chrome设置的脚本。坚持是在别处完成的。

/**
 * Checks which value has been selected and generates the proxy config that will
 * be used for the the chrome settings and persisted.
 *
 * @param {string} mode The mode requested by the user
 * @return {ProxyConfig} the proxy configuration reprensented by the user's selections
 * @public
 *
 */
function generateProxyConfig(mode) {
  switch(mode) {
    case proxyValues.NORMAL:
      return { mode: 'system' }
    case 'production':
      return {
        mode: 'pac_script',
        pacScript: {
          url: 'https://awesomeurl.com/proxy.pac',
          mandatory: true
        }
      }
  }
}
function applyChanges ( mode, cb ) {
  config = generateProxyConfig( mode );
  chrome.proxy.settings.set({
    value: config,
    scope: 'regular'
  }, cb );
}
applyChanges('production', function() {console.log('Why doesn't this work for https') })

我发现的唯一一个与之毫不相关的资源就是这个。这似乎意味着,对于iOS,由于安全问题,PAC文件不能用于重定向HTTPS流量。

我想我想看看Chrome是否也一样。请提供任何已知的潜在解决方案。

好吧,这是这个问题的一个潜在解决方案,只是不是一个非常优雅的解决方案。

我能够按照Xan的建议使用webRequest API。

首先,我向manifest.json文件添加了以下权限。

"permissions": [
    "tabs",
    "activeTab",
    "http://*/*",
    "https://*/*",
    "proxy", 
    "webRequest", // new
    "webRequestBlocking" // new
  ],

然后在加载脚本后立即运行的新文件中。我能够使用以下内容。

// Let's play around with redirecting all https requests to http
'use strict';
console.info('awesome is here')
var domains = ["url1", "url2", "url3", "url4"], 
    base = ".awesomecompany.com";
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    console.log(details);
    return {redirectUrl: details.url.replace('https', 'http')};
  }, {urls: domains.map(function(domain) {var url = 'https://' + domain + base + '/*'; return url;})},["blocking"]
);

所以基本上,我将所有的URL重定向到http,它正在工作。

这种破解让我内心感觉很糟糕,但我已经花了太多时间寻找解决方案了。