Skip to content

Commit 4248145

Browse files
committed
fix: account buggy apt responses properly, assume curl variant is fine
1 parent fb62bf0 commit 4248145

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

main.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,6 @@ func (r *githubRedirector) Serve(ctx context.Context) error {
161161
}
162162

163163
func (r *githubRedirector) ServeHTTP(w http.ResponseWriter, req *http.Request) {
164-
if r.buggyAPTVersion(req) {
165-
slog.Info("serving proxied for buggy APT", "ua", req.Header.Get("User-Agent"))
166-
r.next.ServeHTTP(w, req)
167-
return
168-
}
169-
170164
file := path.Base(req.URL.Path)
171165
if unesc, err := url.PathUnescape(file); err == nil {
172166
file = unesc
@@ -180,15 +174,22 @@ func (r *githubRedirector) ServeHTTP(w http.ResponseWriter, req *http.Request) {
180174
}
181175
r.mut.Unlock()
182176

183-
if ok {
184-
slog.Info("serving redirect", "file", file, "ua", req.Header.Get("User-Agent"))
185-
http.Redirect(w, req, url, http.StatusTemporaryRedirect)
186-
metricFileRequests.WithLabelValues("redirect").Inc()
177+
if !ok {
178+
r.next.ServeHTTP(w, req)
179+
metricFileRequests.WithLabelValues("proxy").Inc()
180+
return
181+
}
182+
183+
if r.buggyAPTVersion(req) {
184+
slog.Info("serving proxied for buggy APT", "ua", req.Header.Get("User-Agent"))
185+
r.next.ServeHTTP(w, req)
186+
metricFileRequests.WithLabelValues("proxy-buggy-apt").Inc()
187187
return
188188
}
189189

190-
r.next.ServeHTTP(w, req)
191-
metricFileRequests.WithLabelValues("proxy").Inc()
190+
slog.Info("serving redirect", "file", file, "ua", req.Header.Get("User-Agent"))
191+
http.Redirect(w, req, url, http.StatusTemporaryRedirect)
192+
metricFileRequests.WithLabelValues("redirect").Inc()
192193
}
193194

194195
func (r *githubRedirector) fetchGithubReleaseAssets(ctx context.Context, url string) (map[string]string, error) {
@@ -220,15 +221,16 @@ func (r *githubRedirector) fetchGithubReleaseAssets(ctx context.Context, url str
220221
// buggyAPTVersion returns true for APT versions that can't properly handle
221222
// a redirect to a signed object storage URL.
222223
func (r *githubRedirector) buggyAPTVersion(req *http.Request) bool {
224+
// "Debian APT-CURL/1.0 (1.2.35)"
223225
// "Debian APT-HTTP/1.3 (1.6.18)"
226+
// "Debian APT-HTTP/1.3 (2.0.11) non-interactive"
224227
// "Debian APT-HTTP/1.3 (2.2.4)"
225228
// "Debian APT-HTTP/1.3 (2.4.13)"
226-
// "Debian APT-HTTP/1.3 (2.7.14)"
227229
fields := strings.Fields(req.Header.Get("User-Agent"))
228230
if len(fields) < 3 {
229231
return false
230232
}
231-
if fields[0] != "Debian" || !strings.HasPrefix(fields[1], "APT") {
233+
if fields[0] != "Debian" || !strings.HasPrefix(fields[1], "APT-HTTP") {
232234
return false
233235
}
234236
parts := strings.Split(strings.Trim(fields[2], "()"), ".")

0 commit comments

Comments
 (0)