GitLabgitlab-org
cli
main
1package gomodproxy
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "io"
9 "io/fs"
10 "os"
11 "os/exec"
12 "regexp"
13 "slices"
14 "strings"
15 "time"
16)
18// goFetcher implements goproxy.Fetcher by running go with the user's
19// GOPROXY, so go itself applies .netrc and GOAUTH credentials, scheme-less
20// and file:// entries, GOINSECURE, and the "," and "|" fallbacks. goproxy's
21// GoFetcher fetches proxy entries in process and supports none of the
22// credential sources except a password in the URL.
23type goFetcher struct {
24 goBin string
25 env []string
26 dir string
27}
29func (f *goFetcher) Query(ctx context.Context, path, query string) (string, time.Time, error) {
30 var info struct {
31 Version string
32 Time time.Time
33 }
34 if err := f.run(ctx, &info, "list", "-m", "-json", path+"@"+query); err != nil {
35 return "", time.Time{}, err
36 }
37 return info.Version, info.Time, nil
38}
40func (f *goFetcher) List(ctx context.Context, path string) ([]string, error) {
41 var list struct{ Versions []string }
42 if err := f.run(ctx, &list, "list", "-m", "-versions", "-retracted", "-json", path); err != nil {
43 return nil, err
44 }
45 return list.Versions, nil
46}
48func (f *goFetcher) Download(ctx context.Context, path, version string) (io.ReadSeekCloser, io.ReadSeekCloser, io.ReadSeekCloser, error) {
49 var files struct{ Info, GoMod, Zip string }
50 if err := f.run(ctx, &files, "mod", "download", "-json", path+"@"+version); err != nil {
51 return nil, nil, nil, err
52 }
53 var opened []*os.File
54 for _, name := range []string{files.Info, files.GoMod, files.Zip} {
55 file, err := os.Open(name)
56 if err != nil {
57 for _, o := range opened {
58 o.Close()
59 }
60 return nil, nil, nil, err
61 }
62 opened = append(opened, file)
63 }
64 return opened[0], opened[1], opened[2], nil
65}
67func (f *goFetcher) run(ctx context.Context, out any, args ...string) error {
68 cmd := exec.CommandContext(ctx, f.goBin, args...)
69 cmd.Env = f.env
70 cmd.Dir = f.dir
71 var stderr bytes.Buffer
72 cmd.Stderr = &stderr
73 stdout, err := cmd.Output()
74 if err == nil {
75 return json.Unmarshal(stdout, out)
76 }
77 if ctxErr := ctx.Err(); ctxErr != nil {
78 return ctxErr
79 }
81 // go mod download -json reports its failure on stdout; go list on stderr.
82 msg := stderr.String()
83 var reported struct{ Error string }
84 if json.Unmarshal(stdout, &reported) == nil && reported.Error != "" {
85 msg = reported.Error
86 }
87 msg = redactUserinfo(strings.TrimSpace(strings.TrimPrefix(msg, "go: ")))
88 if msg == "" {
89 msg = err.Error()
90 }
91 if isNotFound(msg) {
92 return notFoundError{msg}
93 }
94 if failure, ok := ctx.Value(fetchFailureKey{}).(*fetchFailure); ok {
95 failure.msg = msg
96 }
97 return errors.New(msg)
98}
100// notFoundPhrases are the cmd/go diagnostics for a module or version the
101// upstream does not have. goproxy answers them with 404, which lets go try
102// the next candidate module path; any other failure ends go's query.
103var notFoundPhrases = []string{
104 "404 Not Found",
105 "410 Gone",
106 "unknown revision",
107 "invalid version",
108 "no matching versions",
109 "unrecognized import path",
110 "malformed module path",
111 "disabled by GOPROXY=off",
112 "disabled by GOPRIVATE/GONOPROXY",
113 // A file:// entry without the requested file.
114 "no such file or directory",
115 "The system cannot find the",
116}
118func isNotFound(msg string) bool {
119 return slices.ContainsFunc(notFoundPhrases, func(p string) bool { return strings.Contains(msg, p) })
120}
122type notFoundError struct{ msg string }
124func (e notFoundError) Error() string { return e.msg }
125func (notFoundError) Is(target error) bool { return target == fs.ErrNotExist }
127type fetchFailureKey struct{}
129type fetchFailure struct{ msg string }
131// userinfoPattern matches the userinfo of a URL. go redacts only a URL's
132// password, and a token is often passed as the user name.
133var userinfoPattern = regexp.MustCompile(`([A-Za-z][A-Za-z0-9+.-]*://)[^/@\s"']+@`)
135func redactUserinfo(s string) string {
136 return userinfoPattern.ReplaceAllString(s, "${1}xxxxx@")
137}
Diagnostics7 git calls · 29 ms
CallStatusSizeTime029 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0200361 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200379 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=379 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=21702eb6df99111b4d7a039e655c720d4209d5b6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=379 git.pkt.payload.max=352 git.sideband.band1.bytes=352 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=352 git.pack.trailer=7d54607262a7ed73d2686c4df38d0272960c21fcOpen pack map
fetch2001.7 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=1727 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=2313c5a5dd123712a3a6f08ff6e0da1963181b87 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1727 git.pkt.payload.max=1700 git.sideband.band1.bytes=1700 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1af084b37f54b
- Request
- 7bee4e8e…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format