From ae73e81ba0e55b1473d951d1e9d09560dea0284b Mon Sep 17 00:00:00 2001 From: formych Date: Tue, 10 Sep 2019 00:50:32 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCI=E6=AD=BB=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ main.go | 2 +- test/get_test.sh | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f1497d0..bc7ab64 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ## Requirements It invokes the local go command to answer requests. + The default cacheDir is GOPATH, you can set it up by yourself according to the situation. ## Build git clone https://github.com/goproxyio/goproxy.git @@ -18,6 +19,9 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test + If you run `go get -v pkg` in the proxy machine, shuold set a new GOPATH which is not different from the old GOPATH, or mayebe deadlock. + See the file test/get_test.sh. + ### Router mode Use the -proxy flag switch to "Router mode", which diff --git a/main.go b/main.go index 53d4e82..b7ee066 100644 --- a/main.go +++ b/main.go @@ -174,7 +174,7 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) { if err != nil { return nil, err } - file := filepath.Join(downloadRoot, escMod+"/@v/listproxy") + file := filepath.Join(downloadRoot, escMod+"/@v/list") if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < listExpire { return os.Open(file) } diff --git a/test/get_test.sh b/test/get_test.sh index 10212b7..d1fb500 100755 --- a/test/get_test.sh +++ b/test/get_test.sh @@ -2,6 +2,7 @@ export GO111MODULE=on export GOPROXY='http://127.0.0.1:8081' +export GOPATH=/tmp/go datafile='test/testdata/get.txt' From ebbcb6b928e567384e21e4b8d9e119a02089c8fc Mon Sep 17 00:00:00 2001 From: Michael Melnyk Date: Mon, 7 Oct 2019 19:51:37 +0000 Subject: [PATCH 2/6] Fix issue with excluded @latest --- proxy/router.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/proxy/router.go b/proxy/router.go index a6663df..4f99906 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -115,11 +115,25 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { rt.srv.ServeHTTP(w, r) return } + file := filepath.Join(rt.downloadRoot, r.URL.Path) if info, err := os.Stat(file); err == nil { if f, err := os.Open(file); err == nil { var ctype string defer f.Close() + if strings.HasSuffix(r.URL.Path, "/@latest") { + if time.Since(info.ModTime()) >= ListExpire { + log.Printf("------ --- %s [proxy]\n", r.URL) + rt.proxy.ServeHTTP(w, r) + } else { + ctype = "text/plain; charset=UTF-8" + w.Header().Set("Content-Type", ctype) + log.Printf("------ --- %s [cached]\n", r.URL) + http.ServeContent(w, r, "", info.ModTime(), f) + } + return + } + i := strings.Index(r.URL.Path, "/@v/") if i < 0 { http.Error(w, "no such path", http.StatusNotFound) From c32cef2d02d294f8ed2225ce93c18741b75f2428 Mon Sep 17 00:00:00 2001 From: Qingshan Luo Date: Mon, 14 Oct 2019 17:55:23 +0800 Subject: [PATCH 3/6] Update README.md Fix typo. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc7ab64..a61e8ec 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test - If you run `go get -v pkg` in the proxy machine, shuold set a new GOPATH which is not different from the old GOPATH, or mayebe deadlock. + If you run `go get -v pkg` in the proxy machine, should set a new GOPATH which is not different from the old GOPATH, or mayebe deadlock. See the file test/get_test.sh. ### Router mode From 5b72060f3720f4020f8006e611112f4d7507c7b5 Mon Sep 17 00:00:00 2001 From: kun Date: Thu, 7 Nov 2019 09:28:16 +0800 Subject: [PATCH 4/6] auto enable go module --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index b7ee066..40cdd81 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,9 @@ func init() { os.Setenv("GOPRIVATE", excludeHost) } + // Enable Go module + os.Setenv("GO111MODULE", "on") + downloadRoot = getDownloadRoot() } From 970dde4fb9b58517f6a9e1084127aa8168f44c99 Mon Sep 17 00:00:00 2001 From: kun Date: Sat, 16 Nov 2019 10:23:14 +0800 Subject: [PATCH 5/6] update go.mod to v2 --- go.mod | 2 +- main.go | 2 +- proxy/router.go | 2 +- renameio/renameio.go | 2 +- renameio/renameio_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 36e710a..3dd0859 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/goproxyio/goproxy +module github.com/goproxyio/goproxy/v2 go 1.12 diff --git a/main.go b/main.go index 40cdd81..0565bc0 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ import ( "syscall" "time" - "github.com/goproxyio/goproxy/proxy" + "github.com/goproxyio/goproxy/v2/proxy" "golang.org/x/mod/module" ) diff --git a/proxy/router.go b/proxy/router.go index 4f99906..33a745e 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -15,7 +15,7 @@ import ( "strings" "time" - "github.com/goproxyio/goproxy/renameio" + "github.com/goproxyio/goproxy/v2/renameio" ) const ListExpire = 5 * time.Minute diff --git a/renameio/renameio.go b/renameio/renameio.go index 5d08f6b..6a02991 100644 --- a/renameio/renameio.go +++ b/renameio/renameio.go @@ -13,7 +13,7 @@ import ( "path/filepath" "strconv" - "github.com/goproxyio/goproxy/robustio" + "github.com/goproxyio/goproxy/v2/robustio" ) const patternSuffix = ".tmp" diff --git a/renameio/renameio_test.go b/renameio/renameio_test.go index 172df35..14f419d 100644 --- a/renameio/renameio_test.go +++ b/renameio/renameio_test.go @@ -20,7 +20,7 @@ import ( "testing" "time" - "github.com/goproxyio/goproxy/robustio" + "github.com/goproxyio/goproxy/v2/robustio" ) func TestConcurrentReadsAndWrites(t *testing.T) { From 02f5e372195ee77df12987c0ec7627b3ec6f24e5 Mon Sep 17 00:00:00 2001 From: kun Date: Thu, 28 Nov 2019 17:31:52 +0800 Subject: [PATCH 6/6] fix non-unix OS use Join --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 0565bc0..96d035d 100644 --- a/main.go +++ b/main.go @@ -119,7 +119,7 @@ func getDownloadRoot() string { } if cacheDir != "" { os.Setenv("GOPATH", cacheDir) - return filepath.Join(cacheDir, "pkg/mod/cache/download") + return filepath.Join(cacheDir, "pkg", "mod", "cache", "download") } if err := goJSON(&env, "go", "env", "-json", "GOPATH"); err != nil { log.Fatal(err) @@ -128,7 +128,7 @@ func getDownloadRoot() string { if len(list) == 0 || list[0] == "" { log.Fatalf("missing $GOPATH") } - return filepath.Join(list[0], "pkg/mod/cache/download") + return filepath.Join(list[0], "pkg", "mod", "cache", "download") } // goJSON runs the go command and parses its JSON output into dst. @@ -177,7 +177,7 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) { if err != nil { return nil, err } - file := filepath.Join(downloadRoot, escMod+"/@v/list") + file := filepath.Join(downloadRoot, escMod, "@v", "list") if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < listExpire { return os.Open(file) }