An http.Get in Go appears not to be using the HTTP proxy specified in the HTTP_PROXY environment variable?

194 Views Asked by At

I'm running a Tor-Privoxy Docker container from https://hub.docker.com/r/dperson/torproxy,

sudo docker run -it -p 8118:8118 -p 9050:9050 -d dperson/torproxy

Consequently, I see that whereas my real IP address appears as 2620:149:13c0:a00::11,

> curl --silent -L http://jsonip.com | jq
{
  "ip": "2620:149:13c0:a00::11",
  "geo-ip": "https://getjsonip.com/#plus",
  "API Help": "https://getjsonip.com/#docs"
}

if I use the Proxy it is different:

> curl --silent -Lx http://localhost:8118 http://jsonip.com | jq
{
  "ip": "5.255.100.243",
  "geo-ip": "https://getjsonip.com/#plus",
  "API Help": "https://getjsonip.com/#docs"
}

I'm now trying to achieve the same effect with a Go program. According to https://pkg.go.dev/net/http#RoundTripper,

DefaultTransport is the default implementation of Transport and is used by DefaultClient. It establishes network connections as needed and caches them for reuse by subsequent calls. It uses HTTP proxies as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and $no_proxy) environment variables.

Based on that, I wrote the following program:

package main

import (
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
)

func main() {
    os.Setenv("HTTP_PROXY", "http://localhost:8118")

    resp, err := http.Get("http://jsonip.com/")
    if err != nil {
        log.Fatalf("Get: %v", err)
    }

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        log.Fatalf("Read response body: %v", err)
    }
    resp.Body.Close()

    fmt.Println(string(body))
}

However, if I run it, I just get a response containing my actual IP address:

> go run main.go | jq
{
  "ip": "2620:149:13c0:a00::11",
  "geo-ip": "https://getjsonip.com/#plus",
  "API Help": "https://getjsonip.com/#docs"
}

Should defining the HTTP_PROXY environment variable not anonymize my IP address using the Tor network?

1

There are 1 best solutions below

0
On

As suggested by Tiago Peczenyj, I need to define the HTTPS_PROXY environment variable as well:

> env HTTPS_PROXY=localhost:8118 go run main.go | jq
{
  "ip": "104.244.76.173",
  "geo-ip": "https://getjsonip.com/#plus",
  "API Help": "https://getjsonip.com/#docs"
}