502 Bad Gateway: net/http GET failed for IPSec tunnel

PHPz
Release: 2024-02-09 13:09:25
forward
658 people have browsed it

502 错误网关:IPSec 隧道的 net/http GET 失败

php Editor Zimo sometimes encounters a 502 Bad Gateway prompt when using the Internet. This error message means that there is a communication problem between the gateway server and the upstream server, resulting in the request not being completed. One of the possible causes is a net/http GET failure for the IPSec tunnel. Although this error can be confusing, by understanding its cause and how to fix it, we can quickly resolve the issue and continue enjoying the convenience of the Internet.

Question content

Issuing a remotegetrequest throughipsec tunnel (vpn) on windowsusing thenet/httppackage When , thegetrequest failed and the response code was502 bad gateway. However, requests sent bycurlwith the sameheaders,uri,methodwork just fine.goThe source code is as follows:

var req *http.request req, err = http.newrequest('get', *uri, reader) if nil != err { level.warn(log).log("msg", "unable to create request", "err", err, "uri", uri) return nil, err } if nil != req.body { defer req.body.close() } for h, v := range opts.headers { req.header.add(h, *v) } client := http.client{} if nil != err { return nil, err } res, err := client.do(req) if nil != err { level.warn(log).log("msg", "unable to send request", "err", err, "uri", uri) return nil, err } defer res.body.close() buf, err := io.readall(res.body) if nil != err { level.warn(log).log("msg", "unable to read response", "err", err, "uri", uri, "status", res.status, "status_code", res.statuscode) return nil, err }
Copy after login

After analyzing the packet capture of the traffic, it was found that thevpn interfacecan normally capture the request sent bycurl, butgetcannot be retrieved fromgo(net /http)The request sent was lost.

Then I guess it may be because the framework chooses the wrong interface to send the packet.

So I try to find the correct local network interface and specify itslocaladdrto send mygetrequest like this:

func Client(u string) (*http.Client, error) { uri, err := url.Parse(u) if nil != err { return nil, err } host := uri.Host port := uri.Port() if Empty == port { if uri.Scheme == "http" { host = host + ":80" } else if uri.Scheme == "https" { host = host + ":443" } } conn, err := net.Dial("tcp", host) if nil != err { return nil, err } addr := conn.LocalAddr().(*net.TCPAddr) // **make sure using random port as local port** addr.Port = 0 dialer := &net.Dialer{LocalAddr: addr} dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) { conn, err := dialer.Dial(network, addr) return conn, err } transport := &http.Transport{DialContext: dialContext} client := &http.Client{ Transport: transport, } return client, nil }
Copy after login

After replacingclientwith theclientfunction, thegetrequest finally works.

client := &http.client{}=>client, err := client(uri)

So my question is whygolangis not selecting the correct local network interface to automatically send the request?

Solution

After debuggingnet.dial#DialContext, I found that the address has been changed to myVisual Studio Codesettings proxy address. My proxy server will not be able to handle my application requests.

When I remove the proxy settings,net.dialworks as expected.

important things

I'm using vscode to work withTypeScript/Java/Golang...and this is my first timedeveloping an application invscodeIDEEncountering proxy issues. In other words, I never thought theIDEagent could do the job it was built for. It makes sense since the extension or the IDE itself uses this proxy setting, but mygolangproject debugging also uses it, which really confuses me.

The above is the detailed content of 502 Bad Gateway: net/http GET failed for IPSec tunnel. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!