webRequest onAuthRequired永不捕获407 Proxy Authentication Required

问题描述

我正在尝试通过chrome扩展来处理代理身份验证。

一方面,我有 chrome扩展(已建立所有权限),该扩展使用 onAuthrequired处理程序(background.js)发送CONNECT请求。

chrome.webRequest.onAuthrequired.addListener(
    (details,callback) => {
        console.log('onAuthrequired',details) // <- this has never been displayed
        callback({
            authCredentials: {
                username: 'someid',password: 'somepwd'
            }
        })
    },{
        urls: ['<all_urls>']
    },['asyncBlocking']
)

const config = {
    mode: "pac_script",pacScript: {
        data: "function FindProxyForURL(url,host) {\n if (shExpMatch(host,\"*.pandora.com\"))\n return 'PROXY 127.0.0.1:8124';\n return 'DIRECT';\n }"
    }
}

chrome.proxy.settings.set({
    value: config,scope: 'regular',},function(){})

另一方面,我有 NodeJS代理服务器,该服务器始终发送specifications

中所述的407状态代码
const http = require('http');

const proxy = http.createServer()
proxy.on('connect',(req,clientSocket,head) => {
    clientSocket.write('HTTP/1.1 407 Proxy Authentication required')
    clientSocket.write('Proxy-Authenticate: Basic realm="Access to site"\r\n\n')
});

proxy.listen(8124)

最后,浏览器返回ERR_PROXY_AUTH_UNSUPPORTED,这意味着状态代码已正确发送...

事实是 onAuthrequired从未触发,有人可以告诉我为什么吗?

提前谢谢

解决方法

Chrome积极地将身份验证调用缓存到代理服务器。因此,每个浏览器会话只会看到一次console.log调用(您需要完全重启Chrome,如果打开了多个配置文件,则需要在清除身份验证缓存之前关闭所有Chrome实例)。

我目前正在尝试弄清如何清除或清空所述缓存。

另请参阅(How to delete Proxy-Authorization Cache on Chrome extension?