2015-02-09 124 views
1

我为自己和我的同事设置了Chrome扩展程序(版本40)在Chrome扩展中使用PAC文件重定向HTTPS请求似乎失败

它的一个功能是自动将在浏览器中接收到的请求重定向到我们的临时服务器IP地址。这使得我们可以避免编辑主机文件来按照目前的做法在分段和生产之间切换。每分钟计数..

无论如何,下面的脚本,我设置完美的作品使用代理配置HTTP请求。但是,当试图在使用https请求url的网站上运行它时,它会失败,并显示以下错误消息。

网:: ERR_TUNNEL_CONNECTION_FAILED

下面是我成立至今代码的主要依据。

PAC Filehttps://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') }) 

我发现的唯一资源甚至是远程相关的was this one。这似乎意味着对于iOS,由于安全隐患,PAC文件不能用于重定向HTTPS通信。

我想我想看看这是否与Chrome相同。请提供任何已知的潜在解决方法。

+0

您可以使用['webRequest' API](https://developer.chrome.com/extensions/webRequest)完全绕过代理的使用来重定向正在进行的请求。 – Xan 2015-02-09 15:50:22

+0

我曾看过 - 但我无法弄清楚如何在['onBeforeRequest'](https://developer.chrome.com/extensions/webRequest#event-onBeforeRequest)中切换IP地址。这似乎只能从['onResponseStarted'](https://developer.chrome.com/extensions/webRequest#event-onResponseStarted)得到,这太迟了。 – ifiokjr 2015-02-09 16:26:26

回答

1

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

我能够按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和它的工作。

这种黑客让我感觉非常糟糕 - 但我已经花费了太多的时间寻找解决方案。

+0

**这在大多数“https”网址上都失败了。 – ifiokjr 2015-02-09 17:18:19

+0

最后,我直接将IP地址重定向到“https”网址。 – ifiokjr 2015-02-09 17:51:22