mirror of
https://github.com/git-pkgs/proxy.git
synced 2026-11-09 15:49:16 -05:00
proxy mirror and mirror API jobs resolved download URLs with a bare resolver, so npm, cargo, gem, golang, hex, pub, maven and nuget packages came from the public registries whatever upstream.* says. The resolver now takes the configured upstreams as base URLs. npm packages in upstream.npm_routes are fetched from their route and stored under the route's filename prefix, the way the npm handler does, so a public package can't stand in for a routed one and clients are served the mirrored tarball. Refs #414
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/git-pkgs/proxy/internal/config"
|
|
"github.com/git-pkgs/proxy/internal/handler"
|
|
"github.com/git-pkgs/registries/fetch"
|
|
)
|
|
|
|
// newMirrorResolver builds the resolver proxy mirror and mirror jobs find
|
|
// download URLs with, using the configured upstreams. Handlers serving
|
|
// clients don't use it; they take the URLs from upstream metadata.
|
|
func newMirrorResolver(u config.UpstreamConfig) *fetch.Resolver {
|
|
return fetch.NewResolver(fetch.WithBaseURLs(map[string]string{
|
|
"npm": u.NPM,
|
|
"cargo": u.CargoDownload,
|
|
"gem": u.Gem,
|
|
"golang": u.Go,
|
|
"hex": u.Hex,
|
|
"pub": u.Pub,
|
|
"maven": u.Maven,
|
|
"nuget": u.NuGet,
|
|
}))
|
|
}
|
|
|
|
// configureMirrorResolvers points proxy's resolver at the configured
|
|
// upstreams, and npm packages in upstream.npm_routes at their route, so a
|
|
// public package can't stand in for a routed one.
|
|
func configureMirrorResolvers(proxy *handler.Proxy, u config.UpstreamConfig) {
|
|
proxy.Resolver = newMirrorResolver(u)
|
|
if len(u.NPMRoutes) == 0 {
|
|
return
|
|
}
|
|
routed := make(map[string]*handler.ArtifactRoute, len(u.NPMRoutes))
|
|
for _, routeURL := range u.NPMRoutes {
|
|
routeUpstream := u
|
|
routeUpstream.NPM = routeURL
|
|
routed[routeURL] = &handler.ArtifactRoute{
|
|
Resolver: newMirrorResolver(routeUpstream),
|
|
FilenamePrefix: handler.RoutedFilenamePrefix(routeURL),
|
|
}
|
|
}
|
|
proxy.RouteFor = func(ecosystem, name string) *handler.ArtifactRoute {
|
|
if ecosystem != "npm" {
|
|
return nil
|
|
}
|
|
routeURL, ok := handler.RoutedPackageURL(u.NPMRoutes, name)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return routed[routeURL]
|
|
}
|
|
}
|