diff --git a/delamain/guard/main.go b/delamain/guard/main.go index 41446e1..5445dfd 100644 --- a/delamain/guard/main.go +++ b/delamain/guard/main.go @@ -86,6 +86,11 @@ func (g *apiGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) { g.readSlice(w, r) return } + case "/api/pool": + if r.Method == http.MethodGet { + g.poolMetadata(w, r) + return + } } if r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/api/trips/") { diff --git a/delamain/guard/slices.go b/delamain/guard/slices.go index cdc9ed9..2143e31 100644 --- a/delamain/guard/slices.go +++ b/delamain/guard/slices.go @@ -21,6 +21,7 @@ type exportedNote struct { } const noteLifetime = 2 * time.Minute +const poolSlots = 32 func (g *apiGuard) rememberExport(response *http.Response) error { if response.Request.URL.Path != "/api/packages" || response.StatusCode != http.StatusOK { @@ -44,7 +45,7 @@ func (g *apiGuard) rememberExport(response *http.Response) error { Data string `json:"data"` } tripID, _ := response.Request.Context().Value(exportSourceKey{}).(string) - if json.Unmarshal(body, &result) != nil || !manifestTripID.MatchString(tripID) || result.Slot == nil || + if json.Unmarshal(body, &result) != nil || !manifestTripID.MatchString(tripID) || result.Slot == nil || *result.Slot >= poolSlots || result.Generation == 0 || result.Size != len(result.Data) || result.Size > 16<<10 { return nil } @@ -86,7 +87,7 @@ func (g *apiGuard) readSlice(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusUnauthorized, "authentication required") return } - if request.Slot >= 32 { + if request.Slot >= poolSlots { writeJSON(w, http.StatusOK, map[string]any{"status": -1, "error": "invalid-slot"}) return } @@ -112,3 +113,57 @@ func (g *apiGuard) readSlice(w http.ResponseWriter, r *http.Request) { Data string `json:"data,omitempty"` }{Status: 0, Generation: request.Generation, Data: data}) } + +func (g *apiGuard) poolMetadata(w http.ResponseWriter, r *http.Request) { + target := *g.upstream + target.Path = "/api/trips" + request, err := http.NewRequestWithContext(r.Context(), http.MethodGet, target.String(), nil) + if err != nil { + writeError(w, http.StatusBadGateway, "service unavailable") + return + } + request.Header.Set("Authorization", r.Header.Get("Authorization")) + response, err := g.client.Do(request) + if err != nil { + writeError(w, http.StatusBadGateway, "service unavailable") + return + } + defer response.Body.Close() + if response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden { + writeError(w, response.StatusCode, "authentication required") + return + } + var owned struct { + Trips []struct { + ID string `json:"id"` + } `json:"trips"` + } + if response.StatusCode != http.StatusOK || json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&owned) != nil { + writeError(w, http.StatusBadGateway, "service unavailable") + return + } + tripIDs := make(map[string]bool, len(owned.Trips)) + for _, trip := range owned.Trips { + tripIDs[trip.ID] = true + } + type slotMetadata struct { + Slot int `json:"slot"` + Type string `json:"type"` + InUse bool `json:"in_use"` + } + pool := make([]slotMetadata, poolSlots) + for i := range pool { + pool[i] = slotMetadata{Slot: i, Type: "free"} + } + // Report only slots associated with the caller's recent exports. + g.notesMu.Lock() + for handle, note := range g.notes { + if tripIDs[note.tripID] && time.Since(note.created) < noteLifetime { + pool[handle.Slot].Type = "note" + } + } + g.notesMu.Unlock() + writeJSON(w, http.StatusOK, struct { + Pool []slotMetadata `json:"pool"` + }{Pool: pool}) +}