|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "path" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/prometheus/client_golang/prometheus" |
| 16 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + metricGithubRedirects = promauto.NewCounter(prometheus.CounterOpts{ |
| 21 | + Name: "github_proxy_redirects_total", |
| 22 | + }) |
| 23 | + metricGithubRedirectBytes = promauto.NewCounter(prometheus.CounterOpts{ |
| 24 | + Name: "github_proxy_redirect_bytes_total", |
| 25 | + }) |
| 26 | + metricGithubRedirectAssets = promauto.NewGauge(prometheus.GaugeOpts{ |
| 27 | + Name: "github_proxy_redirect_assets_loaded", |
| 28 | + }) |
| 29 | +) |
| 30 | + |
| 31 | +type githubRedirector struct { |
| 32 | + releasesURLs []string |
| 33 | + refreshInterval time.Duration |
| 34 | + next http.Handler |
| 35 | + |
| 36 | + mut sync.Mutex |
| 37 | + assets map[string]asset |
| 38 | +} |
| 39 | + |
| 40 | +type asset struct { |
| 41 | + Name string `json:"name"` |
| 42 | + BrowserURL string `json:"browser_download_url"` |
| 43 | + Size int |
| 44 | +} |
| 45 | + |
| 46 | +type release struct { |
| 47 | + Tag string `json:"tag_name"` |
| 48 | + Assets []asset `json:"assets"` |
| 49 | +} |
| 50 | + |
| 51 | +func (r *githubRedirector) Serve(ctx context.Context) error { |
| 52 | + slog.Info("starting GitHub redirector") |
| 53 | + defer slog.Info("stopping GitHub redirector") |
| 54 | + |
| 55 | + timer := time.NewTimer(0) |
| 56 | + defer timer.Stop() |
| 57 | + for { |
| 58 | + select { |
| 59 | + case <-timer.C: |
| 60 | + newAssets := make(map[string]asset) |
| 61 | + nonUnique := make(map[string]struct{}) |
| 62 | + for _, url := range r.releasesURLs { |
| 63 | + assets, err := r.fetchGithubReleaseAssets(ctx, url) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + for key, asset := range assets { |
| 68 | + if _, ok := nonUnique[key]; ok { |
| 69 | + continue |
| 70 | + } |
| 71 | + if _, ok := newAssets[key]; ok { |
| 72 | + nonUnique[key] = struct{}{} |
| 73 | + delete(newAssets, key) |
| 74 | + slog.Info("skipping non-unique asset", "key", key) |
| 75 | + continue |
| 76 | + } |
| 77 | + newAssets[key] = asset |
| 78 | + } |
| 79 | + } |
| 80 | + r.mut.Lock() |
| 81 | + r.assets = newAssets |
| 82 | + r.mut.Unlock() |
| 83 | + metricGithubRedirectAssets.Set(float64(len(newAssets))) |
| 84 | + timer.Reset(r.refreshInterval) |
| 85 | + case <-ctx.Done(): |
| 86 | + return ctx.Err() |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (r *githubRedirector) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 92 | + file := path.Base(req.URL.Path) |
| 93 | + if unesc, err := url.PathUnescape(file); err == nil { |
| 94 | + file = unesc |
| 95 | + } |
| 96 | + |
| 97 | + r.mut.Lock() |
| 98 | + asset, ok := r.assets[file] |
| 99 | + // Special case; tildes become dots in GitHub assets... |
| 100 | + if !ok { |
| 101 | + asset, ok = r.assets[strings.Replace(file, "~", ".", 1)] |
| 102 | + } |
| 103 | + r.mut.Unlock() |
| 104 | + |
| 105 | + if !ok { |
| 106 | + r.next.ServeHTTP(w, req) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + if r.buggyAPTVersion(req) { |
| 111 | + slog.Info("serving proxied for buggy APT", "file", file, "ua", req.Header.Get("User-Agent")) |
| 112 | + r.next.ServeHTTP(w, req) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + slog.Info("serving redirect", "file", file, "ua", req.Header.Get("User-Agent")) |
| 117 | + http.Redirect(w, req, asset.BrowserURL, http.StatusTemporaryRedirect) |
| 118 | + metricGithubRedirects.Inc() |
| 119 | + metricGithubRedirectBytes.Add(float64(asset.Size)) |
| 120 | +} |
| 121 | + |
| 122 | +func (r *githubRedirector) fetchGithubReleaseAssets(ctx context.Context, url string) (map[string]asset, error) { |
| 123 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + resp, err := http.DefaultClient.Do(req) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + defer resp.Body.Close() |
| 132 | + var rels []release |
| 133 | + if err := json.NewDecoder(resp.Body).Decode(&rels); err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + assets := make(map[string]asset) |
| 138 | + for _, rel := range rels { |
| 139 | + for _, asset := range rel.Assets { |
| 140 | + assets[asset.Name] = asset |
| 141 | + } |
| 142 | + } |
| 143 | + return assets, nil |
| 144 | +} |
| 145 | + |
| 146 | +// buggyAPTVersion returns true for APT versions that can't properly handle |
| 147 | +// a redirect to a signed object storage URL. |
| 148 | +func (r *githubRedirector) buggyAPTVersion(req *http.Request) bool { |
| 149 | + // "Debian APT-CURL/1.0 (1.2.35)" |
| 150 | + // "Debian APT-HTTP/1.3 (1.6.18)" |
| 151 | + // "Debian APT-HTTP/1.3 (2.0.11) non-interactive" |
| 152 | + // "Debian APT-HTTP/1.3 (2.2.4)" |
| 153 | + // "Debian APT-HTTP/1.3 (2.4.13)" |
| 154 | + fields := strings.Fields(req.Header.Get("User-Agent")) |
| 155 | + if len(fields) < 3 { |
| 156 | + return false |
| 157 | + } |
| 158 | + if fields[0] != "Debian" || !strings.HasPrefix(fields[1], "APT-HTTP") { |
| 159 | + return false |
| 160 | + } |
| 161 | + parts := strings.Split(strings.Trim(fields[2], "()"), ".") |
| 162 | + if len(parts) < 2 { |
| 163 | + return false |
| 164 | + } |
| 165 | + |
| 166 | + // Major versions lower than 2 are guaranteed buggy, higher should be |
| 167 | + // fine. Precisely equals two requires further investigation. |
| 168 | + if maj, err := strconv.ParseInt(parts[0], 10, 32); err != nil { |
| 169 | + return false |
| 170 | + } else if maj < 2 { |
| 171 | + return true |
| 172 | + } else if maj > 2 { |
| 173 | + return false |
| 174 | + } |
| 175 | + |
| 176 | + // Minor versions lower than 2 are buggy. |
| 177 | + if min, err := strconv.ParseInt(parts[1], 10, 32); err != nil { |
| 178 | + return false |
| 179 | + } else if min < 2 { |
| 180 | + return true |
| 181 | + } |
| 182 | + |
| 183 | + return false |
| 184 | +} |
0 commit comments