2

I have a dockerfile which runs tor -

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

and ran it using command - docker run --name tor -p 11000:9050 tor

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

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

    // TODO work with the response
    fmt.Println(resp)
}

But running this program threw error -

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

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.

Thanks.

--------------------another approach that I tried ----------------

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

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)
}

but received error -

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

Any help is appreciable.

aquaman
  • 1,523
  • 5
  • 20
  • 39
  • Can you check that you are able to rich "http://facebookcorewwwi.onion" with this proxy and Firefox, or smth else? – AmaHacka Aug 31 '20 at 13:16
  • @AmaHacka found the problem and solution [here](https://stackoverflow.com/questions/57825953/cant-connect-to-dockerized-tor-proxy) – aquaman Aug 31 '20 at 15:01

2 Answers2

4

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 https://wtfismyip.com/json

Can you try this

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))
}
Jeffy Mathew
  • 570
  • 4
  • 16
0

As suggested in the answer/comment above, main problem was socks5 connection to dockerized tor container.

Found the solution here, i just had to modify dockerfile like -

FROM alpine:edge
RUN apk update && apk add tor
RUN cp /etc/tor/torrc.sample /etc/tor/torrc && echo "SocksPort 0.0.0.0:9050" > /etc/tor/torrc
EXPOSE 9050
CMD ["/usr/bin/tor"]

Other code pieces are working fine.

aquaman
  • 1,523
  • 5
  • 20
  • 39