forked from ipfs/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
551 lines (474 loc) · 16.6 KB
/
handlers.go
File metadata and controls
551 lines (474 loc) · 16.6 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/ipfs/boxo/blockstore"
leveldb "github.com/ipfs/go-ds-leveldb"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
_ "embed"
_ "net/http/pprof"
"github.com/felixge/httpsnoop"
"github.com/ipfs/boxo/gateway"
servertiming "github.com/mitchellh/go-server-timing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
//go:embed static/index.html
var indexHTML []byte
func makeMetricsAndDebuggingHandler() *http.ServeMux {
mux := http.NewServeMux()
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
}
options := promhttp.HandlerOpts{}
mux.Handle("/debug/metrics/prometheus", promhttp.HandlerFor(gatherers, options))
mux.Handle("/debug/vars", http.DefaultServeMux)
mux.Handle("/debug/pprof/", http.DefaultServeMux)
mux.HandleFunc("/debug/stack", func(w http.ResponseWriter, r *http.Request) {
if err := writeAllGoroutineStacks(w); err != nil {
goLog.Error(err)
}
})
MutexFractionOption("/debug/pprof-mutex/", mux)
BlockProfileRateOption("/debug/pprof-block/", mux)
return mux
}
func addLogHandlers(mux *http.ServeMux) {
mux.HandleFunc("/mgr/log/level", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
q := r.URL.Query()
subsystem := q.Get("subsystem")
level := q.Get("level")
if subsystem == "" || level == "" {
http.Error(w, "both subsystem and level must be passed", http.StatusBadRequest)
return
}
if err := log.SetLogLevel(subsystem, level); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/mgr/log/ls", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Join(log.GetSubsystems(), ",")))
})
}
func gcHandler(gnd *Node) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var body struct {
BytesToFree int64
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := gnd.GC(r.Context(), body.BytesToFree); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func purgePeerHandler(p2pHost host.Host) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
q := r.URL.Query()
peerIDStr := q.Get("peer")
if peerIDStr == "" {
http.Error(w, "missing peer id", http.StatusBadRequest)
return
}
if peerIDStr == "all" {
purgeCount, err := purgeAllConnections(p2pHost)
if err != nil {
goLog.Errorw("Error closing all libp2p connections", "err", err)
http.Error(w, "error closing connections", http.StatusInternalServerError)
return
}
goLog.Infow("Purged connections", "count", purgeCount)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, "Peer connections purged:", purgeCount)
return
}
peerID, err := peer.Decode(peerIDStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = purgeConnection(p2pHost, peerID)
if err != nil {
goLog.Errorw("Error closing libp2p connection", "err", err, "peer", peerID)
http.Error(w, "error closing connection", http.StatusInternalServerError)
return
}
goLog.Infow("Purged connection", "peer", peerID)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, "Purged connection to peer", peerID)
}
}
func purgeConnection(p2pHost host.Host, peerID peer.ID) error {
peerStore := p2pHost.Peerstore()
if peerStore != nil {
peerStore.RemovePeer(peerID)
peerStore.ClearAddrs(peerID)
}
return p2pHost.Network().ClosePeer(peerID)
}
func purgeAllConnections(p2pHost host.Host) (int, error) {
net := p2pHost.Network()
peers := net.Peers()
peerStore := p2pHost.Peerstore()
if peerStore != nil {
for _, peerID := range peers {
peerStore.RemovePeer(peerID)
peerStore.ClearAddrs(peerID)
}
}
var errCount, purgeCount int
for _, peerID := range peers {
err := net.ClosePeer(peerID)
if err != nil {
goLog.Errorw("Closing libp2p connection", "err", err, "peer", peerID)
errCount++
} else {
purgeCount++
}
}
if errCount != 0 {
return 0, fmt.Errorf("error closing connections to %d peers", errCount)
}
return purgeCount, nil
}
func showPeersHandler(p2pHost host.Host) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
peers := p2pHost.Network().Peers()
body := struct {
Count int64
Peers []string
}{
Count: int64(len(peers)),
}
if len(peers) != 0 {
peerStrs := make([]string, len(peers))
for i, peerID := range peers {
peerStrs[i] = peerID.String()
}
body.Peers = peerStrs
}
enc := json.NewEncoder(w)
if err := enc.Encode(body); err != nil {
goLog.Errorw("cannot write response", "err", err)
http.Error(w, "", http.StatusInternalServerError)
}
}
}
func withConnect(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ServeMux does not support requests with CONNECT method,
// so we need to handle them separately
// https://golang.org/src/net/http/request.go#L111
if r.Method == http.MethodConnect {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func withRequestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(next, w, r)
goLog.Infow(r.Method, "url", r.URL, "host", r.Host, "code", m.Code, "duration", m.Duration, "written", m.Written, "ua", r.UserAgent(), "referer", r.Referer())
})
}
func setupGatewayHandler(cfg Config, nd *Node) (http.Handler, error) {
var (
backend gateway.IPFSBackend
err error
)
options := []gateway.BackendOption{
gateway.WithValueStore(nd.vs),
gateway.WithNameSystem(nd.ns),
gateway.WithResolver(nd.resolver), // May be nil, but that is fine.
}
if len(cfg.RemoteBackends) > 0 && cfg.RemoteBackendMode == RemoteBackendCAR {
var fetcher gateway.CarFetcher
fetcher, err = gateway.NewRemoteCarFetcher(cfg.RemoteBackends, nil)
if err != nil {
return nil, err
}
backend, err = gateway.NewCarBackend(fetcher, options...)
} else {
backend, err = gateway.NewBlocksBackend(nd.bsrv, options...)
}
if err != nil {
return nil, err
}
headers := map[string][]string{}
// Note: in the future we may want to make this more configurable.
//noDNSLink := false
// Helper function to check if a domain should have DNSLink enabled
isDNSLinkAllowedForDomain := func(domain string) bool {
// If no domains specified, allow all (backward compatibility)
if len(cfg.DNSLinkGatewayDomains) == 0 {
return true
}
// Check if the domain matches any allowed domain
for _, allowed := range cfg.DNSLinkGatewayDomains {
// Exact match
if domain == allowed {
return true
}
// Subdomain match (e.g., "sub.example.com" matches "example.com")
if strings.HasSuffix(domain, "."+allowed) {
return true
}
}
goLog.Debugf("DNSLink blocked for domain %s (not in allowed list)", domain)
return false
}
// TODO: allow appending hostnames to this list via ENV variable (separate PATH_GATEWAY_HOSTS & SUBDOMAIN_GATEWAY_HOSTS)
publicGateways := map[string]*gateway.PublicGateway{
"localhost": {
Paths: []string{"/ipfs", "/ipns", "/version"},
NoDNSLink: len(cfg.DNSLinkGatewayDomains) > 0,
InlineDNSLink: false,
DeserializedResponses: true,
UseSubdomains: true,
},
}
for _, domain := range cfg.GatewayDomains {
publicGateways[domain] = &gateway.PublicGateway{
Paths: []string{"/ipfs", "/ipns", "/version"},
NoDNSLink: !isDNSLinkAllowedForDomain(domain),
InlineDNSLink: true,
DeserializedResponses: true,
UseSubdomains: false,
}
}
for _, domain := range cfg.SubdomainGatewayDomains {
publicGateways[domain] = &gateway.PublicGateway{
Paths: []string{"/ipfs", "/ipns", "/version"},
NoDNSLink: !isDNSLinkAllowedForDomain(domain),
InlineDNSLink: true,
DeserializedResponses: true,
UseSubdomains: true,
}
}
for _, domain := range cfg.TrustlessGatewayDomains {
publicGateways[domain] = &gateway.PublicGateway{
Paths: []string{"/ipfs", "/ipns", "/version"},
NoDNSLink: true,
InlineDNSLink: true,
DeserializedResponses: false,
UseSubdomains: contains(cfg.SubdomainGatewayDomains, domain),
}
}
// If we're doing tests, ensure the right public gateways are enabled.
if os.Getenv("GATEWAY_CONFORMANCE_TEST") == "true" {
publicGateways["example.com"] = &gateway.PublicGateway{
Paths: []string{"/ipfs", "/ipns"},
NoDNSLink: !isDNSLinkAllowedForDomain("example.com"),
InlineDNSLink: true,
DeserializedResponses: true,
UseSubdomains: true,
}
// TODO: revisit the below once we clarify desired behavior in https://specs.ipfs.tech/http-gateways/subdomain-gateway/
publicGateways["localhost"].InlineDNSLink = true
}
// After configuring all the standard domains, add DNSLink-only domains
for _, domain := range cfg.DNSLinkGatewayDomains {
// Only add if not already configured
if _, exists := publicGateways[domain]; !exists {
publicGateways[domain] = &gateway.PublicGateway{
Paths: []string{"/ipfs", "/ipns", "/version"},
NoDNSLink: false,
InlineDNSLink: true,
DeserializedResponses: true,
UseSubdomains: false,
}
}
}
gwConf := gateway.Config{
DeserializedResponses: true,
PublicGateways: publicGateways,
NoDNSLink: len(cfg.DNSLinkGatewayDomains) > 0,
MaxConcurrentRequests: cfg.MaxConcurrentRequests, // When exceeded, returns 429 with Retry-After: 60 (hardcoded in boxo)
RetrievalTimeout: cfg.RetrievalTimeout,
MaxRangeRequestFileSize: cfg.MaxRangeRequestFileSize,
DiagnosticServiceURL: cfg.DiagnosticServiceURL,
}
gwHandler := gateway.NewHandler(gwConf, backend)
ipfsHandler := withHTTPMetrics(gwHandler, "ipfs", cfg.disableMetrics)
ipnsHandler := withHTTPMetrics(gwHandler, "ipns", cfg.disableMetrics)
topMux := http.NewServeMux()
topMux.Handle("/ipfs/", ipfsHandler)
topMux.Handle("/ipns/", ipnsHandler)
topMux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Client: %s\n", name)
fmt.Fprintf(w, "Version: %s\n", version)
})
topMux.HandleFunc("/api/v0/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte("The /api/v0 Kubo RPC is not part of IPFS Gateway Specs (https://specs.ipfs.tech/http-gateways/). Consider refactoring your app. If you still need this Kubo endpoint, please self-host a Kubo instance yourself: https://docs.ipfs.tech/install/command-line/ with proper auth https://github.com/ipfs/kubo/blob/master/docs/config.md#apiauthorizations"))
})
topMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(indexHTML)
})
// Construct the HTTP handler for the gateway.
handler := withConnect(topMux)
handler = http.Handler(gateway.NewHostnameHandler(gwConf, backend, handler))
// Add custom headers and liberal CORS.
handler = gateway.NewHeaders(headers).ApplyCors().Wrap(handler)
handler = servertiming.Middleware(handler, nil)
// Add logging.
handler = withRequestLogger(handler)
// Add tracing.
handler = withTracingAndDebug(handler, cfg.TracingAuthToken)
// Add pin origins support (X-PIN-ORIGINS).
handler = withPinOrigins(nd, handler)
return handler, nil
}
func withTracingAndDebug(next http.Handler, authToken string) http.Handler {
next = otelhttp.NewHandler(next, "Gateway")
// Remove tracing and cache skipping headers if not authorized
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Disable tracing/debug headers if auth token missing or invalid
if authToken == "" || request.Header.Get("Authorization") != authToken {
if request.Header.Get("Traceparent") != "" || request.Header.Get("Tracestate") != "" || request.Header.Get(NoBlockcacheHeader) != "" {
http.Error(writer, "The request is not accompanied by a valid authorization header", http.StatusUnauthorized)
return
}
}
// Process cache skipping header
if noBlockCache := request.Header.Get(NoBlockcacheHeader); noBlockCache == "true" {
ds, err := leveldb.NewDatastore("", nil)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
newCtx := context.WithValue(request.Context(), NoBlockcache{}, blockstore.NewBlockstore(ds))
request = request.WithContext(newCtx)
}
next.ServeHTTP(writer, request)
})
}
const NoBlockcacheHeader = "Rainbow-No-Blockcache"
type NoBlockcache struct{}
const PinOriginsHeader = "X-PIN-ORIGINS"
// withPinOrigins inspects the request for pin origins header(s) and, if present,
// attempts short, best-effort libp2p connects to the provided multiaddresses.
// This increases the likelihood that Bitswap can retrieve content directly
// from the specified origins even if they are not advertising on DHT/IPNI.
func withPinOrigins(nd *Node, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if nd != nil && nd.host != nil {
// Collect all header values and split by comma to support multiple origins.
rawValues := append([]string{}, r.Header.Values(PinOriginsHeader)...)
if len(rawValues) != 0 {
unique := make(map[string]struct{})
for _, v := range rawValues {
for _, part := range strings.Split(v, ",") {
s := strings.TrimSpace(part)
if s == "" {
continue
}
unique[s] = struct{}{}
}
}
for maStr := range unique {
ai, err := peer.AddrInfoFromString(maStr)
if err != nil {
goLog.Warnw("Invalid peer origin value", "value", maStr, "err", err)
continue
}
// Fire-and-forget connects to speed up retrieval; don't block the request path.
go func(ai peer.AddrInfo) {
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
if err := nd.host.Connect(ctx, ai); err != nil {
goLog.Debugw("Failed to connect to hinted peer", "peer", ai.ID, "err", err)
return
}
goLog.Debugw("Connected to hinted peer", "peer", ai.ID)
}(*ai)
}
}
}
next.ServeHTTP(w, r)
})
}
// MutexFractionOption allows to set runtime.SetMutexProfileFraction via HTTP
// using POST request with parameter 'fraction'.
func MutexFractionOption(path string, mux *http.ServeMux) *http.ServeMux {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
asfr := r.Form.Get("fraction")
if len(asfr) == 0 {
http.Error(w, "parameter 'fraction' must be set", http.StatusBadRequest)
return
}
fr, err := strconv.Atoi(asfr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
runtime.SetMutexProfileFraction(fr)
})
return mux
}
// BlockProfileRateOption allows to set runtime.SetBlockProfileRate via HTTP
// using POST request with parameter 'rate'.
// The profiler tries to sample 1 event every <rate> nanoseconds.
// If rate == 1, then the profiler samples every blocking event.
// To disable, set rate = 0.
func BlockProfileRateOption(path string, mux *http.ServeMux) *http.ServeMux {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rateStr := r.Form.Get("rate")
if len(rateStr) == 0 {
http.Error(w, "parameter 'rate' must be set", http.StatusBadRequest)
return
}
rate, err := strconv.Atoi(rateStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
runtime.SetBlockProfileRate(rate)
})
return mux
}
func contains[T comparable](collection []T, element T) bool {
for _, item := range collection {
if item == element {
return true
}
}
return false
}