fix: randomize competitor preview gateway

This commit is contained in:
2026-09-21 10:48:53 +08:00
parent ebe8bc0175
commit 184cab3677
2 changed files with 28 additions and 13 deletions
+10 -13
View File
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
@@ -1630,6 +1631,10 @@ func waitForAnonymousCleanup(ctx context.Context, delay time.Duration) error {
}
}
func randomGateway(gateways []hub.Gateway) hub.Gateway {
return gateways[rand.Intn(len(gateways))]
}
func newAnonymousBrowser(ctx context.Context, store *hub.Store) (anonymousBrowserLease, error) {
if store == nil {
return anonymousBrowserLease{}, creator.ErrUnavailable
@@ -1646,23 +1651,15 @@ func newAnonymousBrowser(ctx context.Context, store *hub.Store) (anonymousBrowse
return anonymousBrowserLease{}, fmt.Errorf("%w: anonymous browser gateway is not configured", creator.ErrUnavailable)
}
gatewayByName := make(map[string]hub.Gateway, len(gateways))
for _, gateway := range gateways {
gatewayByName[gateway.Name] = gateway
}
gateway := randomGateway(gateways)
var template hub.Env
var gateway hub.Gateway
for _, candidate := range envs {
candidateGateway, ok := gatewayByName[candidate.Gateway]
if !ok {
continue
if candidate.Gateway == gateway.Name {
template = candidate
break
}
template, gateway = candidate, candidateGateway
break
}
if gateway.Name == "" {
gateway = gateways[0]
if template.Gateway == "" {
template = hub.Env{Fingerprint: hub.Fingerprint{Seed: 1}}
}
@@ -42,6 +42,24 @@ func TestCompetitorSharePlatform(t *testing.T) {
}
}
func TestRandomGatewaySelectsFromRegisteredGateways(t *testing.T) {
gateways := []hub.Gateway{
{Name: "gateway-a"},
{Name: "gateway-b"},
{Name: "gateway-c"},
}
allowed := map[string]bool{
"gateway-a": true,
"gateway-b": true,
"gateway-c": true,
}
for range 32 {
if gateway := randomGateway(gateways); !allowed[gateway.Name] {
t.Fatalf("randomGateway selected unregistered gateway %q", gateway.Name)
}
}
}
func TestAnonymousBrowserLeasePurgesRuntimeAndProfile(t *testing.T) {
var requestBody map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {