Search code examples
goproxynetwork-programminghttp-proxy

"EOF" when making request through proxy


I'm using ssh to create a proxy connection to an EC2 instance:

ssh -v -D 8123 [email protected]

and I've written a very simple Go program that makes http requests:

package main

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

)

func main(){

    resp, err := http.Get("http://example.com")

    if err != nil {
        log.Println(err)
        os.Exit(1)
    }

    fmt.Println(string(ioutil.ReadAll(resp.Body)))

}

When I run my program normally, it works as expected. However, when I use the HTTP_PROXY env var (godoc, example) to specify an http proxy (or if I make a custom http client that uses that proxy for transport), I get the following:

HTTP_PROXY=localhost:8123 ./main 2018/11/29 23:33:27 Get http://example.com: EOF

and the debug for my SSH session where I've established the proxy gives me:

debug1: Connection to port 8123 forwarding to socks port 0 requested.
debug1: channel 3: new [dynamic-tcpip]
debug1: channel 3: free: dynamic-tcpip, nchannels 4

When I configure my macbook's wifi adapter to use localhost:8123 as a SOCKS proxy, it appears to work. My public-facing IP address changes, and traffic seems to flow (in fact, I'm using it now). Any idea why my Go program is having problems?


Solution

  • socks -D creates a SOCKS proxy. In your test you set a HTTP proxy. SOCKS proxy and HTTP proxy use different protocols, which means the request your client makes does not fit into what the proxy expects and thus it fails.

    For using a SOCKS proxy with Go see Creating a go socks5 client.