forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
824 lines (767 loc) · 22.8 KB
/
fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
package goproxy
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/mod/sumdb"
"golang.org/x/mod/sumdb/dirhash"
"golang.org/x/mod/sumdb/note"
"golang.org/x/mod/zip"
)
// Fetcher defines a set of intuitive methods used to fetch module files for
// [Goproxy].
//
// Note that any error returned by Fetcher that matches [fs.ErrNotExist]
// indicates that the module cannot be fetched.
type Fetcher interface {
// Query performs the version query for the given module path.
//
// The version query can be one of the following:
// - A fully-specified semantic version, such as "v1.2.3", which
// selects a specific version.
// - A semantic version prefix, such as "v1" or "v1.2", which selects
// the highest available version with that prefix.
// - A revision identifier for the underlying source repository, such
// as a commit hash prefix, revision tag, or branch name. If the
// revision is tagged with a semantic version, this query selects
// that version. Otherwise, this query selects a pseudo-version for
// the underlying commit. Note that branches and tags with names
// matched by other version queries cannot be selected this way. For
// example, the query "v2" selects the latest version starting with
// "v2", not the branch named "v2".
// - The string "latest", which selects the highest available release
// version. If there are no release versions, "latest" selects the
// highest pre-release version. If there are no tagged versions,
// "latest" selects a pseudo-version for the commit at the tip of
// the repository's default branch.
Query(ctx context.Context, path, query string) (version string, time time.Time, err error)
// List lists the available versions for the given module path.
//
// The returned versions contains only tagged versions, not
// pseudo-versions. Versions covered by "retract" directives in the
// "go.mod" file from the "latest" version of the same module are also
// ignored.
List(ctx context.Context, path string) (versions []string, err error)
// Download downloads the module files for the given module path and
// version.
//
// The returned error is nil only if all three kinds of module files
// are successfully downloaded.
Download(ctx context.Context, path, version string) (info, mod, zip io.ReadSeekCloser, err error)
}
// GoFetcher implements [Fetcher] using the local Go binary.
//
// Make sure that the Go binary and the version control systems (such as Git)
// that need to be supported are installed and properly configured in your
// environment, as they are required for direct module fetching.
//
// During a direct module fetch, the Go binary is called while holding a lock
// file in the module cache directory (specified by GOMODCACHE) to prevent
// potential conflicts. Misuse of a shared GOMODCACHE may lead to deadlocks.
//
// Note that GoFetcher will still adhere to your environment variables. This
// means you can set GOPROXY to run GoFetcher itself under other proxies. By
// setting GONOPROXY and GOPRIVATE, you can instruct GoFetcher on which modules
// to fetch directly, rather than using those proxies. Additionally, you can set
// GOSUMDB, GONOSUMDB, and GOPRIVATE to specify how GoFetcher should verify the
// modules it has just fetched. Importantly, all of these mentioned environment
// variables are built-in supported, resulting in fewer external command calls
// and a significant performance boost.
type GoFetcher struct {
// Env is the environment. Each entry is in the form "key=value".
//
// If Env is nil, [os.Environ] is used.
//
// If Env contains duplicate environment keys, only the last value in
// the slice for each duplicate key is used.
//
// Make sure that all environment values are valid, particularly for
// GOPROXY and GOSUMDB, to prevent constant fetch failures.
Env []string
// GoBin is the path to the Go binary that is used to execute direct
// fetches.
//
// If GoBin is empty, "go" is used.
GoBin string
// MaxDirectFetches is the maximum number of concurrent direct fetches.
//
// If MaxDirectFetches is zero, there is no limit.
MaxDirectFetches int
// TempDir is the directory for storing temporary files.
//
// If TempDir is empty, [os.TempDir] is used.
TempDir string
// Transport is used to execute outgoing requests, excluding those
// initiated by direct fetches.
//
// If Transport is nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
initOnce sync.Once
initErr error
env []string
envGOPROXY string
envGONOPROXY string
directFetchWorkerPool chan struct{}
httpClient *http.Client
sumdbClient *sumdb.Client
}
// init initializes the f.
func (gf *GoFetcher) init() {
env := gf.Env
if env == nil {
env = os.Environ()
}
var envGOSUMDB, envGONOSUMDB, envGOPRIVATE string
for _, e := range env {
if k, v, ok := strings.Cut(e, "="); ok {
switch k {
case "GO111MODULE":
case "GOPROXY":
gf.envGOPROXY = v
case "GONOPROXY":
gf.envGONOPROXY = v
case "GOSUMDB":
envGOSUMDB = v
case "GONOSUMDB":
envGONOSUMDB = v
case "GOPRIVATE":
envGOPRIVATE = v
default:
gf.env = append(gf.env, e)
}
}
}
gf.envGOPROXY, gf.initErr = cleanEnvGOPROXY(gf.envGOPROXY)
if gf.initErr != nil {
return
}
if gf.envGONOPROXY == "" {
gf.envGONOPROXY = envGOPRIVATE
}
gf.envGONOPROXY = cleanCommaSeparatedList(gf.envGONOPROXY)
envGOSUMDB = cleanEnvGOSUMDB(envGOSUMDB)
if envGONOSUMDB == "" {
envGONOSUMDB = envGOPRIVATE
}
envGONOSUMDB = cleanCommaSeparatedList(envGONOSUMDB)
gf.env = append(
gf.env,
"GO111MODULE=on",
"GOPROXY=direct",
"GONOPROXY=",
"GOSUMDB=off",
"GONOSUMDB=",
"GOPRIVATE=",
)
if gf.MaxDirectFetches > 0 {
gf.directFetchWorkerPool = make(chan struct{}, gf.MaxDirectFetches)
}
gf.httpClient = &http.Client{Transport: gf.Transport}
if envGOSUMDB != "off" {
sco, err := newSumdbClientOps(gf.envGOPROXY, envGOSUMDB, gf.httpClient)
if err != nil {
gf.initErr = err
return
}
gf.sumdbClient = sumdb.NewClient(sco)
gf.sumdbClient.SetGONOSUMDB(envGONOSUMDB)
}
}
// skipProxy reports whether the module path should be fetched directly rather
// than using a proxy.
func (gf *GoFetcher) skipProxy(path string) bool {
return module.MatchPrefixPatterns(gf.envGONOPROXY, path)
}
// Query implements [Fetcher].
func (gf *GoFetcher) Query(ctx context.Context, path, query string) (version string, time time.Time, err error) {
if gf.initOnce.Do(gf.init); gf.initErr != nil {
err = gf.initErr
return
}
if gf.skipProxy(path) {
version, time, err = gf.directQuery(ctx, path, query)
} else {
err = walkEnvGOPROXY(gf.envGOPROXY, func(proxy *url.URL) error {
version, time, err = gf.proxyQuery(ctx, path, query, proxy)
return err
}, func() error {
version, time, err = gf.directQuery(ctx, path, query)
return err
})
}
return
}
// proxyQuery performs the version query for the given module path using the
// given proxy.
func (gf *GoFetcher) proxyQuery(ctx context.Context, path, query string, proxy *url.URL) (version string, time time.Time, err error) {
escapedPath, err := module.EscapePath(path)
if err != nil {
return
}
escapedQuery, err := module.EscapeVersion(query)
if err != nil {
return
}
var u *url.URL
if escapedQuery == "latest" {
u = appendURL(proxy, escapedPath+"/@latest")
} else {
u = appendURL(proxy, escapedPath+"/@v/"+escapedQuery+".info")
}
var info bytes.Buffer
err = httpGet(ctx, gf.httpClient, u.String(), &info)
if err != nil {
return
}
version, time, err = unmarshalInfo(info.String())
if err != nil {
err = notExistErrorf("invalid info response: %w", err)
return
}
return
}
// directQuery performs the version query for the given module path using the
// local Go binary.
func (gf *GoFetcher) directQuery(ctx context.Context, path, query string) (version string, t time.Time, err error) {
output, err := gf.execGo(ctx, "list", "-json", "-m", path+"@"+query)
if err != nil {
return
}
var info struct {
Version string
Time time.Time
}
return info.Version, info.Time, json.Unmarshal(output, &info)
}
// List implements [Fetcher].
func (gf *GoFetcher) List(ctx context.Context, path string) (versions []string, err error) {
if gf.initOnce.Do(gf.init); gf.initErr != nil {
err = gf.initErr
return
}
if gf.skipProxy(path) {
versions, err = gf.directList(ctx, path)
} else {
err = walkEnvGOPROXY(gf.envGOPROXY, func(proxy *url.URL) error {
versions, err = gf.proxyList(ctx, path, proxy)
return err
}, func() error {
versions, err = gf.directList(ctx, path)
return err
})
}
if err != nil {
return
}
for i := range versions {
parts := strings.Fields(versions[i])
if len(parts) > 0 && semver.IsValid(parts[0]) && !module.IsPseudoVersion(parts[0]) {
versions[i] = parts[0]
} else {
versions[i] = ""
}
}
semver.Sort(versions)
firstNotEmptyIndex := 0
for ; firstNotEmptyIndex < len(versions) && versions[firstNotEmptyIndex] == ""; firstNotEmptyIndex++ {
}
versions = versions[firstNotEmptyIndex:]
return
}
// proxyList lists the available versions for the given module path using the
// given proxy.
func (gf *GoFetcher) proxyList(ctx context.Context, path string, proxy *url.URL) (versions []string, err error) {
escapedPath, err := module.EscapePath(path)
if err != nil {
return
}
var list bytes.Buffer
err = httpGet(ctx, gf.httpClient, appendURL(proxy, escapedPath+"/@v/list").String(), &list)
if err != nil {
return
}
versions = strings.Split(list.String(), "\n")
return
}
// directList lists the available versions for the given module path using the
// local Go binary.
func (gf *GoFetcher) directList(ctx context.Context, path string) (versions []string, err error) {
output, err := gf.execGo(ctx, "list", "-json", "-m", "-versions", path)
if err != nil {
return
}
var list struct{ Versions []string }
return list.Versions, json.Unmarshal(output, &list)
}
// Download implements [Fetcher].
func (gf *GoFetcher) Download(ctx context.Context, path, version string) (info, mod, zip io.ReadSeekCloser, err error) {
if gf.initOnce.Do(gf.init); gf.initErr != nil {
err = gf.initErr
return
}
if err = checkCanonicalVersion(path, version); err != nil {
return
}
var (
infoFile, modFile, zipFile string
// cleanup is the cleanup function that will be called when the
// infoFile, modFile, and zipFile are no longer needed, or when
// an error occurs.
cleanup func()
)
if gf.skipProxy(path) {
infoFile, modFile, zipFile, err = gf.directDownload(ctx, path, version)
} else {
err = walkEnvGOPROXY(gf.envGOPROXY, func(proxy *url.URL) error {
infoFile, modFile, zipFile, cleanup, err = gf.proxyDownload(ctx, path, version, proxy)
return err
}, func() error {
infoFile, modFile, zipFile, err = gf.directDownload(ctx, path, version)
return err
})
}
if err != nil {
return
}
if cleanup != nil {
defer func() {
if err != nil {
cleanup()
}
}()
} else {
cleanup = func() {} // Avoid nil cleanup.
}
infoVersion, infoTime, err := unmarshalInfoFile(infoFile)
if err != nil {
return
}
err = checkModFile(modFile)
if err != nil {
return
}
err = checkZipFile(zipFile, path, version)
if err != nil {
return
}
if gf.sumdbClient != nil {
err = verifyModFile(gf.sumdbClient, modFile, path, version)
if err != nil {
return
}
err = verifyZipFile(gf.sumdbClient, zipFile, path, version)
if err != nil {
return
}
}
infoContent := strings.NewReader(marshalInfo(infoVersion, infoTime))
modContent, err := os.Open(modFile)
if err != nil {
return
}
zipContent, err := os.Open(zipFile)
if err != nil {
modContent.Close()
return
}
var (
closers int32 = 3
closed = func() {
if atomic.AddInt32(&closers, -1) == 0 {
cleanup()
}
}
)
var infoClosedOnce sync.Once
info = struct {
io.ReadSeeker
io.Closer
}{infoContent, closerFunc(func() error {
infoClosedOnce.Do(closed)
return nil
})}
var modClosedOnce sync.Once
mod = struct {
io.ReadSeeker
io.Closer
}{modContent, closerFunc(func() error {
defer modClosedOnce.Do(closed)
return modContent.Close()
})}
var zipClosedOnce sync.Once
zip = struct {
io.ReadSeeker
io.Closer
}{zipContent, closerFunc(func() error {
defer zipClosedOnce.Do(closed)
return zipContent.Close()
})}
return
}
// proxyDownload downloads the module files for the given module path and
// version using the given proxy.
func (gf *GoFetcher) proxyDownload(ctx context.Context, path, version string, proxy *url.URL) (infoFile, modFile, zipFile string, cleanup func(), err error) {
escapedPath, err := module.EscapePath(path)
if err != nil {
return
}
escapedVersion, err := module.EscapeVersion(version)
if err != nil {
return
}
urlWithoutExt := appendURL(proxy, escapedPath+"/@v/"+escapedVersion).String()
tempDir, err := os.MkdirTemp(gf.TempDir, tempDirPattern)
if err != nil {
return
}
defer func() {
if err != nil {
os.RemoveAll(tempDir)
}
}()
infoFile, err = httpGetTemp(ctx, gf.httpClient, urlWithoutExt+".info", tempDir)
if err != nil {
return
}
modFile, err = httpGetTemp(ctx, gf.httpClient, urlWithoutExt+".mod", tempDir)
if err != nil {
return
}
zipFile, err = httpGetTemp(ctx, gf.httpClient, urlWithoutExt+".zip", tempDir)
if err != nil {
return
}
cleanup = func() { os.RemoveAll(tempDir) }
return
}
// directDownload downloads the module files for the given module path and
// version using the local Go binary.
func (gf *GoFetcher) directDownload(ctx context.Context, path, version string) (infoFile, modFile, zipFile string, err error) {
output, err := gf.execGo(ctx, "mod", "download", "-json", path+"@"+version)
if err != nil {
return
}
var download struct{ Info, GoMod, Zip string }
return download.Info, download.GoMod, download.Zip, json.Unmarshal(output, &download)
}
// execGo executes the local Go binary with the given args and returns the output.
func (gf *GoFetcher) execGo(ctx context.Context, args ...string) ([]byte, error) {
if gf.directFetchWorkerPool != nil {
gf.directFetchWorkerPool <- struct{}{}
defer func() { <-gf.directFetchWorkerPool }()
}
tempDir, err := os.MkdirTemp(gf.TempDir, tempDirPattern)
if err != nil {
return nil, err
}
defer os.RemoveAll(tempDir)
goBin := gf.GoBin
if goBin == "" {
goBin = "go"
}
cmd := exec.CommandContext(ctx, goBin, args...)
cmd.Env = gf.env
cmd.Dir = tempDir
output, err := cmd.Output()
if err != nil {
if err := ctx.Err(); err != nil {
return nil, err
}
if len(output) > 0 {
var goErr struct{ Error string }
if err := json.Unmarshal(output, &goErr); err == nil && goErr.Error != "" {
output = []byte(goErr.Error)
}
} else if ee, ok := err.(*exec.ExitError); ok {
output = ee.Stderr
}
if len(output) == 0 {
return nil, err
}
var msg string
for _, line := range strings.Split(string(output), "\n") {
if !strings.HasPrefix(line, "go: finding") {
msg += line + "\n"
}
}
msg = strings.TrimPrefix(msg, "go: ")
msg = strings.TrimPrefix(msg, "go list -m: ")
msg = strings.TrimRight(msg, "\n")
return nil, notExistErrorf(msg)
}
return output, nil
}
const defaultEnvGOPROXY = "https://proxy.golang.org,direct"
// cleanEnvGOPROXY returns the cleaned envGOPROXY.
func cleanEnvGOPROXY(envGOPROXY string) (string, error) {
if envGOPROXY == "" || envGOPROXY == defaultEnvGOPROXY {
return defaultEnvGOPROXY, nil
}
var cleaned string
for envGOPROXY != "" {
var proxy, sep string
if i := strings.IndexAny(envGOPROXY, ",|"); i >= 0 {
proxy = envGOPROXY[:i]
sep = string(envGOPROXY[i])
envGOPROXY = envGOPROXY[i+1:]
if envGOPROXY == "" {
sep = ""
}
} else {
proxy = envGOPROXY
envGOPROXY = ""
}
proxy = strings.TrimSpace(proxy)
switch proxy {
case "":
continue
case "direct", "off":
sep = ""
envGOPROXY = ""
default:
if _, err := url.Parse(proxy); err != nil {
return "", fmt.Errorf("invalid GOPROXY URL: %w", err)
}
}
cleaned += proxy + sep
}
if cleaned == "" {
return "", errors.New("GOPROXY list is not the empty string, but contains no entries")
}
return cleaned, nil
}
// walkEnvGOPROXY walks through the proxy list parsed from the envGOPROXY.
func walkEnvGOPROXY(envGOPROXY string, onProxy func(proxy *url.URL) error, onDirect func() error) error {
if envGOPROXY == "" {
return errors.New("missing GOPROXY")
}
var lastErr error
for envGOPROXY != "" {
var (
proxy string
fallBackOnError bool
)
if i := strings.IndexAny(envGOPROXY, ",|"); i >= 0 {
proxy = envGOPROXY[:i]
fallBackOnError = envGOPROXY[i] == '|'
envGOPROXY = envGOPROXY[i+1:]
} else {
proxy = envGOPROXY
envGOPROXY = ""
}
switch proxy {
case "direct":
return onDirect()
case "off":
return notExistErrorf("module lookup disabled by GOPROXY=off")
}
u, err := url.Parse(proxy)
if err != nil {
return err
}
if err := onProxy(u); err != nil {
if fallBackOnError || errors.Is(err, fs.ErrNotExist) {
lastErr = err
continue
}
return err
}
return nil
}
return lastErr
}
const defaultEnvGOSUMDB = "sum.golang.org"
// cleanEnvGOSUMDB returns the cleaned envGOSUMDB.
func cleanEnvGOSUMDB(envGOSUMDB string) string {
if envGOSUMDB == "" || envGOSUMDB == defaultEnvGOSUMDB {
return defaultEnvGOSUMDB
}
return envGOSUMDB
}
const sumGolangOrgKey = "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8"
// parseEnvGOSUMDB parses the envGOSUMDB.
func parseEnvGOSUMDB(envGOSUMDB string) (name string, key string, u *url.URL, isDirectURL bool, err error) {
parts := strings.Fields(envGOSUMDB)
if l := len(parts); l == 0 {
return "", "", nil, false, errors.New("missing GOSUMDB")
} else if l > 2 {
return "", "", nil, false, errors.New("invalid GOSUMDB: too many fields")
}
switch parts[0] {
case "sum.golang.google.cn":
if len(parts) == 1 {
parts = append(parts, "https://"+parts[0])
}
fallthrough
case defaultEnvGOSUMDB:
parts[0] = sumGolangOrgKey
}
verifier, err := note.NewVerifier(parts[0])
if err != nil {
return "", "", nil, false, fmt.Errorf("invalid GOSUMDB: %w", err)
}
name = verifier.Name()
key = parts[0]
u, err = url.Parse("https://" + name)
if err != nil ||
strings.HasSuffix(name, "/") ||
u.Host == "" ||
u.RawPath != "" ||
*u != (url.URL{Scheme: "https", Host: u.Host, Path: u.Path, RawPath: u.RawPath}) {
return "", "", nil, false, fmt.Errorf("invalid sumdb name (must be host[/path]): %s %+v", name, *u)
}
isDirectURL = true
if len(parts) > 1 {
u, err = url.Parse(parts[1])
if err != nil {
return "", "", nil, false, fmt.Errorf("invalid GOSUMDB URL: %w", err)
}
isDirectURL = false
}
return
}
// cleanCommaSeparatedList returns the cleaned comma-separated list.
func cleanCommaSeparatedList(list string) string {
var ss []string
for _, s := range strings.Split(list, ",") {
s = strings.TrimSpace(s)
if s != "" {
ss = append(ss, s)
}
}
return strings.Join(ss, ",")
}
// checkCanonicalVersion is like [module.Check] but also checks whether the
// version is canonical.
func checkCanonicalVersion(path, version string) error {
if err := module.Check(path, version); err != nil {
return err
}
if version != module.CanonicalVersion(version) {
return &module.ModuleError{
Path: path,
Err: &module.InvalidVersionError{Version: version, Err: errors.New("not a canonical version")},
}
}
return nil
}
// marshalInfo marshals the version and t as info.
func marshalInfo(version string, t time.Time) string {
return fmt.Sprintf(`{"Version":%q,"Time":%q}`, version, t.UTC().Format(time.RFC3339Nano))
}
// unmarshalInfo unmarshals the s as info and returns version and time.
func unmarshalInfo(s string) (string, time.Time, error) {
var info struct {
Version string
Time time.Time
}
if err := json.Unmarshal([]byte(s), &info); err != nil {
return "", time.Time{}, err
}
if !semver.IsValid(info.Version) {
return "", time.Time{}, errors.New("invalid version")
}
return info.Version, info.Time.UTC(), nil
}
// unmarshalInfoFile is like [unmarshalInfo] but reads the info from the file
// targeted by the name.
func unmarshalInfoFile(name string) (string, time.Time, error) {
b, err := os.ReadFile(name)
if err != nil {
return "", time.Time{}, err
}
version, t, err := unmarshalInfo(string(b))
if err != nil {
return "", time.Time{}, notExistErrorf("invalid info file: %w", err)
}
return version, t, nil
}
// checkModFile checks the mod file targeted by the name.
func checkModFile(name string) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.HasPrefix(strings.TrimSpace(scanner.Text()), "module") {
return nil
}
}
if err := scanner.Err(); err != nil {
return err
}
return notExistErrorf("invalid mod file: missing module directive")
}
// verifyModFile uses the sumdbClient to verify the mod file targeted by the
// name with the modulePath and moduleVersion.
func verifyModFile(sumdbClient *sumdb.Client, name, modulePath, moduleVersion string) error {
sumLines, err := sumdbClient.Lookup(modulePath, moduleVersion+"/go.mod")
if err != nil {
if errors.Is(err, sumdb.ErrGONOSUMDB) {
return nil
}
return err
}
modHash, err := dirhash.DefaultHash([]string{"go.mod"}, func(string) (io.ReadCloser, error) { return os.Open(name) })
if err != nil {
return err
}
modSumLine := fmt.Sprintf("%s %s/go.mod %s", modulePath, moduleVersion, modHash)
for _, sumLine := range sumLines {
if sumLine == modSumLine {
return nil
}
}
return notExistErrorf("%s@%s: invalid version: untrusted revision %s", modulePath, moduleVersion, moduleVersion)
}
// checkZipFile checks the zip file targeted by the name with the modulePath and
// moduleVersion.
func checkZipFile(name, modulePath, moduleVersion string) error {
if _, err := zip.CheckZip(module.Version{Path: modulePath, Version: moduleVersion}, name); err != nil {
return notExistErrorf("invalid zip file: %w", err)
}
return nil
}
// verifyZipFile uses the sumdbClient to verify the zip file targeted by the
// name with the modulePath and moduleVersion.
func verifyZipFile(sumdbClient *sumdb.Client, name, modulePath, moduleVersion string) error {
sumLines, err := sumdbClient.Lookup(modulePath, moduleVersion)
if err != nil {
if errors.Is(err, sumdb.ErrGONOSUMDB) {
return nil
}
return err
}
zipHash, err := dirhash.HashZip(name, dirhash.DefaultHash)
if err != nil {
return err
}
zipSumLine := fmt.Sprintf("%s %s %s", modulePath, moduleVersion, zipHash)
for _, sumLine := range sumLines {
if sumLine == zipSumLine {
return nil
}
}
return notExistErrorf("%s@%s: invalid version: untrusted revision %s", modulePath, moduleVersion, moduleVersion)
}
// closerFunc is an adapter to allow the use of an ordinary function as an [io.Closer].
type closerFunc func() error
// Close implements [io.Closer].
func (f closerFunc) Close() error { return f() }