Skip to content

Commit c83198c

Browse files
authored
Merge pull request #176 from jiaqiluo/fix-proxy
2 parents 96370b9 + fd9a741 commit c83198c

3 files changed

Lines changed: 29 additions & 29 deletions

File tree

libmachine/cert/cert.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import (
1111
"math/big"
1212
"net"
1313
"net/http"
14-
"net/url"
1514
"os"
1615
"time"
1716

1817
"errors"
1918

2019
"github.com/rancher/machine/libmachine/auth"
2120
"github.com/rancher/machine/libmachine/log"
22-
"github.com/rancher/machine/libmachine/util"
2321
)
2422

2523
var defaultGenerator = NewX509CertGenerator()
@@ -264,21 +262,13 @@ func (xcg *X509CertGenerator) ValidateCertificate(addr string, authOptions *auth
264262

265263
transport := http.Transport{
266264
TLSClientConfig: tlsConfig,
267-
}
268-
269-
envVar := util.GetProxyURL()
270-
if envVar != "" {
271-
log.Debugf("proxy address is used: %s", envVar)
272-
url, err := url.Parse("http://" + envVar)
273-
if err != nil {
274-
return false, err
275-
}
276-
transport.Proxy = http.ProxyURL(url)
265+
Proxy: http.ProxyFromEnvironment,
277266
}
278267
client := http.Client{
279268
Transport: &transport,
280269
Timeout: time.Second * 20,
281270
}
271+
// A https request is used to validate the certificates
282272
_, err = client.Get("https://" + addr + "/version")
283273
if err != nil {
284274
return false, err

libmachine/ssh/client.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,20 @@ func NewExternalClient(sshBinaryPath, user, host string, port int, auth *Auth) (
338338
client := &ExternalClient{
339339
BinaryPath: sshBinaryPath,
340340
}
341-
342341
var args []string
343-
envVar := util.GetProxyURL()
342+
// http proxy should be used for the SSH connection
343+
proxy, err := util.GetProxyURL("http://" + host)
344+
if err != nil {
345+
return nil, fmt.Errorf("failed to get the http proxy for the exernal client: %v", err)
346+
}
347+
proxy_url := ""
348+
if proxy != nil {
349+
proxy_url = proxy.Host
350+
}
344351
ncBinaryPath, _ := exec.LookPath("nc")
345-
log.Debugf("envVar: %s; ncBinaryPath: %s", envVar, ncBinaryPath)
346-
if envVar != "" && ncBinaryPath != "" {
347-
args = append(baseSSHArgs, "-o", fmt.Sprintf(SSHProxyArg, ncBinaryPath, envVar), fmt.Sprintf("%s@%s", user, host))
352+
log.Debugf("proxy_url: %s; ncBinaryPath: %s", proxy_url, ncBinaryPath)
353+
if proxy_url != "" && ncBinaryPath != "" {
354+
args = append(baseSSHArgs, "-o", fmt.Sprintf(SSHProxyArg, ncBinaryPath, proxy_url), fmt.Sprintf("%s@%s", user, host))
348355
} else {
349356
args = append(baseSSHArgs, fmt.Sprintf("%s@%s", user, host))
350357
}

libmachine/util/util.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package util
22

33
import (
4+
"net/http"
5+
"net/url"
46
"os"
5-
"strings"
6-
7-
"github.com/rancher/machine/libmachine/log"
87
)
98

109
func FindEnvAny(names ...string) string {
@@ -16,14 +15,18 @@ func FindEnvAny(names ...string) string {
1615
return ""
1716
}
1817

19-
func GetProxyURL() string {
20-
urlRaw := FindEnvAny("HTTP_PROXY", "HTTPS_PROXY")
21-
if urlRaw == "" {
22-
log.Debug("env var HTTP_PROXY or HTTPS_PROXY is not found")
23-
return ""
18+
// GetProxyURL returns the URL of the proxy to use for this given hostUrl as indicated by the environment variables
19+
// HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof).
20+
// HTTPS_PROXY takes precedence over HTTP_PROXY for https requests.
21+
// The hostUrl may be either a complete URL or a "host[:port]", in which case the "http" scheme is assumed.
22+
func GetProxyURL(hostUrl string) (*url.URL, error) {
23+
req, err := http.NewRequest(http.MethodGet, hostUrl, nil)
24+
if err != nil {
25+
return nil, err
26+
}
27+
proxy, err := http.ProxyFromEnvironment(req)
28+
if err != nil {
29+
return nil, err
2430
}
25-
urlRaw = strings.ToLower(urlRaw)
26-
urlRaw = strings.TrimPrefix(urlRaw, "http://")
27-
urlRaw = strings.TrimPrefix(urlRaw, "https://")
28-
return urlRaw
31+
return proxy, nil
2932
}

0 commit comments

Comments
 (0)