Skip to content

Commit

Permalink
Allow configurable suffixes for handle services for TCP connection reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvolp12 committed Nov 16, 2024
1 parent 0fc4c7e commit 4e31aff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions atproto/identity/base_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion atproto/identity/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e31aff

Please sign in to comment.