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
/
blockstore_proxy.go
185 lines (158 loc) · 4.77 KB
/
blockstore_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"time"
"github.com/ipfs/bifrost-gateway/lib"
blockstore "github.com/ipfs/boxo/blockstore"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
// Blockstore backed by a verifiable gateway. This is vendor-agnostic proxy interface,
// one can use Gateway provided by Kubo, or any other implementation that follows
// the spec for verifiable responses:
// https://docs.ipfs.tech/reference/http/gateway/#trustless-verifiable-retrieval
// https://github.com/ipfs/specs/blob/main/http-gateways/TRUSTLESS_GATEWAY.md
const (
EnvProxyGateway = "PROXY_GATEWAY_URL"
)
type proxyBlockStore struct {
httpClient *http.Client
gatewayURL []string
validate bool
rand *rand.Rand
}
func (ps *proxyBlockStore) Fetch(ctx context.Context, path string, cb lib.DataCallback) error {
urlStr := fmt.Sprintf("%s%s", ps.getRandomGatewayURL(), path)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return err
}
goLog.Debugw("car fetch", "url", req.URL)
req.Header.Set("Accept", "application/vnd.ipld.car;order=dfs;dups=y")
resp, err := ps.httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
errData, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("could not read error message: %w", err)
} else {
err = fmt.Errorf("%q", string(errData))
}
return fmt.Errorf("http error from car gateway: %s: %w", resp.Status, err)
}
err = cb(path, resp.Body)
if err != nil {
resp.Body.Close()
return err
}
return resp.Body.Close()
}
var _ lib.CarFetcher = (*proxyBlockStore)(nil)
func newProxyBlockStore(gatewayURL []string, cdns *cachedDNS) blockstore.Blockstore {
s := rand.NewSource(time.Now().Unix())
rand := rand.New(s)
if len(gatewayURL) == 0 {
log.Fatal("Missing PROXY_GATEWAY_URL. See https://github.com/ipfs/bifrost-gateway/blob/main/docs/environment-variables.md")
}
return &proxyBlockStore{
gatewayURL: gatewayURL,
httpClient: &http.Client{
Timeout: GetBlockTimeout,
Transport: otelhttp.NewTransport(&customTransport{
// Roundtripper with increased defaults than http.Transport such that retrieving
// multiple blocks from a single gateway concurrently is fast.
RoundTripper: &http.Transport{
MaxIdleConns: 1000,
MaxConnsPerHost: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
DialContext: cdns.dialWithCachedDNS,
ForceAttemptHTTP2: true,
},
}),
},
// Enables block validation by default. Important since we are
// proxying block requests to an untrusted gateway.
validate: true,
rand: rand,
}
}
func (ps *proxyBlockStore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error) {
urlStr := fmt.Sprintf("%s/ipfs/%s?format=raw", ps.getRandomGatewayURL(), c)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
goLog.Debugw("raw fetch", "url", req.URL)
req.Header.Set("Accept", "application/vnd.ipld.raw")
resp, err := ps.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error from block 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) {
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
}
func (ps *proxyBlockStore) getRandomGatewayURL() string {
return ps.gatewayURL[ps.rand.Intn(len(ps.gatewayURL))]
}
var _ blockstore.Blockstore = (*proxyBlockStore)(nil)