Unable to connect with HTTP proxy


(SKU) #1

go version: go1.13.5 linux/amd64

I am using “x/net/proxy” to connect with the “http_proxy”.

I have referred following proxy page: https://godoc.org/golang.org/x/net/proxy

To get proxy information I have set environment variable “all_proxy” to the the desired proxy “http://192.130.0.10:3200”, and performed the tcp connection, but following error is raised:

[Network Error : socks connect tcp 192.130.0.10:3200->mx.eu1.mico.io:8883: read tcp 172.17.0.2:48118->192.130.0.10:3200: read: connection reset by peer]

I have looked “x/net/proxy”, It seems “http_proxy” support is not available instead of “SOCKS5” proxy is supported. I have similar implementation for “http_proxy”, but unfortunately it does not worked.

I have created a sample code (with port 1883) which is working for the non proxy environment, Please suggest how I can enable “http_proxy” or “https_proxy” support?

package main

import (
    "fmt"
    "os"

    "golang.org/x/net/proxy"
)

//The host address which we want to connect with the proxy
var host = "google.com:80"

func main() {
    fmt.Println("Inside main...")

    //Setting the proxy before starting the application
    if os.Getenv("http_proxy") == "" {
        os.Setenv("http_proxy", "http://192.130.0.10:3200")
    }
    os.Setenv("all_proxy", os.Getenv("http_proxy"))

    if os.Getenv("all_proxy") != os.Getenv("http_proxy") {
        fmt.Println("Environment variables are not matching...")
        return
    }

    fmt.Println("System proxy is:", os.Getenv("all_proxy"))

    proxyDialer := proxy.FromEnvironment()

    fmt.Println("Connecting to...", host)

    conn, err := proxyDialer.Dial("tcp", host)
    if err != nil {
        fmt.Println("Unable to dial...", err)
        return
    }
    fmt.Println("Connected...", conn)
}
Output:
Inside main...
System proxy is: http://192.130.0.10:3200
Connecting to... google.com:80
Unable to dial... dial tcp 172.217.23.174:80: connect: connection timed out