本文介绍了无法在Golang中使用socks5代理-读取:对等连接重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个运行tor的dockerfile-

I have a dockerfile which runs tor -

FROM alpine:edge RUN apk update && apk add tor EXPOSE 9050 USER tor CMD ["/usr/bin/tor"]

并使用命令-docker run --name tor -p 11000:9050 tor

并使用-telnet 127.0.0.1 11000检查连接,并显示已连接

and checked connection using - telnet 127.0.0.1 11000 and it showed connected

现在,当go程序发出任何请求时,我都想使用tor作为代理.我试过-

Now I want to use tor as proxy while any request from go program. I tried -

package main import ( "fmt" "net/http" "net/url" "time" ) func main() { proxyUrl, err := url.Parse("socks5://127.0.0.1:11000") if err != nil { // TODO handle me panic(err) } cl := http.Client{ Transport: &http.Transport{ Proxy: http.ProxyURL(proxyUrl), }, Timeout: 18000 * time.Millisecond, } resp, err := cl.Get("google") if err != nil { // TODO handle me panic(err) } // TODO work with the response fmt.Println(resp) }

但是运行该程序会引发错误-

But running this program threw error -

panic: Get google: socks connect tcp 127.0.0.1:11000->google:80: read tcp 127.0.0.1:59630->127.0.0.1:11000: read: connection reset by peer goroutine 1 [running]: <stacktrace> exit status 2

我也尝试了其他方法,尤其是在此处和此处,但一直出现相同的错误-read: connection reset by peer

I tried other approaches also, notably mentioned here and here but kept getting same error - read: connection reset by peer

请在此处帮助说明哪个部分不正确.

Please help which part is incorrect here.

谢谢.

--------------------我尝试过的另一种方法

如其中一个链接中所述,我也尝试了此代码-

As mentioned in one of the links, I tried this code also -

const ( PROXY_ADDR = "127.0.0.1:11000" URL = "facebookcorewwwi.onion" ) func main() { // create a socks5 dialer dialer, err := proxy.SOCKS5("tcp", PROXY_ADDR, nil, proxy.Direct) if err != nil { fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err) os.Exit(1) } dialContext := func(ctx context.Context, network, address string) (net.Conn, error) { // do anything with ctx return dialer.Dial(network, address) } // setup a http client httpTransport := &http.Transport{ DialContext: dialContext, } httpClient := &http.Client{Transport: httpTransport} // create a request req, err := http.NewRequest("GET", URL, nil) if err != nil { fmt.Fprintln(os.Stderr, "can't create request:", err) os.Exit(2) } resp, err := httpClient.Do(req) if err != nil { fmt.Fprintln(os.Stderr, "cannot make get request: ", err) os.Exit(2) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Fprintln(os.Stderr, "cannot read response body: ", err) os.Exit(2) } fmt.Println("received response -> ", body) }

但收到错误-

cannot make get request: Get facebookcorewwwi.onion: socks connect tcp 127.0.0.1:11000->facebookcorewwwi.onion:80: read tcp 127.0.0.1:59826->127.0.0.1:11000: read: connection reset by peer exit status 2

任何帮助都是有意义的.

Any help is appreciable.

推荐答案

确保tor在端口9050上正常工作之后. 尝试使用以下curl命令,以确保tor正常工作.

After making sure tor is working properly on port 9050. Try the following curl command to ensure tor is working properly.

curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s wtfismyip/json

你能试试吗

package main import ( "context" "fmt" "io/ioutil" "net" "net/http" "golang/x/net/proxy" ) func main() { proxyUrl := "127.0.0.1:9050" dialer, err := proxy.SOCKS5("tcp", proxyUrl, nil, proxy.Direct) dialContext := func(ctx context.Context, network, address string) (net.Conn, error) { return dialer.Dial(network, address) } transport := &http.Transport{DialContext: dialContext, DisableKeepAlives: true} cl := &http.Client{Transport: transport} resp, err := cl.Get("wtfismyip/json") if err != nil { // TODO handle me panic(err) } body, err := ioutil.ReadAll(resp.Body) // TODO work with the response if err != nil { fmt.Println("body read failed") } fmt.Println(string(body)) }