Skip to content
Merged
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
- staticcheck
- unconvert
- unused
- modernize
settings:
goconst:
min-len: 2
Expand Down
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ server:
pypi:
pypi.org: https://pypi.org/simple
rubygems:
rubygems.org: https://rubygems.org
rubygems: https://rubygems.org
galaxy:
ansible:
url: https://galaxy.ansible.com
Expand All @@ -20,6 +20,8 @@ server:
get_helm: https://get.helm.sh
goproxy:
golang: https://proxy.golang.org
npm:
npmjs: https://registry.npmjs.org
```

## Usage
Expand Down Expand Up @@ -74,8 +76,6 @@ source "https://rubygems.org" do
end
```



### Static files

Access cached static files:
Expand Down Expand Up @@ -105,3 +105,31 @@ The proxy supports all standard GOPROXY protocol endpoints:
- `/{module}/@v/{version}.mod` - go.mod file for the version
- `/{module}/@v/{version}.zip` - source code archive
- `/{module}/@latest` - latest version info

### NPM

To use HUB as an npm registry proxy, set the `registry` to your HUB instance:

```bash
npm config set registry http://localhost:6587/npm/npmjs
```

For a scoped registry:

```bash
npm config set @my-scope:registry http://localhost:6587/npm/npmjs
```

Or it can be used in the file `.npmrc`

```ini
registry=http://localhost:6587/npm/npmjs
```

The proxy supports the following npm registry endpoints:

- `/{package}` - package metadata (packument)
- `/@scope%2F{name}` - scoped package metadata (packument)
- `/{package}/-/{tarball}.tgz` - package tarball
- `/@scope/{name}/-/{tarball}.tgz` - scoped package tarball
- `/-/v1/search` - search (cached for 10 minutes)
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ server:
get_helm: https://get.helm.sh
goproxy:
golang: https://proxy.golang.org
npm:
npmjs: https://registry.npmjs.org
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,20 @@ func startServer(c *cli.Context) error {
c.Error(err)
}
stop := time.Now()
cacheStatus := res.Header().Get("X-Cache-Status")
if cacheStatus == "" {
cacheStatus = "UNKNOWN"
}
message := fmt.Sprintf(
"[%s] %s %s requested from %s with status %d in %s [%s]",
"[%s] %s %s requested from %s with status %d in %s [%s] cache=%s",
c.Request().Host,
req.Method,
req.RequestURI,
c.RealIP(),
res.Status,
stop.Sub(start).String(),
c.Path(),
cacheStatus,
)

logger := c.Get("logger").(*zap.SugaredLogger)
Expand Down Expand Up @@ -186,6 +191,11 @@ func startServer(c *cli.Context) error {
}).Name = fmt.Sprintf("goproxy::%s", k)
}

for k := range cfg.Server.NPM {
n := e.Group(fmt.Sprintf("/npm/%s", k))
n.GET("/*", handlers.NpmProxy(k)).Name = fmt.Sprintf("npm::%s", k)
}

for k, v := range cfg.Server.Galaxy {
g := e.Group(fmt.Sprintf("/galaxy/%s", k))
if v.URL != "" && v.Dir != "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/handlers/galaxy_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GalaxyLocalCollection(key string) echo.HandlerFunc {
collection.HighestVersion.Href = fmt.Sprintf("/api/v3/collections/%s/%s/versions/%s/", namespace, name, collectionLocal.Latest.Version)
collection.UpdatedAt = collectionLocal.Latest.Time.UTC()

c.Response().Header().Add("X-Cache-Status", "LOCAL")
return c.JSON(http.StatusOK, collection)
}
}
Expand Down Expand Up @@ -78,6 +79,7 @@ func GalaxyLocalCollectionVersions(key string) echo.HandlerFunc {
collectionVersions.Data = append(collectionVersions.Data, verInfo)
}

