From 4ca478b12000edf9a11e69eddd0b2ffee3af6694 Mon Sep 17 00:00:00 2001 From: kun Date: Fri, 26 Feb 2021 14:15:44 +0800 Subject: [PATCH] enable sumdb if server can not access sum.golang.org --- sumdb/handler.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/sumdb/handler.go b/sumdb/handler.go index 0378466..2787c8a 100644 --- a/sumdb/handler.go +++ b/sumdb/handler.go @@ -31,11 +31,6 @@ func init() { //Handler handles sumdb request func Handler(w http.ResponseWriter, r *http.Request) { - if !enableGoogleSumDB { - w.WriteHeader(http.StatusNotFound) - return - } - if strings.HasSuffix(r.URL.Path, "/supported") { for _, supported := range supportedSumDB { uri := fmt.Sprintf("/sumdb/%s/supported", supported) @@ -49,6 +44,11 @@ func Handler(w http.ResponseWriter, r *http.Request) { return } + if !enableGoogleSumDB { + sumViaGoproxy(w, r) + return + } + p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") _, err := url.Parse(p) if err != nil { @@ -69,6 +69,29 @@ func Handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, err.Error()) return } + return +} +func sumViaGoproxy(w http.ResponseWriter, r *http.Request) { + p := "https://goproxy.io" + r.URL.Path + _, err := url.Parse(p) + if err != nil { + w.WriteHeader(http.StatusGone) + fmt.Fprintf(w, err.Error()) + return + } + + resp, err := http.Get(p) + if err != nil { + w.WriteHeader(http.StatusGone) + fmt.Fprintf(w, err.Error()) + return + } + defer resp.Body.Close() + w.WriteHeader(resp.StatusCode) + if _, err := io.Copy(w, resp.Body); err != nil { + fmt.Fprintf(w, err.Error()) + return + } return }