mirror of https://github.com/goproxyio/goproxy
fix: allow pass proxy env when connect directly
parent
9e48374173
commit
d9e6dffbb0
|
@ -3,3 +3,5 @@ go_repos/*
|
||||||
cacheDir/*
|
cacheDir/*
|
||||||
.idea/*
|
.idea/*
|
||||||
bin/
|
bin/
|
||||||
|
.netrc
|
||||||
|
.env
|
||||||
|
|
6
main.go
6
main.go
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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"
|
|
||||||
_, err := http.Get(p)
|
|
||||||
if err == nil {
|
|
||||||
enableGoogleSumDB = true
|
enableGoogleSumDB = true
|
||||||
}
|
// go func() {
|
||||||
}()
|
// p := "https://sum.golang.org"
|
||||||
|
// _, 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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue