2015-02-09 53 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