|
2 | 2 | package main
|
3 | 3 |
|
4 | 4 | import (
|
| 5 | + "bytes" |
5 | 6 | "context"
|
| 7 | + "encoding/json" |
6 | 8 | "errors"
|
7 | 9 | "fmt"
|
| 10 | + "io" |
8 | 11 | "net"
|
9 | 12 | "net/http"
|
10 | 13 | "os"
|
11 | 14 | "runtime/pprof"
|
12 | 15 | "strings"
|
13 | 16 | "time"
|
14 | 17 |
|
| 18 | + "github.com/blang/semver/v4" |
15 | 19 | "github.com/google/uuid"
|
16 | 20 | u "github.com/ipfs/boxo/util"
|
17 | 21 | cmds "github.com/ipfs/go-ipfs-cmds"
|
18 | 22 | "github.com/ipfs/go-ipfs-cmds/cli"
|
19 | 23 | cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
|
20 | 24 | logging "github.com/ipfs/go-log"
|
| 25 | + ipfs "github.com/ipfs/kubo" |
21 | 26 | "github.com/ipfs/kubo/cmd/ipfs/util"
|
22 | 27 | oldcmds "github.com/ipfs/kubo/commands"
|
23 | 28 | "github.com/ipfs/kubo/core"
|
@@ -224,6 +229,10 @@ func apiAddrOption(req *cmds.Request) (ma.Multiaddr, error) {
|
224 | 229 | return ma.NewMultiaddr(apiAddrStr)
|
225 | 230 | }
|
226 | 231 |
|
| 232 | +// encodedAbsolutePathVersion is the version from which the absolute path header in |
| 233 | +// multipart requests is %-encoded. Before this version, its sent raw. |
| 234 | +var encodedAbsolutePathVersion = semver.MustParse("0.23.0-dev") |
| 235 | + |
227 | 236 | func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
|
228 | 237 | exe := tracingWrappedExecutor{cmds.NewExecutor(req.Root)}
|
229 | 238 | cctx := env.(*oldcmds.Context)
|
@@ -315,9 +324,18 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
|
315 | 324 | default:
|
316 | 325 | return nil, fmt.Errorf("unsupported API address: %s", apiAddr)
|
317 | 326 | }
|
318 |
| - opts = append(opts, cmdhttp.ClientWithHTTPClient(&http.Client{ |
| 327 | + |
| 328 | + httpClient := &http.Client{ |
319 | 329 | Transport: otelhttp.NewTransport(tpt),
|
320 |
| - })) |
| 330 | + } |
| 331 | + opts = append(opts, cmdhttp.ClientWithHTTPClient(httpClient)) |
| 332 | + |
| 333 | + // Fetch remove version, as some feature compatibility might change depending on it. |
| 334 | + remoteVersion, err := getRemoteVersion(tracingWrappedExecutor{cmdhttp.NewClient(host, opts...)}) |
| 335 | + if err != nil { |
| 336 | + return nil, err |
| 337 | + } |
| 338 | + opts = append(opts, cmdhttp.ClientWithRawAbsPath(remoteVersion.LT(encodedAbsolutePathVersion))) |
321 | 339 |
|
322 | 340 | return tracingWrappedExecutor{cmdhttp.NewClient(host, opts...)}, nil
|
323 | 341 | }
|
@@ -417,3 +435,40 @@ func resolveAddr(ctx context.Context, addr ma.Multiaddr) (ma.Multiaddr, error) {
|
417 | 435 |
|
418 | 436 | return addrs[0], nil
|
419 | 437 | }
|
| 438 | + |
| 439 | +type nopWriter struct { |
| 440 | + io.Writer |
| 441 | +} |
| 442 | + |
| 443 | +func (nw nopWriter) Close() error { |
| 444 | + return nil |
| 445 | +} |
| 446 | + |
| 447 | +func getRemoteVersion(exe cmds.Executor) (*semver.Version, error) { |
| 448 | + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30)) |
| 449 | + defer cancel() |
| 450 | + |
| 451 | + req, err := cmds.NewRequest(ctx, []string{"version"}, nil, nil, nil, Root) |
| 452 | + if err != nil { |
| 453 | + return nil, err |
| 454 | + } |
| 455 | + |
| 456 | + var buf bytes.Buffer |
| 457 | + re, err := cmds.NewWriterResponseEmitter(nopWriter{&buf}, req) |
| 458 | + if err != nil { |
| 459 | + return nil, err |
| 460 | + } |
| 461 | + |
| 462 | + err = exe.Execute(req, re, nil) |
| 463 | + if err != nil { |
| 464 | + return nil, err |
| 465 | + } |
| 466 | + |
| 467 | + var out ipfs.VersionInfo |
| 468 | + dec := json.NewDecoder(&buf) |
| 469 | + if err := dec.Decode(&out); err != nil { |
| 470 | + return nil, err |
| 471 | + } |
| 472 | + |
| 473 | + return semver.New(out.Version) |
| 474 | +} |
0 commit comments