enable sumdb if server can not access sum.golang.org

pull/170/head
kun 2021-02-26 14:15:44 +08:00
parent 9e48374173
commit 4ca478b120
1 changed files with 29 additions and 6 deletions

View File

@ -31,11 +31,6 @@ func init() {
//Handler handles sumdb request //Handler handles sumdb request
func Handler(w http.ResponseWriter, r *http.Request) { func Handler(w http.ResponseWriter, r *http.Request) {
if !enableGoogleSumDB {
w.WriteHeader(http.StatusNotFound)
return
}
if strings.HasSuffix(r.URL.Path, "/supported") { if strings.HasSuffix(r.URL.Path, "/supported") {
for _, supported := range supportedSumDB { for _, supported := range supportedSumDB {
uri := fmt.Sprintf("/sumdb/%s/supported", supported) uri := fmt.Sprintf("/sumdb/%s/supported", supported)
@ -49,6 +44,11 @@ func Handler(w http.ResponseWriter, r *http.Request) {
return return
} }
if !enableGoogleSumDB {
sumViaGoproxy(w, r)
return
}
p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/")
_, err := url.Parse(p) _, err := url.Parse(p)
if err != nil { if err != nil {
@ -69,6 +69,29 @@ func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, err.Error()) fmt.Fprintf(w, err.Error())
return 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 return
} }