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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,7 @@ The proxy serves a web UI under `/ui`. No separate frontend build is needed -- t
- **Dashboard** (`/ui/`) -- cache stats, popular packages, recently cached artifacts, and vulnerability overview.
- **Analytics** (`/ui/analytics`) -- accumulated download size as a ring broken down by ecosystem with the total in the middle, the cache size, artifact, package and version counts, a per-ecosystem table, the vulnerability overview, and a Runtime card mirroring every counter `/metrics` exposes. See [Analytics](#analytics).
- **Install guide** (`/ui/install`) -- per-ecosystem configuration instructions, so you don't have to look them up here.
- **Upstreams** (`/ui/upstreams`) -- the upstreams each ecosystem's requests go to, as configured: the config key, the entry name for a map, and the proxy path clients use. Upstream URLs and whether credentials apply aren't shown, since `/ui` has no auth of its own.
- **Package browser** (`/ui/packages`) -- browse all cached packages with filtering by ecosystem and sorting by hits, size, name, or vulnerability count.
- **Search** (`/ui/search?q=...`) -- search cached packages by name.
- **Package detail** (`/ui/package/{ecosystem}/{name}`) -- metadata, license, vulnerabilities, and version list for a package. You can select two versions to compare.
Expand Down
20 changes: 14 additions & 6 deletions internal/handler/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ func NewArchHandler(proxy *Proxy, proxyURL string, repositories map[string]strin
h := &ArchHandler{
proxy: proxy,
proxyURL: strings.TrimSuffix(proxyURL, "/"),
repositories: make(map[string]string, len(repositories)),
repositories: ArchRepositories(repositories),
}
for name, repositoryURL := range repositories {
h.repositories[name] = strings.TrimSuffix(repositoryURL, "/")
return h
}

// ArchRepositories returns the upstream.arch repositories by name without
// trailing slashes, or the Arch Linux GeoIP mirror as "archlinux" when none
// are configured.
func ArchRepositories(configured map[string]string) map[string]string {
repositories := make(map[string]string, len(configured))
for name, repositoryURL := range configured {
repositories[name] = strings.TrimSuffix(repositoryURL, "/")
}
if len(h.repositories) == 0 {
h.repositories[defaultArchRepositoryName] = defaultArchUpstream
if len(repositories) == 0 {
repositories[defaultArchRepositoryName] = defaultArchUpstream
}
return h
return repositories
}

// Routes returns the HTTP handler for pacman requests.
Expand Down
20 changes: 14 additions & 6 deletions internal/handler/freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,23 @@ func NewFreeBSDHandler(proxy *Proxy, proxyURL string, repositories map[string]st
h := &FreeBSDHandler{
proxy: proxy,
proxyURL: strings.TrimSuffix(proxyURL, "/"),
repositories: make(map[string]string, len(repositories)),
repositories: FreeBSDRepositories(repositories),
}
for name, repositoryURL := range repositories {
h.repositories[name] = strings.TrimSuffix(repositoryURL, "/")
return h
}

// FreeBSDRepositories returns the upstream.freebsd repositories by name
// without trailing slashes, or the official FreeBSD mirror as "freebsd"
// when none are configured.
func FreeBSDRepositories(configured map[string]string) map[string]string {
repositories := make(map[string]string, len(configured))
for name, repositoryURL := range configured {
repositories[name] = strings.TrimSuffix(repositoryURL, "/")
}
if len(h.repositories) == 0 {
h.repositories[defaultFreeBSDRepositoryName] = defaultFreeBSDUpstream
if len(repositories) == 0 {
repositories[defaultFreeBSDRepositoryName] = defaultFreeBSDUpstream
}
return h
return repositories
}

// Routes returns the HTTP handler for pkg requests.
Expand Down
18 changes: 12 additions & 6 deletions internal/handler/go_sumdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,20 @@ func (p *goSumDBProbes) recordFailure(name string, start time.Time) {
// clients have no route to the database of their own.
func (h *GoHandler) SetSumDBs(sumDBs map[string]string, offline bool) {
h.sumDBOffline = offline
if len(sumDBs) == 0 {
h.sumDBs = map[string]string{defaultGoSumDBName: defaultGoSumDBURL}
return
h.sumDBs = GoSumDBs(sumDBs)
}

// GoSumDBs returns the upstream.go_sumdbs checksum databases by name without
// trailing slashes, or sum.golang.org when none are configured.
func GoSumDBs(configured map[string]string) map[string]string {
if len(configured) == 0 {
return map[string]string{defaultGoSumDBName: defaultGoSumDBURL}
}
h.sumDBs = make(map[string]string, len(sumDBs))
for name, sumDBURL := range sumDBs {
h.sumDBs[name] = strings.TrimSuffix(sumDBURL, "/")
sumDBs := make(map[string]string, len(configured))
for name, sumDBURL := range configured {
sumDBs[name] = strings.TrimSuffix(sumDBURL, "/")
}
return sumDBs
}

// handleSumDB serves checksum database requests that the go command sends
Expand Down
19 changes: 13 additions & 6 deletions internal/handler/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@ type TerraformHandler struct {
// registry hostnames to base URLs. With none, DefaultTerraformRegistries are
// mirrored.
func NewTerraformHandler(proxy *Proxy, registries map[string]string) *TerraformHandler {
if len(registries) == 0 {
registries = DefaultTerraformRegistries
return &TerraformHandler{proxy: proxy, registries: TerraformRegistries(registries)}
}

// TerraformRegistries is upstream.terraform as the mirror uses it:
// lowercased hostnames mapped to base URLs without a trailing slash, or
// DefaultTerraformRegistries when none are configured.
func TerraformRegistries(configured map[string]string) map[string]string {
if len(configured) == 0 {
configured = DefaultTerraformRegistries
}
h := &TerraformHandler{proxy: proxy, registries: make(map[string]string, len(registries))}
for hostname, baseURL := range registries {
h.registries[strings.ToLower(hostname)] = strings.TrimSuffix(baseURL, "/")
registries := make(map[string]string, len(configured))
for hostname, baseURL := range configured {
registries[strings.ToLower(hostname)] = strings.TrimSuffix(baseURL, "/")
}
return h
return registries
}

// WithGit has module downloads from a Git repository that git serves
Expand Down
10 changes: 10 additions & 0 deletions internal/handler/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,13 @@ func TestTerraformProviderResolvesDiscoveryOncePerRequest(t *testing.T) {
t.Errorf("discovery requests after an archive = %d, want 2", got)
}
}

func TestTerraformRegistries(t *testing.T) {
got := TerraformRegistries(map[string]string{"Registry.Example.ORG": "https://registry.example.org/"})
if len(got) != 1 || got["registry.example.org"] != "https://registry.example.org" {
t.Errorf("TerraformRegistries = %v", got)
}
if got := TerraformRegistries(nil); len(got) != len(DefaultTerraformRegistries) || got["registry.opentofu.org"] != "https://registry.opentofu.org" {
t.Errorf("TerraformRegistries(nil) = %v", got)
}
}
1 change: 1 addition & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ func (s *Server) serve(listener net.Listener) error {
ui.Get("/", s.handleRoot)
ui.Get("/analytics", s.handleAnalytics)
ui.Get("/install", s.handleInstall)
ui.Get("/upstreams", s.handleUpstreams)
ui.Get("/search", s.handleSearch)
ui.Get("/packages", s.handlePackagesList)
ui.Get("/package/{ecosystem}/*", s.handlePackagePath)
Expand Down
1 change: 1 addition & 0 deletions internal/server/templates/layout/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
{{if .Projects}}<a href="/ui/projects" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">Projects</a>{{end}}
<a href="/ui/analytics" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">Analytics</a>
<a href="/ui/install" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">Install</a>
<a href="/ui/upstreams" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">Upstreams</a>
<a href="/health" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">Health</a>
<a href="/stats" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 py-2 md:py-0">API</a>
{{end}}
24 changes: 24 additions & 0 deletions internal/server/templates/pages/upstreams.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{define "title"}}Upstreams - git-pkgs proxy{{end}}

{{define "content"}}
<div class="mb-8">
<h1 class="text-3xl font-bold mb-2">Upstreams</h1>
<p class="text-gray-600 dark:text-gray-400">The upstreams each ecosystem's requests go to, as configured, with the path clients reach them at. Upstream URLs aren't shown.</p>
</div>

<div class="space-y-6">
{{range .Ecosystems}}
<div class="bg-white dark:bg-gray-900 rounded-xl shadow-sm border border-gray-200 dark:border-gray-800">
<h2 class="px-6 py-3 font-semibold border-b border-gray-200 dark:border-gray-800">{{.Name}}</h2>
<div class="divide-y divide-gray-200 dark:divide-gray-800">
{{range .Upstreams}}
<div class="px-6 py-3 flex flex-col sm:flex-row sm:items-center justify-between gap-2">
<div class="min-w-0 text-sm font-mono break-all">{{.Path}}</div>
<div class="text-sm text-gray-500 dark:text-gray-400 font-mono shrink-0">{{.Setting}}{{if .Name}} · {{.Name}}{{end}}</div>
</div>
{{end}}
</div>
</div>
{{end}}
</div>
{{end}}
161 changes: 161 additions & 0 deletions internal/server/upstreams_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package server

import (
"net/http"
"net/url"
"sort"
"strings"

"github.com/git-pkgs/proxy/internal/config"
"github.com/git-pkgs/proxy/internal/handler"
)

// UpstreamsData is the upstreams page: the upstreams each ecosystem's
// requests go to, as configured.
type UpstreamsData struct {
Layout
Ecosystems []UpstreamEcosystem
}

// UpstreamEcosystem is one ecosystem on the upstreams page.
type UpstreamEcosystem struct {
Name string
Upstreams []UpstreamRow
}

// UpstreamRow is one configured upstream. It names where clients reach it
// and the config key that sets it, but not its URL or whether credentials
// apply: /ui has no auth of its own, so that would list internal hosts for
// anyone who can reach the proxy.
type UpstreamRow struct {
// Setting is the config key that sets the upstream, such as
// upstream.apk, and Name the entry's key in it for a map.
Setting, Name string
// Path is where clients reach the upstream through the proxy.
Path string
}

func (s *Server) handleUpstreams(w http.ResponseWriter, r *http.Request) {
data := UpstreamsData{
Layout: s.layoutFor(r),
Ecosystems: upstreamEcosystems(s.cfg.BaseURL, &s.cfg.Upstream),
}
if err := s.templates.Render(w, "upstreams", data); err != nil {
s.logger.Error("failed to render upstreams", "error", err)
}
}

// upstreamSetting is one config key on the upstreams page: a single URL
// or, with named set, a map of them. path is where it is served, with
// {name} for a map entry's key.
type upstreamSetting struct {
key string
path string
url string
named map[string]string
}

// upstreamEcosystems lists the upstreams u configures, per ecosystem.
// A map the proxy fills with a default when it is empty, such as
// upstream.apk, lists that default.
func upstreamEcosystems(baseURL string, u *config.UpstreamConfig) []UpstreamEcosystem {
git := make(map[string]string, len(u.Git))
for name, upstream := range u.Git {
git[name] = upstream.URL
}
single := func(key, path, url string) upstreamSetting { return upstreamSetting{key: key, path: path, url: url} }
named := func(key, path string, m map[string]string) upstreamSetting {
return upstreamSetting{key: key, path: path, named: m}
}
groups := []struct {
name string
settings []upstreamSetting
}{
{"npm", []upstreamSetting{single("upstream.npm", "/npm/", u.NPM), named("upstream.npm_routes", "/npm/", u.NPMRoutes)}},
{"Cargo", []upstreamSetting{single("upstream.cargo", "/cargo/", u.Cargo), single("upstream.cargo_download", "/cargo/", u.CargoDownload)}},
{"RubyGems", []upstreamSetting{single("upstream.gem", "/gem/", u.Gem)}},
{"Go", []upstreamSetting{
single("upstream.go", "/go/", u.Go),
named("upstream.go_sumdbs", "/go/sumdb/{name}/", handler.GoSumDBs(u.GoSumDBs)),
single("upstream.go_vulndb", "/go/vulndb/", u.GoVulnDB),
}},
{"Hex", []upstreamSetting{single("upstream.hex", "/hex/", u.Hex), single("upstream.hex_api", "/hex/", u.HexAPI)}},
{"pub.dev", []upstreamSetting{single("upstream.pub", "/pub/", u.Pub)}},
{"PyPI", []upstreamSetting{single("upstream.pypi", "/pypi/", u.PyPI), single("upstream.pypi_download", "/pypi/", u.PyPIDownload)}},
{"Maven", []upstreamSetting{
single("upstream.maven", "/maven/", u.Maven),
named("upstream.maven_repositories", "/maven/{name}/", u.MavenRepositories),
single("upstream.gradle_plugin_portal", "/maven/", u.GradlePluginPortal),
}},
{"NuGet", []upstreamSetting{single("upstream.nuget", "/nuget/", u.NuGet), single("upstream.nuget_search", "/nuget/", u.NuGetSearch)}},
{"Composer", []upstreamSetting{
single("upstream.composer", "/composer/", u.Composer),
single("upstream.composer_repository", "/composer/", u.ComposerRepository),
named("upstream.composer_routes", "/composer/", u.ComposerRoutes),
}},
{"Conan", []upstreamSetting{single("upstream.conan", "/conan/", u.Conan)}},
{"Chef", []upstreamSetting{single("upstream.chef", "/chef/", u.Chef)}},
{"Conda", []upstreamSetting{single("upstream.conda", "/conda/", u.Conda)}},
{"CRAN", []upstreamSetting{single("upstream.cran", "/cran/", u.CRAN)}},
{"Julia", []upstreamSetting{single("upstream.julia", "/julia/", u.Julia)}},
{"Swift", []upstreamSetting{single("upstream.swift", "/swift/", u.Swift)}},
{"Homebrew", []upstreamSetting{
single("upstream.homebrew_api", "/homebrew/", u.HomebrewAPI),
// Bottles are served by the container handler (see
// handler.RegisterHomebrewArtifacts).
single("upstream.homebrew_artifact", "/v2/homebrew/core/", u.HomebrewArtifact),
}},
{"OCI", []upstreamSetting{single("upstream.oci_default", "/v2/", u.OCIDefault), named("upstream.oci", "/v2/upstream/{name}/", u.OCI)}},
{"Helm", []upstreamSetting{named("upstream.helm", "/helm/{name}/", u.Helm)}},
{"Alpine", []upstreamSetting{named("upstream.apk", "/apk/{name}/", handler.APKRepositories(u.APK))}},
{"Arch Linux", []upstreamSetting{named("upstream.arch", "/arch/{name}/", handler.ArchRepositories(u.Arch))}},
{"FreeBSD", []upstreamSetting{named("upstream.freebsd", "/freebsd/{name}/", handler.FreeBSDRepositories(u.FreeBSD))}},
{"Terraform", []upstreamSetting{named("upstream.terraform", "/terraform/providers/{name}/", handler.TerraformRegistries(u.Terraform))}},
{"Debian", []upstreamSetting{single("upstream.debian", "/debian/", u.Debian), named("upstream.debian_repositories", "/debian/{name}/", u.DebianRepositories)}},
{"RPM", []upstreamSetting{single("upstream.rpm", "/rpm/", u.RPM)}},
{"Generic", []upstreamSetting{named("upstream.generic", "/generic/{name}/", u.Generic)}},
{"Git", []upstreamSetting{named("upstream.git", "/git/{name}/", git)}},
}
base := strings.TrimSuffix(baseURL, "/")
ecosystems := make([]UpstreamEcosystem, 0, len(groups))
for _, group := range groups {
var rows []UpstreamRow
for _, setting := range group.settings {
rows = append(rows, setting.rows(base)...)
}
if len(rows) > 0 {
ecosystems = append(ecosystems, UpstreamEcosystem{Name: group.name, Upstreams: rows})
}
}
return ecosystems
}

// rows lists the setting's upstreams, map entries sorted by name, leaving
// out empty ones.
func (s upstreamSetting) rows(base string) []UpstreamRow {
row := func(name string) UpstreamRow {
return UpstreamRow{
Setting: s.key,
Name: name,
// Names are configured unescaped; clients send reserved
// characters in them path-escaped.
Path: base + strings.ReplaceAll(s.path, "{name}", url.PathEscape(name)),
}
}
if s.named == nil {
if s.url == "" {
return nil
}
return []UpstreamRow{row("")}
}
names := make([]string, 0, len(s.named))
for name := range s.named {
names = append(names, name)
}
sort.Strings(names)
rows := make([]UpstreamRow, 0, len(names))
for _, name := range names {
rows = append(rows, row(name))
}
return rows
}
Loading
Loading