Skip to content

Commit

Permalink
fix(identify): ascii-only versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Dec 6, 2022
1 parent a3913d9 commit a593889
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,18 @@ func printPeer(keyEnc ke.KeyEncoder, ps pstore.Peerstore, p peer.ID) (interface{

protocols, _ := ps.GetProtocols(p) // don't care about errors here.
for _, p := range protocols {
info.Protocols = append(info.Protocols, string(p))
info.Protocols = append(info.Protocols, version.TrimVersion(string(p)))
}
sort.Strings(info.Protocols)

if v, err := ps.Get(p, "ProtocolVersion"); err == nil {
if vs, ok := v.(string); ok {
info.ProtocolVersion = vs
info.ProtocolVersion = version.TrimVersion(vs)
}
}
if v, err := ps.Get(p, "AgentVersion"); err == nil {
if vs, ok := v.(string); ok {
info.AgentVersion = vs
info.AgentVersion = version.TrimVersion(vs)
}
}

Expand Down
9 changes: 7 additions & 2 deletions core/commands/stat_dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"text/tabwriter"
"time"

version "github.com/ipfs/kubo"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"

cmds "github.com/ipfs/go-ipfs-cmds"
Expand Down Expand Up @@ -92,7 +93,9 @@ This interface is not stable and may change from release to release.
info := dhtPeerInfo{ID: p.String()}

if ver, err := nd.Peerstore.Get(p, "AgentVersion"); err == nil {
info.AgentVersion, _ = ver.(string)
if vs, ok := ver.(string); ok {
info.AgentVersion = version.TrimVersion(vs)
}
} else if err == pstore.ErrNotFound {
// ignore
} else {
Expand Down Expand Up @@ -143,7 +146,9 @@ This interface is not stable and may change from release to release.
info := dhtPeerInfo{ID: pi.Id.String()}

if ver, err := nd.Peerstore.Get(pi.Id, "AgentVersion"); err == nil {
info.AgentVersion, _ = ver.(string)
if vs, ok := ver.(string); ok {
info.AgentVersion = version.TrimVersion(vs)
}
} else if err == pstore.ErrNotFound {
// ignore
} else {
Expand Down
3 changes: 2 additions & 1 deletion core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

files "github.com/ipfs/go-ipfs-files"
version "github.com/ipfs/kubo"
"github.com/ipfs/kubo/commands"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core/commands/cmdenv"
Expand Down Expand Up @@ -284,7 +285,7 @@ var swarmPeersCmd = &cmds.Command{
}

for _, s := range strs {
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s)})
ci.Streams = append(ci.Streams, streamInfo{Protocol: version.TrimVersion(string(s))})
}
}
sort.Sort(&ci)
Expand Down
23 changes: 13 additions & 10 deletions test/sharness/t0026-id.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ test_id_compute_agent() {
fi
AGENT_VERSION="$AGENT_VERSION$AGENT_SUFFIX"
fi
echo "$AGENT_VERSION"
# limit of 64 characters: https://github.com/ipfs/kubo/pull/9465
echo -n "$AGENT_VERSION" | head -c 64
}

test_expect_success "checking AgentVersion" '
Expand Down Expand Up @@ -52,20 +53,22 @@ test_expect_success "checking and converting ID of a random peer while offline"
'

# agent-version-suffix (local, offline)
test_launch_ipfs_daemon --agent-version-suffix=test-suffix
test_expect_success "checking AgentVersion with suffix (local)" '
test_id_compute_agent test-suffix > expected-agent-version &&
ipfs id -f "<aver>\n" > actual-agent-version &&
test_cmp expected-agent-version actual-agent-version
test_launch_ipfs_daemon --agent-version-suffix=test-suffix-ąę-123456789012345678901234567890
test_expect_success "checking AgentVersion with suffix (local, only ascii chars)" '
test_id_compute_agent "test-suffix--1234567890123456789" > expected &&
ipfs id -f "<aver>" > actual &&
test_should_not_contain "ą" actual &&
test_cmp expected actual
'

# agent-version-suffix (over libp2p identify protocol)
iptb testbed create -type localipfs -count 2 -init
startup_cluster 2 --agent-version-suffix=test-suffix-identify
startup_cluster 2 --agent-version-suffix=test-suffix-identify-óź-123456789012345678901234567890
test_expect_success "checking AgentVersion with suffix (fetched via libp2p identify protocol)" '
ipfsi 0 id -f "<aver>\n" > expected-identify-agent-version &&
ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "<aver>\n" > actual-libp2p-identify-agent-version &&
test_cmp expected-identify-agent-version actual-libp2p-identify-agent-version
test_id_compute_agent "test-suffix-identify--1234567890" > expected &&
ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "<aver>" > actual &&
test_should_not_contain "ó" actual &&
test_cmp expected actual
'
test_kill_ipfs_daemon

Expand Down
21 changes: 19 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ipfs

import (
"fmt"
"regexp"
"runtime"

"github.com/ipfs/kubo/repo/fsrepo"
Expand All @@ -15,6 +16,8 @@ const CurrentVersionNumber = "0.18.0-dev"

const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint

const maxVersionLen = 64

// GetUserAgentVersion is the libp2p user agent used by go-ipfs.
//
// Note: This will end in `/` when no commit is available. This is expected.
Expand All @@ -26,13 +29,27 @@ func GetUserAgentVersion() string {
}
userAgent += userAgentSuffix
}
return userAgent
return TrimVersion(userAgent)
}

var userAgentSuffix string
var onlyASCII = regexp.MustCompile("[[:^ascii:]]")

func SetUserAgentSuffix(suffix string) {
userAgentSuffix = suffix
userAgentSuffix = TrimVersion(suffix)
}

func TrimVersion(version string) string {
ascii := onlyASCII.ReplaceAllLiteralString(version, "")
chars := 0
for i := range ascii {
if chars >= maxVersionLen {
ascii = ascii[:i]
break
}
chars++
}
return ascii
}

type VersionInfo struct {
Expand Down

0 comments on commit a593889

Please sign in to comment.