c.Response().Header().Add("X-Cache-Status", "LOCAL")
return c.JSON(http.StatusOK, collectionVersions)
}
}
Expand Down Expand Up @@ -177,6 +179,7 @@ func GalaxyLocalCollectionVersionInfo(key string) echo.HandlerFunc {
collectionVersionInfo.Metadata.Dependencies = manifest.CollectionInfo.Dependencies
collectionVersionInfo.Files = files

c.Response().Header().Add("X-Cache-Status", "LOCAL")
return c.JSON(http.StatusOK, collectionVersionInfo)
}(c)
}
Expand All @@ -201,6 +204,7 @@ func GalaxyLocalCollectionGet(key string) echo.HandlerFunc {
logger.Named(loggerNS).Debugf("Collection not found: %s/%s", namespace, name)
return c.String(http.StatusNotFound, "")
}
c.Response().Header().Add("X-Cache-Status", "LOCAL")
c.Response().Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-%s-%s.tar.gz\"", namespace, name, version))
return c.File(dest)
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/handlers/galaxy_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func GalaxyProxyCollection(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, fmt.Sprintf("%v", err))
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
logger.Named(loggerNS).Debugf("Remote %s saved as %s", url, dest)
}
var collection types.GalaxyCollection
Expand Down Expand Up @@ -69,11 +71,13 @@ func GalaxyProxyCollectionVersions(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, fmt.Sprintf("%v", err))
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
logger.Named(loggerNS).Debugf("Remote %s saved as %s", url, dest)
}
var collectionVersions types.GalaxyCollectionVersions
Expand Down Expand Up @@ -107,11 +111,13 @@ func GalaxyProxyCollectionVersionInfo(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(http.StatusNotFound, "")
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
logger.Named(loggerNS).Debugf("Remote %s saved as %s", url, dest)
}
var CollectionVersionInfo types.GalaxyCollectionVersionInfo
Expand Down Expand Up @@ -148,6 +154,7 @@ func GalaxyProxyCollectionGet(key string) echo.HandlerFunc {
_, err := misc.DownloadFile(url, versionFile, headers)
if err != nil {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(http.StatusBadRequest, "Downloading error")
}
logger.Named(loggerNS).Debugf("Remote %s saved as %s", url, versionFile)
Expand All @@ -167,6 +174,7 @@ func GalaxyProxyCollectionGet(key string) echo.HandlerFunc {
status, err := misc.DownloadFile(url, dest, headers)
if err != nil {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, fmt.Sprintf("%v", err))
}
c.Response().Header().Add("X-Cache-Status", "MISS")
Expand All @@ -183,6 +191,7 @@ func GalaxyProxyCollectionGet(key string) echo.HandlerFunc {
status, err := misc.DownloadFile(url, dest, headers)
if err != nil {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, fmt.Sprintf("%v", err))
}
logger.Named(loggerNS).Debugf("Downloaded %s", url)
Expand Down
11 changes: 8 additions & 3 deletions pkg/handlers/goproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func downloadAndCacheFile(c echo.Context, key, loggerNS, url, dest string) error
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, "410 Gone\n")
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
Expand Down Expand Up @@ -62,9 +63,10 @@ func GoProxyList(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, "410 Gone\n")
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
Expand Down Expand Up @@ -161,8 +163,10 @@ func GoProxyZip(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, "410 Gone\n")
}
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
Expand Down Expand Up @@ -205,9 +209,10 @@ func GoProxyLatest(key string) echo.HandlerFunc {
logger.Named(loggerNS).Errorf("[Downloading] %s", err)
if _, err = os.Stat(dest); errors.Is(err, os.ErrNotExist) {
logger.Named(loggerNS).Errorf("[FS]: %s", err)
c.Response().Header().Add("X-Cache-Status", "ERROR")
return c.String(status, "410 Gone\n")
}
c.Response().Header().Add("X-Cache-Status", "HIT")
c.Response().Header().Add("X-Cache-Status", "STALE")
logger.Named(loggerNS).Debugf("Remote %s served from local file %s", url, dest)
} else {
c.Response().Header().Add("X-Cache-Status", "MISS")
Expand Down
Loading