This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 20
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
3 changed files
with
123 additions
and
2 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
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,119 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/ipfs/go-cid" | ||
blockstore "github.com/ipfs/go-ipfs-blockstore" | ||
"github.com/ipfs/go-libipfs/blocks" | ||
) | ||
|
||
type proxyBlockStore struct { | ||
httpClient *http.Client | ||
gatewayURL string | ||
validate bool | ||
} | ||
|
||
func newProxyBlockStore(gatewayURL string, client *http.Client) blockstore.Blockstore { | ||
if client == nil { | ||
client = http.DefaultClient | ||
} | ||
|
||
return &proxyBlockStore{ | ||
gatewayURL: gatewayURL, | ||
httpClient: client, | ||
// Enables block validation by default. Important since we are | ||
// proxying block requests to an untrusted gateway. | ||
validate: true, | ||
} | ||
} | ||
|
||
func (ps *proxyBlockStore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error) { | ||
u, err := url.Parse(fmt.Sprintf("%s/ipfs/%s?format=raw", ps.gatewayURL, c)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := ps.httpClient.Do(&http.Request{ | ||
Method: http.MethodGet, | ||
URL: u, | ||
Header: http.Header{ | ||
"Accept": []string{"application/vnd.ipld.raw"}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status from remote gateway: %s", resp.Status) | ||
} | ||
|
||
rb, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if ps.validate { | ||
nc, err := c.Prefix().Sum(rb) | ||
if err != nil { | ||
return nil, blocks.ErrWrongHash | ||
} | ||
if !nc.Equals(c) { | ||
fmt.Printf("got %s vs %s\n", nc, c) | ||
return nil, blocks.ErrWrongHash | ||
} | ||
} | ||
|
||
return blocks.NewBlockWithCid(rb, c) | ||
} | ||
|
||
func (ps *proxyBlockStore) Has(ctx context.Context, c cid.Cid) (bool, error) { | ||
blk, err := ps.fetch(ctx, c) | ||
if err != nil { | ||
return false, err | ||
} | ||
return blk != nil, nil | ||
} | ||
|
||
func (ps *proxyBlockStore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) { | ||
blk, err := ps.fetch(ctx, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return blk, nil | ||
} | ||
|
||
func (ps *proxyBlockStore) GetSize(ctx context.Context, c cid.Cid) (int, error) { | ||
blk, err := ps.fetch(ctx, c) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return len(blk.RawData()), nil | ||
} | ||
|
||
func (ps *proxyBlockStore) HashOnRead(enabled bool) { | ||
ps.validate = enabled | ||
} | ||
|
||
func (c *proxyBlockStore) Put(context.Context, blocks.Block) error { | ||
return errNotImplemented | ||
} | ||
|
||
func (c *proxyBlockStore) PutMany(context.Context, []blocks.Block) error { | ||
return errNotImplemented | ||
} | ||
|
||
func (c *proxyBlockStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { | ||
return nil, errNotImplemented | ||
} | ||
|
||
func (c *proxyBlockStore) DeleteBlock(context.Context, cid.Cid) error { | ||
return errNotImplemented | ||
} | ||
|
||
var _ blockstore.Blockstore = (*proxyBlockStore)(nil) |