doulu3399 2019-07-19 22:02
浏览 270

通过我的扩展程序在Chrome中设置代理

I'm trying to make Chrome extension which turns on-off proxy in browser. I wrote some code in javascript using documentation about chrome extensions, and what is interesting, some times extension works right.

Here is some code in Golang, which iterates through list of https proxy addresses, makes http requests to www.stackoverflow using this proxy addresses and if response status code is 2xx (because some of addresses are invalid, deprecated, unavailable, banned and etc), it marks proxy address correct and prints it.

The second block of the code sets chrome.proxy.settings property with found 'good' proxy address. By now I'm just copy-pasting it by hands.

The question is: Why is Golang sorts out "bad" proxy address, gives me list of "good" proxies, but Chrome, when I trying to set any of this "good" proxies gives me ERR_PROXY_CONNECTION_FAILED?

EDIT: in Go add User-Agent header, in JavaScript add scheme : https field to config object for proxy. Still not working.

// checkProxy returns true if proxy adress a is valid and
// response by http request with this proxy is ok (2xx)
//185.132.179.108:1080
//185.132.179.107:1080
func checkProxy(a string) (string, bool) {
    proxyUrl, err := url.Parse("http://" + a)
    httpClient := &http.Client{     
        Transport: &http.Transport{
            Proxy: http.ProxyURL(proxyUrl),
        },
    }
    req, err := http.NewRequest("GET", "http://stackoverflow.com", nil)
    if err != nil {
        return "", false
    }
    req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
    response, err := httpClient.Do(req)
    if err != nil {
        return "", false
    }   
    if response.StatusCode <= 200 && response.StatusCode >= 299 {
        return "", false
    } else {
        return a, true
    }
}

function setProxySettings(iip, pport) {
    var config = {
        mode: "fixed_servers",
        rules: {
            //https://www.proxy-list.download/HTTPS
            // host: "107.191.63.8",
            // port: 32482
            singleProxy: {
                scheme: "https",
                host: iip,
                port: parseInt(pport)
            },
        }
    };
    chrome.proxy.settings.set({
            value: config,
            scope: 'regular'
        },
        function () {});
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器