无法在golang中使用socks5代理 - 阅读:连接被对等方重置

aqu*_*man 2 proxy http tor go socks

我有一个运行 tor 的 dockerfile -

FROM alpine:edge
RUN apk update && apk add tor
EXPOSE 9050
USER tor
CMD ["/usr/bin/tor"]
Run Code Online (Sandbox Code Playgroud)

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

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

现在我想在来自 go 程序的任何请求时使用 tor 作为代理。我试过 -

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("http://google.com")
    if err != nil {
        // TODO handle me
        panic(err)
    }

    // TODO work with the response
    fmt.Println(resp)
}
Run Code Online (Sandbox Code Playgroud)

但是运行这个程序会抛出错误 -

panic: Get http://google.com: socks connect tcp 127.0.0.1:11000->google.com: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
Run Code Online (Sandbox Code Playgroud)

我也尝试了其他方法,尤其是此处此处提到的方法,但不断遇到相同的错误 -read: connection reset by peer

请帮忙看看哪一部分是不正确的。

谢谢。

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

正如其中一个链接中提到的,我也尝试了这段代码 -

const (
    PROXY_ADDR = "127.0.0.1:11000"
    URL        = "http://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)
}
Run Code Online (Sandbox Code Playgroud)

但收到错误 -

cannot make get request:  Get http://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
Run Code Online (Sandbox Code Playgroud)

任何帮助都是值得赞赏的。

小智 5

确保tor在端口9050上正常工作后。尝试以下curl命令以确保tor正常工作。

curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s https://wtfismyip.com/json

你能试试这个吗

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "golang.org/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("https://wtfismyip.com/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))
}
Run Code Online (Sandbox Code Playgroud)