Golang Dialer在Cloud Foundry中

huangapple go评论92阅读模式
英文:

Golang Dialer in Cloud Foundry

问题

我正在尝试访问一个URL,该URL返回一个JSON响应,但只有在我连接到公司的VPN时才能访问。

使用标准的Golang库,即使我连接到公司的VPN,下面的代码也会出现错误:

myClient := &http.Client{}
req, err := http.NewRequest("POST", "https://mysite/getJSONResponse", nil)
req.Header.Add("myHeader", "myHeaderValue")
resp, err := myClient.Do(req)

我得到的错误信息如下:

502 Bad Gateway: Registered endpoint failed to handle the request.

然而,当我连接到公司的VPN时,下面这段代码(也是来自标准库)可以获取到JSON响应:

envDialer := proxy.FromEnvironment()
myTransport := &http.Transport{Dial: envDialer.Dial}
myClient := &http.Client{Transport: myTransport}
req, err := http.NewRequest("POST", "https://mysite/getJSONResponse", nil)
req.Header.Add("myHeader", "myHeaderValue")
resp, err := myClient.Do(req)

问题是,当我将可工作的代码推送到Cloud Foundry(也连接到公司的VPN)时,我的代码却出现了以下错误:

Post https://mysite/getJSONResponse: read tcp 10.254.2.182:45320->165.156.25.94:443:
read: connection reset by peer

似乎代码无法连接到公司的VPN,即使已经推送到Cloud Foundry,这就是URL拒绝提供JSON响应的原因。

然而,当我尝试使用Beego框架的测试Web应用程序访问相同的URL时,它可以正常获取到JSON响应。

值得一提的是,即使未设置http_proxy、https_proxy和no_proxy环境变量,Beego版本也可以正常工作:

req := httplib.Post("https://mysite/getJSONResponse")
req.Header("myHeader", "myHeaderValue")

str, err := req.String()
if err != nil {
    fmt.Println(err)
}

beego.Info(str)
u.Data["json"] = str
u.ServeJSON()

我的问题是:

  1. 为什么第二段标准库代码在本地工作正常,但推送到Cloud Foundry后却不行?

  2. Beego框架在幕后做了什么,使其能够在Cloud Foundry上连接到公司的VPN,标准库能否实现相同的功能?

我有一种感觉,Beego在代理/端口设置方面做了一些工作,以便能够连接到我们公司的VPN。

我真的不想将我们的后端代码与Beego框架集成,只是为了连接到公司的VPN。我一定是忽略了一些可以使用标准库完成的非常简单的事情。有什么想法吗?

英文:

I am trying to access a URL that gives me a JSON response, and said URL is only accessible when I am connected to my company's VPN.

Using the Standard Golang Library, the code below is getting an error even when I am connected to my company's VPN:

myClient := &http.Client{}
req, err := http.NewRequest("POST", "https://mysite/getJSONResponse", nil)    
req.Header.Add("myHeader", "myHeaderValue")
resp, err := myClient.Do(req)

Here is the error I am getting:

502 Bad Gateway: Registered endpoint failed to handle the request.

However, this code (also from the Standard Library) is able to get the JSON response when I am connected to my company's VPN:

envDialer := proxy.FromEnvironment()    
myTransport := &http.Transport{Dial: envDialer.Dial}
myClient := &http.Client{Transport: myTransport}
req, err := http.NewRequest("POST", "https://mysite/getJSONResponse", nil)
req.Header.Add("myHeader", "myHeaderValue")
resp, err := myClient.Do(req)

My problem is that when I push the working code to Cloud Foundry (which is also connected to my company's VPN), my code is getting the below error instead:

Post https://mysite/getJSONResponse: read tcp 10.254.2.182:45320->165.156.25.94:443:
read: connection reset by peer

It is as if the code is unable to connect to my company's VPN even when it is already Pushed to Cloud Foundry, which is why the URL refused to give the JSON response.

However, when I try to access the same URL in Cloud Foundry with a test Web Application that uses the Beego framework, it is able to get the JSON response just fine.

I should mention that the Beego version works even when the http_proxy, https_proxy, and no_proxy environment variables are not set:

req := httplib.Post("https://mysite/getJSONResponse")
req.Header("myHeader", "myHeaderValue")

str, err := req.String()
if err != nil {
    fmt.Println(err)
}

beego.Info(str)
u.Data["json"] = str
u.ServeJSON()

My questions are:

  1. Why is the second Standard Library code working fine locally, but not when Pushed to Cloud Foundry?

  2. What is the Beego framework doing behind the scenes that lets it connect to my company's VPN over at Cloud Foundry, and can the same be done with the Standard Library?

I've got a feeling Beego is doing something to its Proxy/Port settings to enable it to connect to our company's VPN.

I really don't want to have to integrate our back-end codes with the Beego framework just to connect to our company's VPN. I must be missing something really simple which can be done with the Standard Library. Any ideas?

答案1

得分: 0

我终于成功地使用beego/httplib包使其正常工作,所以不需要整合整个Beego框架(谢天谢地)。以下是我的代码:

import (
    "github.com/astaxie/beego/httplib"
)

-----------------------------------------------------

req := httplib.Post("https://mysite/getJSONResponse")
req.Header("myHeader", "myHeaderValue")

str, err := req.String()

if err == nil {
    strBytes := []byte(str)
    // 处理JSON响应
}

我没有时间去研究httplib包是如何工作的,但我*注意到它只使用标准库来建立适当的连接进行请求。有人可以帮忙解释一下吗?

编辑:我看到它使用crypto/tls包的*tls.Config来建立与适当证书的连接。这可能是它连接到我们的VPN进行请求的方式。

英文:

I finally managed to get it to work using only the beego/httplib package, so there was no need to integrate the entire Beego framework (thank goodness). Here is my code:

import (
    "github.com/astaxie/beego/httplib"
)

-----------------------------------------------------

req := httplib.Post("https://mysite/getJSONResponse")
req.Header("myHeader", "myHeaderValue")

str, err := req.String()

if err == nil {
    strBytes := []byte(str)
    // Do something with the JSON response.
}

I do not have time to investigate what makes the httplib package work, although I have seen that it only uses the Standard Library to Dial the appropriate connections to make Requests. Could somebody help shed some light on this?

EDIT: I see that it uses the crypto/tls package's *tls.Config to set up a connection with the appropriate Certificates. This may be how it makes Requests that connect to our VPN.

huangapple
  • 本文由 发表于 2017年5月19日 18:41:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/44067956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定