From 80633402c2b891744d62c03045d00135853606e7 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 8 Dec 2020 16:43:10 +0300 Subject: [PATCH] Fix bug with proxying requests to GitHub --- common/pkgre.spec | 5 ++++- server/morpher/morpher.go | 22 +++++++++++++++++----- server/server.go | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/common/pkgre.spec b/common/pkgre.spec index 1637f52..2247406 100644 --- a/common/pkgre.spec +++ b/common/pkgre.spec @@ -54,7 +54,7 @@ Summary: pkg.re morpher server Name: pkgre -Version: 4.0.0 +Version: 4.1.0 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -153,6 +153,9 @@ rm -rf %{buildroot} ################################################################################ %changelog +* Tue Dec 08 2020 Anton Novojilov - 4.1.0-0 +- Fixed bug with proxying requests to GitHub + * Thu Nov 12 2020 Anton Novojilov - 4.0.0-0 - Proxying all requests due to problems with Go Modules Services - ek package updated to v12 diff --git a/server/morpher/morpher.go b/server/morpher/morpher.go index 9d67899..dfa7c55 100644 --- a/server/morpher/morpher.go +++ b/server/morpher/morpher.go @@ -8,6 +8,7 @@ package morpher // ////////////////////////////////////////////////////////////////////////////////// // import ( + "bytes" "errors" "fmt" "net/http" @@ -62,6 +63,13 @@ type Metrics struct { // ////////////////////////////////////////////////////////////////////////////////// // +var ( + UAGit = []byte("git/") // Git User-Agent + UAGo = []byte("Go-http-client/") // Go User-Agent +) + +// ////////////////////////////////////////////////////////////////////////////////// // + // majorVerRegExp regexp for extracting major version var majorVerRegExp = regexp.MustCompile(`^[a-zA-Z]{0,}([0-9]{1}.*)`) @@ -187,15 +195,19 @@ func requestHandler(ctx *fasthttp.RequestCtx) { return } - atomic.AddUint64(&metrics.Redirects, 1) - - // Redirect to github appendProcHeader(ctx, start) url := repoInfo.GitHubURL(pkgInfo.TargetName) - log.Debug("Proxying request to %s", url) - proxyRequest(ctx, url) + // Proxy only requests from Go and Git + if bytes.HasPrefix(ctx.UserAgent(), UAGit) || bytes.HasPrefix(ctx.UserAgent(), UAGo) { + log.Debug("Proxying request to %s", url) + proxyRequest(ctx, url) + } else { + atomic.AddUint64(&metrics.Redirects, 1) + log.Debug("Redirecting request to %s", url) + redirectRequest(ctx, url) + } } // processBasicRequest redirect requests from main page to page defined in config diff --git a/server/server.go b/server/server.go index 1858916..5b7ff22 100644 --- a/server/server.go +++ b/server/server.go @@ -31,7 +31,7 @@ import ( // Application info const ( APP = "PkgRE Morpher Server" - VER = "4.0.0" + VER = "4.1.0" DESC = "HTTP Server for morphing go get requests" )