Skip to content

Commit de08310

Browse files
committed
feat(gateway): redirect ipns b58mh peer id to cid
1 parent 999d939 commit de08310

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

gateway/gateway_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,30 @@ func TestGoGetSupport(t *testing.T) {
546546
assert.Nil(t, err)
547547
assert.Equal(t, http.StatusOK, res.StatusCode)
548548
}
549+
550+
func TestIpnsBase58MultihashRedirect(t *testing.T) {
551+
ts, _, _ := newTestServerAndNode(t, nil)
552+
t.Logf("test server url: %s", ts.URL)
553+
554+
t.Run("ED25519 Base58-encoded key", func(t *testing.T) {
555+
t.Parallel()
556+
557+
req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/12D3KooWRBy97UB99e3J6hiPesre1MZeuNQvfan4gBziswrRJsNK?keep=query", nil)
558+
assert.Nil(t, err)
559+
560+
res, err := doWithoutRedirect(req)
561+
assert.Nil(t, err)
562+
assert.Equal(t, "/ipns/k51qzi5uqu5dlvj2baxnqndepeb86cbk3ng7n3i46uzyxzyqj2xjonzllnv0v8?keep=query", res.Header.Get("Location"))
563+
})
564+
565+
t.Run("RSA Base58-encoded key", func(t *testing.T) {
566+
t.Parallel()
567+
568+
req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/QmcJM7PRfkSbcM5cf1QugM5R37TLRKyJGgBEhXjLTB8uA2?keep=query", nil)
569+
assert.Nil(t, err)
570+
571+
res, err := doWithoutRedirect(req)
572+
assert.Nil(t, err)
573+
assert.Equal(t, "/ipns/k2k4r8ol4m8kkcqz509c1rcjwunebj02gcnm5excpx842u736nja8ger?keep=query", res.Header.Get("Location"))
574+
})
575+
}

gateway/handler.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
ipath "github.com/ipfs/boxo/coreiface/path"
2020
cid "github.com/ipfs/go-cid"
2121
logging "github.com/ipfs/go-log"
22+
"github.com/libp2p/go-libp2p/core/peer"
23+
"github.com/multiformats/go-multibase"
2224
prometheus "github.com/prometheus/client_golang/prometheus"
2325
"go.opentelemetry.io/otel/attribute"
2426
"go.opentelemetry.io/otel/trace"
@@ -204,6 +206,10 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
204206
return
205207
}
206208

209+
if handleIpnsB58mhToCidRedirection(w, r) {
210+
return
211+
}
212+
207213
contentPath := ipath.New(r.URL.Path)
208214
ctx := context.WithValue(r.Context(), ContentPathKey, contentPath)
209215
r = r.WithContext(ctx)
@@ -728,6 +734,52 @@ func handleServiceWorkerRegistration(r *http.Request) (err *ErrorResponse) {
728734
return nil
729735
}
730736

737+
// handleIpnsB58mhToCidRedirection redirects from /ipns/b58mh to /ipns/cid in
738+
// the most cost-effective way.
739+
func handleIpnsB58mhToCidRedirection(w http.ResponseWriter, r *http.Request) bool {
740+
fmt.Println(w.Header().Get("Location"))
741+
if w.Header().Get("Location") != "" {
742+
// Ignore this if there is already a redirection in place. This happens
743+
// if there is a subdomain redirection. In that case, the path is already
744+
// converted to CIDv1.
745+
return false
746+
}
747+
748+
pathParts := strings.Split(r.URL.Path, "/")
749+
fmt.Println(pathParts)
750+
if len(pathParts) < 3 {
751+
return false
752+
}
753+
754+
if pathParts[1] != "ipns" {
755+
return false
756+
}
757+
758+
id, err := peer.Decode(pathParts[2])
759+
if err != nil {
760+
return false
761+
}
762+
763+
// Convert the peer ID to a CIDv1.
764+
cid := peer.ToCid(id)
765+
766+
// Encode CID in base36 to match the subdomain URLs.
767+
encodedCID, err := cid.StringOfBase(multibase.Base36)
768+
if err != nil {
769+
return false
770+
}
771+
772+
// If the CID was already encoded, do not redirect.
773+
if encodedCID == pathParts[2] {
774+
return false
775+
}
776+
777+
pathParts[2] = encodedCID
778+
r.URL.Path = strings.Join(pathParts, "/")
779+
http.Redirect(w, r, r.URL.String(), http.StatusFound)
780+
return true
781+
}
782+
731783
// Attempt to fix redundant /ipfs/ namespace as long as resulting
732784
// 'intended' path is valid. This is in case gremlins were tickled
733785
// wrong way and user ended up at /ipfs/ipfs/{cid} or /ipfs/ipns/{id}

0 commit comments

Comments
 (0)