From d9e6dffbb0895afcee721473a23c3a294f9c3bb6 Mon Sep 17 00:00:00 2001 From: xingpeng Date: Tue, 23 Feb 2021 14:23:08 +0800 Subject: [PATCH] fix: allow pass proxy env when connect directly --- .gitignore | 2 ++ main.go | 6 ++++++ netrc/netrc.go | 34 ++++++++++++++++++++++++++++++++++ sumdb/handler.go | 36 +++++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 netrc/netrc.go diff --git a/.gitignore b/.gitignore index 3792cb8..5d6fc24 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ go_repos/* cacheDir/* .idea/* bin/ +.netrc +.env diff --git a/main.go b/main.go index 6b1afab..47caf12 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ import ( "syscall" "time" + "github.com/goproxyio/goproxy/v2/netrc" "github.com/goproxyio/goproxy/v2/proxy" "golang.org/x/mod/module" @@ -73,6 +74,9 @@ func init() { downloadRoot = getDownloadRoot() os.Setenv("GOMODCACHE", downloadRoot) + if err := netrc.GenerateDotNetrcFile(); err != nil { + log.Fatalf("Failed to generate .netrc file, Error: %s", err.Error()) + } } func main() { @@ -140,6 +144,8 @@ func goJSON(dst interface{}, command ...string) error { var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr + cmd.Env = os.Environ() + fmt.Println(cmd.Env) if err := cmd.Run(); err != nil { return fmt.Errorf("%s:\n%s%s", strings.Join(command, " "), stderr.String(), stdout.String()) } diff --git a/netrc/netrc.go b/netrc/netrc.go new file mode 100644 index 0000000..f26683c --- /dev/null +++ b/netrc/netrc.go @@ -0,0 +1,34 @@ +package netrc + +import ( + "bytes" + "errors" + "os" + "os/exec" +) + +// GenerateDotNetrcFile 生成 .netrc 文件 +func GenerateDotNetrcFile() error { + githubTokenLogin := os.Getenv("GITHUB_TOKEN_LOGIN") + githubTokenPassword := os.Getenv("GITHUB_TOKEN_PASSWORD") + if githubTokenLogin == "" || githubTokenPassword == "" { + return errors.New("The env GITHUB_TOKEN_LOGIN or GITHUB_TOKEN_PASSWORD cannot be blank") + } + bashScript := ` +#!/bin/sh -e +cat << EOF > /root/.netrc +machine github.com +login ${GITHUB_TOKEN_LOGIN} +password ${GITHUB_TOKEN_PASSWORD} +EOF +` + command := os.ExpandEnv(bashScript) + cmd := exec.Command("/bin/sh", "-c", command) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return errors.New(stderr.String()) + } + return nil +} diff --git a/sumdb/handler.go b/sumdb/handler.go index 0378466..d32cc50 100644 --- a/sumdb/handler.go +++ b/sumdb/handler.go @@ -8,6 +8,7 @@ package sumdb import ( "fmt" "io" + "log" "net/http" "net/url" "strings" @@ -20,13 +21,15 @@ var supportedSumDB = []string{ } func init() { - go func() { - p := "https://sum.golang.org" - _, err := http.Get(p) - if err == nil { - enableGoogleSumDB = true - } - }() + // 启动 sumdb,在真实请求时会使用 goproxy.cn 代理该请求 + enableGoogleSumDB = true + // go func() { + // p := "https://sum.golang.org" + // _, err := http.Get(p) + // if err == nil { + // enableGoogleSumDB = true + // } + // }() } //Handler handles sumdb request @@ -48,8 +51,14 @@ func Handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusGone) return } - - p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") + var p string + // 使用 proxy.cn 代理 supportedSumDB + if isSupportedSumDB(r.URL.Path) { + p = "https://goproxy.cn" + r.URL.Path + log.Printf("Proxy sum db with goproxy.cn, url: %s\n", p) + } else { + p = "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") + } _, err := url.Parse(p) if err != nil { w.WriteHeader(http.StatusGone) @@ -72,3 +81,12 @@ func Handler(w http.ResponseWriter, r *http.Request) { return } + +func isSupportedSumDB(hostPath string) bool { + for _, sumdbHost := range supportedSumDB { + if strings.Contains(hostPath, sumdbHost) { + return true + } + } + return false +}