【问题标题】:Unable to connect with HTTP proxy无法与 HTTP 代理连接
【发布时间】:2020-01-07 05:42:14
【问题描述】:

go 版本:go1.13.5 linux/amd64

我正在使用“x/net/proxy”连接“http_proxy”。

我参考了以下代理页面: https://godoc.org/golang.org/x/net/proxy

为了获取代理信息,我已将环境变量“all_proxy”设置为所需的代理“http://192.130.0.10:3200”,并执行了 tcp 连接,但出现以下错误:

[网络错误:socks connect tcp 192.130.0.10:3200->mx.eu1.mico.io:8883:读取 tcp 172.17.0.2:48118->192.130.0.10:3200:读取:连接重置同行]

我看过“x/net/proxy”,似乎“http_proxy”支持不可用,而不是支持“SOCKS5”代理。我对“http_proxy”有类似的实现,但不幸的是它不起作用。

我创建了一个适用于非代理环境的示例代码(端口 1883),请建议我如何启用“http_proxy”或“https_proxy”支持?

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

【问题讨论】:

标签: go


【解决方案1】:

你的目的是什么? 如果您需要为 http 请求使用 http-proxy 服务器,您只需配置您的 http-client 即可,无需使用其他包:

package main

import (
    "fmt"
    "net/http"
    "net/url"
    "time"
)

func main() {
    proxyUrl, err := url.Parse("http://192.130.0.10:3200")
    if err != nil {
        // TODO handle me
        panic(err)
    }

    cl := http.Client{
        Transport: &http.Transport{
            Proxy: http.ProxyURL(proxyUrl),
        },
        Timeout: 3000 * 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)
}

【讨论】:

  • 嗨,Igor,感谢您的回答...以下是我的用例:- I am using "paho.mqtt.golang" to connect to various cloud, its a TCP base approach where I am using 1883 and 8883 port to communicate further, the mqtt library is using "golang.org/x/net/proxy" to handle proxy but support is only for the "SOCKS5" proxy. - I am under corporate proxy which is of http, so can not use SOCKS for the same( hence thought to implement http proxy for the same). - I will create a connection and keep it open, so that later I can use use it as soon as I will get a request from the client.
  • @IAMSKU:HTTP 代理不是像 SOCKS 这样的通用 TCP 代理。虽然可能会尝试使用 CONNECT 请求通过隧道连接到任意端口,但通常仅限于几个端口。换句话说:您可能无论如何都无法为您的非 HTTP 协议使用 HTTP 代理。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
相关资源
最近更新 更多