forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goproxy.go
465 lines (423 loc) · 12.9 KB
/
goproxy.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
/*
Package goproxy implements a minimalist Go module proxy handler.
*/
package goproxy
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"sync"
"golang.org/x/mod/module"
)
// tempDirPattern is the pattern for creating temporary directories.
const tempDirPattern = "goproxy.tmp.*"
// Goproxy is the top-level struct of this project.
//
// For requests involving the download of a large number of modules (e.g., for
// bulk static analysis), Goproxy supports a non-standard header,
// "Disable-Module-Fetch: true", which instructs it to return only cached
// content.
type Goproxy struct {
// Fetcher is used to fetch module files.
//
// If Fetcher is nil, [GoFetcher] is used.
//
// Note that any error returned by Fetcher that matches [fs.ErrNotExist]
// will result in a 404 response with the error message in the response
// body.
Fetcher Fetcher
// ProxiedSumDBs is a list of proxied checksum databases (see
// https://go.dev/design/25530-sumdb#proxying-a-checksum-database). Each
// entry is in the form "<sumdb-name>" or "<sumdb-name> <sumdb-URL>".
// The first form is a shorthand for the second, where the corresponding
// <sumdb-URL> will be the <sumdb-name> itself as a host with an "https"
// scheme. Invalid entries will be silently ignored.
//
// If ProxiedSumDBs contains duplicate checksum database names, only the
// last value in the slice for each duplicate checksum database name is
// used.
ProxiedSumDBs []string
// Cacher is used to cache module files.
//
// If Cacher is nil, caching is disabled.
Cacher Cacher
// 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.
//
// If Transport is nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
// ErrorLogger is used to log errors that occur during proxying.
//
// If ErrorLogger is nil, [log.Default] is used.
ErrorLogger *log.Logger
initOnce sync.Once
fetcher Fetcher
proxiedSumDBs map[string]*url.URL
httpClient *http.Client
}
// init initializes the g.
func (g *Goproxy) init() {
g.fetcher = g.Fetcher
if g.fetcher == nil {
g.fetcher = &GoFetcher{TempDir: g.TempDir, Transport: g.Transport}
}
g.proxiedSumDBs = map[string]*url.URL{}
for _, sumdb := range g.ProxiedSumDBs {
parts := strings.Fields(sumdb)
if len(parts) == 0 {
continue
}
name := parts[0]
rawURL := "https://" + name
if len(parts) > 1 {
rawURL = parts[1]
}
u, err := url.Parse(rawURL)
if err != nil {
continue
}
g.proxiedSumDBs[name] = u
}
g.httpClient = &http.Client{Transport: g.Transport}
}
// ServeHTTP implements [http.Handler].
func (g *Goproxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
g.initOnce.Do(g.init)
switch req.Method {
case http.MethodGet, http.MethodHead:
default:
responseMethodNotAllowed(rw, req, 86400)
return
}
path := cleanPath(req.URL.Path)
if path != req.URL.Path || path[len(path)-1] == '/' {
responseNotFound(rw, req, 86400)
return
}
target := path[1:] // Remove the leading slash.
if strings.HasPrefix(target, "sumdb/") {
g.serveSumDB(rw, req, target)
return
}
g.serveFetch(rw, req, target)
}
// serveFetch serves fetch requests.
func (g *Goproxy) serveFetch(rw http.ResponseWriter, req *http.Request, target string) {
noFetch, _ := strconv.ParseBool(req.Header.Get("Disable-Module-Fetch"))
escapedModulePath, after, ok := strings.Cut(target, "/@")
if !ok {
responseNotFound(rw, req, 86400, "missing /@v/")
return
}
modulePath, err := module.UnescapePath(escapedModulePath)
if err != nil {
responseNotFound(rw, req, 86400, err)
return
}
switch after {
case "latest":
g.serveFetchQuery(rw, req, target, modulePath, after, noFetch)
return
case "v/list":
g.serveFetchList(rw, req, target, modulePath, noFetch)
return
}
if !strings.HasPrefix(after, "v/") {
responseNotFound(rw, req, 86400, "missing /@v/")
return
}
after = after[2:] // Remove the leading "v/".
ext := path.Ext(after)
switch ext {
case ".info", ".mod", ".zip":
case "":
responseNotFound(rw, req, 86400, fmt.Sprintf("no file extension in filename %q", after))
return
default:
responseNotFound(rw, req, 86400, fmt.Sprintf("unexpected extension %q", ext))
return
}
escapedModuleVersion := strings.TrimSuffix(after, ext)
moduleVersion, err := module.UnescapeVersion(escapedModuleVersion)
if err != nil {
responseNotFound(rw, req, 86400, err)
return
}
switch moduleVersion {
case "latest", "upgrade", "patch":
responseNotFound(rw, req, 86400, "invalid version")
return
}
if checkCanonicalVersion(modulePath, moduleVersion) == nil {
g.serveFetchDownload(rw, req, target, modulePath, moduleVersion, noFetch)
} else if ext == ".info" {
g.serveFetchQuery(rw, req, target, modulePath, moduleVersion, noFetch)
} else {
responseNotFound(rw, req, 86400, "unrecognized version")
}
}
// serveFetchQuery serves fetch query requests.
func (g *Goproxy) serveFetchQuery(rw http.ResponseWriter, req *http.Request, target, modulePath, moduleQuery string, noFetch bool) {
const (
contentType = "application/json; charset=utf-8"
cacheControlMaxAge = 60
)
if noFetch {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, nil)
return
}
version, time, err := g.fetcher.Query(req.Context(), modulePath, moduleQuery)
if err != nil {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, func() {
g.logErrorf("failed to query module version: %s: %v", target, err)
responseError(rw, req, err, true)
})
return
}
g.servePutCache(rw, req, target, contentType, cacheControlMaxAge, strings.NewReader(marshalInfo(version, time)))
}
// serveFetchList serves fetch list requests.
func (g *Goproxy) serveFetchList(rw http.ResponseWriter, req *http.Request, target, modulePath string, noFetch bool) {
const (
contentType = "text/plain; charset=utf-8"
cacheControlMaxAge = 60
)
if noFetch {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, nil)
return
}
versions, err := g.fetcher.List(req.Context(), modulePath)
if err != nil {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, func() {
g.logErrorf("failed to list module versions: %s: %v", target, err)
responseError(rw, req, err, true)
})
return
}
g.servePutCache(rw, req, target, contentType, cacheControlMaxAge, strings.NewReader(strings.Join(versions, "\n")))
}
// serveFetchDownload serves fetch download requests.
func (g *Goproxy) serveFetchDownload(rw http.ResponseWriter, req *http.Request, target, modulePath, moduleVersion string, noFetch bool) {
const cacheControlMaxAge = 604800
ext := path.Ext(target)
var contentType string
switch ext {
case ".info":
contentType = "application/json; charset=utf-8"
case ".mod":
contentType = "text/plain; charset=utf-8"
case ".zip":
contentType = "application/zip"
}
if noFetch {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, nil)
return
}
if content, err := g.cache(req.Context(), target); err == nil {
responseSuccess(rw, req, content, contentType, cacheControlMaxAge)
return
} else if !errors.Is(err, fs.ErrNotExist) {
g.logErrorf("failed to get cached module file: %s: %v", target, err)
responseInternalServerError(rw, req)
return
}
info, mod, zip, err := g.fetcher.Download(req.Context(), modulePath, moduleVersion)
if err != nil {
g.logErrorf("failed to download module version: %s: %v", target, err)
responseError(rw, req, err, false)
return
}
defer func() {
info.Close()
mod.Close()
zip.Close()
}()
targetWithoutExt := strings.TrimSuffix(target, path.Ext(target))
for _, cache := range []struct {
ext string
content io.ReadSeeker
}{
{".info", info},
{".mod", mod},
{".zip", zip},
} {
if err := g.putCache(req.Context(), targetWithoutExt+cache.ext, cache.content); err != nil {
g.logErrorf("failed to cache module file: %s: %v", target, err)
responseInternalServerError(rw, req)
return
}
}
var content io.ReadSeeker
switch ext {
case ".info":
content = info
case ".mod":
content = mod
case ".zip":
content = zip
}
if _, err := content.Seek(0, io.SeekStart); err != nil {
g.logErrorf("failed to seek: %v", err)
responseInternalServerError(rw, req)
return
}
responseSuccess(rw, req, content, contentType, 604800)
}
// serveSumDB serves checksum database proxy requests.
func (g *Goproxy) serveSumDB(rw http.ResponseWriter, req *http.Request, target string) {
name, path, ok := strings.Cut(strings.TrimPrefix(target, "sumdb/"), "/")
if !ok {
responseNotFound(rw, req, 86400)
return
}
path = "/" + path // Add the leading slash back.
u, ok := g.proxiedSumDBs[name]
if !ok {
responseNotFound(rw, req, 86400)
return
}
var (
contentType string
cacheControlMaxAge int
)
switch {
case path == "/supported":
setResponseCacheControlHeader(rw, 86400)
rw.WriteHeader(http.StatusOK)
return
case path == "/latest":
contentType = "text/plain; charset=utf-8"
cacheControlMaxAge = 3600
case strings.HasPrefix(path, "/lookup/"):
contentType = "text/plain; charset=utf-8"
cacheControlMaxAge = 86400
case strings.HasPrefix(path, "/tile/"):
contentType = "application/octet-stream"
cacheControlMaxAge = 86400
default:
responseNotFound(rw, req, 86400)
return
}
tempDir, err := os.MkdirTemp(g.TempDir, tempDirPattern)
if err != nil {
g.logErrorf("failed to create temporary directory: %v", err)
responseInternalServerError(rw, req)
return
}
defer os.RemoveAll(tempDir)
file, err := httpGetTemp(req.Context(), g.httpClient, appendURL(u, path).String(), tempDir)
if err != nil {
g.serveCache(rw, req, target, contentType, cacheControlMaxAge, func() {
g.logErrorf("failed to proxy checksum database: %s: %v", target, err)
responseError(rw, req, err, true)
})
return
}
g.servePutCacheFile(rw, req, target, contentType, cacheControlMaxAge, file)
}
// serveCache serves requests with cached module files.
func (g *Goproxy) serveCache(rw http.ResponseWriter, req *http.Request, name, contentType string, cacheControlMaxAge int, onNotFound func()) {
content, err := g.cache(req.Context(), name)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
if onNotFound != nil {
onNotFound()
} else {
responseNotFound(rw, req, 60, "temporarily unavailable")
}
return
}
g.logErrorf("failed to get cached module file: %s: %v", name, err)
responseInternalServerError(rw, req)
return
}
defer content.Close()
responseSuccess(rw, req, content, contentType, cacheControlMaxAge)
}
// servePutCache serves requests after putting the content to the g.Cacher.
func (g *Goproxy) servePutCache(rw http.ResponseWriter, req *http.Request, name, contentType string, cacheControlMaxAge int, content io.ReadSeeker) {
if err := g.putCache(req.Context(), name, content); err != nil {
g.logErrorf("failed to cache module file: %s: %v", name, err)
responseInternalServerError(rw, req)
return
}
if _, err := content.Seek(0, io.SeekStart); err != nil {
g.logErrorf("failed to seek: %v", err)
responseInternalServerError(rw, req)
return
}
responseSuccess(rw, req, content, contentType, cacheControlMaxAge)
}
// servePutCacheFile is like [servePutCache] but reads the content from the
// local file.
func (g *Goproxy) servePutCacheFile(rw http.ResponseWriter, req *http.Request, name, contentType string, cacheControlMaxAge int, file string) {
f, err := os.Open(file)
if err != nil {
g.logErrorf("failed to open file: %v", err)
responseInternalServerError(rw, req)
return
}
defer f.Close()
g.servePutCache(rw, req, name, contentType, cacheControlMaxAge, f)
}
// cache returns the matched cache for the name from the g.Cacher.
func (g *Goproxy) cache(ctx context.Context, name string) (io.ReadCloser, error) {
if g.Cacher == nil {
return nil, fs.ErrNotExist
}
return g.Cacher.Get(ctx, name)
}
// putCache puts a cache to the g.Cacher for the name with the content.
func (g *Goproxy) putCache(ctx context.Context, name string, content io.ReadSeeker) error {
if g.Cacher == nil {
return nil
}
return g.Cacher.Put(ctx, name, content)
}
// putCacheFile is like [putCache] but reads the content from the local file.
func (g *Goproxy) putCacheFile(ctx context.Context, name, file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
return g.putCache(ctx, name, f)
}
// logErrorf formats according to a format specifier and writes to the g.ErrorLogger.
func (g *Goproxy) logErrorf(format string, v ...any) {
msg := "goproxy: " + fmt.Sprintf(format, v...)
if g.ErrorLogger != nil {
g.ErrorLogger.Output(2, msg)
} else {
log.Output(2, msg)
}
}
// cleanPath returns the canonical path for the p.
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
np := path.Clean(p)
if p[len(p)-1] == '/' && np != "/" {
if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
return p
}
return np + "/"
}
return np
}