Skip to content

Commit

Permalink
Check http proto (#100)
Browse files Browse the repository at this point in the history
* new version

* Check http protocol to determine if really http or no connection

If the publisher did not responding to a query for libp2phttp and the publisher address is an HTTP address, then make an HTTP request to determine if the protocol really is HTTP or if the publisher if just not responding.
  • Loading branch information
gammazero authored Apr 17, 2024
1 parent c0d3b99 commit 22c7a1c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package provider

import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/ipfs/go-cid"
"github.com/ipni/go-libipni/dagsync/ipnisync"
"github.com/ipni/go-libipni/find/model"
"github.com/ipni/go-libipni/maurl"
"github.com/ipni/go-libipni/mautil"
"github.com/ipni/go-libipni/pcache"
"github.com/ipni/ipni-cli/pkg/dtrack"
Expand Down Expand Up @@ -357,7 +360,7 @@ func showProviderInfo(cctx *cli.Context, pinfo *model.ProviderInfo) {
proto = fmt.Sprintf("Error: %s", err)
} else {
defer p2pHost.Close()
proto, err = getProtocol(*pinfo.Publisher, p2pHost)
proto, err = getProtocol(cctx.Context, *pinfo.Publisher, p2pHost)
if err != nil {
proto = fmt.Sprintf("Error: %s", err)
}
Expand Down Expand Up @@ -399,7 +402,7 @@ func showProviderInfo(cctx *cli.Context, pinfo *model.ProviderInfo) {
fmt.Println()
}

func getProtocol(peerInfo peer.AddrInfo, p2pHost host.Host) (string, error) {
func getProtocol(ctx context.Context, peerInfo peer.AddrInfo, p2pHost host.Host) (string, error) {
clientHost := &libp2phttp.Host{
StreamHost: p2pHost,
}
Expand All @@ -426,6 +429,20 @@ func getProtocol(peerInfo peer.AddrInfo, p2pHost host.Host) (string, error) {
if len(httpAddrs) == 0 {
return "data-transfer/graphsync", nil
}
u, err := maurl.ToURL(peerInfo.Addrs[0])
if err != nil {
return "", err
}
fetchURL := u.JoinPath(ipnisync.IPNIPath, "head")
req, err := http.NewRequestWithContext(ctx, "GET", fetchURL.String(), nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
resp.Body.Close()
return "http", nil
}
return "libp2phttp", nil
Expand Down

0 comments on commit 22c7a1c

Please sign in to comment.