From d08b92a0dddd6408abbb261c3cd240464d59cc88 Mon Sep 17 00:00:00 2001 From: kun Date: Tue, 18 Feb 2020 21:56:04 +0800 Subject: [PATCH] support sumdb --- proxy/router.go | 7 +++++ proxy/server.go | 9 +++++++ sumdb/handler.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 sumdb/handler.go diff --git a/proxy/router.go b/proxy/router.go index 33a745e..c5d0caa 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -16,6 +16,7 @@ import ( "time" "github.com/goproxyio/goproxy/v2/renameio" + "github.com/goproxyio/goproxy/v2/sumdb" ) const ListExpire = 5 * time.Minute @@ -110,6 +111,12 @@ func (rt *Router) Direct(path string) bool { } func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // sumdb handler + if strings.HasPrefix(r.URL.Path, "/sumdb/") { + sumdb.Handler(w, r) + return + } + if rt.proxy == nil || rt.Direct(strings.TrimPrefix(r.URL.Path, "/")) { log.Printf("------ --- %s [direct]\n", r.URL) rt.srv.ServeHTTP(w, r) diff --git a/proxy/server.go b/proxy/server.go index 72198ad..8c7c3e2 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -15,6 +15,8 @@ import ( "strings" "time" + "github.com/goproxyio/goproxy/v2/sumdb" + "golang.org/x/mod/module" ) @@ -119,6 +121,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + + // sumdb handler + if strings.HasPrefix(r.URL.Path, "/sumdb/") { + sumdb.Handler(w, r) + return + } + i := strings.Index(r.URL.Path, "/@") if i < 0 { http.Error(w, "no such path", http.StatusNotFound) diff --git a/sumdb/handler.go b/sumdb/handler.go new file mode 100644 index 0000000..1721d66 --- /dev/null +++ b/sumdb/handler.go @@ -0,0 +1,68 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// Package proxy implements the HTTP protocols for serving a Go module proxy. +package sumdb + +import ( + "fmt" + "io" + "net/http" + "net/url" + "strings" +) + +var enableGoogleSumDB bool +var supportedSumDB = []string{ + "sum.golang.org", + "gosum.io", +} + +func init() { + go func() { + p := "https://sum.google.com" + _, err := http.Get(p) + if err == nil { + enableGoogleSumDB = true + } + }() +} + +//Handler handles sumdb request +func Handler(w http.ResponseWriter, r *http.Request) { + if !enableGoogleSumDB { + w.WriteHeader(http.StatusNotFound) + return + } + + for _, supported := range supportedSumDB { + uri := fmt.Sprintf("/sumdb/%s/supported", supported) + if r.URL.Path == uri { + w.WriteHeader(http.StatusOK) + return + } + } + + p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") + _, 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 +}