fix: allow pass proxy env when connect directly

pull/167/head
xingpeng 2021-02-23 14:23:08 +08:00
parent 9e48374173
commit d9e6dffbb0
4 changed files with 69 additions and 9 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ go_repos/*
cacheDir/* cacheDir/*
.idea/* .idea/*
bin/ bin/
.netrc
.env

View File

@ -33,6 +33,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/goproxyio/goproxy/v2/netrc"
"github.com/goproxyio/goproxy/v2/proxy" "github.com/goproxyio/goproxy/v2/proxy"
"golang.org/x/mod/module" "golang.org/x/mod/module"
@ -73,6 +74,9 @@ func init() {
downloadRoot = getDownloadRoot() downloadRoot = getDownloadRoot()
os.Setenv("GOMODCACHE", downloadRoot) os.Setenv("GOMODCACHE", downloadRoot)
if err := netrc.GenerateDotNetrcFile(); err != nil {
log.Fatalf("Failed to generate .netrc file, Error: %s", err.Error())
}
} }
func main() { func main() {
@ -140,6 +144,8 @@ func goJSON(dst interface{}, command ...string) error {
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
cmd.Env = os.Environ()
fmt.Println(cmd.Env)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return fmt.Errorf("%s:\n%s%s", strings.Join(command, " "), stderr.String(), stdout.String()) return fmt.Errorf("%s:\n%s%s", strings.Join(command, " "), stderr.String(), stdout.String())
} }

34
netrc/netrc.go Normal file
View File

@ -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
}

View File

@ -8,6 +8,7 @@ package sumdb
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -20,13 +21,15 @@ var supportedSumDB = []string{
} }
func init() { func init() {
go func() { // 启动 sumdb在真实请求时会使用 goproxy.cn 代理该请求
p := "https://sum.golang.org" enableGoogleSumDB = true
_, err := http.Get(p) // go func() {
if err == nil { // p := "https://sum.golang.org"
enableGoogleSumDB = true // _, err := http.Get(p)
} // if err == nil {
}() // enableGoogleSumDB = true
// }
// }()
} }
//Handler handles sumdb request //Handler handles sumdb request
@ -48,8 +51,14 @@ func Handler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusGone) w.WriteHeader(http.StatusGone)
return return
} }
var p string
p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/") // 使用 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) _, err := url.Parse(p)
if err != nil { if err != nil {
w.WriteHeader(http.StatusGone) w.WriteHeader(http.StatusGone)
@ -72,3 +81,12 @@ func Handler(w http.ResponseWriter, r *http.Request) {
return return
} }
func isSupportedSumDB(hostPath string) bool {
for _, sumdbHost := range supportedSumDB {
if strings.Contains(hostPath, sumdbHost) {
return true
}
}
return false
}