/
sumdb_client_ops.go
134 lines (122 loc) · 3.53 KB
/
sumdb_client_ops.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
package goproxy
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"net/http"
"net/url"
"strings"
"sync"
)
// sumGolangOrgKey is the key for the sum.golang.org.
const sumGolangOrgKey = "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8"
// sumdbClientOps implements [golang.org/x/mod/sumdb.ClientOps].
type sumdbClientOps struct {
initOnce sync.Once
initError error
key []byte
endpointURL *url.URL
envGOPROXY string
envGOSUMDB string
httpClient *http.Client
}
// init initializes the sco.
func (sco *sumdbClientOps) init() {
sumdbParts := strings.Fields(sco.envGOSUMDB)
if l := len(sumdbParts); l == 0 {
sco.initError = errors.New("missing GOSUMDB")
return
} else if l > 2 {
sco.initError = errors.New("invalid GOSUMDB: too many fields")
return
}
if sumdbParts[0] == "sum.golang.google.cn" {
sumdbParts[0] = "sum.golang.org"
if len(sumdbParts) == 1 {
sumdbParts = append(sumdbParts, "https://sum.golang.google.cn")
}
}
if sumdbParts[0] == "sum.golang.org" {
sumdbParts[0] = sumGolangOrgKey
}
sumdbName := sumdbParts[0]
if i := strings.Index(sumdbName, "+"); i >= 0 {
sumdbName = sumdbName[:i]
}
if len(sumdbParts) == 1 {
sumdbParts = append(sumdbParts, sumdbName)
}
sco.key = []byte(sumdbParts[0])
sco.endpointURL, sco.initError = parseRawURL(sumdbParts[1])
if sco.initError != nil {
return
}
if err := walkGOPROXY(sco.envGOPROXY, func(proxy string) error {
proxyURL, err := parseRawURL(proxy)
if err != nil {
return err
}
endpointURL := appendURL(proxyURL, "sumdb", sumdbName)
if err := httpGet(context.Background(), sco.httpClient, appendURL(endpointURL, "/supported").String(), nil); err != nil {
return err
}
sco.endpointURL = endpointURL
return nil
}, func() error {
return nil
}, func() error {
return nil
}); err != nil && !errors.Is(err, errNotFound) {
sco.initError = err
return
}
}
// ReadRemote implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) ReadRemote(path string) ([]byte, error) {
if sco.initOnce.Do(sco.init); sco.initError != nil {
return nil, sco.initError
}
var buf bytes.Buffer
if err := httpGet(context.Background(), sco.httpClient, appendURL(sco.endpointURL, path).String(), &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ReadConfig implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) ReadConfig(file string) ([]byte, error) {
if sco.initOnce.Do(sco.init); sco.initError != nil {
return nil, sco.initError
}
if file == "key" {
return sco.key, nil
} else if strings.HasSuffix(file, "/latest") {
return []byte{}, nil // Empty result means empty tree
}
return nil, fmt.Errorf("unknown config %s", file)
}
// WriteConfig implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) WriteConfig(file string, old, new []byte) error {
sco.initOnce.Do(sco.init)
return sco.initError
}
// ReadCache implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) ReadCache(file string) ([]byte, error) {
if sco.initOnce.Do(sco.init); sco.initError != nil {
return nil, sco.initError
}
return nil, fs.ErrNotExist
}
// WriteCache implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) WriteCache(file string, data []byte) {
sco.initOnce.Do(sco.init)
}
// Log implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) Log(msg string) {
sco.initOnce.Do(sco.init)
}
// SecurityError implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) SecurityError(msg string) {
sco.initOnce.Do(sco.init)
}