Skip to content

Commit 6566d9e

Browse files
committed
Handle redirects better
By using a token for a host if we have one
1 parent 50044c3 commit 6566d9e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

p2p/http/auth/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func (a *ClientPeerIDAuth) AuthenticateWithRoundTripper(rt http.RoundTripper, re
8686
return serverPeerID, resp, nil
8787
}
8888

89+
func (a *ClientPeerIDAuth) HasToken(hostname string) bool {
90+
_, hasToken := a.tm.get(hostname, a.TokenTTL)
91+
return hasToken
92+
}
93+
8994
func (a *ClientPeerIDAuth) runHandshake(rt http.RoundTripper, req *http.Request, b bodyMeta, hs *handshake.PeerIDAuthHandshakeClient) (peer.ID, *http.Response, error) {
9095
maxSteps := 5 // Avoid infinite loops in case of buggy handshake. Shouldn't happen.
9196
var resp *http.Response

p2p/http/libp2phttp.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,18 @@ func (h *Host) RoundTrip(r *http.Request) (*http.Response, error) {
749749
switch r.URL.Scheme {
750750
case "http", "https":
751751
h.initDefaultRT()
752+
if r.Host == "" {
753+
r.Host = r.URL.Host
754+
}
755+
if h.ClientPeerIDAuth != nil && h.ClientPeerIDAuth.HasToken(r.Host) {
756+
serverID, resp, err := h.ClientPeerIDAuth.AuthenticateWithRoundTripper(h.DefaultClientRoundTripper, r)
757+
if err != nil {
758+
return nil, err
759+
}
760+
ctxWithServerID := context.WithValue(r.Context(), serverPeerIDContextKey{}, serverID)
761+
resp.Request = resp.Request.WithContext(ctxWithServerID)
762+
return resp, nil
763+
}
752764
return h.DefaultClientRoundTripper.RoundTrip(r)
753765
case "multiaddr":
754766
break
@@ -780,7 +792,12 @@ func (h *Host) RoundTrip(r *http.Request) (*http.Response, error) {
780792

781793
h.initDefaultRT()
782794
rt := h.DefaultClientRoundTripper
783-
if parsed.sni != parsed.host {
795+
sni := parsed.sni
796+
if sni == "" {
797+
sni = parsed.host
798+
}
799+
800+
if sni != parsed.host {
784801
// We have a different host and SNI (e.g. using an IP address but specifying a SNI)
785802
// We need to make our own transport to support this.
786803
//
@@ -803,6 +820,8 @@ func (h *Host) RoundTrip(r *http.Request) (*http.Response, error) {
803820
}
804821

805822
serverID, resp, err := h.ClientPeerIDAuth.AuthenticateWithRoundTripper(rt, r)
823+
// c := http.Client{Transport: rt}
824+
// serverID, resp, err := h.ClientPeerIDAuth.AuthenticatedDo(&c, r)
806825
if err != nil {
807826
return nil, err
808827
}
@@ -1165,6 +1184,9 @@ func connectionCloseHeaderMiddleware(next http.Handler) http.Handler {
11651184
// maybeDecorateContextWithAuth decorates the request context with
11661185
// authentication information if serverAuth is provided.
11671186
func maybeDecorateContextWithAuthMiddleware(serverAuth *httpauth.ServerPeerIDAuth, next http.Handler) http.Handler {
1187+
if next == nil {
1188+
return nil
1189+
}
11681190
if serverAuth == nil {
11691191
return next
11701192
}

0 commit comments

Comments
 (0)