@cryptotaxi247 / kubo / commits / 52ca37075

feat(routing/http): support IPIP-484 and streaming (#10534)

Marcin Rataj committed Oct 4, 2024 at 00:58 UTC 52ca3707591cad8ae99fd0a03b7e278487192e71
4 files changed +44 -17
config/routing.go
+28 -1
@@ -3,14 +3,31 @@ package config
3 import (
4 "encoding/json"
5 "fmt"
6 + "os"
7 "runtime"
8 + "strings"
9 )
10
9 -var (
11 +const (
12 DefaultAcceleratedDHTClient = false
13 DefaultLoopbackAddressesOnLanDHT = false
14 )
15
16 +var (
17 + // Default HTTP routers used in parallel to DHT when Routing.Type = "auto"
18 + DefaultHTTPRouters = getEnvOrDefault("IPFS_HTTP_ROUTERS", []string{
19 + "https://cid.contact", // https://github.com/ipfs/kubo/issues/9422#issuecomment-1338142084
20 + })
21 +
22 + // Default filter-protocols to pass along with delegated routing requests (as defined in IPIP-484)
23 + // and also filter out locally
24 + DefaultHTTPRoutersFilterProtocols = getEnvOrDefault("IPFS_HTTP_ROUTERS_FILTER_PROTOCOLS", []string{
25 + "unknown", // allow results without protocol list, we can do libp2p identify to test them
26 + "transport-bitswap",
27 + // TODO: add 'transport-ipfs-gateway-http' once https://github.com/ipfs/rainbow/issues/125 is addressed
28 + })
29 +)
30 +
31 // Routing defines configuration options for libp2p routing.
32 type Routing struct {
33 // Type sets default daemon routing mode.
@@ -180,3 +197,13 @@ type ConfigRouter struct {
197 type Method struct {
198 RouterName string
199 }
200 +
201 +// getEnvOrDefault reads space or comma separated strings from env if present,
202 +// and uses provided defaultValue as a fallback
203 +func getEnvOrDefault(key string, defaultValue []string) []string {
204 + if value, exists := os.LookupEnv(key); exists {
205 + splitFunc := func(r rune) bool { return r == ',' || r == ' ' }
206 + return strings.FieldsFunc(value, splitFunc)
207 + }
208 + return defaultValue
209 +}
core/node/libp2p/routingopt.go
+1 -16
@@ -2,8 +2,6 @@ package libp2p
2
3 import (
4 "context"
5 - "os"
6 - "strings"
5 "time"
6
7 "github.com/ipfs/go-datastore"
@@ -31,23 +29,10 @@ type RoutingOptionArgs struct {
29
30 type RoutingOption func(args RoutingOptionArgs) (routing.Routing, error)
31
34 -// Default HTTP routers used in parallel to DHT when Routing.Type = "auto"
35 -var defaultHTTPRouters = []string{
36 - "https://cid.contact", // https://github.com/ipfs/kubo/issues/9422#issuecomment-1338142084
37 - // TODO: add an independent router from Cloudflare
38 -}
39 -
40 -func init() {
41 - // Override HTTP routers if custom ones were passed via env
42 - if routers := os.Getenv("IPFS_HTTP_ROUTERS"); routers != "" {
43 - defaultHTTPRouters = strings.Split(routers, " ")
44 - }
45 -}
46 -
32 func constructDefaultHTTPRouters(cfg *config.Config) ([]*routinghelpers.ParallelRouter, error) {
33 var routers []*routinghelpers.ParallelRouter
34 // Append HTTP routers for additional speed
50 - for _, endpoint := range defaultHTTPRouters {
35 + for _, endpoint := range config.DefaultHTTPRouters {
36 httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, httpAddrsFromConfig(cfg.Addresses), cfg.Identity.PrivKey)
37 if err != nil {
38 return nil, err
docs/environment-variables.md
+12
@@ -131,6 +131,18 @@ The above will replace implicit HTTP routers with single one, allowing for
131 inspection/debug of HTTP requests sent by Kubo via `while true ; do nc -l 7423; done`
132 or more advanced tools like [mitmproxy](https://docs.mitmproxy.org/stable/#mitmproxy).
133
134 +Default: `config.DefaultHTTPRouters`
135 +
136 +## `IPFS_HTTP_ROUTERS_FILTER_PROTOCOLS`
137 +
138 +Overrides values passed with `filter-protocols` parameter defined in IPIP-484.
139 +Value is space-separated.
140 +
141 +```console
142 +$ IPFS_HTTP_ROUTERS_FILTER_PROTOCOLS="unknown transport-bitswap transport-foo" ipfs daemon
143 +```
144 +
145 +Default: `config.DefaultHTTPRoutersFilterProtocols`
146
147 ## `IPFS_CONTENT_BLOCKING_DISABLE`
148
routing/delegated.go
+3
@@ -206,6 +206,9 @@ func httpRoutingFromConfig(conf config.Router, extraHTTP *ExtraHTTPParams) (rout
206 drclient.WithIdentity(key),
207 drclient.WithProviderInfo(addrInfo.ID, addrInfo.Addrs),
208 drclient.WithUserAgent(version.GetUserAgentVersion()),
209 + drclient.WithProtocolFilter(config.DefaultHTTPRoutersFilterProtocols),
210 + drclient.WithStreamResultsRequired(), // https://specs.ipfs.tech/routing/http-routing-v1/#streaming
211 + drclient.WithDisabledLocalFiltering(false), // force local filtering in case remote server does not support IPIP-484
212 )
213 if err != nil {
214 return nil, err