-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package finderhttpclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/ipni/go-indexer-core/store/dhash" | ||
"github.com/ipni/storetheindex/api/v0/finder/model" | ||
"github.com/ipni/storetheindex/api/v0/httpclient" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
b58 "github.com/mr-tron/base58/base58" | ||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
const ( | ||
metadataPath = "/metadata" | ||
) | ||
|
||
type DHashClient struct { | ||
Client | ||
|
||
metadataUrl string | ||
} | ||
|
||
func NewDHashClient(baseURL string, options ...httpclient.Option) (*DHashClient, error) { | ||
c, err := New(baseURL, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DHashClient{ | ||
Client: *c, | ||
metadataUrl: baseURL + metadataPath, | ||
}, nil | ||
} | ||
|
||
func (c *DHashClient) Find(ctx context.Context, mh multihash.Multihash) (*model.FindResponse, error) { | ||
// query value keys from indexer | ||
smh, err := dhash.SecondMultihash(mh) | ||
if err != nil { | ||
return nil, err | ||
} | ||
u := fmt.Sprint(c.finderURL, "/", smh.B58String()) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Accept", "application/json") | ||
|
||
resp, err := c.c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, httpclient.ReadError(resp.StatusCode, body) | ||
} | ||
|
||
findResponse := &model.FindResponse{} | ||
err = json.Unmarshal(body, findResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.decryptFindResponse(ctx, findResponse, map[string]multihash.Multihash{smh.B58String(): mh}) | ||
|
||
return findResponse, nil | ||
} | ||
|
||
// decryptFindResponse decrypts EncMultihashResults and appends the decrypted values to | ||
// MultihashResults. It also fetches provider info and metadata for each value key. | ||
func (c *DHashClient) decryptFindResponse(ctx context.Context, resp *model.FindResponse, unhasher map[string]multihash.Multihash) error { | ||
pinfoCache := make(map[peer.ID]*model.ProviderInfo) | ||
|
||
// decrypt each value key using the original multihash | ||
// then for each decrypted value key fetch provider's addr info | ||
for _, encRes := range resp.EncMultihashResults { | ||
mh, found := unhasher[encRes.Multihash.B58String()] | ||
if !found { | ||
continue | ||
} | ||
|
||
mhr := model.MultihashResult{ | ||
Multihash: mh, | ||
} | ||
for _, evk := range encRes.ValueKeys { | ||
vk, err := dhash.DecryptValueKey(evk, mh) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pid, ctxId, err := dhash.SplitValueKey(vk) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// fetch provider info to populate peer.AddrInfo | ||
pinfo := pinfoCache[pid] | ||
if pinfo == nil { | ||
pinfo, err = c.GetProvider(ctx, pid) | ||
if err != nil { | ||
continue | ||
} | ||
pinfoCache[pid] = pinfo | ||
} | ||
|
||
// fetch metadata | ||
metadata, err := c.fetchMetadata(ctx, vk) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
mhr.ProviderResults = append(mhr.ProviderResults, model.ProviderResult{ | ||
ContextID: ctxId, | ||
Metadata: metadata, | ||
Provider: pinfo.AddrInfo, | ||
}) | ||
} | ||
if len(mhr.ProviderResults) > 0 { | ||
resp.MultihashResults = append(resp.MultihashResults, mhr) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *DHashClient) fetchMetadata(ctx context.Context, vk []byte) ([]byte, error) { | ||
u := fmt.Sprint(c.metadataUrl, "/", b58.Encode(dhash.SHA256(vk, nil))) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Accept", "application/json") | ||
|
||
resp, err := c.c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, httpclient.ReadError(resp.StatusCode, body) | ||
} | ||
|
||
return dhash.DecryptMetadata(body, vk) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters