【发布时间】: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
【问题讨论】:
-
在大多数情况下,您不必做任何特别的事情。 The standard library respects http_proxy by default.
标签: go