Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ make

## Started

### Proxy mode
### Proxy mode

```shell
./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test
Expand All @@ -32,7 +32,7 @@ If you run `go get -v pkg` in the proxy machine, you should set a new `GOPATH` w

See [`test/get_test.sh`](./test/get_test.sh).

### Router mode
### Router mode

```shell
./bin/goproxy -listen=0.0.0.0:80 -proxy https://goproxy.io
Expand All @@ -52,7 +52,7 @@ go get +-------> |goproxy| +-------> |goproxy.io| +---> golang.org/x/net
router mode proxy mode
```

In `Router mode`, use the `-exclude` flag to set a glob pattern. The glob will specify what packages should not try to resolve with the value of `-proxy`. Modules which match the `-exclude` pattern will resolve direct to the repo which
In `Router mode`, use the `-exclude` flag to set a glob pattern. The glob will specify what packages should not try to resolve with the value of `-proxy`. Modules which match the `-exclude` pattern will resolve direct to the repo which
matches the module path.

NOTE: Patterns are matched to the full path specified, not only to the host component.
Expand All @@ -61,6 +61,16 @@ NOTE: Patterns are matched to the full path specified, not only to the host comp
./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test -proxy https://goproxy.io -exclude "*.corp.example.com,rsc.io/private"
```

### SumDB Proxy

By default, sumdb (Checksum Database) requests use the same proxy host specified by the `-proxy` flag. You can specify a different sumdb proxy using the `-sumdbProxy` flag:

```shell
./bin/goproxy -listen=0.0.0.0:80 -proxy https://goproxy.io -sumdbProxy https://goproxy.cn
```

When the `-sumdbProxy` flag is set, all sumdb requests (including `sum.golang.org`, `sum.golang.google.cn`, and `gosum.io`) will be proxied through the specified host.

### Private module authentication

Some private modules are gated behind `git` authentication. To resolve this, you can force git to rewrite the URL with a personal access token present for auth
Expand All @@ -77,7 +87,7 @@ This can be done for other git providers as well, following the same pattern
docker run -d -p80:8081 goproxy/goproxy
```

Use the -v flag to persisting the proxy module data (change ___cacheDir___ to your own dir):
Use the -v flag to persisting the proxy module data (change **_cacheDir_** to your own dir):

```
docker run -d -p80:8081 -v cacheDir:/go goproxy/goproxy
Expand Down Expand Up @@ -108,27 +118,27 @@ spec:
app: goproxy
spec:
containers:
- args:
- -proxy
- https://goproxy.io
- -listen
- 0.0.0.0:8081
- -cacheDir
- /tmp/test
- -exclude
- github.com/my-org/*
image: goproxy/goproxy
name: goproxy
ports:
- containerPort: 8081
volumeMounts:
- mountPath: /tmp/test
- args:
- -proxy
- https://goproxy.io
- -listen
- 0.0.0.0:8081
- -cacheDir
- /tmp/test
- -exclude
- github.com/my-org/*
image: goproxy/goproxy
name: goproxy
ports:
- containerPort: 8081
volumeMounts:
- mountPath: /tmp/test
name: goproxy
volumes:
- emptyDir:
medium: Memory
sizeLimit: 500Mi
name: goproxy
- emptyDir:
medium: Memory
sizeLimit: 500Mi
name: goproxy
```

Deployment (with gitconfig secret):
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/goproxyio/goproxy/v2

go 1.12
go 1.26

require (
github.com/goproxyio/windows v0.0.0-20191126033816-f4a809841617
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"time"

"github.com/goproxyio/goproxy/v2/proxy"
"github.com/goproxyio/goproxy/v2/sumdb"

"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/mod/module"
Expand All @@ -43,12 +44,14 @@ var downloadRoot string
var listen, promListen string
var cacheDir string
var proxyHost string
var sumdbProxy string
var excludeHost string
var cacheExpire time.Duration

func init() {
flag.StringVar(&excludeHost, "exclude", "", "exclude host pattern, you can exclude internal Git services")
flag.StringVar(&proxyHost, "proxy", "", "next hop proxy for Go Modules, recommend use https://goproxy.io")
flag.StringVar(&sumdbProxy, "sumdbProxy", "", "sumdb proxy host, default use proxy value")
flag.StringVar(&cacheDir, "cacheDir", "", "Go Modules cache dir, default is $GOPATH/pkg/mod/cache/download")
flag.StringVar(&listen, "listen", "0.0.0.0:8081", "service listen address")
flag.DurationVar(&cacheExpire, "cacheExpire", 5*time.Minute, "Go Modules cache expiration (min), default is 5 min")
Expand Down Expand Up @@ -79,6 +82,12 @@ func main() {
log.SetFlags(0)

var handle http.Handler

if sumdbProxy != "" {
log.Printf("SumDBProxy %s\n", sumdbProxy)
sumdb.SetSumdbProxy(sumdbProxy)
}

if proxyHost != "" {
log.Printf("ProxyHost %s\n", proxyHost)
if excludeHost != "" {
Expand Down
17 changes: 16 additions & 1 deletion sumdb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -86,14 +87,17 @@ func proxySumdb(ctx context.Context, host, path string, respChan chan<- *http.Re
if err != nil {
return
}
urlPath.Path = path

urlPath.Path = strings.TrimSuffix(urlPath.Path, "/") + "/" + path
log.Printf("[sumdb] proxy request to: %s\n", urlPath.String())

req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlPath.String(), nil)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("[sumdb] proxy request error: %v\n", err)
return
}

Expand All @@ -104,3 +108,14 @@ func proxySumdb(ctx context.Context, host, path string, respChan chan<- *http.Re
}

}

func SetSumdbProxy(proxyHost string) {
if proxyHost == "" {
return
}
proxyHost = strings.TrimSuffix(proxyHost, "/")
for dbName := range supportedSumDB {
proxyURL := proxyHost + "/sumdb/" + dbName + "/"
supportedSumDB[dbName] = []string{proxyURL}
}
}
Loading