From 4e31affcb4b42d3f79b75b07014baacae87ceaea Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Sat, 16 Nov 2024 00:01:08 +0000 Subject: [PATCH] Allow configurable suffixes for handle services for TCP connection reuse --- atproto/identity/base_directory.go | 5 +++++ atproto/identity/handle.go | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/atproto/identity/base_directory.go b/atproto/identity/base_directory.go index 308d19b64..19b0a6300 100644 --- a/atproto/identity/base_directory.go +++ b/atproto/identity/base_directory.go @@ -29,8 +29,13 @@ type BaseDirectory struct { SkipDNSDomainSuffixes []string // set of fallback DNS servers (eg, domain registrars) to try as a fallback. each entry should be "ip:port", eg "8.8.8.8:53" FallbackDNSServers []string + // Map of well-known suffixes to call the same host for handle resolution, adds a host header for virtual routing + WellKnownSuffixMap WellKnownSuffixMap } +// WellKnownSuffixMap is a map of well-known suffixes to call the same host for handle resolution, adds a host header for virtual routing +type WellKnownSuffixMap map[string]string + var _ Directory = (*BaseDirectory)(nil) func (d *BaseDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) { diff --git a/atproto/identity/handle.go b/atproto/identity/handle.go index 77a157f85..3365618b3 100644 --- a/atproto/identity/handle.go +++ b/atproto/identity/handle.go @@ -127,11 +127,24 @@ func (d *BaseDirectory) ResolveHandleDNSFallback(ctx context.Context, handle syn } func (d *BaseDirectory) ResolveHandleWellKnown(ctx context.Context, handle syntax.Handle) (syntax.DID, error) { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/.well-known/atproto-did", handle), nil) + // Check for well-known suffixes to route to a specific host + // allowing for reuse of TCP connections for high-traffic providers + requestHost := handle.String() + for suffix, host := range d.WellKnownSuffixMap { + if strings.HasSuffix(handle.String(), suffix) { + requestHost = host + break + } + } + + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/.well-known/atproto-did", requestHost), nil) if err != nil { return "", fmt.Errorf("constructing HTTP request for handle resolution: %w", err) } + // Add host header for virtual routing + req.Header.Set("Host", handle.String()) + resp, err := d.HTTPClient.Do(req) if err != nil { // check for NXDOMAIN