Commit 385b2e0c authored by Russ Cox's avatar Russ Cox

cmd/go: respect default proxy setting, add direct fallback

Getenv("GOPROXY") says what the environment variable is
(including looking in the go env file), but it doesn't include
the default setting. This code needs to use cfg.GOPROXY
to get the actual default. Fix and test that.

Also, we forgot to include the fallback to direct for when
the proxy serves a 404. Add and test that too.

Also add HTTP fetch information to -x build flag output.
(It does not belong in the -v output, despite the GOPATH go get
command doing this.)

Change-Id: Ieab7ef13cda3e1ad041dbe04921af206e2232c9c
Reviewed-on: https://go-review.googlesource.com/c/go/+/178720
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
Reviewed-by: default avatarHyang-Ah Hana Kim <hyangah@gmail.com>
parent c7385e27
...@@ -303,7 +303,7 @@ func goproxy() string { ...@@ -303,7 +303,7 @@ func goproxy() string {
return v return v
} }
return "https://proxy.golang.org" return "https://proxy.golang.org,direct"
} }
func gosumdb() string { func gosumdb() string {
......
...@@ -14,9 +14,11 @@ package web ...@@ -14,9 +14,11 @@ package web
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"log" "io/ioutil"
"net/http" "net/http"
urlpkg "net/url" urlpkg "net/url"
"os"
"strings"
"time" "time"
"cmd/go/internal/auth" "cmd/go/internal/auth"
...@@ -50,9 +52,28 @@ var securityPreservingHTTPClient = &http.Client{ ...@@ -50,9 +52,28 @@ var securityPreservingHTTPClient = &http.Client{
} }
func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
start := time.Now()
if os.Getenv("TESTGOPROXY404") == "1" && url.Host == "proxy.golang.org" {
res := &Response{
URL: Redacted(url),
Status: "404 testing",
StatusCode: 404,
Header: make(map[string][]string),
Body: ioutil.NopCloser(strings.NewReader("")),
}
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s: %v (%.3fs)\n", Redacted(url), res.Status, time.Since(start).Seconds())
}
return res, nil
}
fetch := func(url *urlpkg.URL) (*urlpkg.URL, *http.Response, error) { fetch := func(url *urlpkg.URL) (*urlpkg.URL, *http.Response, error) {
if cfg.BuildV { // Note: The -v build flag does not mean "print logging information",
log.Printf("Fetching %s", url) // despite its historical misuse for this in GOPATH-based go get.
// We print extra logging in -x mode instead, which traces what
// commands are executed.
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s\n", Redacted(url))
} }
req, err := http.NewRequest("GET", url.String(), nil) req, err := http.NewRequest("GET", url.String(), nil)
...@@ -84,8 +105,8 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { ...@@ -84,8 +105,8 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
fetched, res, err = fetch(secure) fetched, res, err = fetch(secure)
if err != nil { if err != nil {
if cfg.BuildV { if cfg.BuildX {
log.Printf("https fetch failed: %v", err) fmt.Fprintf(os.Stderr, "# get %s: %v\n", Redacted(url), err)
} }
if security != Insecure || url.Scheme == "https" { if security != Insecure || url.Scheme == "https" {
// HTTPS failed, and we can't fall back to plain HTTP. // HTTPS failed, and we can't fall back to plain HTTP.
...@@ -99,6 +120,9 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { ...@@ -99,6 +120,9 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
switch url.Scheme { switch url.Scheme {
case "http": case "http":
if security == SecureOnly { if security == SecureOnly {
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s: insecure\n", Redacted(url))
}
return nil, fmt.Errorf("insecure URL: %s", Redacted(url)) return nil, fmt.Errorf("insecure URL: %s", Redacted(url))
} }
case "": case "":
...@@ -106,6 +130,9 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { ...@@ -106,6 +130,9 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
panic("should have returned after HTTPS failure") panic("should have returned after HTTPS failure")
} }
default: default:
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s: unsupported\n", Redacted(url))
}
return nil, fmt.Errorf("unsupported scheme: %s", Redacted(url)) return nil, fmt.Errorf("unsupported scheme: %s", Redacted(url))
} }
...@@ -113,11 +140,17 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { ...@@ -113,11 +140,17 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
*insecure = *url *insecure = *url
insecure.Scheme = "http" insecure.Scheme = "http"
if insecure.User != nil && security != Insecure { if insecure.User != nil && security != Insecure {
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s: insecure credentials\n", Redacted(url))
}
return nil, fmt.Errorf("refusing to pass credentials to insecure URL: %s", Redacted(insecure)) return nil, fmt.Errorf("refusing to pass credentials to insecure URL: %s", Redacted(insecure))
} }
fetched, res, err = fetch(insecure) fetched, res, err = fetch(insecure)
if err != nil { if err != nil {
if cfg.BuildX {
fmt.Fprintf(os.Stderr, "# get %s: %v\n", Redacted(url), err)
}
// HTTP failed, and we already tried HTTPS if applicable. // HTTP failed, and we already tried HTTPS if applicable.
// Report the error from the HTTP attempt. // Report the error from the HTTP attempt.
return nil, err return nil, err
...@@ -126,8 +159,8 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) { ...@@ -126,8 +159,8 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
// Note: accepting a non-200 OK here, so people can serve a // Note: accepting a non-200 OK here, so people can serve a
// meta import in their http 404 page. // meta import in their http 404 page.
if cfg.BuildV { if cfg.BuildX {
log.Printf("reading from %s: status code %d", Redacted(fetched), res.StatusCode) fmt.Fprintf(os.Stderr, "# get %s: %v (%.3fs)\n", Redacted(url), res.Status, time.Since(start).Seconds())
} }
r := &Response{ r := &Response{
URL: Redacted(fetched), URL: Redacted(fetched),
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
[!net] skip [!net] skip
env GO111MODULE=on env GO111MODULE=on
env GOPROXY= env GOPROXY=direct
env GOSUMDB=off
! go get -d vcs-test.golang.org/insecure/go/insecure ! go get -d vcs-test.golang.org/insecure/go/insecure
stderr 'redirected .* to insecure URL' stderr 'redirected .* to insecure URL'
......
...@@ -3,7 +3,7 @@ env GO111MODULE=on ...@@ -3,7 +3,7 @@ env GO111MODULE=on
# Testing stderr for git ls-remote; turn off proxy. # Testing stderr for git ls-remote; turn off proxy.
[!net] skip [!net] skip
[!exec:git] skip [!exec:git] skip
env GOPROXY= env GOPROXY=direct
! go get github.com/golang/nonexist ! go get github.com/golang/nonexist
stderr 'Confirm the import path was entered correctly.' stderr 'Confirm the import path was entered correctly.'
......
env GO111MODULE=on env GO111MODULE=on
env GOPROXY= env GOPROXY=direct
# Testing that git export-subst is disabled # Testing that git export-subst is disabled
[!net] skip [!net] skip
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
env GOPROXY= env GOPROXY=
env GOSUMDB= env GOSUMDB=
go env GOPROXY go env GOPROXY
stdout '^https://proxy.golang.org$' stdout '^https://proxy.golang.org,direct$'
go env GOSUMDB go env GOSUMDB
stdout '^sum.golang.org$' stdout '^sum.golang.org$'
env GOPROXY=https://proxy.golang.org env GOPROXY=https://proxy.golang.org
...@@ -15,11 +15,29 @@ env GOSUMDB=sum.golang.org ...@@ -15,11 +15,29 @@ env GOSUMDB=sum.golang.org
env GOPROXY=direct env GOPROXY=direct
go get -m rsc.io/quote go get -m rsc.io/quote
# download from proxy.golang.org # download from proxy.golang.org with go.sum entry already
go clean -modcache go clean -modcache
env GOSUMDB='sum.golang.org https://sum.golang.org' # TODO remove URL env GOSUMDB=
env GOPROXY=https://proxy.golang.org env GOPROXY=
go get -m rsc.io/quote go get -x -m rsc.io/quote
! stderr github
stderr proxy.golang.org/rsc.io/quote
! stderr sum.golang.org/tile
! stderr sum.golang.org/lookup/rsc.io/quote
# download again, using checksum database to validate new go.sum lines
rm go.sum
go get -x -m rsc.io/quote
! stderr github
stderr proxy.golang.org/rsc.io/quote
stderr sum.golang.org/tile
stderr sum.golang.org/lookup/rsc.io/quote
# test fallback to direct
env TESTGOPROXY404=1
go get -x -m rsc.io/quote
stderr 'proxy.golang.org.*404 testing'
stderr github.com/rsc
-- go.mod -- -- go.mod --
module m module m
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
[!net] skip [!net] skip
env GO111MODULE=on env GO111MODULE=on
env GOPROXY= env GOPROXY=direct
cd empty cd empty
! go list launchpad.net/gocheck ! go list launchpad.net/gocheck
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment