From 3e74b3dee2a8f9310cc57824bb93e7ce354731b6 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 10 Mar 2025 09:55:07 +0100 Subject: [PATCH] chore(strelaypoolsrv): limit number of returned relays Avoid unnecessarily enormous responses by returning a random subset of relays. --- cmd/infra/strelaypoolsrv/main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/infra/strelaypoolsrv/main.go b/cmd/infra/strelaypoolsrv/main.go index 77770cae4..a2e5f65ef 100644 --- a/cmd/infra/strelaypoolsrv/main.go +++ b/cmd/infra/strelaypoolsrv/main.go @@ -29,6 +29,7 @@ import ( _ "github.com/syncthing/syncthing/lib/automaxprocs" "github.com/syncthing/syncthing/lib/geoip" "github.com/syncthing/syncthing/lib/protocol" + "github.com/syncthing/syncthing/lib/rand" "github.com/syncthing/syncthing/lib/relay/client" "github.com/syncthing/syncthing/lib/sync" "github.com/syncthing/syncthing/lib/tlsutil" @@ -110,6 +111,7 @@ var ( requestProcessors = 8 geoipLicenseKey = os.Getenv("GEOIP_LICENSE_KEY") geoipAccountID, _ = strconv.Atoi(os.Getenv("GEOIP_ACCOUNT_ID")) + maxRelaysReturned = 100 requests chan request @@ -141,6 +143,7 @@ func main() { flag.IntVar(&requestQueueLen, "request-queue", requestQueueLen, "Queue length for incoming test requests") flag.IntVar(&requestProcessors, "request-processors", requestProcessors, "Number of request processor routines") flag.StringVar(&geoipLicenseKey, "geoip-license-key", geoipLicenseKey, "License key for GeoIP database") + flag.IntVar(&maxRelaysReturned, "max-relays-returned", maxRelaysReturned, "Maximum number of relays returned for a normal endpoint query") flag.Parse() @@ -331,6 +334,10 @@ func handleEndpointShort(rw http.ResponseWriter, r *http.Request) { relays = append(relays, relayShort{URL: slimURL(r.URL)}) } mut.RUnlock() + if len(relays) > maxRelaysReturned { + rand.Shuffle(relays) + relays = relays[:maxRelaysReturned] + } _ = json.NewEncoder(rw).Encode(map[string][]relayShort{ "relays": relays,